HLOAPI ;ALB/CJM-HL7 - Developer API's for sending & receiving messages ;05/12/2009
 ;;1.6;HEALTH LEVEL SEVEN;**126,133,138,139,146**;Oct 13, 1995;Build 16
 ;Per VHA Directive 2004-038, this routine should not be modified.
 ;
NEWMSG(PARMS,HLMSTATE,ERROR) ;; Starts a new message.
 ;;
 ;;** External API **
 ;;  
 ;;Input: 
 ;;   PARMS( *pass by reference*
 ;;     "COUNTRY")=3 character country code (optional)
 ;;     "CONTINUATION POINTER" -indicates a fragmented message
 ;;     "EVENT")=3 character event type (required)
 ;;     "FIELD SEPARATOR")=field separator (optional, defaults to "|")
 ;;     "ENCODING CHARACTERS")= 4 HL7 encoding characters (optional,defaults to "^~\&")
 ;;     "MESSAGE STRUCTURE" - MSH 9, component 3 - a code from the standard HL7 table (optional)
 ;;     "MESSAGE TYPE")=3 character message type (required)
 ;;     "PROCESSING MODE" - MSH 11, component 2 - a 1 character code (optional)
 ;;     "VERSION")=the HL7 Version ID, for example, "2.4" (optional, defaults to 2.4)
 ;;Output:
 ;;  Function- returns 1 on success, 0 on failure
 ;;  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!
 ;;  PARMS - left defined when the function returns
 ;;  ERROR (optional, pass by reference) - returns an error message on failure
 ;;
 ;
 N DATA,I,SYSTEM,SUCCESS
 S SUCCESS=0
 K ERROR,HLMSTATE
 D
 .I $L($G(PARMS("PROCESSING MODE"))),$L(PARMS("PROCESSING MODE"))'=1 S ERROR="INVALID PROCESSING MODE" Q
 .I $L($G(PARMS("COUNTRY"))),$L(PARMS("COUNTRY"))'=3 S ERROR="INVALID COUNTRY CODE" Q
 .I $L($G(PARMS("EVENT")))'=3 S ERROR="INVALID EVENT CODE" Q
 .I $L($G(PARMS("MESSAGE TYPE")))'=3 S ERROR="INVALID MESSAGE TYPE" Q
 .I $L($G(PARMS("ENCODING CHARACTERS"))),$L(PARMS("ENCODING CHARACTERS"))'=4 S ERROR="INVALID ENCODING CHARACTERS" Q
 .I $L($G(PARMS("FIELD SEPARATOR"))),$L(PARMS("FIELD SEPARATOR"))'=1 S ERROR="INVALID FIELD SEPARATOR" Q
 .I '$L($G(PARMS("FIELD SEPARATOR"))) S PARMS("FIELD SEPARATOR")="|"
 .I '$L($G(PARMS("ENCODING CHARACTERS"))) S PARMS("ENCODING CHARACTERS")="^~\&"
 .I $G(PARMS("VERSION"))="" S PARMS("VERSION")="2.4"
 .I ($L($G(PARMS("VERSION")))>20) S ERROR="VERSION > 20 CHARACTERS" Q
 .F I="MESSAGE TYPE","EVENT","COUNTRY","FIELD SEPARATOR","ENCODING CHARACTERS","VERSION","CONTINUATION POINTER","MESSAGE STRUCTURE","PROCESSING MODE" S HLMSTATE("HDR",I)=$G(PARMS(I))
 .S HLMSTATE("BATCH")=0 ;not a batch
 .S HLMSTATE("DIRECTION")="OUT"
 .S HLMSTATE("IEN")=""
 .S HLMSTATE("BODY")="" ;record not yet created
 .S HLMSTATE("CURRENT SEGMENT")=0 ;no segments cached
 .S HLMSTATE("UNSTORED LINES")=0 ;nothing in cache
 .S HLMSTATE("LINE COUNT")=0
 .D GETSYS(.HLMSTATE)
 .S SUCCESS=1
 Q SUCCESS
 ;
