1 package ca.uhn.hl7v2.testpanel.controller;
2
3 import java.io.File;
4 import java.io.FileWriter;
5 import java.io.IOException;
6
7 import javax.swing.Icon;
8 import javax.swing.JOptionPane;
9 import javax.swing.SwingUtilities;
10
11 import org.apache.commons.lang.StringUtils;
12 import org.apache.log4j.Level;
13 import org.apache.log4j.spi.LoggingEvent;
14
15 import ca.uhn.hl7v2.HL7Exception;
16 import ca.uhn.hl7v2.model.v25.message.ADT_A01;
17 import ca.uhn.hl7v2.testpanel.ui.tools.Hl7V2FileSortDialog;
18 import ca.uhn.hl7v2.testpanel.util.Hl7V2FileSorter;
19 import ca.uhn.hl7v2.testpanel.util.IProgressCallback.OperationCancelRequestedException;
20 import ca.uhn.hl7v2.testpanel.util.SwingLogAppender;
21 import ca.uhn.hl7v2.testpanel.util.SwingLogAppender.ILogListener;
22
23 public class Hl7V2FileSortController {
24
25 private static final String OVERWRITE = "Overwrite";
26 private static final String APPEND = "Append";
27 private static final String CANCEL = "Cancel";
28 private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(Hl7V2FileSortController.class);
29
30 private final Hl7V2FileSortDialog myView;
31 private final Controller myMasterController;
32 private RunningThread myThread;
33
34 public Hl7V2FileSortController(Controller theMasterController) {
35 myMasterController = theMasterController;
36 myView = new Hl7V2FileSortDialog(this);
37 }
38
39 public void begin() {
40 assert SwingUtilities.isEventDispatchThread() : "Shouldn't be called from " + Thread.currentThread().getName();
41 if (myThread != null) {
42 return;
43 }
44
45 File inputFile = new File(myView.getInputFileText());
46 if (!inputFile.exists() || !inputFile.canRead()) {
47 myMasterController.showDialogError("Input file does not exist or can not be read:\n" + myView.getInputFileText());
48 return;
49 }
50
51 File outputFile = new File(myView.getOutputFileText());
52 if (outputFile.isDirectory()) {
53 myMasterController.showDialogError("Output file is a directory:\n" + myView.getOutputFileText());
54 return;
55 }
56
57 File outputDir = outputFile.getParentFile();
58 if (!outputDir.exists()) {
59 myMasterController.showDialogError("Output file directory does not exist:\n" + outputDir.getAbsolutePath());
60 return;
61 }
62
63 boolean append = false;
64 if (outputFile.exists()) {
65 String message = "Output file already exists:\n" + outputFile.getName();
66 String title = "TestPanel - File Exists";
67 int messageType = JOptionPane.WARNING_MESSAGE;
68 Icon icon = null;
69 String[] selectionValues = { APPEND, OVERWRITE };
70 String initialSelectionValue = Prefs.getInstance().getHl7V2SortOverwriteMode();
71 String opt = (String) JOptionPane.showInputDialog(myView, message, title, messageType, icon, selectionValues, initialSelectionValue);
72 if (opt == APPEND) {
73 Prefs.getInstance().setHl7V2SortOverwriteMode(opt);
74 append = true;
75 } else if (opt == OVERWRITE) {
76 Prefs.getInstance().setHl7V2SortOverwriteMode(opt);
77 append = false;
78 } else if (opt == CANCEL) {
79 Prefs.getInstance().setHl7V2SortOverwriteMode(opt);
80 return;
81 } else {
82 return;
83 }
84 }
85
86 Hl7V2FileSorter sorter = new Hl7V2FileSorter();
87 sorter.setComparator(myView.provideMessageComparator());
88 sorter.setProgressCallback(myView.provideProgressCallback());
89 sorter.setInputFile(inputFile);
90 sorter.setOutputFile(outputFile);
91 sorter.setAppendOutputFile(append);
92
93 myThread = new RunningThread(sorter);
94 myMasterController.invokeInBackground(myThread);
95 }
96
97 private class RunningThread implements Runnable, ILogListener {
98
99 private Hl7V2FileSorter mySorter;
100
101 public RunningThread(Hl7V2FileSorter theSorter) {
102 mySorter = theSorter;
103 }
104
105 public void run() {
106 try {
107 SwingLogAppender.addListener(this);
108 mySorter.sort();
109 } catch (OperationCancelRequestedException e) {
110
111 } catch (Exception e) {
112 ourLog.error("Failed to sort", e);
113 myMasterController.showDialogError("Failed with message: " + e.getMessage());
114 } finally {
115 SwingLogAppender.removeListener(this);
116 myThread = null;
117 }
118 }
119
120 public void handle(LoggingEvent theEvent, String theFormattedLine) {
121 if (!theEvent.getLoggerName().startsWith(Hl7V2FileSorter.class.getName())) {
122 return;
123 }
124
125 if (!theEvent.getLevel().isGreaterOrEqual(Level.INFO)) {
126 return;
127 }
128
129 String msg = (String) theEvent.getMessage();
130 if (StringUtils.isNotBlank(msg)) {
131 myView.addMessage(msg);
132 }
133 }
134
135 }
136
137 public static void main(String[] args) throws IOException, HL7Exception {
138
139 FileWriter fw = new FileWriter("/Users/james/tmp/input.txt");
140
141
142 for (int i = 0; i < 10000; i++) {
143
144 ADT_A01 msg = new ADT_A01();
145 msg.initQuickstart("ADT", "A01", "T");
146 String cid = Integer.toString((int)(Math.random() * Integer.MAX_VALUE));
147 msg.getMSH().getMessageControlID().setValue(cid);
148 fw.append(msg.encode());
149 fw.append("\n\n");
150 }
151
152 fw.close();
153 }
154
155 public void show() {
156 myView.setVisible(true);
157 }
158
159 }