| 1 | HLOAPI ;ALB/CJM-HL7 - Developer API's for sending & receiving messages ;09/13/2006 | 
|---|
| 2 | ;;1.6;HEALTH LEVEL SEVEN;**126,133**;Oct 13, 1995;Build 13 | 
|---|
| 3 | ;Per VHA Directive 2004-038, this routine should not be modified. | 
|---|
| 4 | ; | 
|---|
| 5 | NEWMSG(PARMS,HLMSTATE,ERROR) ; | 
|---|
| 6 | ;Starts a new message. | 
|---|
| 7 | ;Input: | 
|---|
| 8 | ;   PARMS( *pass by reference* | 
|---|
| 9 | ;     "COUNTRY")=3 character country code (optional) | 
|---|
| 10 | ;     "CONTINUATION POINTER" -indicates a fragmented message | 
|---|
| 11 | ;     "EVENT")=3 character event type (required) | 
|---|
| 12 | ;     "FIELD SEPARATOR")=field separator (optional, defaults to "|") | 
|---|
| 13 | ;     "ENCODING CHARACTERS")= 4 HL7 encoding characters (optional,defaults to "^~\&") | 
|---|
| 14 | ;     "MESSAGE STRUCTURE" - MSH 9, component 3 - a code from the standard HL7 table (optional) | 
|---|
| 15 | ;     "MESSAGE TYPE")=3 character message type (required) | 
|---|
| 16 | ;     "PROCESSING MODE" - MSH 11, component 2 - a 1 character code (optional) | 
|---|
| 17 | ;     "VERSION")=the HL7 Version ID, for example, "2.4" (optional, defaults to 2.4) | 
|---|
| 18 | ;Output: | 
|---|
| 19 | ;  Function- returns 1 on success, 0 on failure | 
|---|
| 20 | ;  HLMSTATE() - (pass by reference, required) This array is used by the HL7 package to track the progress of the message.  The application MUST NOT touch it! | 
|---|
| 21 | ;  PARMS - left defined when the function returns | 
|---|
| 22 | ;  ERROR (optional, pass by reference) - returns an error message on failure | 
|---|
| 23 | ; | 
|---|
| 24 | ; | 
|---|
| 25 | N DATA,I,SYSTEM,SUCCESS | 
|---|
| 26 | S SUCCESS=0 | 
|---|
| 27 | K ERROR,HLMSTATE | 
|---|
| 28 | D | 
|---|
| 29 | .I $L($G(PARMS("PROCESSING MODE"))),$L(PARMS("PROCESSING MODE"))'=1 S ERROR="INVALID PROCESSING MODE" Q | 
|---|
| 30 | .I $L($G(PARMS("COUNTRY"))),$L(PARMS("COUNTRY"))'=3 S ERROR="INVALID COUNTRY CODE" Q | 
|---|
| 31 | .I $L($G(PARMS("EVENT")))'=3 S ERROR="INVALID EVENT CODE" Q | 
|---|
| 32 | .I $L($G(PARMS("MESSAGE TYPE")))'=3 S ERROR="INVALID MESSAGE TYPE" Q | 
|---|
| 33 | .I $L($G(PARMS("ENCODING CHARACTERS"))),$L(PARMS("ENCODING CHARACTERS"))'=4 S ERROR="INVALID ENCODING CHARACTERS" Q | 
|---|
| 34 | .I $L($G(PARMS("FIELD SEPARATOR"))),$L(PARMS("FIELD SEPARATOR"))'=1 S ERROR="INVALID FIELD SEPARATOR" Q | 
|---|
| 35 | .I '$L($G(PARMS("FIELD SEPARATOR"))) S PARMS("FIELD SEPARATOR")="|" | 
|---|
| 36 | .I '$L($G(PARMS("ENCODING CHARACTERS"))) S PARMS("ENCODING CHARACTERS")="^~\&" | 
|---|
| 37 | .I $G(PARMS("VERSION"))="" S PARMS("VERSION")="2.4" | 
|---|
| 38 | .I ($L($G(PARMS("VERSION")))>20) S ERROR="VERSION > 20 CHARACTERS" Q | 
|---|
| 39 | .F I="MESSAGE TYPE","EVENT","COUNTRY","FIELD SEPARATOR","ENCODING CHARACTERS","VERSION","CONTINUATION POINTER","MESSAGE STRUCTURE","PROCESSING MODE" S HLMSTATE("HDR",I)=$G(PARMS(I)) | 
|---|
| 40 | .S HLMSTATE("BATCH")=0 ;not a batch | 
|---|
| 41 | .S HLMSTATE("DIRECTION")="OUT" | 
|---|
| 42 | .S HLMSTATE("IEN")="" | 
|---|
| 43 | .S HLMSTATE("BODY")="" ;record not yet created | 
|---|
| 44 | .S HLMSTATE("CURRENT SEGMENT")=0 ;no segments cached | 
|---|
| 45 | .S HLMSTATE("UNSTORED LINES")=0 ;nothing in cache | 
|---|
| 46 | .S HLMSTATE("LINE COUNT")=0 | 
|---|
| 47 | .D GETSYS(.HLMSTATE) | 
|---|
| 48 | .S SUCCESS=1 | 
|---|
| 49 | Q SUCCESS | 
|---|
| 50 | ; | 
|---|
| 51 | NEWBATCH(PARMS,HLMSTATE,ERROR) ; | 
|---|
| 52 | ;Starts a new batch message. | 
|---|
| 53 | ;Input: | 
|---|
| 54 | ;  PARMS( *pass by reference* | 
|---|
| 55 | ;   "COUNTRY")=3 character country code (optional) | 
|---|
| 56 | ;   "FIELD SEPARATOR")=field separator (optional, defaults to "|") | 
|---|
| 57 | ;   "ENCODING CHARACTERS")= 4 HL7 encoding characters (optional,defaults to "^~\&") | 
|---|
| 58 | ;   "VERSION")=the HL7 Version ID, for example, "2.4" (optional, defaults to 2.4) | 
|---|
| 59 | ;Output: | 
|---|
| 60 | ;  Function - returns 1 on success, 0 on failure | 
|---|
| 61 | ;  PARMS - left defined when the function returns | 
|---|
| 62 | ;  HLMSTATE() - (pass by reference, required) This array is used by the HL7 package to track the progress of the message.  The application MUST NOT touch it! | 
|---|
| 63 | ;  ERROR (optional, pass by reference) - returns an error message on failure | 
|---|
| 64 | ; | 
|---|
| 65 | ; | 
|---|
| 66 | N DATA,I,SYSTEM,SUCCESS | 
|---|
| 67 | S SUCCESS=0 | 
|---|
| 68 | K ERROR,HLMSTATE | 
|---|
| 69 | D | 
|---|
| 70 | .I $L($G(PARMS("COUNTRY"))),$L(PARMS("COUNTRY"))'=3 S ERROR="INVALID COUNTRY CODE" Q | 
|---|
| 71 | .I $L($G(PARMS("ENCODING CHARACTERS"))),$L(PARMS("ENCODING CHARACTERS"))'=4 S ERROR="INVALID ENCODING CHARACTERS" Q | 
|---|
| 72 | .I $L($G(PARMS("FIELD SEPARATOR"))),$L(PARMS("FIELD SEPARATOR"))'=1 S ERROR="INVALID FIELD SEPARATOR" Q | 
|---|
| 73 | .I '$L($G(PARMS("FIELD SEPARATOR"))) S PARMS("FIELD SEPARATOR")="|" | 
|---|
| 74 | .I '$L($G(PARMS("ENCODING CHARACTERS"))) S PARMS("ENCODING CHARACTERS")="^~\&" | 
|---|
| 75 | .I $G(PARMS("VERSION"))="" S PARMS("VERSION")="2.4" | 
|---|
| 76 | .I ($L(PARMS("VERSION"))>20) S ERROR="VERSION > 20 CHARACTERS" Q | 
|---|
| 77 | .F I="COUNTRY","FIELD SEPARATOR","ENCODING CHARACTERS","VERSION" S HLMSTATE("HDR",I)=$G(PARMS(I)) | 
|---|
| 78 | .S HLMSTATE("IEN")="" | 
|---|
| 79 | .S HLMSTATE("BODY")="" ;msg not yet stored | 
|---|
| 80 | .S HLMSTATE("BATCH")=1 | 
|---|
| 81 | .S HLMSTATE("DIRECTION")="OUT" | 
|---|
| 82 | .S HLMSTATE("BATCH","CURRENT MESSAGE")=0 ;no messages in batch | 
|---|
| 83 | .S HLMSTATE("CURRENT SEGMENT")=0 ;no segments in cache | 
|---|
| 84 | .S HLMSTATE("UNSTORED LINES")=0 ;nothing in cache | 
|---|
| 85 | .S HLMSTATE("LINE COUNT")=0 ;no lines within message stored | 
|---|
| 86 | .D GETSYS(.HLMSTATE) | 
|---|
| 87 | .S SUCCESS=1 | 
|---|
| 88 | Q SUCCESS | 
|---|
| 89 | ; | 
|---|
| 90 | SET(SEG,VALUE,FIELD,COMP,SUBCOMP,REP) ; | 
|---|
| 91 | ;Sets a value to the array SEG(), used for building segments. | 
|---|
| 92 | ;Input: | 
|---|
| 93 | ; SEG - (required, pass by reference) - this is the array where the segment is being built. | 
|---|
| 94 | ; VALUE - the individual value to be set into the segment | 
|---|
| 95 | ; FIELD - the sequence # of the field (optional, defaults to 0) | 
|---|
| 96 | ;     *NOTE: FIELD=0 is used to denote the segment type. | 
|---|
| 97 | ; COMP - the # of the component (optional, defaults to 1) | 
|---|
| 98 | ; SUBCOMP - the # of the subcomponent (optional, defaults to 1) | 
|---|
| 99 | ; REP - the occurrence# (optional, defaults to 1)  For a non-repeating field, the occurrence # need not be provided, because it would be 1. | 
|---|
| 100 | ;Output: | 
|---|
| 101 | ;  SEG array | 
|---|
| 102 | ; | 
|---|
| 103 | ;  Example: | 
|---|
| 104 | ;    D SET(.SEG,"MSA",0) creates an MSA segment | 
|---|
| 105 | ;    D SET(.SEG,"AE",1) will place the value into the array position | 
|---|
| 106 | ;    reserved for the 1st field,1st occurence,1st comp,1st subcomp | 
|---|
| 107 | ; | 
|---|
| 108 | ;Implementation Note - This format is used for the segment array built by calls to SET: SEGMENT(<SEQ #>,<occurrence #>,<component #>,<subcomponent #>)=<subcomponent value> | 
|---|
| 109 | ; | 
|---|
| 110 | S:'$G(FIELD) FIELD=0 | 
|---|
| 111 | S:'$G(COMP) COMP=1 | 
|---|
| 112 | S:'$G(SUBCOMP) SUBCOMP=1 | 
|---|
| 113 | S:'$G(REP) REP=1 | 
|---|
| 114 | S SEG(FIELD,REP,COMP,SUBCOMP)=$G(VALUE) | 
|---|
| 115 | Q | 
|---|
| 116 | ; | 
|---|
| 117 | ADDSEG(HLMSTATE,SEG,ERROR) ;Adds a segment to the message. | 
|---|
| 118 | ;Input: | 
|---|
| 119 | ;  HLMSTATE() - (pass by reference, required) This array is used by the HL7 package to track the progress of the message.  The application MUST NOT touch it! | 
|---|
| 120 | ;  SEG() - (pass by reference, required) Contains the data.  It must be built by calls to SET prior to calling $$ADDSEG. | 
|---|
| 121 | ; | 
|---|
| 122 | ;Note#1:  The message control segments, including the MSH and BHS segments, are added automatically. | 
|---|
| 123 | ;Note#2:  The 0th field must be a 3 character segment type | 
|---|
| 124 | ;Note#3: ***SEG is killed upon successfully adding the segment*** | 
|---|
| 125 | ; | 
|---|
| 126 | ;Output: | 
|---|
| 127 | ;   HLMSTATE() - (pass by reference, required) This array is used by the HL7 package to track the progress of the message. | 
|---|
| 128 | ;  FUNCTION - returns 1 on success, 0 on failure | 
|---|
| 129 | ;  ERROR (optional, pass by reference) - returns an error message on failure | 
|---|
| 130 | ; | 
|---|
| 131 | ; | 
|---|
| 132 | K ERROR | 
|---|
| 133 | N TOARY,TYPE | 
|---|
| 134 | ; | 
|---|
| 135 | S TYPE=$G(SEG(0,1,1,1)) ;segment type | 
|---|
| 136 | ; | 
|---|
| 137 | ;if a 'generic' app ack MSA was built, add it as the first segment before this one | 
|---|
| 138 | I $D(HLMSTATE("MSA")) D | 
|---|
| 139 | .I TYPE'="MSA" S TOARY(1)=HLMSTATE("MSA") D ADDSEG^HLOMSG(.HLMSTATE,.TOARY) K TOARY | 
|---|
| 140 | .K HLMSTATE("MSA") | 
|---|
| 141 | ; | 
|---|
| 142 | I ($L(TYPE)'=3) S ERROR="INVALID SEGMENT TYPE" Q 0 | 
|---|
| 143 | I (TYPE="MSH")!(TYPE="BHS")!(TYPE="BTS")!(TYPE="FHS")!(TYPE="FTS") S ERROR="INVALID SEGMENT TYPE" Q 0 | 
|---|
| 144 | I HLMSTATE("BATCH"),'HLMSTATE("BATCH","CURRENT MESSAGE") S ERROR="NO MESSAGES IN BATCH, SO SEGMENTS NOT ALLOWED" Q 0 | 
|---|
| 145 | I $$BUILDSEG^HLOPBLD(.HLMSTATE,.SEG,.TOARY,.ERROR) D ADDSEG^HLOMSG(.HLMSTATE,.TOARY) K SEG Q 1 | 
|---|
| 146 | Q 0 | 
|---|
| 147 | ; | 
|---|
| 148 | ADDMSG(HLMSTATE,PARMS,ERROR) ; | 
|---|
| 149 | ;Begins a new message in the batch. | 
|---|
| 150 | ;Input: | 
|---|
| 151 | ;  HLMSTATE() - (pass by reference, required) This array is used by the HL7 package to track the progress of the message.  The application MUST NOT touch it! | 
|---|
| 152 | ;  PARMS( *pass by reference* | 
|---|
| 153 | ;    "EVENT")=3 character event type (required) | 
|---|
| 154 | ;    "MESSAGE TYPE")=3 character message type (required) | 
|---|
| 155 | ; | 
|---|
| 156 | ;Output: | 
|---|
| 157 | ;   FUNCTION - returns 1 on success, 0 on failure | 
|---|
| 158 | ;   HLMSTATE() - (pass by reference, required) This array is used by the HL7 package to track the progress of the message. | 
|---|
| 159 | ;   PARMS - left defined when this function returns | 
|---|
| 160 | ;   ERROR (optional, pass by reference) - returns an error message on failure | 
|---|
| 161 | ; | 
|---|
| 162 | N I | 
|---|
| 163 | K ERROR | 
|---|
| 164 | ;if a 'generic' app ack MSA was built, add it as the first segment before this one | 
|---|
| 165 | I $D(HLMSTATE("MSA")) D | 
|---|
| 166 | .I TYPE'="MSA" N TOARY M TOARY=HLMSTATE("MSA") D ADDSEG^HLOMSG(.HLMSTATE,.TOARY) K TOARY | 
|---|
| 167 | .K HLMSTATE("MSA") | 
|---|
| 168 | I $L($G(PARMS("EVENT")))'=3 S ERROR="EVENT TYPE INVALID" Q 0 | 
|---|
| 169 | I $L($G(PARMS("MESSAGE TYPE")))'=3 S ERROR="MESSAGE TYPE INVALID" Q 0 | 
|---|
| 170 | D ADDMSG^HLOMSG(.HLMSTATE,.PARMS) | 
|---|
| 171 | Q 1 | 
|---|
| 172 | ; | 
|---|
| 173 | GETSYS(HLMSTATE) ; | 
|---|
| 174 | N SYS,SUB | 
|---|
| 175 | D SYSPARMS^HLOSITE(.SYS) | 
|---|
| 176 | F SUB="DOMAIN","STATION","PROCESSING ID","MAXSTRING","ERROR PURGE","NORMAL PURGE","PORT" S HLMSTATE("SYSTEM",SUB)=SYS(SUB) | 
|---|
| 177 | S HLMSTATE("SYSTEM","BUFFER")=SYS("USER BUFFER") | 
|---|
| 178 | Q | 
|---|
| 179 | ; | 
|---|
| 180 | MOVEMSG(HLMSTATE,ARY) ; | 
|---|
| 181 | ;If a message was built in the 'old' way, and resides in an array, this  routine will move it into file 777 (HL7 Message Body) | 
|---|
| 182 | ;Input: | 
|---|
| 183 | ;  HLMSTATE (pass by reference) the array created by calling $$NEWMSG or $$NEWBATCH | 
|---|
| 184 | ;  ARY - is the name of the array, local or global, where the message was built, used to reference the array by indirection. | 
|---|
| 185 | ; | 
|---|
| 186 | N I S I=0 | 
|---|
| 187 | F  S I=$O(@ARY@(I)) Q:'I  D | 
|---|
| 188 | .N SEG,J,J2 | 
|---|
| 189 | .S J=0,J2=1 | 
|---|
| 190 | .S SEG(J2)=@ARY@(I) | 
|---|
| 191 | .F  S J=$O(@ARY@(I,J)) Q:'J  S J2=J2+1,SEG(J2)=@ARY@(I,J) | 
|---|
| 192 | .I 'HLMSTATE("BATCH") D | 
|---|
| 193 | ..D ADDSEG^HLOMSG(.HLMSTATE,.SEG) | 
|---|
| 194 | .E  D | 
|---|
| 195 | ..I $E(SEG(1),1,3)="MSH" D | 
|---|
| 196 | ...D SPLITHDR^HLOSRVR1(.SEG) | 
|---|
| 197 | ...D ADDMSG2^HLOMSG(.HLMSTATE,.SEG) | 
|---|
| 198 | ..E  D ADDSEG^HLOMSG(.HLMSTATE,.SEG) | 
|---|
| 199 | ; | 
|---|
| 200 | ;signal SENDACK^HLOAPI2 that the application built its own msg | 
|---|
| 201 | K HLMSTATE("MSA") | 
|---|
| 202 | Q | 
|---|