NEWBATCH(PARMS,HLMSTATE,ERROR) ;;Starts a new batch message.  
 ;;Input: 
 ;;  PARMS( *pass by reference*
 ;;   "COUNTRY")=3 character country code (optional)
 ;;   "FIELD SEPARATOR")=field separator (optional, defaults to "|")
 ;;   "ENCODING CHARACTERS")= 4 HL7 encoding characters (optional,defaults to "^~\&") 
 ;;   "VERSION")=the HL7 Version ID, for example, "2.4" (optional, defaults to 2.4)
 ;;Output:
 ;;  Function - returns 1 on success, 0 on failure
 ;;  PARMS - left defined when the function returns
 ;;  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!
 ;;  ERROR (optional, pass by reference) - returns an error message on failure
 ;;
 ;
 N DATA,I,SYSTEM,SUCCESS
 S SUCCESS=0
 K ERROR,HLMSTATE
 D
 .I $L($G(PARMS("COUNTRY"))),$L(PARMS("COUNTRY"))'=3 S ERROR="INVALID COUNTRY CODE" Q
 .I $L($G(PARMS("ENCODING CHARACTERS"))),$L(PARMS("ENCODING CHARACTERS"))'=4 S ERROR="INVALID ENCODING CHARACTERS" Q
 .I $L($G(PARMS("FIELD SEPARATOR"))),$L(PARMS("FIELD SEPARATOR"))'=1 S ERROR="INVALID FIELD SEPARATOR" Q
 .I '$L($G(PARMS("FIELD SEPARATOR"))) S PARMS("FIELD SEPARATOR")="|"
 .I '$L($G(PARMS("ENCODING CHARACTERS"))) S PARMS("ENCODING CHARACTERS")="^~\&"
 .I $G(PARMS("VERSION"))="" S PARMS("VERSION")="2.4"
 .I ($L(PARMS("VERSION"))>20) S ERROR="VERSION > 20 CHARACTERS" Q
 .F I="COUNTRY","FIELD SEPARATOR","ENCODING CHARACTERS","VERSION" S HLMSTATE("HDR",I)=$G(PARMS(I))
 .S HLMSTATE("IEN")=""
 .S HLMSTATE("BODY")="" ;msg not yet stored
 .S HLMSTATE("BATCH")=1
 .S HLMSTATE("DIRECTION")="OUT"
 .S HLMSTATE("BATCH","CURRENT MESSAGE")=0 ;no messages in batch
 .S HLMSTATE("CURRENT SEGMENT")=0 ;no segments in cache
 .S HLMSTATE("UNSTORED LINES")=0 ;nothing in cache
 .S HLMSTATE("LINE COUNT")=0 ;no lines within message stored
 .D GETSYS(.HLMSTATE)
 .S SUCCESS=1
 Q SUCCESS
 ;
