View Javadoc

1   /**
2    * The contents of this file are subject to the Mozilla Public License Version 1.1
3    * (the "License"); you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at http://www.mozilla.org/MPL/
5    * Software distributed under the License is distributed on an "AS IS" basis,
6    * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
7    * specific language governing rights and limitations under the License.
8    *
9    * The Original Code is ""  Description:
10   * ""
11   *
12   * The Initial Developer of the Original Code is University Health Network. Copyright (C)
13   * 2001.  All Rights Reserved.
14   *
15   * Contributor(s): ______________________________________.
16   *
17   * Alternatively, the contents of this file may be used under the terms of the
18   * GNU General Public License (the  "GPL"), in which case the provisions of the GPL are
19   * applicable instead of those above.  If you wish to allow use of your version of this
20   * file only under the terms of the GPL and not to allow others to use your version
21   * of this file under the MPL, indicate your decision by deleting  the provisions above
22   * and replace  them with the notice and other provisions required by the GPL License.
23   * If you do not delete the provisions above, a recipient may use your version of
24   * this file under either the MPL or the GPL.
25   */
26  package ca.uhn.hl7v2.testpanel.model.conn;
27  
28  import java.io.StringReader;
29  import java.io.StringWriter;
30  import java.util.ArrayList;
31  import java.util.Iterator;
32  import java.util.List;
33  
34  import javax.xml.bind.JAXB;
35  import javax.xml.bind.annotation.XmlAccessType;
36  import javax.xml.bind.annotation.XmlAccessorType;
37  import javax.xml.bind.annotation.XmlType;
38  
39  import org.apache.commons.lang.Validate;
40  
41  import ca.uhn.hl7v2.testpanel.controller.Controller;
42  import ca.uhn.hl7v2.testpanel.controller.Prefs;
43  import ca.uhn.hl7v2.testpanel.model.AbstractModelClass;
44  import ca.uhn.hl7v2.testpanel.xsd.Hl7V2EncodingTypeEnum;
45  
46  @XmlAccessorType(XmlAccessType.FIELD)
47  @XmlType(name = "InboundConnectionList")
48  public class InboundConnectionList extends AbstractModelClass {
49  
50  	public static final String PROP_LIST = InboundConnectionList.class.getName() + "_LIST";
51  	
52  	private List<InboundConnection> myConnections = new ArrayList<InboundConnection>();
53  
54  	private transient Controller myController;
55  	
56  	public InboundConnectionList() {
57  	}
58  
59  	public InboundConnection createDefaultConnection(int port) {
60  		InboundConnection initialConnection = new InboundConnection();
61  		initialConnection.setCharSet(Prefs.getInstance().getMostRecentConnectionCharset().displayName());
62  		initialConnection.setDualPort(false);
63  		initialConnection.setIncomingOrSinglePort(port);
64  		initialConnection.setHost("localhost");
65  		initialConnection.setEncoding(Hl7V2EncodingTypeEnum.ER_7);
66  		initialConnection.setController(myController);
67  		return initialConnection;
68  	}
69  
70  	@Override
71  	public String exportConfigToXml() {
72  		StringWriter writer = new StringWriter();
73  		JAXB.marshal(this, writer);
74  		return writer.toString();
75  	}
76  
77  	public static InboundConnectionList fromXml(Controller theController, String theXml) {
78  		InboundConnectionList retVal = JAXB.unmarshal(new StringReader(theXml), InboundConnectionList.class);
79  		retVal.myController = theController;
80  		
81  		for (InboundConnection next : retVal.getConnections()) {
82  			next.setController(theController);
83  		}
84  		
85  		return retVal;
86  	}
87  
88  	/**
89  	 * @return the connections
90  	 */
91  	public List<InboundConnection> getConnections() {
92  		return myConnections;
93  	}
94  
95  	public void addConnection(InboundConnection theCon) {
96  		Validate.notNull(theCon);
97  		
98  		myConnections.add(theCon);
99  		theCon.setController(myController);
100 		
101 		firePropertyChange(PROP_LIST, null, null);
102 	}
103 
104 	public void removeConnecion(InboundConnection theCon) {
105 		Validate.notNull(theCon);
106 		myConnections.remove(theCon);
107 		firePropertyChange(PROP_LIST, null, null);
108 	}
109 
110 	/**
111 	 * Remove all connections from this list which are not {@link AbstractConnection#isPersistent() persistent}
112 	 */
113 	public void removeNonPersistantConnections() {
114 		for (Iterator<InboundConnection> iter = myConnections.iterator(); iter.hasNext(); ) {
115 			if (!iter.next().isPersistent()) {
116 				iter.remove();
117 			}
118 		}
119 	}
120 
121 	public InboundConnection getWithId(String theId) {
122 		for (InboundConnection next : myConnections) {
123 			if (next.getId().equals(theId)) {
124 				return next;
125 			}
126 		}
127 		return null;
128 	}
129 
130 	
131 }