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