SET(SEG,VALUE,FIELD,COMP,SUBCOMP,REP) ;;Sets a value to the array SEG(), used for building segments.
 ;;Input:
 ;; SEG - (required, pass by reference) - this is the array where the segment is being built.
 ;; VALUE - the individual value to be set into the segment
 ;; FIELD - the sequence # of the field (optional, defaults to 0)
 ;;     *NOTE: FIELD=0 is used to denote the segment type.
 ;; COMP - the # of the component (optional, defaults to 1)
 ;; SUBCOMP - the # of the subcomponent (optional, defaults to 1)
 ;; REP - the occurrence# (optional, defaults to 1)  For a non-repeating field, the occurrence # need not be provided, because it would be 1.
 ;;Output: 
 ;;  SEG array
 ;;
 ;;  Example:
 ;;    D SET(.SEG,"MSA",0) creates an MSA segment 
 ;;    D SET(.SEG,"AE",1) will place the value into the array position
 ;;    reserved for the 1st field,1st occurence,1st comp,1st subcomp
 ;;
 ;;Implementation Note - This format is used for the segment array built by calls to SET: SEGMENT(<SEQ #>,<occurrence #>,<component #>,<subcomponent #>)=<subcomponent value> 
 ;
 S:'$G(FIELD) FIELD=0
 S:'$G(COMP) COMP=1
 S:'$G(SUBCOMP) SUBCOMP=1
 S:'$G(REP) REP=1
 S SEG(FIELD,REP,COMP,SUBCOMP)=$G(VALUE)
 Q
 ;
ADDSEG(HLMSTATE,SEG,ERROR,TOARY) ;; Adds a segment to the message.
 ;;Input:
 ;;  HLMSTATE() - (pass by reference, required) This array is a workspace for HLO.  The application MUST NOT touch it!
 ;;  SEG() - (pass-by-reference, required) Contains the data.  It be created prior to calling $$ADDSEG.
 ;;
 ;;Note#1:  The message control segments, including the MSH and BHS segments, are added automatically.
 ;;Note#2:  The 0th field must be a 3 character segment type
 ;;Note#3: ***SEG is killed upon successfully adding the segment***
 ;;
 ;;Output:
 ;;   HLMSTATE() - (pass-by-reference, required) This array is used by the HL7 package to track the progress of the message.
 ;;  FUNCTION - returns 1 on success, 0 on failure
 ;;  TOARY (optional, pass by reference) returns the built segment in
 ;;        this format:
 ;;         TOARY(1)
 ;;         TOARY(2)
 ;;         TOARY(3), etc.
 ;;    If the segment fits on a single line, only TOARY(1) is returned.
 ;;
 ;;  ERROR (optional, pass by reference) - returns an error message on failure
 ;;
 ;
 K ERROR
 N TYPE
 K TOARY
 ;
 S TYPE=$G(SEG(0,1,1,1)) ;segment type
 ;
 ;if a 'generic' app ack MSA was built, add it as the first segment before this one
 I $D(HLMSTATE("MSA")) D
 .I TYPE'="MSA" S TOARY(1)=HLMSTATE("MSA") D ADDSEG^HLOMSG(.HLMSTATE,.TOARY) K TOARY
 .K HLMSTATE("MSA")
 ;
 I ($L(TYPE)'=3) S ERROR="INVALID SEGMENT TYPE" Q 0
 I (TYPE="MSH")!(TYPE="BHS")!(TYPE="BTS")!(TYPE="FHS")!(TYPE="FTS") S ERROR="INVALID SEGMENT TYPE" Q 0
 I HLMSTATE("BATCH"),'HLMSTATE("BATCH","CURRENT MESSAGE") S ERROR="NO MESSAGES IN BATCH, SO SEGMENTS NOT ALLOWED" Q 0
 I $$BUILDSEG^HLOPBLD(.HLMSTATE,.SEG,.TOARY,.ERROR) D ADDSEG^HLOMSG(.HLMSTATE,.TOARY) K SEG Q 1
 Q 0
 ;
 ;**P146 START CJM
MOVESEG(HLMSTATE,SEG,ERROR) ;Adds a segment built in the 'traditional' way as an array of lines into the message.
 ;;Input:
 ;;  HLMSTATE() - (pass by reference, required) This array is a workspace for HLO. 
 ;;  SEG() - (pass-by-reference, required) Contains the segment.  The segement.  If the segment is short enough it should consist of only SEG or SEG(1).  If longer, additional lines can be added as SEG(<n>). 
 ;;
 ;;Note#1:  The message control segments, including the MSH, BHS & FTS segments, are added automatically, so may not be added by MOVESEG.
 ;;
 ;;Output:
 ;;   HLMSTATE() - (pass-by-reference, required) This array is the workspace used by HLO.
 ;;  FUNCTION - returns 1 on success, 0 on failure
 ;;
 ;;  ERROR (optional, pass by reference) - returns an error message on failure
 ;;
 ;
 K ERROR
 N TYPE,NEWCOUNT,OLDCOUNT,TOARY
 ;
 S NEWCOUNT=1
 I $L($G(SEG)) S TOARY(1)=SEG,NEWCOUNT=2
 S OLDCOUNT=0
 F  S OLDCOUNT=$O(SEG(OLDCOUNT)) Q:'OLDCOUNT  S TOARY(NEWCOUNT)=SEG(OLDCOUNT),NEWCOUNT=NEWCOUNT+1
 S TYPE=$P($G(TOARY(1)),HLMSTATE("HDR","FIELD SEPARATOR")) ;segment type
 ;
 ;if a 'generic' app ack MSA was built, add it as the first segment before this one
 I $D(HLMSTATE("MSA")) D
 .I TYPE'="MSA" N TOARY S TOARY(1)=HLMSTATE("MSA") D ADDSEG^HLOMSG(.HLMSTATE,.TOARY)
 .K HLMSTATE("MSA")
 ;
 I ($L(TYPE)'=3) S ERROR="INVALID SEGMENT TYPE" Q 0
 I (TYPE="MSH")!(TYPE="BHS")!(TYPE="BTS")!(TYPE="FHS")!(TYPE="FTS") S ERROR="INVALID SEGMENT TYPE" Q 0
 I HLMSTATE("BATCH"),'HLMSTATE("BATCH","CURRENT MESSAGE") S ERROR="NO MESSAGES IN BATCH, SO SEGMENTS NOT ALLOWED" Q 0
 D ADDSEG^HLOMSG(.HLMSTATE,.TOARY)
 Q 1
 ;**P146 END CJM
 ;
ADDMSG(HLMSTATE,PARMS,ERROR) ;; Begins a new message in the batch.
 ;;Input:
 ;;  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!
 ;;  PARMS( *pass by reference*
 ;;    "EVENT")=3 character event type (required)
 ;;    "MESSAGE TYPE")=3 character message type (required)
 ;;
 ;;Output:
 ;;   FUNCTION - returns 1 on success, 0 on failure
 ;;   HLMSTATE() - (pass by reference, required) This array is used by the HL7 package to track the progress of the message.
 ;;   PARMS - left defined when this function returns
 ;;   ERROR (optional, pass by reference) - returns an error message on failure
 ;;
 N I
 K ERROR
 ;if a 'generic' app ack MSA was built, add it as the first segment before this one
 I $D(HLMSTATE("MSA")) D
 .N TOARY S TOARY(1)=HLMSTATE("MSA") D ADDSEG^HLOMSG(.HLMSTATE,.TOARY)
 .K HLMSTATE("MSA")
 I $L($G(PARMS("EVENT")))'=3 S ERROR="EVENT TYPE INVALID" Q 0
 I $L($G(PARMS("MESSAGE TYPE")))'=3 S ERROR="MESSAGE TYPE INVALID" Q 0
 D ADDMSG^HLOMSG(.HLMSTATE,.PARMS)
 Q 1
 ;
GETSYS(HLMSTATE) ;
 N SYS,SUB
 D SYSPARMS^HLOSITE(.SYS)
 F SUB="DOMAIN","STATION","PROCESSING ID","MAXSTRING","ERROR PURGE","NORMAL PURGE","PORT" S HLMSTATE("SYSTEM",SUB)=SYS(SUB)
 S HLMSTATE("SYSTEM","BUFFER")=SYS("USER BUFFER")
 Q
 ;
MOVEMSG(HLMSTATE,ARY) ;;
 ;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)
 ;Input:
 ;  HLMSTATE (pass by reference) the array created by calling $$NEWMSG or $$NEWBATCH
 ;  ARY - is the name of the array, local or global, where the message was built, used to reference the array by indirection.
 ;Output:
 ;  HLMSTATE (pass by reference) Is updated with information about the
 ;            message.
 ;;
 N I S I=0
 F  S I=$O(@ARY@(I)) Q:'I  D
 .N SEG,J,J2
 .S J=0,J2=1
 .S SEG(J2)=@ARY@(I)
 .F  S J=$O(@ARY@(I,J)) Q:'J  S J2=J2+1,SEG(J2)=@ARY@(I,J)
 .I 'HLMSTATE("BATCH") D
 ..D ADDSEG^HLOMSG(.HLMSTATE,.SEG)
 .E  D
 ..I $E(SEG(1),1,3)="MSH" D
 ...D SPLITHDR^HLOSRVR1(.SEG)
 ...D ADDMSG2^HLOMSG(.HLMSTATE,.SEG)
 ..E  D ADDSEG^HLOMSG(.HLMSTATE,.SEG)
 ;
 ;signal SENDACK^HLOAPI2 that the application built its own msg
 K HLMSTATE("MSA")
 Q
 
--- Routine Detail   --- with STRUCTURED ROUTINE LISTING ---[H[J[2J[HHLOAPI   11730     printed  Sep 23, 2025@19:34:30                                                                                                                                                                                                     Page 2
HLOAPI    ;ALB/CJM-HL7 - Developer API's for sending & receiving messages ;05/12/2009
 +1       ;;1.6;HEALTH LEVEL SEVEN;**126,133,138,139,146**;Oct 13, 1995;Build 16
 +2       ;Per VHA Directive 2004-038, this routine should not be modified.
 +3       ;
NEWMSG(PARMS,HLMSTATE,ERROR) ;; Starts a new message.
 +1       ;;
 +2       ;;** External API **
 +3       ;;  
 +4       ;;Input: 
 +5       ;;   PARMS( *pass by reference*
 +6       ;;     "COUNTRY")=3 character country code (optional)
 +7       ;;     "CONTINUATION POINTER" -indicates a fragmented message
 +8       ;;     "EVENT")=3 character event type (required)
 +9       ;;     "FIELD SEPARATOR")=field separator (optional, defaults to "|")
 +10      ;;     "ENCODING CHARACTERS")= 4 HL7 encoding characters (optional,defaults to "^~\&")
 +11      ;;     "MESSAGE STRUCTURE" - MSH 9, component 3 - a code from the standard HL7 table (optional)
 +12      ;;     "MESSAGE TYPE")=3 character message type (required)
 +13      ;;     "PROCESSING MODE" - MSH 11, component 2 - a 1 character code (optional)
 +14      ;;     "VERSION")=the HL7 Version ID, for example, "2.4" (optional, defaults to 2.4)
 +15      ;;Output:
 +16      ;;  Function- returns 1 on success, 0 on failure
 +17      ;;  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!
 +18      ;;  PARMS - left defined when the function returns
 +19      ;;  ERROR (optional, pass by reference) - returns an error message on failure
 +20      ;;
 +21      ;
 +22       NEW DATA,I,SYSTEM,SUCCESS
 +23       SET SUCCESS=0
 +24       KILL ERROR,HLMSTATE
 +25       Begin DoDot:1
 +26           IF $LENGTH($GET(PARMS("PROCESSING MODE")))
                   IF $LENGTH(PARMS("PROCESSING MODE"))'=1
                       SET ERROR="INVALID PROCESSING MODE"
                       QUIT 
 +27           IF $LENGTH($GET(PARMS("COUNTRY")))
                   IF $LENGTH(PARMS("COUNTRY"))'=3
                       SET ERROR="INVALID COUNTRY CODE"
                       QUIT 
 +28           IF $LENGTH($GET(PARMS("EVENT")))'=3
                   SET ERROR="INVALID EVENT CODE"
                   QUIT 
 +29           IF $LENGTH($GET(PARMS("MESSAGE TYPE")))'=3
                   SET ERROR="INVALID MESSAGE TYPE"
                   QUIT 
 +30           IF $LENGTH($GET(PARMS("ENCODING CHARACTERS")))
                   IF $LENGTH(PARMS("ENCODING CHARACTERS"))'=4
                       SET ERROR="INVALID ENCODING CHARACTERS"
                       QUIT 
 +31           IF $LENGTH($GET(PARMS("FIELD SEPARATOR")))
                   IF $LENGTH(PARMS("FIELD SEPARATOR"))'=1
                       SET ERROR="INVALID FIELD SEPARATOR"
                       QUIT 
 +32           IF '$LENGTH($GET(PARMS("FIELD SEPARATOR")))
                   SET PARMS("FIELD SEPARATOR")="|"
 +33           IF '$LENGTH($GET(PARMS("ENCODING CHARACTERS")))
                   SET PARMS("ENCODING CHARACTERS")="^~\&"
 +34           IF $GET(PARMS("VERSION"))=""
                   SET PARMS("VERSION")="2.4"
 +35           IF ($LENGTH($GET(PARMS("VERSION")))>20)
                   SET ERROR="VERSION > 20 CHARACTERS"
                   QUIT 
 +36           FOR I="MESSAGE TYPE","EVENT","COUNTRY","FIELD SEPARATOR","ENCODING CHARACTERS","VERSION","CONTINUATION POINTER","MESSAGE STRUCTURE","PROCESSING MODE"
                   SET HLMSTATE("HDR",I)=$GET(PARMS(I))
 +37      ;not a batch
               SET HLMSTATE("BATCH")=0
 +38           SET HLMSTATE("DIRECTION")="OUT"
 +39           SET HLMSTATE("IEN")=""
 +40      ;record not yet created
               SET HLMSTATE("BODY")=""
 +41      ;no segments cached
               SET HLMSTATE("CURRENT SEGMENT")=0
 +42      ;nothing in cache
               SET HLMSTATE("UNSTORED LINES")=0
 +43           SET HLMSTATE("LINE COUNT")=0
 +44           DO GETSYS(.HLMSTATE)
 +45           SET SUCCESS=1
           End DoDot:1
 +46       QUIT SUCCESS
 +47      ;
