View Javadoc

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 		@Override
106 		public void run() {
107 			try {
108 				SwingLogAppender.addListener(this);
109 				mySorter.sort();
110 			} catch (OperationCancelRequestedException e) {
111 				// user clicked "cancel"
112 			} catch (Exception e) {
113 				ourLog.error("Failed to sort", e);
114 				myMasterController.showDialogError("Failed with message: " + e.getMessage());
115 			} finally {
116 				SwingLogAppender.removeListener(this);
117 				myThread = null;
118 			}
119 		}
120 
121 		@Override
122 		public void handle(LoggingEvent theEvent, String theFormattedLine) {
123 			if (!theEvent.getLoggerName().startsWith(Hl7V2FileSorter.class.getName())) {
124 				return;
125 			}
126 
127 			if (!theEvent.getLevel().isGreaterOrEqual(Level.INFO)) {
128 				return;
129 			}
130 			
131 			String msg = (String) theEvent.getMessage();
132 			if (StringUtils.isNotBlank(msg)) {
133 				myView.addMessage(msg);
134 			}
135 		}
136 
137 	}
138 
139 	public static void main(String[] args) throws IOException, HL7Exception {
140 		
141 		FileWriter fw = new FileWriter("/Users/james/tmp/input.txt");
142 		
143 		// Create random file
144 		for (int i = 0; i < 10000; i++) {
145 			
146 			ADT_A01 msg = new ADT_A01();
147 			msg.initQuickstart("ADT", "A01", "T");
148 			String cid = Integer.toString((int)(Math.random() * Integer.MAX_VALUE));
149 			msg.getMSH().getMessageControlID().setValue(cid);
150 			fw.append(msg.encode());
151 			fw.append("\n\n");
152 		}
153 		
154 		fw.close();
155 	}
156 
157 	public void show() {
158 		myView.setVisible(true);
159 	}
160 	
161 }