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 OutboundConnectionList extends AbstractModelClass {
49  
50  	public static final String PROP_LIST = OutboundConnectionList.class.getName() + "_LIST";
51  
52  	private List<OutboundConnection> myConnections = new ArrayList<OutboundConnection>();
53  
54  	public OutboundConnectionList() {
55  	}
56  
57  	public void addConnection(OutboundConnection theCon) {
58  		Validate.notNull(theCon);
59  		myConnections.add(theCon);
60  		firePropertyChange(PROP_LIST, null, null);
61  	}
62  
63  	public OutboundConnection createDefaultConnection(int port) {
64  		OutboundConnection initialConnection = new OutboundConnection();
65  		initialConnection.setCharSet(Prefs.getInstance().getMostRecentConnectionCharset().displayName());
66  		initialConnection.setDualPort(false);
67  		initialConnection.setIncomingOrSinglePort(port);
68  		initialConnection.setHost("localhost");
69  		initialConnection.setEncoding(Hl7V2EncodingTypeEnum.ER_7);
70  		return initialConnection;
71  	}
72  
73  	@Override
74  	public String exportConfigToXml() {
75  		StringWriter writer = new StringWriter();
76  		JAXB.marshal(this, writer);
77  		return writer.toString();
78  	}
79  
80  	public static OutboundConnectionList fromXml(Controller theController, String theXml) {
81  		OutboundConnectionList retVal = JAXB.unmarshal(new StringReader(theXml), OutboundConnectionList.class);
82  		
83  		for (OutboundConnection next : retVal.getConnections()) {
84  			next.setController(theController);
85  		}
86  		
87  		return retVal;
88  	}
89  
90  	/**
91  	 * @return the connections
92  	 */
93  	public List<OutboundConnection> getConnections() {
94  		return myConnections;
95  	}
96  
97  	public void removeConnecion(OutboundConnection theCon) {
98  		Validate.notNull(theCon);
99  		myConnections.remove(theCon);
100 		firePropertyChange(PROP_LIST, null, null);
101 	}
102 
103 	/**
104 	 * Remove all connections from this list which are not {@link AbstractConnection#isPersistent() persistent}
105 	 */
106 	public void removeNonPersistantConnections() {
107 		for (Iterator<OutboundConnection> iter = myConnections.iterator(); iter.hasNext(); ) {
108 			if (!iter.next().isPersistent()) {
109 				iter.remove();
110 			}
111 		}
112 	}
113 
114 	public OutboundConnection getWithId(String theId) {
115 		for (OutboundConnection next : myConnections) {
116 			if (next.getId().equals(theId)) {
117 				return next;
118 			}
119 		}
120 		return null;
121 	}
122 
123 
124 }