org.ehealth_connector.demo.iti.xd.DemoDocSource.java Source code

Java tutorial

Introduction

Here is the source code for org.ehealth_connector.demo.iti.xd.DemoDocSource.java

Source

/*******************************************************************************
 *
 * The authorship of this code and the accompanying materials is held by medshare GmbH, Switzerland.
 * All rights reserved. http://medshare.net
 *
 * Project Team: https://sourceforge.net/p/ehealthconnector/wiki/Team/
 *
 * This code is are made available under the terms of the Eclipse Public License v1.0.
 *
 * Accompanying materials are made available under the terms of the Creative Commons
 * Attribution-ShareAlike 4.0 License.
 *
 * Year of publication: 2015
 *
 *******************************************************************************/

package org.ehealth_connector.demo.iti.xd;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.xml.DOMConfigurator;
import org.ehealth_connector.cda.ch.enums.LanguageCode;
import org.ehealth_connector.common.Code;
import org.ehealth_connector.common.Identificator;
import org.ehealth_connector.communication.ConvenienceCommunication;
import org.ehealth_connector.communication.Destination;
import org.ehealth_connector.communication.DocumentMetadata;
import org.ehealth_connector.demo.iti.pix.DemoMPIClient;
import org.openhealthtools.ihe.xds.document.DocumentDescriptor;
import org.openhealthtools.ihe.xds.response.XDSResponseType;

public class DemoDocSource {

    private final Log log = LogFactory.getLog(DemoDocSource.class);

    /**
     * String to display after the demo has run
     */
    private static StringBuffer outStr = new StringBuffer();

    // NIST Repository
    public static final String NIST = "http://ihexds.nist.gov/tf6/services/xdsrepositoryb";

    // NIST SECURED Repository (query interface)
    public static final String NIST_SECURED = "https://ihexds.nist.gov:12091/tf6/services/xdsrepositoryb";
    // Keystore for secured communication
    public static final String KEY_STORE = "/demoDocSource/security/keystore";
    public static final String KEY_STORE_PASS = "nistbill";

    // The ID of your Organization
    public static final String ORGANIZATIONAL_ID = "1.3.6.1.4.1.21367.2010.1.2.666";

    // One PDF and one CDA Document that will be transfered
    public static final String pdfFilePath = "/demoDocSource/patientconsent.pdf";
    public static final String cdaFilePath = "/demoDocSource/CDA-CH-VACD_Impfausweis.xml";

    /**
     * Returns the results of the demo
     * 
     * @return results of the demo
     */
    public static String getOutputString() {
        return outStr.toString();
    }

    /**
     * Executes some demo methods
     * 
     */
    public void doDemo() {

        // switch off logging for console output
        DOMConfigurator.configure(DemoMPIClient.class.getResource("/log4jConfigs/log4j.xml"));

        log.debug("PerformanceTimestamp: Starting DemoDocSource.doDemo");

        // Create reusable elements
        XDSResponseType response;
        URI repositoryUnsecureUri;
        URI repositorySecureUri;
        InputStream inputStream;

        // Setup the Endpoint URIs
        try {
            repositoryUnsecureUri = new java.net.URI(NIST);
            repositorySecureUri = new java.net.URI(NIST_SECURED);
        } catch (URISyntaxException e) {
            System.out.println("SOURCE URI CANNOT BE SET: \n" + e.getMessage());
            return;
        }

        // Setup keystore
        String keystore = prepareKeystore();

        // Setup destinations
        Destination dest = new Destination(ORGANIZATIONAL_ID, repositoryUnsecureUri);
        Destination destSec = new Destination(ORGANIZATIONAL_ID, repositorySecureUri, keystore, KEY_STORE_PASS);
        try {
            ConvenienceCommunication comm = new ConvenienceCommunication();

            log.debug("PerformanceTimestamp: DemoDocSource.doDemo: setup complete");

            inputStream = getClass().getResourceAsStream(cdaFilePath);

            // Step 1: Sending CDA document NON-TLS
            comm.setDestination(dest);
            DocumentMetadata metaData = comm.addDocument(DocumentDescriptor.CDA_R2, inputStream);
            completeMetadata(metaData);

            outStr = outStr.append("1. Sending CDA Document (NON-TLS)...");
            response = comm.submit();
            outStr = outStr.append("done. Response: " + response.getStatus().getName() + "\n");

            // Step 2: Sending CDA document TLS
            inputStream = getClass().getResourceAsStream(cdaFilePath);
            comm.setDestination(destSec);
            comm.clearDocuments();
            metaData = comm.addDocument(DocumentDescriptor.CDA_R2, inputStream);
            completeMetadata(metaData);

            outStr = outStr.append("2. Sending CDA Document (TLS)...");
            response = comm.submit();
            outStr = outStr.append("done. Response: " + response.getStatus().getName() + "\n");

            // Step 3: Sending PDF document NON-TLS
            inputStream = getClass().getResourceAsStream(pdfFilePath);
            comm.setDestination(dest);
            comm.clearDocuments();
            metaData = comm.addDocument(DocumentDescriptor.PDF, inputStream);
            completeMetadata2(metaData);

            outStr = outStr.append("3. Sending PDF Document (NON-TLS)...");
            response = comm.submit();
            outStr = outStr.append("done. Response: " + response.getStatus().getName() + "\n");

            // Step 4: Sending PDF document TLS
            inputStream = getClass().getResourceAsStream(pdfFilePath);
            comm.setDestination(destSec);
            comm.clearDocuments();
            metaData = comm.addDocument(DocumentDescriptor.PDF, inputStream);
            completeMetadata2(metaData);

            outStr = outStr.append("4. Sending PDF Document (TLS)...");
            response = comm.submit();
            outStr = outStr.append("done. Response: " + response.getStatus().getName() + "\n");

        } catch (Exception e) {
            e.printStackTrace();
        }

        log.debug("PerformanceTimestamp: DemoDocSource.doDemo finished");
    }

