001    /*
002     * Created on Apr 14, 2005
003     * HL7FileMsgCorrector.java
004     * 
005     */
006    package ca.uhn.hl7v2.app;
007    
008    import java.io.BufferedReader;
009    import java.io.BufferedWriter;
010    import java.io.FileWriter;
011    import java.io.IOException;
012    import java.io.InputStream;
013    import java.io.InputStreamReader;
014    
015    import org.apache.commons.logging.Log;
016    import org.apache.commons.logging.LogFactory;
017    
018    /**
019     * This is an application that will perform specific translations on the
020     * hl7 messages stored within a flat file.  It will output the new messages to 
021     * "./correctedMessageFile.txt"  
022     * 
023     * @author <a href="mailto:neal.acharya@uhn.on.ca">Neal Acharya</a>
024     * @version $Revision: 1.1 $ updated on $Date: 2007/02/19 02:24:37 $ by $Author: jamesagnew $
025     */
026    public class HL7FileMsgCorrector {
027        
028        private static final Log ourLog =  LogFactory.getLog(HL7FileMsgCorrector.class);
029    
030        /**
031         * 
032         */
033        public HL7FileMsgCorrector() {
034            super();
035            // TODO Auto-generated constructor stub
036        }
037    
038    
039        public static void main(String[] args) {
040            
041            //NOTE, depending on the size of the flat file you may need to increase the stack
042            //and heap size of the JVM when running this class.
043            //If so then use the following VM args -Xmx512m -Xms512m -Xss2m
044            
045            String fileName = "./QueuedRoutStatOrders.dat";
046            InputStream inputStream = HL7FileMsgCorrector.class.getClassLoader().getResourceAsStream(fileName);
047                  
048            try {
049                BufferedReader reader = new BufferedReader( new InputStreamReader( inputStream  ) );  
050                BufferedWriter writer = new BufferedWriter(new FileWriter("./correctedMessageFile.txt"));
051                
052                String lineRead = null;
053                StringBuffer buf = new StringBuffer();
054                while ((lineRead = reader.readLine()) != null) {                
055                    buf.append(lineRead);
056                    buf.append("\r");
057                }
058                String fileString = buf.toString().trim();
059                
060                //Perform a translation
061                String newFileString = addPreMshCarriageReturns(fileString);
062                
063                //write the string to a file
064                writer.write(newFileString);            
065                writer.close();
066                ourLog.info("file conversion completed");
067            }
068            catch (IOException e) {
069                // TODO Auto-generated catch block
070                e.printStackTrace();
071            }       
072            
073        }
074    
075    
076        /**
077         * @param theFileString ..
078         * @return ...
079         * Adds two carriage returns before each MSH segment
080         * we expect the file to be used by the HL7ServerTestHelper         
081         */
082        private static String addPreMshCarriageReturns(String theFileString) {
083            theFileString = theFileString.replaceAll(".MSH\\|", "\rMSH|");
084            theFileString = theFileString.replaceAll("MSH\\|", "\rMSH|");
085            theFileString = theFileString.replaceAll("//", "");
086            return theFileString;
087        }
088    }