C0CDIC   ; CCDCCR/GPL - CCR Dictionary utilities; 6/1/08
 ;;0.1;CCDCCR;nopatch;noreleasedate
 ;Copyright 2008 WorldVistA.  Licensed under the terms of the GNU
 ;General Public License See attached copy of the License.
 ;
 ;This program is free software; you can redistribute it and/or modify
 ;it under the terms of the GNU General Public License as published by
 ;the Free Software Foundation; either version 2 of the License, or
 ;(at your option) any later version.
 ;
 ;This program is distributed in the hope that it will be useful,
 ;but WITHOUT ANY WARRANTY; without even the implied warranty of
 ;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ;GNU General Public License for more details.
 ;
 ;You should have received a copy of the GNU General Public License along
 ;with this program; if not, write to the Free Software Foundation, Inc.,
 ;51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 ;
 W "This is the CCR Dictionary Utility Library ",!
 W !
 Q
 ;
GVARS(C0CVARS,C0CT) ; Get the CCR variables from the CCR template
 ; and return them in C0CVARS, which is passed by name
 ; FIRST PIECE OF C0CVARS(x) IS THE VARIABLE NAME, SECOND PIECE
 ; IS THE LINE NUMBER OF THE VARIABLE IN THE TEMPLATE
 ; C0CT IS RETURNED AS THE CCR TEMPLATE
 N C0CTVARS ; ARRAY FOR THE TEMPLATE AND ARRAY FOR THE VARS
 D LOAD^GPLCCR0(C0CT) ; LOAD THE CCR TEMPLATE
 D XVARS^GPLXPATH("C0CTVARS",C0CT) ; PULL OUT THE VARS
 N C0CI,C0CX
 S @C0CVARS@(0)=C0CTVARS(0) ; SAME COUNT
 F C0CI=1:1:C0CTVARS(0) D  ; FOR EVERY LINE IN THE ARRAY
 . S C0CX=C0CTVARS(C0CI) ; THE VARIABLE - 3 PIECES, FIRST ONE NULL
 . S @C0CVARS@(C0CI)=$P(C0CX,"^",2)_"^"_$P(C0CX,"^",3) ; VAR NAME^LINE NUMBER
 ;D PARY^GPLXPATH("C0CVARS")
 Q
 ;
GXPATH(C0CPVARS,C0CPT) ; LOAD THE CCR TEMPLATE INTO C0CPT, PULL OUT VARIABLES
 ; AND THE XPATH TO THE VARIABLES INTO C0CPVARS
 ; BY INDEXING THE TEMPLATE C0CT AND MATCHING THE XPATH TO THE VARIABLE
 ; BOTH ARE PASSED BY NAME
 ; C0CPVARS(x) IS VAR^LINENUM^XPATH SORTED BY LINENUM
 ; C0CPVARS(0) IS NUMBER OF VARIABLES
 ; C0CPT(0) IS NUMBER OF LINES IN THE TEMPLATE
 D GVARS(C0CPVARS,C0CPT) ; GET THE VARIABLES AND LINE NUMBERS
 ;N C0CTVARS ; HASH TABLE FOR VARIABLE BY LINE NUMBER
 D HASHV ; PUT THE VARIABLES IN A LINE NUMBER HASH FOR MATCHING TO XPATHS
 ; NOW GO GET THE XPATH INDEXES
 D INDEX^GPLXPATH(C0CPT) ; ADD THE XPATH INDEXES TO THE TEMPLATE ARRAY
 S C0CI="" ; GOING TO LOOP THROUGH THE WHOLE ARRAY LOOKING AT XPATHS
 F  S C0CI=$O(@C0CPT@(C0CI)) Q:C0CI=""  D  ; VISIT EVERY LINE
 . I +C0CI'=0 Q  ; SKIP EVERYTHING BUT THE XPATH INDEX
 . I C0CI=0 Q  ; SKIP THE ZERO NODE
 . S C0CX=@C0CPT@(C0CI) ; PULL OUT THE LINE NUMBERS X^Y
 . S C0CY=$P(C0CX,"^",1) ; STARTING LINE NUMBER
 . S C0CZ=$P(C0CX,"^",2) ; ENDING LINE NUMBER
 . I C0CY=C0CZ D  ; THIS IS AN XPATH END NODE, HAS A VARIABLE (WE HOPE)
 . . ; W "FOUND ",C0CI,!
 . . I $D(C0CTVARS(C0CY)) D  ; IF THERE IS A VARIABLE THERE
 . . . S $P(C0CTVARS(C0CY),"^",3)=C0CI ; INSERT THE XPATH FOR THE VAR
 D SORTV ; SORT THE ARRAY BY LINE NUMBER
 Q
 ;
