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

RORHL7.m

Go to the documentation of this file.
  1. RORHL7 ;HCIOFO/SG - HL7 UTILITIES ; 11/2/05 10:30am
  1. ;;1.5;CLINICAL CASE REGISTRIES;;Feb 17, 2006
  1. ;
  1. Q
  1. ;
  1. ;***** ADDS THE SEGMENT TO THE HL7 MESSAGE BUFFER
  1. ;
  1. ; .SOURCE Reference to a local variable where the
  1. ; source data is stored
  1. ;
  1. ; [SRCTYPE] Type and format of the source data
  1. ; "C" Complete segment (see the ADDSEGC^RORHL7A
  1. ; for source data format description)
  1. ; "F" List of field values (see the ADDSEGF^RORHL7A
  1. ; for source data format description).
  1. ; This is the default parameter value.
  1. ;
  1. ADDSEG(SOURCE,SRCTYPE) ;
  1. I $G(SRCTYPE)?."F" D ADDSEGF^RORHL7A(.SOURCE) Q
  1. I SRCTYPE="C" D ADDSEGC^RORHL7A(.SOURCE) Q
  1. D ERROR^RORERR(-88,,,,"SRCTYPE",$G(SRCTYPE))
  1. Q
  1. ;
  1. ;***** CREATES A NEW MESSAGE IN THE BATCH
  1. ;
  1. ; The function adds a new message header to the batch. If the batch
  1. ; does not exist yet, it is created.
  1. ;
  1. ; [.RORMSH] Reference to a variable in what a MSH segment of
  1. ; the message is returned.
  1. ;
  1. ; Return Values:
  1. ; <0 Error Code
  1. ; >0 Index of a subnode of the ^TMP("HLS",$J) that
  1. ; contains the new MSH segment.
  1. ;
  1. ; MSH segment is returned as a value of the RORMSH parameter. In case
  1. ; of a long segment, continuations are returned as subnodes.
  1. ;
  1. ; Several nodes (HL7*) in ROREXT are set and the ^TMP("HLS",$J) node
  1. ; is deleted by this entry point before it creates a new batch.
  1. ;
  1. CREATE(RORMSH) ;
  1. N NDX,RC,TMP K RORMSH
  1. Q:$G(ROREXT("HL7PROT"))="" $$ERROR^RORERR(-25)
  1. ;--- Create a message stub for the new batch message
  1. ; (if it has not been created before)
  1. I '$G(ROREXT("HL7MTIEN")) D Q:$G(RC)<0 RC
  1. . N RORMID,RORIEN,RORDT
  1. . ;--- Set up HL7 environment variables
  1. . S RC=$$INIT($NA(^TMP("HLS",$J))) Q:RC<0
  1. . ;--- Create a stub
  1. . S RORDT=$S($G(ROREXT("HDTIEN"))>0:$G(ROREXT("HL7DT")),1:"")
  1. . D CREATE^HLTF(.RORMID,.RORIEN,.RORDT)
  1. . ;--- Save parameters of the new batch message
  1. . S (ROREXT("HL7CNT"),ROREXT("HL7SIZE"))=0
  1. . S ROREXT("HL7DT")=RORDT
  1. . S ROREXT("HL7MID")=RORMID
  1. . S ROREXT("HL7MTIEN")=RORIEN
  1. . ;--- Initialize temporary storage
  1. . K ^TMP("HLS",$J)
  1. ;--- Initialize the HL7 environment variables
  1. S RC=$$INIT() Q:RC<0 RC
  1. S NDX=$G(ROREXT("HL7PTR"))+1
  1. ;--- Reset the Set ID's for all supported segments
  1. F TMP="OBR","OBX","PID","PV1","ZRD","ZSP" D
  1. . S ROREXT("HL7SID",TMP)=1
  1. ;--- Create and store a MSH segment for individual message
  1. S ROREXT("HL7CNT")=ROREXT("HL7CNT")+1
  1. S TMP=ROREXT("HL7MID")_"-"_ROREXT("HL7CNT")
  1. D MSH^HLFNC2(.RORHL,TMP,.RORMSH)
  1. S:$P(RORMSH,RORHL("FS"),17)="US" $P(RORMSH,RORHL("FS"),17)="USA"
  1. M ^TMP("HLS",$J,NDX)=RORMSH
  1. S ROREXT("HL7SIZE")=ROREXT("HL7SIZE")+$L(RORMSH)+$L($G(RORMSH(1)))+1
  1. S ROREXT("HL7PTR")=NDX
  1. Q NDX
  1. ;
  1. ;***** REPLACES ENCODING CHARACTERS WITH ESCAPE CODES
  1. ;
  1. ; STR Source string
  1. ;
  1. ; The HLFS and HLECH variables must be initialized before
  1. ; calling this function (either by the INIT^HLFNC2 or manually).
  1. ;
  1. ; The function returns the source string with encoding
  1. ; characters replaced with corresponding escape codes.
  1. ;
  1. ESCAPE(STR) ;
  1. Q:STR="" STR
  1. N BUF,ESC,CH,I1,I2,SCLST
  1. S SCLST=HLECH_HLFS
  1. ;--- Find all occurrences of encoding characters and
  1. ; save their positions to a local array
  1. F I1=1:1:5 S CH=$E(SCLST,I1),I2=1 Q:CH="" D
  1. . F S I2=$F(STR,CH,I2) Q:'I2 S BUF(I2-1)=I1
  1. Q:$D(BUF)<10 STR
  1. ;--- Replace encoding characters with escape codes
  1. S (BUF,I2)="",ESC=$E(HLECH,3) S:ESC="" ESC="\"
  1. F S I1=I2,I2=$O(BUF(I2)) Q:I2="" D
  1. . S BUF=BUF_$E(STR,I1+1,I2-1)_ESC_$E("SRETF",BUF(I2))_ESC
  1. Q BUF_$E(STR,I1+1,$L(STR))
  1. ;
  1. ;***** CHECKS THE DATE/TIME AND CONVERTS IT TO HL7 FORMAT
  1. ;
  1. ; DATE Date/time in FileMan format
  1. ;
  1. FM2HL(DATE) ;
  1. Q:'$G(DATE) """"""
  1. S DATE=$$FMTHL7^XLFDT(DATE)
  1. Q $S(DATE>0:DATE,1:"")
  1. ;
  1. ;***** INITIALIZES THE HL7 SEPARATORS
  1. ;
  1. ; [.CS] Reference to a local variable where the
  1. ; component separator will be returned to.
  1. ;
  1. ; [.SCS] Reference to a local variable where the
  1. ; sub-component separator will be returned to.
  1. ;
  1. ; [.RPS] Reference to a local variable where the
  1. ; repetition separator will be returned to.
  1. ;
  1. ECH(CS,SCS,RPS) ;
  1. S HLECH=$G(RORHL("ECH"),"^~\&")
  1. S CS=$E(HLECH,1),SCS=$E(HLECH,4),RPS=$E(HLECH,2)
  1. Q
  1. ;
  1. ;***** INITIALIZES THE HL7 ENVIRONMENT VARIABLES
  1. ;
  1. ; [ROR8FILE] Closed root of the buffer that will be used for
  1. ; construction of the HL7 message.
  1. ;
  1. ; Return Values:
  1. ; <0 Error Code
  1. ; 0 Ok
  1. ;
  1. INIT(ROR8FILE) ;
  1. N TMP K RORHL
  1. D INIT^HLFNC2(ROREXT("HL7PROT"),.RORHL)
  1. Q:$G(RORHL) $$ERROR^RORERR(-23,,RORHL)
  1. S TMP=$G(RORHL("ECH"))
  1. Q:$L(TMP)<4 $$ERROR^RORERR(-75)
  1. ;--- Initialize the nodes required for the API's
  1. S:$G(ROR8FILE)'="" ROREXT("HL7BUF")=ROR8FILE
  1. D:$G(ROREXT("HL7BUF"))'=""
  1. . S ROREXT("HL7PTR")=+$O(@ROREXT("HL7BUF")@(""),-1)
  1. Q 0
  1. ;
  1. ;***** CHECKS IF MAXIMUM BATCH SIZE IS REACHED
  1. ;
  1. ; [RESERVE] Number of bytes reserved in the batch (0 by default)
  1. ;
  1. ; Return Values:
  1. ; 0 Messages can be added to the batch
  1. ; 1 Maximum size of the batch has been reached
  1. ;
  1. ISMAXSZ(RESERVE) ;
  1. Q:$G(ROREXT("MAXHL7SIZE"))'>0 0
  1. Q:($G(ROREXT("HL7SIZE"))+$G(RESERVE))<ROREXT("MAXHL7SIZE") 0
  1. S $P(ROREXT("HL7SIZE"),U,2)=1
  1. Q 1
  1. ;
  1. ;***** RETURNS NUMBER OF MESSAGES IN THE CURRENT BATCH
  1. MSGCNT() ;
  1. Q $G(ROREXT("HL7CNT"))
  1. ;
  1. ;***** RETURNS THE POINTER TO LAST SEGMENT IN THE MESSAGE BUFFER
  1. PTR() Q +$G(ROREXT("HL7PTR"))
  1. ;
  1. ;***** DELETES THE SEGMENTS FROM THE HL7 MESSAGE BUFFER
  1. ;
  1. ; SEGPTR An index of the HL7 segment in the message buffer
  1. ;
  1. ; [KEEP] Keep the segment referenced by the SEGPTR and start
  1. ; the rollback from the next segment.
  1. ;
  1. ROLLBACK(SEGPTR,KEEP) ;
  1. N BUF,I,I1,MSH,NODE,SEGNAME
  1. S NODE=ROREXT("HL7BUF"),HLFS=$G(RORHL("FS"),"|")
  1. S I=$S($G(KEEP):$O(@NODE@(SEGPTR)),1:+SEGPTR)
  1. S MSH=$S(I>0:$P($G(@NODE@(I)),HLFS)="MSH",1:0)
  1. ;---
  1. F Q:I'>0 D S I=$O(@NODE@(I))
  1. . S BUF=$G(@NODE@(I))
  1. . ;--- Decrement the batch size indicator
  1. . S ROREXT("HL7SIZE")=$G(ROREXT("HL7SIZE"))-$L(BUF)-1
  1. . S I1=""
  1. . F S I1=$O(@NODE@(I,I1)) Q:I1="" D
  1. . . S ROREXT("HL7SIZE")=ROREXT("HL7SIZE")-$L(@NODE@(I,I1))
  1. . ;--- Decrement the 'Set ID' counter if necessary
  1. . S SEGNAME=$P(BUF,HLFS),I1=+$G(ROREXT("HL7SID",SEGNAME))
  1. . I I1>0 S:$P(BUF,HLFS,2)>0 ROREXT("HL7SID",SEGNAME)=I1-1
  1. . ;--- Delete the segment
  1. . K @NODE@(I)
  1. ;--- Validate current size of the batch
  1. S:$G(ROREXT("HL7SIZE"))<0 ROREXT("HL7SIZE")=0
  1. ;--- Decrease number of messages in the batch if necessary
  1. I MSH S:$G(ROREXT("HL7CNT"))>0 ROREXT("HL7CNT")=ROREXT("HL7CNT")-1
  1. Q
  1. ;
  1. ;***** SENDS THE BATCH MESSAGE
  1. ;
  1. ; .MID Reference to a local variable where the batch
  1. ; message ID (returned by the GENERATE^HLMA) is
  1. ; returned to.
  1. ;
  1. ; Return Values:
  1. ; <0 Error Code
  1. ; 0 Ok
  1. ; >0 There was nothing to send
  1. ;
  1. ; Several nodes (HL7*) in the ROREXT and the ^TMP("HLS",$J) node
  1. ; are deleted by this entry point.
  1. ;
  1. SEND(MID) ;
  1. N RC,RORBUF,RORHLP S MID=""
  1. Q:$G(ROREXT("HL7PROT"))="" $$ERROR^RORERR(-25)
  1. ;--- Quit if there is nothing to send
  1. Q:'$G(ROREXT("HL7MTIEN"))!($D(^TMP("HLS",$J))<10) 1
  1. ;--- Set up the HL7 environment variables
  1. D INIT^HLFNC2(ROREXT("HL7PROT"),.RORHL)
  1. Q:$G(RORHL) $$ERROR^RORERR(-23,,RORHL)
  1. ;--- Send the message
  1. S RORHLP("NAMESPACE")="ROR"
  1. D GENERATE^HLMA(ROREXT("HL7PROT"),"GB",1,.RORBUF,ROREXT("HL7MTIEN"),.RORHLP)
  1. S RC=$S($P(RORBUF,U,2):$$ERROR^RORERR(-24,,RORBUF),1:0)
  1. S MID=$P(RORBUF,U)
  1. ;--- Cleanup if there is no error or not in debug mode
  1. D:'$G(RORPARM("DEBUG"))!(RC'<0)
  1. . F TMP="HL7CNT","HL7MTIEN","HL7SIZE" K ROREXT(TMP)
  1. . K ^TMP("HLS",$J)
  1. Q RC