View Javadoc

1   package ca.uhn.hl7v2.testpanel.model.conf;
2   
3   import java.io.File;
4   import java.io.FileReader;
5   import java.io.IOException;
6   import java.io.StringWriter;
7   import java.util.ArrayList;
8   import java.util.Iterator;
9   import java.util.List;
10  import java.util.UUID;
11  
12  import javax.xml.bind.JAXB;
13  import javax.xml.bind.annotation.XmlAccessType;
14  import javax.xml.bind.annotation.XmlAccessorType;
15  import javax.xml.bind.annotation.XmlElement;
16  import javax.xml.bind.annotation.XmlType;
17  
18  import org.apache.commons.lang.StringUtils;
19  
20  import ca.uhn.hl7v2.conf.ProfileException;
21  import ca.uhn.hl7v2.testpanel.model.AbstractModelClass;
22  
23  @XmlAccessorType(XmlAccessType.FIELD)
24  @XmlType(name = "ProfileGroup")
25  public class ProfileGroup extends AbstractModelClass implements Cloneable {
26  
27  	public static final String PROP_NAME = ProfileGroup.class.getName() + "_NAME";
28  
29  	public static final String PROP_PROFILES = ProfileGroup.class.getName() + "_PROFILES";
30  
31  	@XmlElement(name = "entry")
32  	private List<Entry> myEntries;
33  	
34  	@XmlElement(name = "id")
35  	private String myId;
36  
37  	@XmlElement(name = "name")
38  	private String myName;
39  
40  	@XmlElement(name = "sourceUrl")
41  	private String mySourceUrl;
42  
43  	public ProfileGroup() {
44  		super();
45  		
46  		myId = UUID.randomUUID().toString();
47  	}
48  	
49  	public ProfileGroup(ProfileProxy theFile) throws IOException, ProfileException {
50  		this();
51  		Entry e = new Entry();
52  		e.setEventType("*");
53  		e.setMessageType("*");
54  		e.setProfileProxy(theFile);
55  		e.myParentProfileGroup = this;
56  		getEntries().add(e);
57  		
58  		myName = theFile.getProfile().getName();
59  	}
60  
61  	public ProfileGroup(String theName) {
62  		this();
63  		myName = theName;
64  	}
65  
66  	public void addProfile(ProfileProxy theProfile) {
67  		Entry entry = new Entry();
68  		entry.setMessageType(theProfile.getStructureMessageType());
69  		entry.setEventType(theProfile.getStructureEventType());
70  		entry.setParentProfileGroup(this);
71  		entry.setProfileProxy(theProfile);
72  		myEntries.add(entry);
73  		firePropertyChange(PROP_PROFILES, null, entry);
74  	}
75  
76  	/* (non-Javadoc)
77  	 * @see java.lang.Object#clone()
78  	 */
79  	@Override
80  	 public ProfileGroup clone() throws CloneNotSupportedException {
81  		throw new UnsupportedOperationException();
82  	}
83  
84  
85  	@Override
86  	public String exportConfigToXml() {
87  		StringWriter writer = new StringWriter();
88  		JAXB.marshal(this, writer);
89  		return writer.toString();
90  	}
91  
92  	/**
93  	 * @return the entries
94  	 */
95  	public List<Entry> getEntries() {
96  		if (myEntries == null) {
97  			myEntries = new ArrayList<ProfileGroup.Entry>();
98  		}
99  		return myEntries;
100 	}
101 
102 	/**
103 	 * @return the id
104 	 */
105 	public String getId() {
106 		return myId;
107 	}
108 
109 	/**
110 	 * @return the name
111 	 */
112 	public String getName() {
113 		return myName;
114 	}
115 
116 	public Entry getProfileForMessage(String theEventType, String theTrigger) throws IOException, ProfileException {
117 		for (Entry next : myEntries) {
118 			if (next.getMessageType().equals("*") || next.getMessageType().equals(theEventType)) {
119 				if (theTrigger == null || StringUtils.isBlank(theTrigger) || next.getEventType().equals("*") || next.getEventType().equals(theTrigger)) {
120 					return next;
121 				}
122 			}
123 		}
124 		return null;
125 	}
126 
127 //	/**
128 //	 * @return the tableFiles
129 //	 */
130 //	public List<TableFile> getTableFiles() {
131 //		if (myTableFiles == null) {
132 //			myTableFiles = new ArrayList<TableFile>();
133 //		}
134 //		return myTableFiles;
135 //	}
136 
137 	/**
138 	 * @return the sourceUrl
139 	 */
140 	public String getSourceUrl() {
141 		return mySourceUrl;
142 	}
143 
144 	public void removeEntry(Entry theSel) {
145 		for (Iterator<Entry> iter = myEntries.iterator(); iter.hasNext(); ) {
146 			if (iter.next() == theSel) {
147 				iter.remove();
148 			}
149 		}
150 		firePropertyChange(PROP_PROFILES, null, null);
151 	}
152 
153 	/**
154 	 * @param theId the id to set
155 	 */
156 	public void setId(String theId) {
157 		myId = theId;
158 	}
159 
160 	public void setName(String theName) {
161 		String oldValue = myName;
162 		myName = theName;
163 		firePropertyChange(PROP_NAME, oldValue, theName);
164 	}
165 
166 	public void setSourceUrl(String theSourceUrl) {
167 		mySourceUrl =theSourceUrl;
168 	}
169 
170 	public static ProfileGroup createFromRuntimeProfile(String theProfileString) throws IOException, ProfileException {
171 		ProfileGroup retVal = new ProfileGroup(ProfileProxy.createFromProfileString(theProfileString));
172 		return retVal;
173 	}
174 
175 	public static ProfileGroup readFromFile(File theFile) throws IOException {
176 		FileReader r = new FileReader(theFile);
177 		ProfileGroup retVal = JAXB.unmarshal(r, ProfileGroup.class);
178 		if (StringUtils.isBlank(retVal.getId())) {
179 			throw new IOException("Invalid file, no ID tag found");
180 		}
181 		return retVal;
182 	}
183 
184 	@XmlAccessorType(XmlAccessType.FIELD)
185 	@XmlType(name = "ProfileGroupEntry")
186 	public static class Entry extends AbstractModelClass {
187 		
188 		public static final String PROP_DESC = Entry.class.getName() + "_DESC";
189 		
190 		@XmlElement(name = "evtType")
191 		private String myEventType;
192 		
193 		@XmlElement(name = "msgType")
194 		private String myMessageType;
195 
196 		private transient ProfileGroup myParentProfileGroup;
197 
198 		@XmlElement(name = "profile")
199 		private ProfileProxy myProfileProxy;
200 
201 		@XmlElement(name = "tablesId")
202 		private String myTablesId;
203 		
204 		@Override
205 		public Object exportConfigToXml() {
206 			// TODO Auto-generated method stub
207 			return null;
208 		}
209 
210 		/**
211 		 * @return the eventType
212 		 */
213 		public String getEventType() {
214 			return myEventType;
215 		}
216 
217 		/**
218 		 * @return the messageType
219 		 */
220 		public String getMessageType() {
221 			return myMessageType;
222 		}
223 
224 		/**
225 		 * @return the profileGroup
226 		 */
227 		public ProfileGroup getParentProfileGroup() {
228 			return myParentProfileGroup;
229 		}
230 
231 		/**
232 		 * @return the profileProxy
233 		 */
234 		public ProfileProxy getProfileProxy() {
235 			return myProfileProxy;
236 		}
237 
238 		/**
239 		 * @return the tablesId
240 		 */
241 		public String getTablesId() {
242 			return myTablesId;
243 		}
244 
245 		/**
246 		 * @param theEventType
247 		 *            the eventType to set
248 		 */
249 		public void setEventType(String theEventType) {
250 			String oldValue = myEventType;
251 			myEventType = theEventType;
252 			firePropertyChange(PROP_DESC, oldValue, myEventType);
253 		}
254 
255 		/**
256 		 * @param theMessageType
257 		 *            the messageType to set
258 		 */
259 		public void setMessageType(String theMessageType) {
260 			String oldValue = myMessageType;
261 			myMessageType = theMessageType;
262 			firePropertyChange(PROP_DESC, oldValue, myMessageType);
263 		}
264 
265 		public void setParentProfileGroup(ProfileGroup theGroup) {
266 			myParentProfileGroup = theGroup;
267 		}
268 
269 		/**
270 		 * @param theProfileProxy
271 		 *            the profileProxy to set
272 		 */
273 		public void setProfileProxy(ProfileProxy theProfileProxy) {
274 			myProfileProxy = theProfileProxy;
275 		}
276 
277 		/**
278 		 * @param theTablesId
279 		 *            the tablesId to set
280 		 */
281 		public void setTablesId(String theTablesId) {
282 			String oldValue = myTablesId;
283 			myTablesId = theTablesId;
284 			firePropertyChange(PROP_DESC, oldValue, myTablesId);
285 		}
286 	}
287 	
288 }