Newer Version Available

This content describes an older version of this product. View Latest

Parser Class Example 1

Here's an example of a parser class that you might write to handle an HL7 message containing information about a patient's admission, discharge or transfer (ADT). It creates a data transfer object that you can use to populate a Salesforce Health Cloud record.

The Parser

1public class HL7ADT_A01Parser implements HL7.EventParser {
2
3    /**
4     * @description: Method implementing parse method of HL7.EventParser
5     * @param HL7Context context
6     * @param Scope
7     * @param Object value to parse
8     * @return HL7ADT Data Transfer Object
9     */
10    public HL7ADT parse(HL7Context context, String scope, Map<String, Object> value) {
11
12        // Create instance of HL7ADT DTO
13        HL7ADT adtObject = new HL7ADT();
14        // Get instance of parser factory
15        HL7ParserFactory parserFactory = HL7ParserFactory.getInstance();
16
17        /*
18        * Parsing the segments using parseElement method of HL7ParserFactory to convert incoming HL7 Message to Data Transfer Objects.
19        * Passing context, scope, name of the element, type of the element, message data object, flag indicating if the data object is a list or not.
20        * In case of exception/error, add message using the ResponseHandler.addMessage method and passing message, severity, scope and message detail. 
21        */
22
23        // Parsing the MSH segment of type HL7MSH
24        Integer startCPU_sub = Limits.getCpuTime();
25        Object mshSegment = HL7Util.getEntityMapValue('MSH', value);
26        if (mshSegment != null) {
27            try {
28                adtObject.MSH = (HL7MSH) parserFactory.parseElement(context, scope, 'MSH', 'HL7MSH', value, false);
29            } catch (Exception e) {
30                ResponseHandler.addMessage(HCGlobals.ELEMENT_NOT_FOUND, HCGlobals.Severity.WARN, scope + '.MSH', 'Unable to parse the MSH message');
31            }
32        } else {
33            ResponseHandler.addMessage(HCGlobals.ELEMENT_NOT_FOUND, HCGlobals.Severity.ERROR, scope, 'Unable to find MSH Segment');
34            throw new HCGlobals.ParseException(scope, 'Unable to find MSH Segment');
35        }
36        HL7Util.addCPUMetric('Message.MSHParsing', startCPU_sub);
37
38        // Parsing the PID segment of type HL7PID
39        startCPU_sub = Limits.getCpuTime();
40        Object pidSegment = HL7Util.getEntityMapValue('PID', value);
41        if (pidSegment != null) {
42            try {
43                adtObject.PID = (HL7PID) parserFactory.parseElement(context, scope, 'PID', 'HL7PID', value, false);
44            } catch (Exception e) {
45                ResponseHandler.addMessage(HCGlobals.ELEMENT_NOT_FOUND, HCGlobals.Severity.WARN, scope + '.PID', 'Unable to parse the PID message');
46            }
47        } else {
48            ResponseHandler.addMessage(HCGlobals.ELEMENT_NOT_FOUND, HCGlobals.Severity.ERROR, scope, 'Unable to find PID Segment');
49            throw new HCGlobals.ParseException(scope, 'Unable to find PID Segment');
50        }
51        HL7Util.addCPUMetric('Message.PIDParsing', startCPU_sub);
52
53        // Parsing the PV1 segment of type HL7PV1
54        startCPU_sub = Limits.getCpuTime();
55        Object pv1Segment = HL7Util.getEntityMapValue('PV1', value);
56        if (pv1Segment != null) {
57            try {
58                adtObject.PV1 = (HL7PV1) parserFactory.parseElement(context, scope, 'PV1', 'HL7PV1', value, false);
59            } catch (Exception e) {
60                ResponseHandler.addMessage(HCGlobals.ELEMENT_NOT_FOUND, HCGlobals.Severity.WARN, scope + '.PV1', 'Unable to parse the PV1 message');
61            }
62        } else {
63            ResponseHandler.addMessage(HCGlobals.ELEMENT_NOT_FOUND, HCGlobals.Severity.ERROR, scope, 'Unable to find PV1 Segment');
64            throw new HCGlobals.ParseException(scope, 'Unable to find PV1 Segment');
65        }
66        HL7Util.addCPUMetric('Message.PV1Parsing', startCPU_sub);
67
68        // Parsing the PV2 segment of type HL7PV2
69        startCPU_sub = Limits.getCpuTime();
70        Object pv2Segment = HL7Util.getEntityMapValue('PV2', value);
71        if (pv2Segment != null) {
72            try {
73                adtObject.PV2 = (HL7PV2) parserFactory.parseElement(context, scope, 'PV2', 'HL7PV2', value, false);
74            } catch (Exception e) {
75                ResponseHandler.addMessage(HCGlobals.ELEMENT_NOT_FOUND, HCGlobals.Severity.WARN, scope + '.PV2', 'Unable to parse the PV2 message');
76            }
77        }
78        HL7Util.addCPUMetric('Message.PV2Parsing', startCPU_sub);
79
80        // Parsing the AL1 segment of type HL7AL1
81        startCPU_sub = Limits.getCpuTime();
82        Object al1Segment = HL7Util.getEntityMapValue('AL1', value);
83        if (al1Segment != null) {
84            try {
85                adtObject.AL1 = (List<HL7AL1>) parserFactory.parseElement(context, scope, 'AL1', 'HL7AL1', value, true);
86            } catch (HCGlobals.ParserException prex) {
87                //Nothing to do here. AL1 is optional.
88            } catch (HCGlobals.ParseException pex) {
89                //Nothing to do here. AL1 is optional.
90            } catch (Exception e) {
91                //AL1 is optional.
92                System.debug(LoggingLevel.WARN, 'ERROR :' + e);
93                System.debug(LoggingLevel.WARN, 'ERROR :' + e.getStackTraceString());
94                ResponseHandler.addMessage(HCGlobals.ELEMENT_NOT_FOUND, HCGlobals.Severity.WARN, scope + '.AL1', 'Unable to parse the AL1 message', e);
95            }
96        }
97        HL7Util.addCPUMetric('Message.AL1Parsing', startCPU_sub);
98
99        return adtObject;
100    }
101    }

