import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import org.jdom.Document; import org.jdom.Element; import org.jdom.ProcessingInstruction; import org.jdom.input.SAXBuilder; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; import ca.uhn.hl7v2.HL7Exception; public class HL7V2ValidationReport { /** * Takes a list of notifications sent by validator and format the result * * @param notifications * @param isAborted : did the validation comes to its end or not * @param isFailed TODO * @param objectType : the type of object being validating * @param isXmlWellFormed * @param isValidCDA * @param isValidEpsos * @return * @throws Exception */ public static String formatValidationResult(List errorList,List warningList, boolean isAborted, boolean isFailed) throws Exception { // root Element root = new Element("SummaryResults"); // document Document result = new Document(root); Element overview = new Element("ValidationResultsOverview"); Calendar cal = Calendar.getInstance(); // validation date Element date = new Element("ValidationDate"); final String DATE_FORMAT_DAY = "yyyy, MM dd"; SimpleDateFormat sdfDate = new SimpleDateFormat(DATE_FORMAT_DAY, Locale.ENGLISH); date.addContent(sdfDate.format(cal.getTime())); overview.addContent(date); // validation time Element time = new Element("ValidationTime"); final String DATE_FORMAT_TIME = "hh:mm (aa)"; SimpleDateFormat sdfTime = new SimpleDateFormat(DATE_FORMAT_TIME, Locale.ENGLISH); time.addContent(sdfTime.format(cal.getTime())); overview.addContent(time); // validation service version Element serviceVersion = new Element("ValidationServiceVersion"); //*****************************A CHANGER************************* //serviceVersion.addContent(VersionConstants.EAR_VERSION); serviceVersion.addContent("1.4"); overview.addContent(serviceVersion); // validation service name Element serviceName = new Element("ValidationServiceName"); serviceName.addContent("HL7V2.x EVS"); overview.addContent(serviceName); // counters for each type of notification Integer errorCount = 0; Integer warningCount = 0; // Integer noteCount = 0; // Integer unknownCount = 0; // Integer reportCount = 0; //Integer checkCount = 0; Element resultStatus = new Element("ValidationTestResult"); Element validationResult = new Element("ValidationResults"); Element resultXML = new Element("ResultXML"); if ((errorList == null || errorList.isEmpty()) && (warningList == null || warningList.isEmpty())) { if (isAborted) resultStatus.addContent("ABORTED"); if (isFailed) resultStatus.addContent("FAILED"); else resultStatus.addContent("PASSED"); } else { if (errorList != null && !errorList.isEmpty()) { for (HL7Exception error : errorList) { errorCount ++; Element xmlNotification = new Element("Error"); Element location = new Element("Location"); location.addContent(error.getSegmentName() + "-" + error.getFieldPosition()); xmlNotification.addContent(location); Element description = new Element("Description"); description.addContent(error.getMessage()); xmlNotification.addContent(description); resultXML.addContent(xmlNotification); } } if (warningList != null && !warningList.isEmpty()) { for (HL7Exception warning : warningList) { warningCount ++; Element xmlNotification = new Element("Warning"); Element location = new Element("Location"); location.addContent(warning.getSegmentName() + "-" + warning.getFieldPosition()); xmlNotification.addContent(location); Element description = new Element("Description"); description.addContent(warning.getMessage()); xmlNotification.addContent(description); resultXML.addContent(xmlNotification); } } validationResult.addContent(resultXML); if (isAborted) resultStatus.addContent("ABORTED"); else if (isFailed) resultStatus.addContent("FAILED"); else if (errorCount > 0) resultStatus.addContent("FAILED"); else resultStatus.addContent("PASSED"); //checkCount = errorList.size(); } overview.addContent(resultStatus); root.addContent(overview); // counters Element validationCounters = new Element("ValidationCounters"); /* Element checks = new Element("NrOfChecks"); checks.addContent(Integer.toString(checkCount)); validationCounters.addContent(checks); */ Element errors = new Element("NrOfValidationErrors"); errors.addContent(Integer.toString(errorCount)); validationCounters.addContent(errors); Element warnings = new Element("NrOfValidationWarnings"); warnings.addContent(Integer.toString(warningCount)); validationCounters.addContent(warnings); /* Element notes = new Element("NrOfValidationNotes"); notes.addContent(Integer.toString(noteCount)); validationCounters.addContent(notes); Element reports = new Element("NrOfValidationReports"); reports.addContent(Integer.toString(reportCount)); validationCounters.addContent(reports); Element unknowns = new Element("NrOfValidationUnknown"); unknowns.addContent(Integer.toString(unknownCount)); validationCounters.addContent(unknowns); */ root.addContent(validationCounters); root.addContent(validationResult); // add a line to declare the location of the stylesheet to use // HashMap parameters = new HashMap(); parameters.put("type", "text/xsl"); parameters.put("href", "XSLT_LOCATION"); ProcessingInstruction pi = new ProcessingInstruction("xml-stylesheet", parameters); result.getContent().add(0, pi); Format format = Format.getPrettyFormat(); return new XMLOutputter(format).outputString(result); } }