View Javadoc

1   /*
2    * Created on Apr 14, 2005
3    * HL7FileMsgCorrector.java
4    * 
5    */
6   package ca.uhn.hl7v2.app;
7   
8   import java.io.BufferedReader;
9   import java.io.BufferedWriter;
10  import java.io.FileWriter;
11  import java.io.IOException;
12  import java.io.InputStream;
13  import java.io.InputStreamReader;
14  
15  import org.slf4j.Logger;
16  import org.slf4j.LoggerFactory;
17  
18  
19  /**
20   * This is an application that will perform specific translations on the
21   * hl7 messages stored within a flat file.  It will output the new messages to 
22   * "./correctedMessageFile.txt"  
23   * 
24   * @author <a href="mailto:neal.acharya@uhn.on.ca">Neal Acharya</a>
25   * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:37 $ by $Author: jamesagnew $
26   * @deprecated
27   */
28  public class HL7FileMsgCorrector {
29      
30      private static final Logger ourLog =  LoggerFactory.getLogger(HL7FileMsgCorrector.class);
31  
32      /**
33       * 
34       */
35      public HL7FileMsgCorrector() {
36          super();
37          // TODO Auto-generated constructor stub
38      }
39  
40  
41      public static void main(String[] args) {
42          
43          //NOTE, depending on the size of the flat file you may need to increase the stack
44          //and heap size of the JVM when running this class.
45          //If so then use the following VM args -Xmx512m -Xms512m -Xss2m
46          
47          String fileName = "./QueuedRoutStatOrders.dat";
48          InputStream inputStream = HL7FileMsgCorrector.class.getClassLoader().getResourceAsStream(fileName);
49                
50          try {
51              BufferedReader reader = new BufferedReader( new InputStreamReader( inputStream  ) );  
52              BufferedWriter writer = new BufferedWriter(new FileWriter("./correctedMessageFile.txt"));
53              
54              String lineRead = null;
55              StringBuffer buf = new StringBuffer();
56              while ((lineRead = reader.readLine()) != null) {                
57                  buf.append(lineRead);
58                  buf.append("\r");
59              }
60              String fileString = buf.toString().trim();
61              
62              //Perform a translation
63              String newFileString = addPreMshCarriageReturns(fileString);
64              
65              //write the string to a file
66              writer.write(newFileString);            
67              writer.close();
68              ourLog.info("file conversion completed");
69          }
70          catch (IOException e) {
71              // TODO Auto-generated catch block
72              e.printStackTrace();
73          }       
74          
75      }
76  
77  
78      /**
79       * @param theFileString ..
80       * @return ...
81       * Adds two carriage returns before each MSH segment
82       * we expect the file to be used by the HL7ServerTestHelper         
83       */
84      private static String addPreMshCarriageReturns(String theFileString) {
85          theFileString = theFileString.replaceAll(".MSH\\|", "\rMSH|");
86          theFileString = theFileString.replaceAll("MSH\\|", "\rMSH|");
87          theFileString = theFileString.replaceAll("//", "");
88          return theFileString;
89      }
90  }