The Output

1global virtual class HL7ADT extends HL7.Entity implements HL7.Event {
2                
3     // Message header segment
4     global HL7MSH MSH;
5     // Event type
6     global HL7EVN EVN;
7     // Patient identification
8     global HL7PID PID;
9     
10     // Patient demographic
11     global HL7PD1 PD1;
12     // Next of kin
13     global List<HL7NK1> nk1List;
14     // Patient visit
15     global HL7PV1 PV1;
16     // Patient visit - additional info
17     global HL7PV2 PV2;
18     // Disability segment
19     global List<HL7DB1> db1List;
20     // Observation segment
21     global List<HL7OBX> obxList;
22     // Patient allergy information
23     global List<HL7AL1> AL1;
24     // Diagnosis
25     global List<HL7DG1> dg1List;
26     // Diagnosis related group
27     global HL7DRG DRG;
28     // Accident
29     global HL7ACC ACC;
30     // UB82 data
31     global HL7UB1 UB1;
32     // UB92 data
33     global HL7UB2 UB2;
34     
35     // -- PROCEDURE
36     // global List<HL7Procedure> procedureList
37     // Procedure
38     global HL7PR1 PR1;
39     // Role
40     global List<HL7ROL> rolList;
41     // Guarantor
42     global List<HL7GT1> gt1List;
43     
44     // -- INSURANCE
45     // global List<HL7Insurance> insuranceList
46     // Insurance
47     global HL7IN1 IN1;
48     // Insurance additional info
49     global HL7IN2 IN2;
50     // Insurance additional info - certification
51     global HL7IN3 IN3;
52     
53}