HMPDCRC ;SLC/MKB,AGP,ASMR/RRB,BL - Compute CRC32 for VistA data;Aug 29, 2016 20:06:27
 ;;2.0;ENTERPRISE HEALTH MANAGEMENT PLATFORM;**1,3**;May 15, 2016;Build 15
 ;Per VA Directive 6402, this routine should not be modified.
 ;
 ; External References          DBIA#
 ; -------------------          -----
 ; ^DPT                         10035
 ; %ZTLOAD                      10063
 ; MPIF001                       2701
 ; XLFCRC                        3156
 ; XLFDT                        10103
 
Q
 
;
CHECK(HMPCRC,FILTER) ; -- Return CRC32 checksums of VistA data
 ; RPC = HMP GET CHECKSUM
 ; where FILTER("system")    = name of calling/client system
 ;       FILTER("patientId") = DFN or DFN;ICN
 ;       FILTER("domain")    = name of desired data type (see HMPDJ0)
 ;       FILTER("uid")       = single item id to return  [opt]
 ;       FILTER("start")     = start date.time of search [opt]
 ;       FILTER("stop")      = stop date.time of search  [opt]
 ;       FILTER("queued")    = true or false
 ;
 ; HMPCRC returns the name of the ^TMP array containing the results
 ;
 
DFN,NODE,QUEUED,SYS,HMPSYS
 
^TMP("HMPDCRC",$J),HMPCRC
 
SYS=$G(FILTER("system")) SYS="" Q
 S 
DFN=$G(FILTER("patientId")) DFN="" Q
 S 
QUEUED=$G(FILTER("queued"))
 
NODE="HMPDCRC "_SYS_"-"_"-"_DFN
 
FILTER("node")=NODE
 
HMPSYS=$$SYS^HMPUTILS
 
;
 ; - if not queued, generate checksums and exit w/values in ^TMP
 
QUEUED'="true" D  Q
 
^XTMP(NODE,0)=$$FMADD^XLFDT(DT,1)_U_DT_U_"Checksum for Server "_SYS_" patient "_DFN
 
EN(.FILTER)
 . 
^TMP("HMPDCRC",$J)=^XTMP(NODE,"data")
 . 
HMPCRC=$NA(^TMP("HMPDCRC",$J))
 . 
^XTMP(NODE)
 
;
 ; - Queue job if not started, else return data if done
 