HASHV ; INTERNAL ROUTINE TO PUT VARIABLE NAMES IN A LINE NUMBER HASH
 ;N C0CI,C0CTVARS,C0CX,C0CY
 F C0CI=1:1:@C0CPVARS@(0) D  ; FOR THE ENTIRE ARRAY
 . S C0CX=$P(@C0CPVARS@(C0CI),"^",2) ; LINE NUMBER
 . S C0CY=$P(@C0CPVARS@(C0CI),"^",1) ; VARIABLE NAME
 . S C0CTVARS(C0CX)=C0CY ; BUILD HASH OF VARIABLES BY LINE NUMBER
 Q
 ;
SORTV ; INTERNAL ROUTINE TO OUTPUT VARIABLES (AND XPATHS) IN LINE NUMBER ORDER
 ;N C0CV2 ; SCRACTH SPACE FOR BUILDING SORTED ARRAY
 S C0CI="" ;
 F  S C0CI=$O(C0CTVARS(C0CI)) Q:C0CI=""  D  ; BY LINE NUMBER
 . S C0CX=C0CTVARS(C0CI) ;VARIABLE NAME
 . S $P(C0CX,"^",2)=C0CI ; LINE NUMBER IS SECOND PIECE
 . D PUSH^GPLXPATH("C0C2",C0CX) ; PUT ONTO ARRAY
 K @C0CPVARS
 M @C0CPVARS=C0C2
 Q
 ;
