import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import ca.uhn.hl7v2.HL7Exception; import ca.uhn.hl7v2.conf.ProfileException; import ca.uhn.hl7v2.conf.check.DefaultValidator; import ca.uhn.hl7v2.conf.parser.ProfileParser; import ca.uhn.hl7v2.conf.spec.RuntimeProfile; import ca.uhn.hl7v2.model.Message; //import ca.uhn.hl7v2.model.v231.message.*; //import ca.uhn.hl7v2.model.v24.message.*; //import ca.uhn.hl7v2.model.v251.message.*; //import ca.uhn.hl7v2.model.v25.message.*; //import ca.uhn.hl7v2.model.v26.message.*; //import ca.uhn.hl7v2.model.v25.*; import ca.uhn.hl7v2.parser.EncodingNotSupportedException; import ca.uhn.hl7v2.parser.FastParser; import ca.uhn.hl7v2.parser.PipeParser; import ca.uhn.hl7v2.validation.impl.NoValidation; //import ca.uhn.hl7v2.model.v25.message.RSP_K21; import ca.uhn.hl7v2.HL7Exception; import ca.uhn.hl7v2.conf.ProfileException; import ca.uhn.hl7v2.conf.parser.ProfileParser; import ca.uhn.hl7v2.conf.spec.RuntimeProfile; import ca.uhn.hl7v2.conf.store.CodeStore; import ca.uhn.hl7v2.conf.store.ProfileCodeStore; import ca.uhn.hl7v2.conf.store.ProfileStoreFactory; import ca.uhn.hl7v2.model.Message; import ca.uhn.hl7v2.parser.PipeParser; import ca.uhn.hl7v2.validation.impl.NoValidation; // those imports are required even if not explicitly used in this class!!! do not remove it import ca.uhn.hl7v2.model.v231.message.*; import ca.uhn.hl7v2.model.v24.message.*; import ca.uhn.hl7v2.model.v251.message.*; import ca.uhn.hl7v2.model.v25.message.*; import ca.uhn.hl7v2.model.v26.message.*; //import net.ihe.gazelle.iti.hl7v2.model.v25.message.QBP_Q21; //import net.ihe.gazelle.iti.hl7v2.model.v25.message.RSP_K22; //import net.ihe.gazelle.iti.hl7v2.model.v25.message.RSP_K23; import net.ihe.gazelle.laboratory.hl7v2.model.v25.message.QBP_Q11; import org.dom4j.io.SAXReader; import org.dom4j.*; import org.ietf.jgss.MessageProp; public class HapiValidator { public static String validateMessage( String version, String oidProfile, String messageType, String message, String packageNameForValidation, String messageProfilePath) { //Need to save this information on the dataBase. String oidUrl = "http://gazelle.ihe.net/InriaHL7MessageProfileRepository/viewProfile.seam?oid=" + oidProfile; SAXReader reader_xml = new SAXReader(); ProfileParser parser = new ProfileParser(false); RuntimeProfile profile = null; try { Document document; if (messageProfilePath != null && !messageProfilePath.isEmpty()) { document = reader_xml.read(messageProfilePath); } else { document = reader_xml.read(oidUrl); } profile = parser.parse(document.asXML()); profile.getMessage().setIdentifier(oidProfile); } catch (ProfileException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DocumentException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { CodeStore store = new ProfileCodeStore( "http://gazelle.ihe.net/InriaHL7MessageProfileRepository/viewTable.seam?version=" + version); ProfileStoreFactory.addCodeStore(store, profile.getMessage().getIdentifier()); } catch (IOException e) { System.out.println("unable to load table 1"); } catch (ProfileException e) { System.out.println("unable to load table 2"); } Message msg = null; //ca.uhn.hl7v2.model.v25.message.RSP_K19 msgRSP = null; try { //msg = (OMP_O09) (new PipeParser()).parse(message); //String messageProfilePackage = "ca.uhn.hl7v2.model.v"+ version + ".message." + messageType; //Class messageClass = Class.forName(messageProfilePackage); //Class messageClass = message.getClass(); //System.out.println("messageClass : " + messageClass.getName()); //msg = (OMP_O09) (new PipeParser()).parse(message); //With Hapi parsing //msg = (new PipeParser()).parse(message); //Without Hapi parsing PipeParser pipeParser = new PipeParser() ; pipeParser.setValidationContext(new NoValidation()); //Map messageMap = new HashMap(); //messageMap.put("component1", "RSP"); //messageMap.put("component2", "SLI"); //FastParser fastParser = new FastParser(messageMap); //msg = pipeParser.parse(message); //ihe.ca.uhn.hl7v2.model.v25.message.RSP_K11 rsp = new ihe.ca.uhn.hl7v2.model.v25.message.RSP_K11(); // try { // Class a = Class.forName("net.ihe.gazelle.iti.hl7v2.model.v25.message.QBP_Q21", false, null); // System.out.println(a.getPackage().toString()); // } catch (ClassNotFoundException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } msg = pipeParser.parseForSpecificPackage(message, packageNameForValidation); // net.ihe.gazelle.iti.hl7v2.model.v25.message. // net.ihe.gazelle.laboratory.hl7v2.model.v25.message.QBP_Q11 q11 = new QBP_Q11(); // q11.getQPD().getQpd3_PatientID() // k23.getQPD().get //msg = pipeParser.parse(message); //msg = fastParser.parse(message); System.out.println("Class du message : " + msg.getClass().toString()); //msgRSP = (RSP_K19) (msg); //messageClass.cast(msg); //msg = (OML_O33)(msg); } catch (HL7Exception e) { // TODO Auto-generated catch block System.out.println("HL7 Exception parsing : " + e.getMessage()); System.out.println("HL7 Exception getSegmentName : " + e.getSegmentName()); System.out.println("HL7 Exception getFieldPosition : " + e.getFieldPosition()); try { System.out.println("HL7 Exception Parsing : "); List hapiErrorList = new ArrayList() ; hapiErrorList.add(e); return HL7V2ValidationReport.formatValidationResult(hapiErrorList,null,false,false); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } e.printStackTrace(); } // Validate HL7Exception[] errors = null; try { //Modifier DefaultValidator d'Hapi //if (!typeClass.equals("Varies")) errors = new MyDefaultValidator().validate(msg, profile.getMessage()); //errors = new MyDefaultValidator().validate(msg, profile.getMessage()); } catch (ProfileException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (HL7Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // Each exception is a validation error List hapiErrorList = Arrays.asList(errors); if (hapiErrorList != null && hapiErrorList.size() != 0){ //System.out.println("Validation getFieldPosition **************: " + errorList.get(0).getMessage()); System.out.println("Validation getCause : " + Arrays.asList(errors).get(0).getErrorCode()); System.out.println("Validation getFieldPosition : " + Arrays.asList(errors).get(0).getFieldPosition()); //System.out.println("Validation getErrorCode : " + Arrays.asList(errors).get(0).getErrorCode()); System.out.println("Validation getLocalizedMessage : " + Arrays.asList(errors).get(0).getLocalizedMessage()); System.out.println("Validation getSegmentName : " + Arrays.asList(errors).get(0).getSegmentName()); //System.out.println("Validation getSegmentRepetition : " + Arrays.asList(errors).get(0).getSegmentRepetition()); //System.out.println("Validation getSegmentRepetition : " + Arrays.asList(errors).get(0).getSegmentRepetition()); //System.out.println("Validation getCause : " + Arrays.asList(errors).get(0).getCause()); //System.out.println("Validation getStackTrace : " + Arrays.asList(errors).get(0).getStackTrace()); } else System.out.println("No ERRORS"); String validationReport = ""; if (hapiErrorList == null || hapiErrorList.size() == 0) { try { validationReport = HL7V2ValidationReport.formatValidationResult(hapiErrorList,null,false,false); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { try { List warningList = new ArrayList(); List errorList = new ArrayList(); for (int i = 0; i < hapiErrorList.size(); i++) { if (hapiErrorList.get(i).getErrorCode() == 998 || hapiErrorList.get(i).getErrorCode() == 999) { warningList.add(hapiErrorList.get(i)); } else { errorList.add(hapiErrorList.get(i)); } } if (errorList.isEmpty()) { validationReport = HL7V2ValidationReport.formatValidationResult(errorList,warningList,false,false); } else { validationReport = HL7V2ValidationReport.formatValidationResult(errorList,warningList,false,true); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } //System.out.println("Report Validation result : " + validationReport); return validationReport; } }