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;
27  
28  import java.io.File;
29  import java.io.FileNotFoundException;
30  import java.io.FileOutputStream;
31  import java.io.IOException;
32  import java.io.OutputStreamWriter;
33  import java.io.Writer;
34  import java.nio.charset.Charset;
35  import java.util.ArrayList;
36  import java.util.Arrays;
37  import java.util.Collections;
38  import java.util.Comparator;
39  import java.util.List;
40  
41  import org.apache.commons.lang.StringUtils;
42  import org.slf4j.Logger;
43  import org.slf4j.LoggerFactory;
44  
45  import ca.uhn.hl7v2.hoh.util.IOUtils;
46  import ca.uhn.hl7v2.testpanel.controller.Controller;
47  import ca.uhn.hl7v2.testpanel.model.msg.Hl7V2MessageCollection;
48  import ca.uhn.hl7v2.testpanel.util.FileUtils;
49  
50  public class MessagesList extends AbstractModelClass {
51  
52  	private static final Logger ourLog = LoggerFactory.getLogger(MessagesList.class);
53  	public static final String PROP_LIST = MessagesList.class.getName() + "_LIST";
54  
55  	private Controller myController;
56  	private List<Hl7V2MessageCollection> myMessages = new ArrayList<Hl7V2MessageCollection>();
57  
58  	public MessagesList(Controller theController) {
59  		myController = theController;
60  		
61  //		Message initialMessage;
62  //		String string = "MSH|^~\\&|ULTRA|TML|OLIS|OLIS|200905011130||ORU^R01|20169838|T|2.3\r" // -
63  //				+ "ZPI|||7005728^^^TML^MR||TEST^RACHEL^DIAMOND||19310313|F|||200 ANYWHERE ST^^TORONTO^ON^M6G 2T9||(416)888-8888||||||1014071185^KR\r" // -
64  //				+ "PID|||7005728^^^TML^MR||TEST^RACHEL^DIAMOND||19310313|F|||200 ANYWHERE ST^^TORONTO^ON^M6G 2T9||(416)888-8888||||||1014071185^KR\r" // -
65  //				+ "PV1|1||OLIS||||OLIST^BLAKE^DONALD^THOR^^^^^921379^^^^OLIST\r" // -
66  //				+ "ORC|RE||T09-100442-RET-0^^OLIS_Site_ID^ISO|||||||||OLIST^BLAKE^DONALD^THOR^^^^L^921379\r" // -
67  //				+ "OBR|0||T09-100442-RET-0^^OLIS_Site_ID^ISO|RET^RETICULOCYTE COUNT^HL79901 literal|||200905011106|||||||200905011106||OLIST^BLAKE^DONALD^THOR^^^^L^921379||7870279|7870279|T09-100442|MOHLTC|200905011130||B7|F||1^^^200905011106^^R\r" // -
68  //				+ "OBX|1|IS|Z114099^Erc^L||ABC||||||F|||200905011111|PMH\r";
69  		// initialMessage = new ORU_R01();
70  		// initialMessage.parse(string);
71  //		Hl7V2MessageCollection initialMessageObj = new Hl7V2MessageCollection();
72  
73  		// try {
74  		// InputStream is =
75  		// MessagesList.class.getClassLoader().getResourceAsStream("testpanel/profiles/ADT_A01.xml");
76  		// StringBuffer profileString = new StringBuffer();
77  		// byte[] buffer = new byte[1000];
78  		// int bytesRead;
79  		// while ((bytesRead = is.read(buffer)) > 0) {
80  		// profileString.append(new String(buffer, 0, bytesRead));
81  		// }
82  		// RuntimeProfile rp = new
83  		// ProfileParser(false).parse(profileString.toString());
84  		// initialMessageObj.setRuntimeProfile(rp);
85  		// } catch (Exception e) {
86  		// throw new Error(e);
87  		// }
88  
89  //		initialMessageObj.setSourceMessage(string);
90  
91  //		myMessages.add(initialMessageObj);
92  
93  	}
94  
95  	public void addMessage(Hl7V2MessageCollection theMessageCollection) {
96  		myMessages.add(theMessageCollection);
97  		firePropertyChange(PROP_LIST, null, null);
98  	}
99  
100 	/**
101 	 * Save all files to work directory
102 	 */
103 	public void dumpToWorkDirectory(File theWorkfilesDir) throws IOException {
104 		ourLog.info("Flushing work files to directory: " + theWorkfilesDir.getAbsolutePath());
105 
106 		IOUtils.deleteAllFromDirectory(theWorkfilesDir);
107 
108 		int index = 0;
109 		for (Hl7V2MessageCollection next : myMessages) {
110 			index++;
111 			String seq = StringUtils.leftPad(Integer.toString(index), 10, '0');
112 
113 			File nextFile = new File(theWorkfilesDir, next.getId() + "-" + seq + ".xml");
114 			nextFile.delete();
115 			Writer nextWriter = new OutputStreamWriter(new FileOutputStream(nextFile), Charset.forName("UTF-8"));
116 			nextWriter.append(next.exportConfigToXml());
117 			nextWriter.close();
118 		}
119 
120 	}
121 
122 	@Override
123 	public Object exportConfigToXml() {
124 		return null;
125 	}
126 
127 	public List<Hl7V2MessageCollection> getMessages() {
128 		return Collections.unmodifiableList(myMessages);
129 	}
130 
131 	public Hl7V2MessageCollection getWithId(String theId) {
132 		for (Hl7V2MessageCollection next : myMessages) {
133 			if (next.getId().equals(theId)) {
134 				return next;
135 			}
136 		}
137 		return null;
138 	}
139 
140 	public void removeMessage(Hl7V2MessageCollection theMsg) {
141 		myMessages.remove(theMsg);
142 		firePropertyChange(PROP_LIST, null, null);
143 	}
144 
145 	public void restoreFromWorkDirectory(File theWorkfilesDir) {
146 		myMessages.clear();
147 
148 		File[] listFiles = theWorkfilesDir.listFiles();
149 		Arrays.sort(listFiles, new Comparator<File>() {
150 			public int compare(File theO1, File theO2) {
151 				String n1 = theO1.getAbsolutePath();
152 				String n2 = theO2.getAbsolutePath();
153 				int lastDash1 = n1.lastIndexOf('-');
154 				int lastDash2 = n2.lastIndexOf('-');
155 
156 				if (lastDash1 == -1 && lastDash2 == -1) {
157 					return 0;
158 				}
159 				if (lastDash1 == -1) {
160 					return -1;
161 				}
162 				if (lastDash2 == -1) {
163 					return 1;
164 				}
165 				return lastDash2 - lastDash1;
166 			}
167 		});
168 
169 		for (File next : listFiles) {
170 			if (next.getAbsolutePath().toLowerCase().endsWith(".xml") == false) {
171 				continue;
172 			}
173 
174 			ourLog.info("Restoring work file: {}", next.getName());
175 			String contents;
176 			try {
177 				contents = FileUtils.readFile(next);
178 			} catch (FileNotFoundException e) {
179 				ourLog.error("Failed to restore work file", e);
180 				continue;
181 			} catch (IOException e) {
182 				ourLog.error("Failed to restore work file", e);
183 				continue;
184 			}
185 
186 			Hl7V2MessageCollection nextMsg = Hl7V2MessageCollection.fromXml(myController.getProfileFileList(), contents);
187 			if (StringUtils.isNotBlank(nextMsg.getSaveFileName())) {
188 				try {
189 					File saveFile = new File(nextMsg.getSaveFileName());
190 					contents = FileUtils.readFile(saveFile, nextMsg.getSaveCharset());
191 					
192 					nextMsg.setSourceMessage(contents);
193 					nextMsg.setSaveFileTimestamp(next.lastModified());
194 					nextMsg.setSaved(true);
195 				} catch (FileNotFoundException e) {
196 					myController.showDialogError("Could not restore the following file as it could not be found:<br>" + nextMsg.getSaveFileName());
197 					continue;
198 				} catch (IOException e) {
199 					myController.showDialogError("Could not restore the following file.<br>Error: " + e.getMessage() + "<br>File: " + nextMsg.getSaveFileName());
200 					continue;
201 				}
202 			}
203 			
204 			myMessages.add(nextMsg);
205 
206 		}
207 	}
208 
209 }