Newer Version Available

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

Parser Class Example 2

Here's an example of a parser class that you might write to handle a segment of an ADT HL7 message containing allergy information. It creates a data transfer object that you can use to populate a Salesforce Health Cloud record.

The Parser

1public class HL7AL1Parser implements HL7.ElementParser {
2    
3    /**
4     * @description: Method implementing parseList method of HL7.ElementParser
5     * @param HL7Context context
6     * @param Scope
7     * @param Object value to parse
8     * @return List<HL7AL1> List of HL7AL1 Data Transfer Object
9     */
10    public List<HL7AL1> parseList(HL7Context context, String scope, Object value) {
11        if (value == null) {
12            // Adding error message. Calling addMessage method and passing message, severity, scope and message detail 
13            ResponseHandler.addMessage(HCGlobals.ELEMENT_NOT_FOUND, HCGlobals.Severity.WARN, scope, 'HL7AL1: Element not Found.');
14            return null;
15        }
16        List<HL7AL1> elementList = new List<HL7AL1>();
17        // Check if a list of objects or single object is to be parsed
18        if (value instanceof List<object>) {
19            // Parsing List of object
20            for (Object eachItem : (List<object>)value) {
21                HL7AL1 parsedObject = new HL7AL1();
22                parsedObject = (HL7AL1)parse(context, scope, eachItem);
23                elementList.add(parsedObject);
24            }
25        } else {
26            // Parsing object
27            HL7AL1 parsedObject = parse(context, scope, value);
28            elementList.add(parsedObject);
29        }
30        return elementList;            
31    }
32    
33    /**
34     * @description: Method implementing parse method of HL7.ElementParser
35     * @param HL7Context context
36     * @param Scope
37     * @param Object value to parse
38     * @return HL7AL1 Data Transfer Object
39     */
40    public HL7AL1 parse(HL7Context context, String scope, Object value) {
41        HL7AL1 al1Obj = new HL7AL1();
42        // Get root level parser from parser factory using this context
43        HL7ParserFactory parserFactory = HL7ParserFactory.getInstance();
44        if (value == null) {
45            ResponseHandler.addMessage(HCGlobals.ELEMENT_NOT_FOUND, HCGlobals.Severity.WARN,scope, 'HL7AL1: Element not Found.');
46            return null;
47        } else if (value instanceof Map<String, Object>) {
48            
49            Map<String, Object> al1Parameters = (Map<String, Object>)value;
50
51            // Parsing AL1.1 segment of type String
52            al1Obj.AL1_1 = HL7Util.getEntityStringValue('AL1_1', scope, al1Parameters);
53
54            /*
55            * Parsing the segments using parseElement method of HL7ParserFactory to convert incoming HL7 Message to Data Transfer Objects.
56            * 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
57            */
58            try {
59                // Parsing AL1.2 segment of type HL7CWE 
60                al1Obj.AL1_2 = (HL7CWE)parserFactory.parseElement(context, scope, 'AL1_2', 'HL7CWE', al1Parameters, false);
61            } catch (Exception ex) {
62                HL7Util.handleParseException(ex, scope + '.' + 'AL1_2', !HCGlobals.ISREQUIRED);
63            }
64
65            try {
66                // Parsing AL1.4 segment of type HL7CWE 
67                al1Obj.AL1_4 = (HL7CWE)parserFactory.parseElement(context, scope, 'AL1_4', 'HL7CWE', al1Parameters, false);
68            } catch (Exception ex) {
69                HL7Util.handleParseException(ex, scope + '.' + 'AL1_4', !HCGlobals.ISREQUIRED);
70            }
71
72            try {
73                // Parsing AL1.3 segment of type HL7CWE 
74                al1Obj.AL1_3 = (HL7CWE)parserFactory.parseElement(context, scope, 'AL1_3', 'HL7CWE', al1Parameters, false);
75                if(al1Obj.AL1_3 == null) {
76                    throw new HCGlobals.ParseException(scope + '.AL1_3', 'Required AL1_3 element missing.');
77                }
78            } catch (Exception ex) {
79                HL7Util.handleParseException(ex, scope + '.' + 'AL1_3', HCGlobals.ISREQUIRED);
80                throw new HCGlobals.ParseException(scope + '.AL1_3', 'Required AL1_3 element missing.');
81            }
82
83            // Parsing AL1.5 segment of type String 
84            al1Obj.AL1_5 = HL7Util.getEntityListStringValue('AL1_5', scope, al1Parameters);
85
86            try {
87                // Parsing AL1.6 segment of type HL7DateTime 
88                al1Obj.AL1_6 = (HL7DateTime) parserFactory.parseElement(context, scope, 'AL1_6', 'HL7DateTime', al1Parameters, false);
89            } catch (Exception ex) {
90                HL7Util.handleParseException(ex, scope + '.' + 'AL1_6', !HCGlobals.ISREQUIRED);
91            }            
92            
93        }
94        
95        return al1Obj;
96        
97    }
98}

The Output

1global virtual class HL7AL1 extends HL7.Entity {
2                
3     // Set ID - AL1_1:
4     global String AL1_1 { get; set; }
5     
6     // Allergen Type Code - AL1_2:
7     global HL7CWE AL1_2  { get; set; }
8     
9     // Allergen Code/Mnemonic/Description - AL1_3:
10     global HL7CWE AL1_3  { get; set; }
11     
12     // Allergy Severity Code - AL1_4:
13     global HL7CWE AL1_4  { get; set; }
14     
15     // Allergy Reaction Code - AL1_5:
16     global List<String> AL1_5  { get; set; }
17     
18     // Identification Date - AL1_6:
19     global HL7DateTime AL1_6  { get; set; }
20     
21     global override void setDefaultFieldValue(Object value) {
22          if(value != null) {
23               AL1_2 = new HL7CWE();
24               AL1_2.setDefaultFieldValue(value);
25          }
26     }
27}