    private void completeMetadata(DocumentMetadata metaData) {
        metaData.setPatientId(new Identificator("1.3.6.1.4.1.21367.2005.13.20.3000", "b8a9a9ad17b5429"));
        metaData.setCodedLanguage(LanguageCode.GERMAN);
        metaData.setTypeCode(new Code("2.16.840.1.113883.6.1", "34133-9", "Summarization of Episode Note"));
        metaData.setFormatCode(new Code("1.3.6.1.4.1.19376.1.2.3", "urn:ihe:iti:xds-sd:pdf:2008",
                "1.3.6.1.4.1.19376.1.2.20 (Scanned Document)"));

        metaData.setClassCode(new Code("1.3.6.1.4.1.21367.100.1", "DEMO-Consult", "Consultation"));

        metaData.setHealthcareFacilityTypeCode(new Code("2.16.840.1.113883.5.11", "AMB", "Ambulance"));
        metaData.setPracticeSettingCode(new Code("2.16.840.1.113883.6.96", "394802001", "General Medicine"));
    }

    private void completeMetadata2(DocumentMetadata metaData) {
        metaData.setPatientId(new Identificator("1.3.6.1.4.1.21367.2005.13.20.2000", "2365680966c8482"));
        metaData.setCodedLanguage(LanguageCode.FRENCH);
        metaData.setTypeCode(
                new Code("2.16.840.1.113883.6.1", "34817-7", "Otorhinolaryngology Evaluation and Management Note"));
        metaData.setFormatCode(new Code("1.3.6.1.4.1.19376.1.2.3", "urn:ihe:rad:TEXT", "urn:ihe:rad:TEXT"));

        metaData.setClassCode(new Code("1.3.6.1.4.1.21367.100.1", "DEMO-Procedure", "Procedure"));

        metaData.setHealthcareFacilityTypeCode(new Code("2.16.840.1.113883.5.11", "AMB", "Ambulance"));
        metaData.setPracticeSettingCode(new Code("2.16.840.1.113883.6.96", "408478003", "Critical Care Medicine"));
    }

    /**
     * Creates the keystore as temporary file on the local filesystem OHT does not
     * support keystores from within the JAR...
     * 
     * @return Full path and file name of the temporary file containing the
     *         keystore
     */
    private String prepareKeystore() {
        String keystore = "/temp/keystore";
        try {
            keystore = File.createTempFile("keystore", "").getAbsolutePath();
            InputStream input = getClass().getResourceAsStream(KEY_STORE);
            OutputStream output = new FileOutputStream(keystore);
            byte[] buffer = new byte[2048];
            int bytesRead;
            while ((bytesRead = input.read(buffer)) != -1) {
                output.write(buffer, 0, bytesRead);
            }
            input.close();
            output.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        return keystore;
    }
}