LOAD ; LOAD VARIABLE NAMES AND XPATH IN ^C0CDIC(170
 ; INITIAL LOAD OF THE CCR DICTIONARY
 ;
 N C0CDIC,C0CARY,C0CXML,C0CFDA,C0CI
 S C0CDIC="^C0CDIC(170," ; ROOT OF THE CCR DICTIONARY
 D GXPATH("C0CARY","C0CXML") ; FETCH THE VARIABLES AND XPATH INTO C0CARY
 ; C0CXML WILL CONTAIN THE TEMPLATE - NOT NEEDED FOR LOAD
 D PARY^GPLXPATH("C0CARY") ;TEST
 F C0CI=1:1:C0CARY(0) D  ; LOAD EACH VARIABLE
 . S C0CFDA(170,"+"_C0CI_",",.01)=$P(C0CARY(C0CI),"^",1) ; VAR NAME
 . S C0CFDA(170,"+"_C0CI_",",2)=$P(C0CARY(C0CI),"^",3) ; XPATH
 . D UPDATE^DIE("","C0CFDA")
 . I $D(^TMP("DIERR",$J)) U $P BREAK
 . W "LOADING:",C0CI," ",C0CARY(C0CI),!
 Q
 ;
FIELDS(C0CFRTN,C0CF) ; RETURNS AN ARRAY OF THE FIELDS IN FILE C0CF,
 ; C0CFRTN IS PASSED BY NAME, C0CF IS PASSED BY VALUE
 ;
 N C0CFI,C0CFJ ;INNER LOOP, OUTER LOOP
 N C0CFN ; FIELD NAME
 S C0CFI=0 S C0CFJ=C0CF
 K @C0CFRTN ; CLEAR THE RETURN ARRAY
 F  Q:C0CFJ'[C0CF  D ; FOR THE C0CF FILE AND ALL SUBFILES INCLUSIVE
 . ;W "1: "_C0CFJ," ",C0CFI,!
 . F  S C0CFI=$O(^DD(C0CFJ,C0CFI)) Q:+C0CFI=0  D  ; EVERY FIELD
 . . ;W "2: "_C0CFJ," ",C0CFI,!
 . . S C0CFN=$P(^DD(C0CFJ,C0CFI,0),"^",1) ;PULL FIELD NAME FROM ^DD
 . . ;W "N: ",C0CFN,!
 . . ;I C0CFN="STR" W C0CFN," ",C0CFJ,!
 . . I $D(@C0CFRTN@(C0CFN)) D  ; IS THIS A DUPLICATE?
 . . . W "DUPLICATE FOUND! ",C0CFJ," ",C0CFI," ",C0CFN,!,@C0CFRTN@(C0CFN),!
 . . . S @C0CFRTN@(C0CFN_"_"_C0CFJ)=C0CFJ_"^"_C0CFI
 . . E  S @C0CFRTN@(C0CFN)=C0CFJ_"^"_C0CFI
 . S C0CFJ=$O(^DD(C0CFJ)) ; NEXT SUBFILE
 Q
 ;
GET(GRTN,GFILE,GIEN,GNN) ; RETURN THE DICTIONARY RECORD GIEN IN ARRAY GRTN, PASSED
 ; BY NAME
 ;
 N C0CTMP,C0CI,C0CJ,C0CREF,C0CNAME
 I $D(GNN) I GNN="ALL" S C0CNN=0 ; NOT NON-NULL (ALL FIELDS TO BE RETURNED)
 E  S C0CNN=1 ; NON-NULL IS TRUE (ONLY POPULATED FIELDS RETURNED)
 S C0CREF=GIEN_"," ; OPEN ROOT REFERENCE INTO FILE
 D CLEAN^DILF ; MAKE SURE WE ARE CLEANED UP
 D GETS^DIQ(GFILE,C0CREF,"**","","C0CTMP")
 D FIELDS(GRTN,GFILE) ;GET ALL THE FIELD NAMES FOR THE FILE
 S (C0CI,C0CJ)=""
 F  S C0CJ=$O(C0CTMP(C0CJ)) Q:C0CJ=""  D  ; FOR ALL SUBFILES
 . S C0CREF=$O(C0CTMP(C0CJ,"")) ; RECORD REFERENCE
 . F  S C0CI=$O(C0CTMP(C0CJ,C0CREF,C0CI)) Q:C0CI=""  D  ; ARRAY OF FIELDS
 . . ;W C0CJ," ",C0CI,!
 . . S C0CNAME=$P(^DD(C0CJ,C0CI,0),"^",1) ;PULL THE FIELD NAME
 . . S C0CVALUE=C0CTMP(C0CJ,C0CREF,C0CI) ;
 . . I C0CVALUE["C0CTMP" S C0CVALUE=C0CTMP(C0CJ,C0CREF,C0CI,1) ;1ST LINE OF WP
 . . S $P(@GRTN@(C0CNAME),"^",3)=C0CVALUE ;RETURN VALUE IN P3
 I C0CNN D  ; IF ONLY NON-NULL VALUES ARE TO BE RETURNED
 . S C0CI=""
 . F  S C0CI=$O(@GRTN@(C0CI)) Q:C0CI=""  D  ; GO THROUGH THE WHOLE ARRAY
 . . I $P(@GRTN@(C0CI),"^",3)="" K @GRTN@(C0CI) ; KILL THE NULL ENTRIES
 Q
 ;
GETALL(GARFLD,GARVAL,GAFILE,GACNT,GASTRT,GANN) ; RETURN FIELD MAP AND VALUES
 ; GARFLD, PASSED BY NAME, RETURNS FIELD MAP @GARFLD@("FIELDNAME")
 ; .. ANY FIELD USED BY ANY RECORD PROCESSED IS IN THE FIELD MAP
 ; .. GARFLD IS NOT INITIALIZED, SO MULTIPLE CALLS ARE CUMULATIVE
 ; .. IF GANN="ALL" THEN ALL FIELDS FOR THE FILE ARE IN THE FIELD MAP
 ; GARVAL, PASSED BY NAME, RETURNS VALUES AS @GARVAL@(IEN,"FIELDNAME")=VALUE
 ; .. FOR EVERY RECORD PROCESSED. IT IS NOT INITIALEZED SO IT IS CUMMULATIVE
 ; .. EVEN IF GANN="ALL" ONLY POPULATED FIELDS ARE RETURNED IN GARVAL
 ; .. NUL FIELDS CAN BE DETERMINED BY CHECKING GARFLD - THIS SAVES SPACE
 ; GARFILE IS THE FILE NUMBER TO BE PROCESSED. IT IS PASSED BY VALUE
 ; GARCNT IS THE NUMBER OF RECORDS TO PROCESS. IT IS PASSED BY VALUE
 ; .. IF GARCNT IS NULL, ALL RECORDS ARE PROCESSED
 ; GARSTRT IS THE IEN OF THE FIRST RECORD TO PROCESS. IT IS PASSED BY VALUE
 ; .. IF GARSTART IS NULL, PROCESSING STARTS AT THE FIRST RECORD
 ; GANN= NOT NULL - IF GANN IS "ALL" THEN EVEN NULL FIELDS WILL BE RETURNED
 ; OTHERWISE, ONLY POPULATED FIELDS ARE RETURNED IN GARFLD AND GARVAL
 N GATMP,GAI,GAF
 S GAF=^DIC(GAFILE,0,"GL") ;OPEN ROOT TO FILE
 S GAF=$P(GAF,",",1)_")" ; CLOSE THE ROOT
 W $O(@GAF@(0)) ;
 S GAI=0 ;ITERATOR
 ; F  S GAI=$O(@GAF@
 Q
 ;
INIT ; INITIALIZE CCR DICTIONARY BASED ON VARIABLE NAMES
 ;
 ; CHEAT SHEET FOR VARIABLE NAMES IN ^C0CDIC(170.xx,
 ; THIS IS WHAT WILL BE IN C0CA FOR EACH DICTIONARY ENTRY
 ;G1("CODING")="170^8"
 ;G1("DATA ELEMENT")="170^7"
 ;G1("DESCRIPTION")="170^3"
 ;G1("ID")="170^1"
 ;G1("M","170^8","CODING")="170.08^.01"
 ;G1("MAPPING METHOD")="170.08^1"
 ;G1("SECTION")="170^10"
 ;G1("SOURCE")="170^4"
 ;G1("STATUS")="170^9"
 ;G1("TYPE")="170^6"
 ;G1("VARIABLE")="170^.01"
 ;G1("XPATH")="170^2"
 ;
 N C0CZA,C0CZX,C0CN,C0CSTAT
 S C0CZX=0
 S C0CSTAT=0 ; INIT STATUS SET FLAG
 F  S C0CZX=$O(^C0CDIC(170,C0CZX)) Q:+C0CZX=0  D  ; FOR EACH DICT ENTRY
 . ;W C0CZX,!
 . K C0CA,C0CN ; CLEAR OUT THE LAST ONE
 . D GET("C0CA",170,C0CZX,"ALL") ; GET VARIABLE HASH
 . ;ZWR C0CA B ;
 . S C0CN=$$ZVALUE("VARIABLE") ;NAME OF THE VARIABLE
 . W "VARIABLE: ",C0CN,!
 . I $E(C0CN,1,5)="ACTOR" D SETFDA("SECTION","ACTORS") ;
 . I $E(C0CN,1,6)="SOCIAL" D  ;
 . . D SETFDA("SECTION","SOC") ;
 . . D SETFDA("STATUS","X") ;SOCIAL HISTORY NOT IMPLEMENTED
 . . S C0CSTAT=1
 . I $E(C0CN,1,6)="FAMILY" D  ;
 . . D SETFDA("SECTION","FAM") ;
 . . D SETFDA("STATUS","X") ;FAMILY HISTORY NOT IMPLEMENTED
 . . S C0CSTAT=1
 . ;D SETFDA("TYPE","") ;CORRECT FOR TYPE ERRORS
 . I $E(C0CN,1,5)="ALERT" D SETFDA("SECTION","ALERTS")
 . I $E(C0CN,1,5)="VITAL" D SETFDA("SECTION","VITALS")
 . I $E(C0CN,1,7)="PROBLEM" D SETFDA("SECTION","PROBLEMS")
 . I $E(C0CN,1,10)="RESULTTEST" D SETFDA("SECTION","TEST")
 . E  I $E(C0CN,1,6)="RESULT" D SETFDA("SECTION","LABS")
 . I C0CN["CODEVALUE" D SETFDA("TYPE","CD") ;CODES
 . I C0CN["CODEVERSION" D SETFDA("TYPE","CV") ; CODE VERSION
 . I C0CN["CODINGSYSTEM" D SETFDA("TYPE","CS") ;CODING SYSTEM
 . I $$ZVALUE("STATUS")=""!'C0CSTAT D SETFDA("STATUS","N") ;BLANK STATUS TO N
 . I $$ZVALUE("XPATH")["/Medication/Directions/" D  ; MEDS DIRECTIONS VAR
 . . D SETFDA("SECTION","DIR") ; SPECIAL SECTION FOR DIRECTIONS
 . E  I $$ZVALUE("XPATH")["/Medications/Medication/" D  ; ALL OTHER MEDS
 . . D SETFDA("SECTION","MEDS") ; A MEDS VAR
 . I $E(C0CN,($L(C0CN)-1),$L(C0CN))="ID" D SETFDA("TYPE","ID") ;CATCH THE IDS
 . I C0CN["DATETIME" D SETFDA("TYPE","DT") ; DATE/TIME VARIABLE
 . W "VARIABLE: ",C0CZX," ",C0CA("VARIABLE"),!
 . ;ZWR C0CFDA
 . I $D(C0CFDA) D  ; WE HAVE CHANGES ON THIS VARIABLE
 . . ;ZWR C0CFDA
 . . D UPDATE^DIE("","C0CFDA(C0CZX)")
 . . I $D(^TMP("DIERR",$J)) U $P BREAK
 . . D CLEAN^DILF ; CLEAN UP
 . ;ZWR C0CFDA
 Q
 ;
SETFDA(C0CSN,C0CSV) ; INTERNAL ROUTINE TO MAKE AN FDA ENTRY FOR FIELD C0CSN
 ; TO SET TO VALUE C0CSV.
 ; C0CFDA,C0CA,C0CZX ARE ASSUMED FROM THE CALLING ROUTINE
 ; C0CSN,C0CSV ARE PASSED BY VALUE
 ;
 N C0CSI,C0CSJ
 S C0CSI=$$ZFILE(C0CSN) ; FILE NUMBER
 S C0CSJ=$$ZFIELD(C0CSN) ; FIELD NUMBER
 S C0CFDA(C0CZX,C0CSI,C0CZX_",",C0CSJ)=C0CSV
 Q
ZFILE(ZFN) ; INTERNAL EXTRINSIC TO RETURN FILE NUMBER FOR FIELD NAME PASSED
 ; BY VALUE IN ZFN. FILE NUMBER IS PIECE 1 OF C0CA(ZFN)
 Q $P(C0CA(ZFN),"^",1)
ZFIELD(ZFN) ;INTERNAL EXTRINSIC TO RETURN FIELD NUMBER FOR FIELD NAME PASSED
 ; BY VALUE IN ZFN. FILE NUMBER IS PIECE 2 OF C0CA(ZFN)
 Q $P(C0CA(ZFN),"^",2)
ZVALUE(ZFN) ;INTERNAL EXTRINSIC TO RETURN VALUE FOR FIELD NAME PASSED
 ; BY VALUE IN ZFN. FILE NUMBER IS PIECE 3 OF C0CA(ZFN)
 Q $P(C0CA(ZFN),"^",3)
 ;