View Javadoc

1   /**
2   The contents of this file are subject to the Mozilla Public License Version 1.1 
3   (the "License"); you may not use this file except in compliance with the License. 
4   You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
5   Software distributed under the License is distributed on an "AS IS" basis, 
6   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
7   specific language governing rights and limitations under the License. 
8   
9   The Original Code is "CorrectInvalidMessageBeforeParsing.java". 
10  
11  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
12  2001.  All Rights Reserved. 
13  
14  Contributor(s): ______________________________________. 
15  
16  Alternatively, the contents of this file may be used under the terms of the 
17  GNU General Public License (the  �GPL�), in which case the provisions of the GPL are 
18  applicable instead of those above.  If you wish to allow use of your version of this 
19  file only under the terms of the GPL and not to allow others to use your version 
20  of this file under the MPL, indicate your decision by deleting  the provisions above 
21  and replace  them with the notice and other provisions required by the GPL License.  
22  If you do not delete the provisions above, a recipient may use your version of 
23  this file under either the MPL or the GPL. 
24  
25  */
26  
27  package ca.uhn.hl7v2.examples;
28  
29  import ca.uhn.hl7v2.HL7Exception;
30  import ca.uhn.hl7v2.model.Message;
31  import ca.uhn.hl7v2.parser.EncodingNotSupportedException;
32  import ca.uhn.hl7v2.parser.Parser;
33  import ca.uhn.hl7v2.parser.PipeParser;
34  
35  /**
36   * Example
37   * 
38   * @author <a href="mailto:jamesagnew@sourceforge.net">James Agnew</a>
39   * @version $Revision: 1.2 $ updated on $Date: 2009-09-15 13:14:40 $ by $Author: jamesagnew $
40   */
41  public class CorrectInvalidMessageBeforeParsing
42  {
43  
44      /**
45       * @param args
46       * @throws HL7Exception 
47       */
48      public static void main(String[] args) throws HL7Exception {
49  
50          /*
51           * In an ideal world, systems would always send perfect messages, but in the real world, systems
52           * will always exist that send crazy data. HAPI tries to be tolerant of this, but occasionally
53           * a system will come along that sends something so wacky that HAPI is unable to parse it.
54           * 
55           * One possible solution to this is to manually fix such problems before parsing them. One
56           * easy way to do that is with a subclass of your parser.
57           */
58          
59          // This message has PI instead of PID in the second line:
60          String msg = "MSH|^~\\&|HIS|RIH|EKG|EKG|199904140038||ADT^A01||P|2.2\r"
61              + "PI|0001|00009874|00001122|A00977|SMITH^JOHN^M|MOM|19581119|F|NOTREAL^LINDA^M|C|564 SPRING ST^^NEEDHAM^MA^02494^US|0002|(818)565-1551|(425)828-3344|E|S|C|0000444444|252-00-4414||||SA|||SA||||NONE|V1|0001|I|D.ER^50A^M110^01|ER|P00055|11B^M011^02|070615^BATMAN^GEORGE^L|555888^NOTREAL^BOB^K^DR^MD|777889^NOTREAL^SAM^T^DR^MD^PHD|ER|D.WT^1A^M010^01|||ER|AMB|02|070615^NOTREAL^BILL^L|ER|000001916994|D||||||||||||||||GDD|WA|NORM|02|O|02|E.IN^02D^M090^01|E.IN^01D^M080^01|199904072124|199904101200|199904101200||||5555112333|||666097^NOTREAL^MANNY^P\r"
62              + "NK1|0222555|NOTREAL^JAMES^R|FA|STREET^OTHER STREET^CITY^ST^55566|(222)111-3333|(888)999-0000|||||||ORGANIZATION\r"
63              + "PV1|0001|I|D.ER^1F^M950^01|ER|P000998|11B^M011^02|070615^BATMAN^GEORGE^L|555888^OKNEL^BOB^K^DR^MD|777889^NOTREAL^SAM^T^DR^MD^PHD|ER|D.WT^1A^M010^01|||ER|AMB|02|070615^VOICE^BILL^L|ER|000001916994|D||||||||||||||||GDD|WA|NORM|02|O|02|E.IN^02D^M090^01|E.IN^01D^M080^01|199904072124|199904101200|||||5555112333|||666097^DNOTREAL^MANNY^P\r"
64              + "PV2|||0112^TESTING|55555^PATIENT IS NORMAL|NONE|||19990225|19990226|1|1|TESTING|555888^NOTREAL^BOB^K^DR^MD||||||||||PROD^003^099|02|ER||NONE|19990225|19990223|19990316|NONE\r"
65              + "AL1||SEV|001^POLLEN\r"
66              + "GT1||0222PL|NOTREAL^BOB^B||STREET^OTHER STREET^CITY^ST^77787|(444)999-3333|(222)777-5555||||MO|111-33-5555||||NOTREAL GILL N|STREET^OTHER STREET^CITY^ST^99999|(111)222-3333\r"
67              + "IN1||022254P|4558PD|BLUE CROSS|STREET^OTHER STREET^CITY^ST^00990||(333)333-6666||221K|LENIX|||19980515|19990515|||PATIENT01 TEST D||||||||||||||||||02LL|022LP554";
68  
69          // Let's create a special parser to handle this:
70          Parser parser = new PipeParser() {
71  
72              // We override the parse method to correct issues
73              public Message parse(String theMessage) throws HL7Exception, EncodingNotSupportedException {
74                  theMessage = theMessage.replace("\rPI|", "\rPID|");
75                  return super.parse(theMessage);
76              }};
77          
78          Message message = parser.parse(msg);
79          
80          System.out.println(parser.encode(message));
81          
82          /* PI has been fixed:
83           
84              MSH|^~\&|HIS|RIH|EKG|EKG|199904140038||ADT^A01||P|2.2
85              PID|0001|00009874|00001122|A00977|SMITH^JOHN^M|MOM|19581119|F|NOTREAL^LINDA^M|C|564 SPRING ST^^NEEDHAM^MA^02494^US|0002|(818)565-1551|(425)828-3344|E|S|C|0000444444|252-00-4414||||SA|||SA||||NONE|V1|0001|I|D.ER^50A^M110^01|ER|P00055|11B^M011^02|070615^BATMAN^GEORGE^L|555888^NOTREAL^BOB^K^DR^MD|777889^NOTREAL^SAM^T^DR^MD^PHD|ER|D.WT^1A^M010^01|||ER|AMB|02|070615^NOTREAL^BILL^L|ER|000001916994|D||||||||||||||||GDD|WA|NORM|02|O|02|E.IN^02D^M090^01|E.IN^01D^M080^01|199904072124|199904101200|199904101200||||5555112333|||666097^NOTREAL^MANNY^P
86              NK1|0222555|NOTREAL^JAMES^R|FA|STREET^OTHER STREET^CITY^ST^55566|(222)111-3333|(888)999-0000|||||||ORGANIZATION
87              PV1|0001|I|D.ER^1F^M950^01|ER|P000998|11B^M011^02|070615^BATMAN^GEORGE^L|555888^OKNEL^BOB^K^DR^MD|777889^NOTREAL^SAM^T^DR^MD^PHD|ER|D.WT^1A^M010^01|||ER|AMB|02|070615^VOICE^BILL^L|ER|000001916994|D||||||||||||||||GDD|WA|NORM|02|O|02|E.IN^02D^M090^01|E.IN^01D^M080^01|199904072124|199904101200|||||5555112333|||666097^DNOTREAL^MANNY^P
88              PV2|||0112^TESTING|55555^PATIENT IS NORMAL|NONE|||19990225|19990226|1|1|TESTING|555888^NOTREAL^BOB^K^DR^MD||||||||||PROD^003^099|02|ER||NONE|19990225|19990223|19990316|NONE
89              AL1||SEV|001^POLLEN
90              GT1||0222PL|NOTREAL^BOB^B||STREET^OTHER STREET^CITY^ST^77787|(444)999-3333|(222)777-5555||||MO|111-33-5555||||NOTREAL GILL N|STREET^OTHER STREET^CITY^ST^99999|(111)222-3333
91              IN1||022254P|4558PD|BLUE CROSS|STREET^OTHER STREET^CITY^ST^00990||(333)333-6666||221K|LENIX|||19980515|19990515|||PATIENT01 TEST D||||||||||||||||||02LL|022LP554
92           */
93          
94      }
95  
96  }