1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 }
94
95 public void addMessage(Hl7V2MessageCollection theMessageCollection) {
96 myMessages.add(theMessageCollection);
97 firePropertyChange(PROP_LIST, null, null);
98 }
99
100
101
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 }