Home   Package List   Routine Alphabetical List   Global Alphabetical List   FileMan Files List   FileMan Sub-Files List   Package Component Lists   Package-Namespace Mapping  
Routine: HLOAPI

HLOAPI.m

Go to the documentation of this file.
  1. HLOAPI ;ALB/CJM-HL7 - Developer API's for sending & receiving messages ;09/13/2006
  1. ;;1.6;HEALTH LEVEL SEVEN;**126,133**;Oct 13, 1995;Build 13
  1. ;Per VHA Directive 2004-038, this routine should not be modified.
  1. ;
  1. NEWMSG(PARMS,HLMSTATE,ERROR) ;
  1. ;Starts a new message.
  1. ;Input:
  1. ; PARMS( *pass by reference*
  1. ; "COUNTRY")=3 character country code (optional)
  1. ; "CONTINUATION POINTER" -indicates a fragmented message
  1. ; "EVENT")=3 character event type (required)
  1. ; "FIELD SEPARATOR")=field separator (optional, defaults to "|")
  1. ; "ENCODING CHARACTERS")= 4 HL7 encoding characters (optional,defaults to "^~\&")
  1. ; "MESSAGE STRUCTURE" - MSH 9, component 3 - a code from the standard HL7 table (optional)
  1. ; "MESSAGE TYPE")=3 character message type (required)
  1. ; "PROCESSING MODE" - MSH 11, component 2 - a 1 character code (optional)
  1. ; "VERSION")=the HL7 Version ID, for example, "2.4" (optional, defaults to 2.4)
  1. ;Output:
  1. ; Function- returns 1 on success, 0 on failure
  1. ; 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!
  1. ; PARMS - left defined when the function returns
  1. ; ERROR (optional, pass by reference) - returns an error message on failure
  1. ;
  1. ;
  1. N DATA,I,SYSTEM,SUCCESS
  1. S SUCCESS=0
  1. K ERROR,HLMSTATE
  1. D
  1. .I $L($G(PARMS("PROCESSING MODE"))),$L(PARMS("PROCESSING MODE"))'=1 S ERROR="INVALID PROCESSING MODE" Q
  1. .I $L($G(PARMS("COUNTRY"))),$L(PARMS("COUNTRY"))'=3 S ERROR="INVALID COUNTRY CODE" Q
  1. .I $L($G(PARMS("EVENT")))'=3 S ERROR="INVALID EVENT CODE" Q
  1. .I $L($G(PARMS("MESSAGE TYPE")))'=3 S ERROR="INVALID MESSAGE TYPE" Q
  1. .I $L($G(PARMS("ENCODING CHARACTERS"))),$L(PARMS("ENCODING CHARACTERS"))'=4 S ERROR="INVALID ENCODING CHARACTERS" Q
  1. .I $L($G(PARMS("FIELD SEPARATOR"))),$L(PARMS("FIELD SEPARATOR"))'=1 S ERROR="INVALID FIELD SEPARATOR" Q
  1. .I '$L($G(PARMS("FIELD SEPARATOR"))) S PARMS("FIELD SEPARATOR")="|"
  1. .I '$L($G(PARMS("ENCODING CHARACTERS"))) S PARMS("ENCODING CHARACTERS")="^~\&"
  1. .I $G(PARMS("VERSION"))="" S PARMS("VERSION")="2.4"
  1. .I ($L($G(PARMS("VERSION")))>20) S ERROR="VERSION > 20 CHARACTERS" Q
  1. .F I="MESSAGE TYPE","EVENT","COUNTRY","FIELD SEPARATOR","ENCODING CHARACTERS","VERSION","CONTINUATION POINTER","MESSAGE STRUCTURE","PROCESSING MODE" S HLMSTATE("HDR",I)=$G(PARMS(I))
  1. .S HLMSTATE("BATCH")=0 ;not a batch
  1. .S HLMSTATE("DIRECTION")="OUT"
  1. .S HLMSTATE("IEN")=""
  1. .S HLMSTATE("BODY")="" ;record not yet created
  1. .S HLMSTATE("CURRENT SEGMENT")=0 ;no segments cached
  1. .S HLMSTATE("UNSTORED LINES")=0 ;nothing in cache
  1. .S HLMSTATE("LINE COUNT")=0
  1. .D GETSYS(.HLMSTATE)
  1. .S SUCCESS=1
  1. Q SUCCESS
  1. ;
  1. NEWBATCH(PARMS,HLMSTATE,ERROR) ;
  1. ;Starts a new batch message.
  1. ;Input:
  1. ; PARMS( *pass by reference*
  1. ; "COUNTRY")=3 character country code (optional)
  1. ; "FIELD SEPARATOR")=field separator (optional, defaults to "|")
  1. ; "ENCODING CHARACTERS")= 4 HL7 encoding characters (optional,defaults to "^~\&")
  1. ; "VERSION")=the HL7 Version ID, for example, "2.4" (optional, defaults to 2.4)
  1. ;Output:
  1. ; Function - returns 1 on success, 0 on failure
  1. ; PARMS - left defined when the function returns
  1. ; 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!
  1. ; ERROR (optional, pass by reference) - returns an error message on failure
  1. ;
  1. ;
  1. N DATA,I,SYSTEM,SUCCESS
  1. S SUCCESS=0
  1. K ERROR,HLMSTATE
  1. D
  1. .I $L($G(PARMS("COUNTRY"))),$L(PARMS("COUNTRY"))'=3 S ERROR="INVALID COUNTRY CODE" Q
  1. .I $L($G(PARMS("ENCODING CHARACTERS"))),$L(PARMS("ENCODING CHARACTERS"))'=4 S ERROR="INVALID ENCODING CHARACTERS" Q
  1. .I $L($G(PARMS("FIELD SEPARATOR"))),$L(PARMS("FIELD SEPARATOR"))'=1 S ERROR="INVALID FIELD SEPARATOR" Q
  1. .I '$L($G(PARMS("FIELD SEPARATOR"))) S PARMS("FIELD SEPARATOR")="|"
  1. .I '$L($G(PARMS("ENCODING CHARACTERS"))) S PARMS("ENCODING CHARACTERS")="^~\&"
  1. .I $G(PARMS("VERSION"))="" S PARMS("VERSION")="2.4"
  1. .I ($L(PARMS("VERSION"))>20) S ERROR="VERSION > 20 CHARACTERS" Q
  1. .F I="COUNTRY","FIELD SEPARATOR","ENCODING CHARACTERS","VERSION" S HLMSTATE("HDR",I)=$G(PARMS(I))
  1. .S HLMSTATE("IEN")=""
  1. .S HLMSTATE("BODY")="" ;msg not yet stored
  1. .S HLMSTATE("BATCH")=1
  1. .S HLMSTATE("DIRECTION")="OUT"
  1. .S HLMSTATE("BATCH","CURRENT MESSAGE")=0 ;no messages in batch
  1. .S HLMSTATE("CURRENT SEGMENT")=0 ;no segments in cache
  1. .S HLMSTATE("UNSTORED LINES")=0 ;nothing in cache
  1. .S HLMSTATE("LINE COUNT")=0 ;no lines within message stored
  1. .D GETSYS(.HLMSTATE)
  1. .S SUCCESS=1
  1. Q SUCCESS
  1. ;
  1. SET(SEG,VALUE,FIELD,COMP,SUBCOMP,REP) ;
  1. ;Sets a value to the array SEG(), used for building segments.
  1. ;Input:
  1. ; SEG - (required, pass by reference) - this is the array where the segment is being built.
  1. ; VALUE - the individual value to be set into the segment
  1. ; FIELD - the sequence # of the field (optional, defaults to 0)
  1. ; *NOTE: FIELD=0 is used to denote the segment type.
  1. ; COMP - the # of the component (optional, defaults to 1)
  1. ; SUBCOMP - the # of the subcomponent (optional, defaults to 1)
  1. ; REP - the occurrence# (optional, defaults to 1) For a non-repeating field, the occurrence # need not be provided, because it would be 1.
  1. ;Output:
  1. ; SEG array
  1. ;
  1. ; Example:
  1. ; D SET(.SEG,"MSA",0) creates an MSA segment
  1. ; D SET(.SEG,"AE",1) will place the value into the array position
  1. ; reserved for the 1st field,1st occurence,1st comp,1st subcomp
  1. ;
  1. ;Implementation Note - This format is used for the segment array built by calls to SET: SEGMENT(<SEQ #>,<occurrence #>,<component #>,<subcomponent #>)=<subcomponent value>
  1. ;
  1. S:'$G(FIELD) FIELD=0
  1. S:'$G(COMP) COMP=1
  1. S:'$G(SUBCOMP) SUBCOMP=1
  1. S:'$G(REP) REP=1
  1. S SEG(FIELD,REP,COMP,SUBCOMP)=$G(VALUE)
  1. Q
  1. ;
  1. ADDSEG(HLMSTATE,SEG,ERROR) ;Adds a segment to the message.
  1. ;Input:
  1. ; 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!
  1. ; SEG() - (pass by reference, required) Contains the data. It must be built by calls to SET prior to calling $$ADDSEG.
  1. ;
  1. ;Note#1: The message control segments, including the MSH and BHS segments, are added automatically.
  1. ;Note#2: The 0th field must be a 3 character segment type
  1. ;Note#3: ***SEG is killed upon successfully adding the segment***
  1. ;
  1. ;Output:
  1. ; HLMSTATE() - (pass by reference, required) This array is used by the HL7 package to track the progress of the message.
  1. ; FUNCTION - returns 1 on success, 0 on failure
  1. ; ERROR (optional, pass by reference) - returns an error message on failure
  1. ;
  1. ;
  1. K ERROR
  1. N TOARY,TYPE
  1. ;
  1. S TYPE=$G(SEG(0,1,1,1)) ;segment type
  1. ;
  1. ;if a 'generic' app ack MSA was built, add it as the first segment before this one
  1. I $D(HLMSTATE("MSA")) D
  1. .I TYPE'="MSA" S TOARY(1)=HLMSTATE("MSA") D ADDSEG^HLOMSG(.HLMSTATE,.TOARY) K TOARY
  1. .K HLMSTATE("MSA")
  1. ;
  1. I ($L(TYPE)'=3) S ERROR="INVALID SEGMENT TYPE" Q 0
  1. I (TYPE="MSH")!(TYPE="BHS")!(TYPE="BTS")!(TYPE="FHS")!(TYPE="FTS") S ERROR="INVALID SEGMENT TYPE" Q 0
  1. I HLMSTATE("BATCH"),'HLMSTATE("BATCH","CURRENT MESSAGE") S ERROR="NO MESSAGES IN BATCH, SO SEGMENTS NOT ALLOWED" Q 0
  1. I $$BUILDSEG^HLOPBLD(.HLMSTATE,.SEG,.TOARY,.ERROR) D ADDSEG^HLOMSG(.HLMSTATE,.TOARY) K SEG Q 1
  1. Q 0
  1. ;
  1. ADDMSG(HLMSTATE,PARMS,ERROR) ;
  1. ;Begins a new message in the batch.
  1. ;Input:
  1. ; 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!
  1. ; PARMS( *pass by reference*
  1. ; "EVENT")=3 character event type (required)
  1. ; "MESSAGE TYPE")=3 character message type (required)
  1. ;
  1. ;Output:
  1. ; FUNCTION - returns 1 on success, 0 on failure
  1. ; HLMSTATE() - (pass by reference, required) This array is used by the HL7 package to track the progress of the message.
  1. ; PARMS - left defined when this function returns
  1. ; ERROR (optional, pass by reference) - returns an error message on failure
  1. ;
  1. N I
  1. K ERROR
  1. ;if a 'generic' app ack MSA was built, add it as the first segment before this one
  1. I $D(HLMSTATE("MSA")) D
  1. .I TYPE'="MSA" N TOARY M TOARY=HLMSTATE("MSA") D ADDSEG^HLOMSG(.HLMSTATE,.TOARY) K TOARY
  1. .K HLMSTATE("MSA")
  1. I $L($G(PARMS("EVENT")))'=3 S ERROR="EVENT TYPE INVALID" Q 0
  1. I $L($G(PARMS("MESSAGE TYPE")))'=3 S ERROR="MESSAGE TYPE INVALID" Q 0
  1. D ADDMSG^HLOMSG(.HLMSTATE,.PARMS)
  1. Q 1
  1. ;
  1. GETSYS(HLMSTATE) ;
  1. N SYS,SUB
  1. D SYSPARMS^HLOSITE(.SYS)
  1. F SUB="DOMAIN","STATION","PROCESSING ID","MAXSTRING","ERROR PURGE","NORMAL PURGE","PORT" S HLMSTATE("SYSTEM",SUB)=SYS(SUB)
  1. S HLMSTATE("SYSTEM","BUFFER")=SYS("USER BUFFER")
  1. Q
  1. ;
  1. 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)
  1. ;Input:
  1. ; HLMSTATE (pass by reference) the array created by calling $$NEWMSG or $$NEWBATCH
  1. ; ARY - is the name of the array, local or global, where the message was built, used to reference the array by indirection.
  1. ;
  1. N I S I=0
  1. F S I=$O(@ARY@(I)) Q:'I D
  1. .N SEG,J,J2
  1. .S J=0,J2=1
  1. .S SEG(J2)=@ARY@(I)
  1. .F S J=$O(@ARY@(I,J)) Q:'J S J2=J2+1,SEG(J2)=@ARY@(I,J)
  1. .I 'HLMSTATE("BATCH") D
  1. ..D ADDSEG^HLOMSG(.HLMSTATE,.SEG)
  1. .E D
  1. ..I $E(SEG(1),1,3)="MSH" D
  1. ...D SPLITHDR^HLOSRVR1(.SEG)
  1. ...D ADDMSG2^HLOMSG(.HLMSTATE,.SEG)
  1. ..E D ADDSEG^HLOMSG(.HLMSTATE,.SEG)
  1. ;
  1. ;signal SENDACK^HLOAPI2 that the application built its own msg
  1. K HLMSTATE("MSA")
  1. Q