+$G(^XTMP(NODE,"start"))=0 QUEUED(.FILTER,NODE,SYS,DFNQ
 I 
+$G(^XTMP(NODE,"stop"))>0 D  K ^XTMP(NODE)
 . 
I $G(^XTMP(NODE,"error"))'=""  HMPCRC=^XTMP(NODE,"error"Q
 
HMPCRC=$NA(^TMP("HMPDCRC",$J))
 . 
^TMP("HMPDCRC",$J)=^XTMP(NODE,"data")
 
Q
 
;
QUEUED(FILTER,NODE,SYS,DFN) ; -- start job to generate checksums
 
ZTDESC,ZTIO,ZTRTN,ZTSAVE,ZTDTH,ZTSK
 
^XTMP(NODE,0)=$$FMADD^XLFDT(DT,1)_U_DT_U_"Checksum for Server "_SYS_" patient "_DFN
 
ZTRTN="EN1^HMPDCRC",ZTDESC="Patient Checksum Extract for "_DFN
 
ZTDTH=$$NOW^XLFDT(),ZTIO="",ZTSAVE("FILTER(")=""
 
^XTMP(NODE,"start")=$$NOW^XLFDT()
 
^%ZTLOAD +$G(ZTSK)>0  ^XTMP(NODE,"task")=+$G(ZTSKQ    ;success
 
^XTMP(NODE,"error")="Cannot start a task job"
 
^XTMP(NODE,"stop")=$$NOW^XLFDT()
 
^XTMP(NODE,"task")=ZTSK
 
Q
 
;
EN(FILTER) ; -- Return CRC values of requested data in ^XTMP(node,"data") as JSON
EN1 ;           [entry point for queued job]
 ;
 
ICN,DFN,NODE,UID,HMPP,TYPE,HMPTN,CRC
 
HMPCRC,HMPSTART,HMPSTOP,HMPMAX,HMPI,HMPID,HMPTYPE ;for HMPDJ0
 
^TMP("HMPCRC",$J),^TMP("HMPCRCF",$J)
 
;
 ; parse & validate input parameters
 
DFN=$G(FILTER("patientId")),HMPCRC=""
 
ICN=+$P($G(DFN),";",2),DFN=+$G(DFN)
 
;DE4496 on next 2 lines, 19 August 2016
 
'(DFN>0),ICN DFN=+$$GETDFN^MPIF001(ICN)
 
'(DFN>0)!'$D(^DPT(DFN)) LOGDPT^HMPLOG(DFNQ  ;ICR 10035 DE 2818 ASF 11/2/15
 
NODE=$G(FILTER("node")) NODE="" NODE="HMPDCRC"
 ;
 
HMPMAX=9999,HMPI=0  ;for HMPDJ0
 
HMPSTART=+$G(FILTER("start"),1410102)
 
HMPSTOP=+$G(FILTER("stop"),4141015)
 
UID=$G(FILTER("uid")),HMPTYPE=$G(FILTER("domain"))
 
I $L(UIDHMPTYPE=$P(UID,":",3),HMPID=$P(UID,":",6)
 
E  S:HMPTYPE="" HMPTYPE=$$ALL
 
;
 
HMPP=1:1:$L(HMPTYPE,";"TYPE=$P(HMPTYPE,";",HMPPI $L(TYPED
 
HMPTN=$$TAG^HMPDJ(TYPE)_"^HMPDJ0" Q:'$L($T(@HMPTN))
 . 
@HMPTN
 
;
 
I $L(UIDD  G ENQ ;single item
 
CRC=$G(^TMP("HMPCRC",$J,HMPTYPE,UID))
 . 
^XTMP(NODE,"data",1)=CRC,^XTMP(NODE,"stop")=$$NOW^XLFDT()
 
; generate checksum for each domain requested
 
TYPE="" F  S TYPE=$O(^TMP("HMPCRC",$J,TYPE)) Q:TYPE=""  D
 
CRC="" GET($NA(^TMP("HMPCRC",$J,TYPE)),.CRC)
 . 
^TMP("HMPCRC",$J,TYPE)=CRC
 
I $L(HMPTYPE,";")>1 D  ;get whole-chart checksum
 
CRC="" GET($NA(^TMP("HMPCRC",$J)),.CRC)
 . 
^TMP("HMPCRC",$J,"patient")=CRC
 
;
ENCODE ; -- return list(s) of checksums as JSON
 
PREP
 
ENCODE^HMPJSON($NA(^TMP("HMPCRCF",$J)),$NA(^XTMP(NODE,"data")),"ERROR")
 
^XTMP(NODE,"stop")=$$NOW^XLFDT()
 
;
ENQ ^TMP("HMPCRC",$J),^TMP("HMPCRCF",$J)
 
Q
 
;
PREP ; -- reformat ^TMP("HMPCRC",$J) for JSON utility -> ^TMP("HMPCRCF",$J)
 
DCNT,DOMAIN,UID,UCNT
 
DOMAIN="",DCNT=0
 
F  S DOMAIN=$O(^TMP("HMPCRC",$J,DOMAIN)) Q:DOMAIN=""  D
 
^TMP("HMPCRCF",$J,DOMAIN,"crc")=^TMP("HMPCRC",$J,DOMAIN)
 . 
UCNT=0,UID="" F  S UID=$O(^TMP("HMPCRC",$J,DOMAIN,UID)) Q:UID=""  D
 
.. UCNT=UCNT+1,^TMP("HMPCRCF",$J,DOMAIN,"uids",UCNT,UID)=^TMP("HMPCRC",$J,DOMAIN,UID)
 
Q
 
;
GET(LIST,CRC) ; -- compute CRC32 value for LIST of strings
 
CRC=$G(CRC),I=""
 
F  S I=$O(@LIST@(I)) Q:I=""  I $G(@LIST@(I))'="" CRC=$$CRC32^XLFCRC(I_":"_@LIST@(I),CRC)
 
Q
 
;
ONE(ARRAY,COLL) ; -- process one data item [save result in ^TMP]
 
LIST,UID,ATTR,CRC
 
LIST=$$ATTR(COLL),UID=$G(@ARRAY@("uid")) Q:UID=""
 
ATTR="" F  S ATTR=$O(@ARRAY@(ATTR)) Q:ATTR=""  LIST'[(U_ATTR_U@ARRAY@(ATTR)
 
GET(ARRAY,.CRC)
 
^TMP("HMPCRC",$J,COLL,UID)=CRC
 
HMPI=HMPI+1
 
Q
 
;
GET1(ARRAY,COLL) ; -- process one data item [return result]
 
LIST,ATTR,ITEM,CRC
 
LIST=$$ATTR(COLL)
 
ATTR="" F  S ATTR=$O(@ARRAY@(ATTR)) Q:ATTR=""  LIST[(U_ATTR_UITEM(ATTR)=@ARRAY@(ATTR)
 
GET(ITEM,.CRC)
 
CRC
 
;
ALL() ; -- return string for all types of data
 
"problem;allergy;consult;vital;lab;procedure;obs;order;treatment;med;ptf;factor;immunization;exam;cpt;education;pov;skin;image;appointment;surgery;document;visit;mh"
 ;
ATTR(X) ; -- return list of attributes needed for collection X
 
Y=""
 
X="vital"        Y="^observed^typeName^result^"
 
X="problem"      Y="^onset^problemText^statusName^"
 
X="allergy"      Y="^entered^summary^"
 
X="order"        Y="^start^name^statusName^"
 
X="treatment"    Y="^start^name^statusName^"
 
X="med"          Y="^overallstart^name^vaStatus^"
 
X="consult"      Y="^startDate^typeName^statusName^"
 
X="procedure"    Y="^dateTime^name^statusName^"
 
X="obs"          Y="^observed^typeName^result^"
 
X="lab"          Y="^observed^typeName^"
 
X="image"        Y="^dateTime^name^statusName^"
 
X="surgery"      Y="^dateTime^typeName^statusName^"
 
X="document"     Y="^referenceDateTime^localTitle^statusName^"
 
X="mh"           Y="^administeredDateTime^name^"
 
X="immunization" Y="^administeredDateTime^name^"
 
X="pov"          Y="^entered^name^"
 
X="skin"         Y="^entered^name^result^"
 
X="exam"         Y="^entered^name^result^"
 
X="cpt"          Y="^entered^name^"
 
X="education"    Y="^entered^name^result^"
 
X="factor"       Y="^entered^name^"
 
X="appointment"  Y="^dateTime^typeName^appointmentStatus^"
 
X="visit"        Y="^dateTime^typeName^"
 
X="ptf"          Y="^arrivalDateTime^icdCode^"
 
Y
 
;
 ;
TEST(FILTER) ;
 
DONE,OUT
 
DONE=0
 
F  D  Q:DONE=1
 .
CHECK(.OUT,.FILTER)
 .
!,$D(OUT)
 .
I $D(OUT)>0 DONE=1
 
Q