NEWBATCH(PARMS,HLMSTATE,ERROR) ;;Starts a new batch message.  
 +1       ;;Input: 
 +2       ;;  PARMS( *pass by reference*
 +3       ;;   "COUNTRY")=3 character country code (optional)
 +4       ;;   "FIELD SEPARATOR")=field separator (optional, defaults to "|")
 +5       ;;   "ENCODING CHARACTERS")= 4 HL7 encoding characters (optional,defaults to "^~\&") 
 +6       ;;   "VERSION")=the HL7 Version ID, for example, "2.4" (optional, defaults to 2.4)
 +7       ;;Output:
 +8       ;;  Function - returns 1 on success, 0 on failure
 +9       ;;  PARMS - left defined when the function returns
 +10      ;;  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!
 +11      ;;  ERROR (optional, pass by reference) - returns an error message on failure
 +12      ;;
 +13      ;
 +14       NEW DATA,I,SYSTEM,SUCCESS
 +15       SET SUCCESS=0
 +16       KILL ERROR,HLMSTATE
 +17       Begin DoDot:1
 +18           IF $LENGTH($GET(PARMS("COUNTRY")))
                   IF $LENGTH(PARMS("COUNTRY"))'=3
                       SET ERROR="INVALID COUNTRY CODE"
                       QUIT 
 +19           IF $LENGTH($GET(PARMS("ENCODING CHARACTERS")))
                   IF $LENGTH(PARMS("ENCODING CHARACTERS"))'=4
                       SET ERROR="INVALID ENCODING CHARACTERS"
                       QUIT 
 +20           IF $LENGTH($GET(PARMS("FIELD SEPARATOR")))
                   IF $LENGTH(PARMS("FIELD SEPARATOR"))'=1
                       SET ERROR="INVALID FIELD SEPARATOR"
                       QUIT 
 +21           IF '$LENGTH($GET(PARMS("FIELD SEPARATOR")))
                   SET PARMS("FIELD SEPARATOR")="|"
 +22           IF '$LENGTH($GET(PARMS("ENCODING CHARACTERS")))
                   SET PARMS("ENCODING CHARACTERS")="^~\&"
 +23           IF $GET(PARMS("VERSION"))=""
                   SET PARMS("VERSION")="2.4"
 +24           IF ($LENGTH(PARMS("VERSION"))>20)
                   SET ERROR="VERSION > 20 CHARACTERS"
                   QUIT 
 +25           FOR I="COUNTRY","FIELD SEPARATOR","ENCODING CHARACTERS","VERSION"
                   SET HLMSTATE("HDR",I)=$GET(PARMS(I))
 +26           SET HLMSTATE("IEN")=""
 +27      ;msg not yet stored
               SET HLMSTATE("BODY")=""
 +28           SET HLMSTATE("BATCH")=1
 +29           SET HLMSTATE("DIRECTION")="OUT"
 +30      ;no messages in batch
               SET HLMSTATE("BATCH","CURRENT MESSAGE")=0
 +31      ;no segments in cache
               SET HLMSTATE("CURRENT SEGMENT")=0
 +32      ;nothing in cache
               SET HLMSTATE("UNSTORED LINES")=0
 +33      ;no lines within message stored
               SET HLMSTATE("LINE COUNT")=0
 +34           DO GETSYS(.HLMSTATE)
 +35           SET SUCCESS=1
           End DoDot:1
 +36       QUIT SUCCESS
 +37      ;
SET(SEG,VALUE,FIELD,COMP,SUBCOMP,REP) ;;Sets a value to the array SEG(), used for building segments.
 +1       ;;Input:
 +2       ;; SEG - (required, pass by reference) - this is the array where the segment is being built.
 +3       ;; VALUE - the individual value to be set into the segment
 +4       ;; FIELD - the sequence # of the field (optional, defaults to 0)
 +5       ;;     *NOTE: FIELD=0 is used to denote the segment type.
 +6       ;; COMP - the # of the component (optional, defaults to 1)
 +7       ;; SUBCOMP - the # of the subcomponent (optional, defaults to 1)
 +8       ;; REP - the occurrence# (optional, defaults to 1)  For a non-repeating field, the occurrence # need not be provided, because it would be 1.
 +9       ;;Output: 
 +10      ;;  SEG array
 +11      ;;
 +12      ;;  Example:
 +13      ;;    D SET(.SEG,"MSA",0) creates an MSA segment 
 +14      ;;    D SET(.SEG,"AE",1) will place the value into the array position
 +15      ;;    reserved for the 1st field,1st occurence,1st comp,1st subcomp
 +16      ;;
 +17      ;;Implementation Note - This format is used for the segment array built by calls to SET: SEGMENT(<SEQ #>,<occurrence #>,<component #>,<subcomponent #>)=<subcomponent value> 
 +18      ;
 +19       if '$GET(FIELD)
               SET FIELD=0
 +20       if '$GET(COMP)
               SET COMP=1
 +21       if '$GET(SUBCOMP)
               SET SUBCOMP=1
 +22       if '$GET(REP)
               SET REP=1
 +23       SET SEG(FIELD,REP,COMP,SUBCOMP)=$GET(VALUE)
 +24       QUIT 
 +25      ;
ADDSEG(HLMSTATE,SEG,ERROR,TOARY) ;; Adds a segment to the message.
 +1       ;;Input:
 +2       ;;  HLMSTATE() - (pass by reference, required) This array is a workspace for HLO.  The application MUST NOT touch it!
 +3       ;;  SEG() - (pass-by-reference, required) Contains the data.  It be created prior to calling $$ADDSEG.
 +4       ;;
 +5       ;;Note#1:  The message control segments, including the MSH and BHS segments, are added automatically.
 +6       ;;Note#2:  The 0th field must be a 3 character segment type
 +7       ;;Note#3: ***SEG is killed upon successfully adding the segment***
 +8       ;;
 +9       ;;Output:
 +10      ;;   HLMSTATE() - (pass-by-reference, required) This array is used by the HL7 package to track the progress of the message.
 +11      ;;  FUNCTION - returns 1 on success, 0 on failure
 +12      ;;  TOARY (optional, pass by reference) returns the built segment in
 +13      ;;        this format:
 +14      ;;         TOARY(1)
 +15      ;;         TOARY(2)
 +16      ;;         TOARY(3), etc.
 +17      ;;    If the segment fits on a single line, only TOARY(1) is returned.
 +18      ;;
 +19      ;;  ERROR (optional, pass by reference) - returns an error message on failure
 +20      ;;
 +21      ;
 +22       KILL ERROR
 +23       NEW TYPE
 +24       KILL TOARY
 +25      ;
 +26      ;segment type
           SET TYPE=$GET(SEG(0,1,1,1))
 +27      ;
 +28      ;if a 'generic' app ack MSA was built, add it as the first segment before this one
 +29       IF $DATA(HLMSTATE("MSA"))
               Begin DoDot:1
 +30               IF TYPE'="MSA"
                       SET TOARY(1)=HLMSTATE("MSA")
                       DO ADDSEG^HLOMSG(.HLMSTATE,.TOARY)
                       KILL TOARY
 +31               KILL HLMSTATE("MSA")
               End DoDot:1
 +32      ;
 +33       IF ($LENGTH(TYPE)'=3)
               SET ERROR="INVALID SEGMENT TYPE"
               QUIT 0
 +34       IF (TYPE="MSH")!(TYPE="BHS")!(TYPE="BTS")!(TYPE="FHS")!(TYPE="FTS")
               SET ERROR="INVALID SEGMENT TYPE"
               QUIT 0
 +35       IF HLMSTATE("BATCH")
               IF 'HLMSTATE("BATCH","CURRENT MESSAGE")
                   SET ERROR="NO MESSAGES IN BATCH, SO SEGMENTS NOT ALLOWED"
                   QUIT 0
 +36       IF $$BUILDSEG^HLOPBLD(.HLMSTATE,.SEG,.TOARY,.ERROR)
               DO ADDSEG^HLOMSG(.HLMSTATE,.TOARY)
               KILL SEG
               QUIT 1
 +37       QUIT 0
 +38      ;
 +39      ;**P146 START CJM
MOVESEG(HLMSTATE,SEG,ERROR) ;Adds a segment built in the 'traditional' way as an array of lines into the message.
 +1       ;;Input:
 +2       ;;  HLMSTATE() - (pass by reference, required) This array is a workspace for HLO. 
 +3       ;;  SEG() - (pass-by-reference, required) Contains the segment.  The segement.  If the segment is short enough it should consist of only SEG or SEG(1).  If longer, additional lines can be added as SEG(<n>). 
 +4       ;;
 +5       ;;Note#1:  The message control segments, including the MSH, BHS & FTS segments, are added automatically, so may not be added by MOVESEG.
 +6       ;;
 +7       ;;Output:
 +8       ;;   HLMSTATE() - (pass-by-reference, required) This array is the workspace used by HLO.
 +9       ;;  FUNCTION - returns 1 on success, 0 on failure
 +10      ;;
 +11      ;;  ERROR (optional, pass by reference) - returns an error message on failure
 +12      ;;
 +13      ;
 +14       KILL ERROR
 +15       NEW TYPE,NEWCOUNT,OLDCOUNT,TOARY
 +16      ;
 +17       SET NEWCOUNT=1
 +18       IF $LENGTH($GET(SEG))
               SET TOARY(1)=SEG
               SET NEWCOUNT=2
 +19       SET OLDCOUNT=0
 +20       FOR 
               SET OLDCOUNT=$ORDER(SEG(OLDCOUNT))
               if 'OLDCOUNT
                   QUIT 
               SET TOARY(NEWCOUNT)=SEG(OLDCOUNT)
               SET NEWCOUNT=NEWCOUNT+1
 +21      ;segment type
           SET TYPE=$PIECE($GET(TOARY(1)),HLMSTATE("HDR","FIELD SEPARATOR"))
 +22      ;
 +23      ;if a 'generic' app ack MSA was built, add it as the first segment before this one
 +24       IF $DATA(HLMSTATE("MSA"))
               Begin DoDot:1
 +25               IF TYPE'="MSA"
                       NEW TOARY
                       SET TOARY(1)=HLMSTATE("MSA")
                       DO ADDSEG^HLOMSG(.HLMSTATE,.TOARY)
 +26               KILL HLMSTATE("MSA")
               End DoDot:1
 +27      ;
 +28       IF ($LENGTH(TYPE)'=3)
               SET ERROR="INVALID SEGMENT TYPE"
               QUIT 0
 +29       IF (TYPE="MSH")!(TYPE="BHS")!(TYPE="BTS")!(TYPE="FHS")!(TYPE="FTS")
               SET ERROR="INVALID SEGMENT TYPE"
               QUIT 0
 +30       IF HLMSTATE("BATCH")
               IF 'HLMSTATE("BATCH","CURRENT MESSAGE")
                   SET ERROR="NO MESSAGES IN BATCH, SO SEGMENTS NOT ALLOWED"
                   QUIT 0
 +31       DO ADDSEG^HLOMSG(.HLMSTATE,.TOARY)
 +32       QUIT 1
 +33      ;**P146 END CJM
 +34      ;
ADDMSG(HLMSTATE,PARMS,ERROR) ;; Begins a new message in the batch.
 +1       ;;Input:
 +2       ;;  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!
 +3       ;;  PARMS( *pass by reference*
 +4       ;;    "EVENT")=3 character event type (required)
 +5       ;;    "MESSAGE TYPE")=3 character message type (required)
 +6       ;;
 +7       ;;Output:
 +8       ;;   FUNCTION - returns 1 on success, 0 on failure
 +9       ;;   HLMSTATE() - (pass by reference, required) This array is used by the HL7 package to track the progress of the message.
 +10      ;;   PARMS - left defined when this function returns
 +11      ;;   ERROR (optional, pass by reference) - returns an error message on failure
 +12      ;;
 +13       NEW I
 +14       KILL ERROR
 +15      ;if a 'generic' app ack MSA was built, add it as the first segment before this one
 +16       IF $DATA(HLMSTATE("MSA"))
               Begin DoDot:1
 +17               NEW TOARY
                   SET TOARY(1)=HLMSTATE("MSA")
                   DO ADDSEG^HLOMSG(.HLMSTATE,.TOARY)
 +18               KILL HLMSTATE("MSA")
               End DoDot:1
 +19       IF $LENGTH($GET(PARMS("EVENT")))'=3
               SET ERROR="EVENT TYPE INVALID"
               QUIT 0
 +20       IF $LENGTH($GET(PARMS("MESSAGE TYPE")))'=3
               SET ERROR="MESSAGE TYPE INVALID"
               QUIT 0
 +21       DO ADDMSG^HLOMSG(.HLMSTATE,.PARMS)
 +22       QUIT 1
 +23      ;
GETSYS(HLMSTATE) ;
 +1        NEW SYS,SUB
 +2        DO SYSPARMS^HLOSITE(.SYS)
 +3        FOR SUB="DOMAIN","STATION","PROCESSING ID","MAXSTRING","ERROR PURGE","NORMAL PURGE","PORT"
               SET HLMSTATE("SYSTEM",SUB)=SYS(SUB)
 +4        SET HLMSTATE("SYSTEM","BUFFER")=SYS("USER BUFFER")
 +5        QUIT 
 +6       ;
MOVEMSG(HLMSTATE,ARY) ;;
 +1       ;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)
 +2       ;Input:
 +3       ;  HLMSTATE (pass by reference) the array created by calling $$NEWMSG or $$NEWBATCH
 +4       ;  ARY - is the name of the array, local or global, where the message was built, used to reference the array by indirection.
 +5       ;Output:
 +6       ;  HLMSTATE (pass by reference) Is updated with information about the
 +7       ;            message.
 +8       ;;
 +9        NEW I
           SET I=0
 +10       FOR 
               SET I=$ORDER(@ARY@(I))
               if 'I
                   QUIT 
               Begin DoDot:1
 +11               NEW SEG,J,J2
 +12               SET J=0
                   SET J2=1
 +13               SET SEG(J2)=@ARY@(I)
 +14               FOR 
                       SET J=$ORDER(@ARY@(I,J))
                       if 'J
                           QUIT 
                       SET J2=J2+1
                       SET SEG(J2)=@ARY@(I,J)
 +15               IF 'HLMSTATE("BATCH")
                       Begin DoDot:2
 +16                       DO ADDSEG^HLOMSG(.HLMSTATE,.SEG)
                       End DoDot:2
 +17              IF '$TEST
                       Begin DoDot:2
 +18                       IF $EXTRACT(SEG(1),1,3)="MSH"
                               Begin DoDot:3
 +19                               DO SPLITHDR^HLOSRVR1(.SEG)
 +20                               DO ADDMSG2^HLOMSG(.HLMSTATE,.SEG)
                               End DoDot:3
 +21                      IF '$TEST
                               DO ADDSEG^HLOMSG(.HLMSTATE,.SEG)
                       End DoDot:2
               End DoDot:1
 +22      ;
 +23      ;signal SENDACK^HLOAPI2 that the application built its own msg
 +24       KILL HLMSTATE("MSA")
 +25       QUIT