source: ccr/trunk/p/C0CDOM.m@ 1806

Last change on this file since 1806 was 1586, checked in by Sam Habiel, 12 years ago

Changed license to AGPL. Some clean-up for XINDEX

  • Property svn:mergeinfo set to (toggle deleted branches)
    /ccr/branches/ohum/o-old/p/C0CDOM.m1290
    /ccr/branches/ohum/p/C0CDOM.m1291-1543
    /ccr/branches/ohum/p/p/C0CDOM.m1287-1289
File size: 12.1 KB
Line 
1C0CDOM ; GPL - DOM PROCESSING ROUTINES ;6/6/11 17:05
2 ;;1.2;CCD/CCR GENERATION UTILITIES;;Oct 30, 2012;Build 50
3 ;Copyright 2011 George Lilly.
4 ;
5 ; This program is free software: you can redistribute it and/or modify
6 ; it under the terms of the GNU Affero General Public License as
7 ; published by the Free Software Foundation, either version 3 of the
8 ; License, or (at your option) any later version.
9 ;
10 ; This program is distributed in the hope that it will be useful,
11 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ; GNU Affero General Public License for more details.
14 ;
15 ; You should have received a copy of the GNU Affero General Public License
16 ; along with this program. If not, see <http://www.gnu.org/licenses/>.
17 ;
18 ;
19 Q
20 ;
21DOMO(ZOID,ZPATH,ZNARY,ZXIDX,ZXPARY,ZNUM,ZREDUX) ; RECURSIVE ROUTINE TO POPULATE
22 ; THE XPATH INDEX ZXIDX, PASSED BY NAME
23 ; THE XPATH ARRAY XPARY, PASSED BY NAME
24 ; ZOID IS THE STARTING OID
25 ; ZPATH IS THE STARTING XPATH, USUALLY "/"
26 ; ZNUM IS THE MULTIPLE NUMBER [x], USUALLY NULL WHEN ON THE TOP NODE
27 ; ZREDUX IS THE XPATH REDUCTION STRING, TAKEN OUT OF EACH XPATH IF PRESENT
28 I $G(ZREDUX)="" S ZREDUX=""
29 N NEWPATH,NARY ; NEWPATH IS AN XPATH NARY IS AN NHIN MUMPS ARRAY
30 N NEWNUM S NEWNUM=""
31 I $G(ZNUM)>0 S NEWNUM="["_ZNUM_"]"
32 S NEWPATH=ZPATH_"/"_$$TAG(ZOID)_NEWNUM ; CREATE THE XPATH FOR THIS NODE
33 I $G(ZREDUX)'="" D ; REDUX PROVIDED?
34 . N GT S GT=$P(NEWPATH,ZREDUX,2)
35 . I GT'="" S NEWPATH=GT
36 S @ZXIDX@(NEWPATH)=ZOID ; ADD THE XPATH FOR THIS NODE TO THE XPATH INDEX
37 N GA D ATT("GA",ZOID) ; GET ATTRIBUTES FOR THIS NODE
38 I $D(GA) D ; PROCESS THE ATTRIBUTES
39 . N ZI S ZI=""
40 . F S ZI=$O(GA(ZI)) Q:ZI="" D ; FOR EACH ATTRIBUTE
41 . . N ZP S ZP=NEWPATH_"@"_ZI ; PATH FOR ATTRIBUTE
42 . . S @ZXPARY@(ZP)=GA(ZI) ; ADD THE ATTRIBUTE XPATH TO THE XP ARRAY
43 . . I GA(ZI)'="" D ADDNARY(ZP,GA(ZI)) ; ADD THE NHIN ARRAY VALUE
44 N GD D DATA("GD",ZOID) ; SEE IF THERE IS DATA FOR THIS NODE
45 I $D(GD(2)) D ;
46 . M @ZXPARY@(NEWPATH)=GD ; IF MULITPLE DATA MERGE TO THE ARRAY
47 E I $D(GD(1)) D ;
48 . S @ZXPARY@(NEWPATH)=GD(1) ; IF SINGLE VALUE, ADD TO ARRAY
49 . I GD(1)'="" D ADDNARY(NEWPATH,GD(1)) ; ADD TO NHIN ARRAY
50 N ZFRST S ZFRST=$$FIRST(ZOID) ; SET FIRST CHILD
51 I ZFRST'=0 D ; THERE IS A CHILD
52 . N ZNUM
53 . N ZMULT S ZMULT=$$ISMULT(ZFRST) ; IS FIRST CHILD A MULTIPLE
54 . D DOMO(ZFRST,NEWPATH,ZNARY,ZXIDX,ZXPARY,$S(ZMULT:1,1:""),ZREDUX) ; THE CHILD
55 N GNXT S GNXT=$$NXTSIB(ZOID)
56 I $$TAG(GNXT)'=$$TAG(ZOID) S ZNUM="" ; RESET COUNTING AFTER MULTIPLES
57 I GNXT'=0 D ;
58 . N ZMULT S ZMULT=$$ISMULT(GNXT) ; IS THE SIBLING A MULTIPLE?
59 . I (ZNUM="")&(ZMULT) D ; SIBLING IS FIRST OF MULTIPLES
60 . . N ZNUM S ZNUM=1 ;
61 . . D DOMO(GNXT,ZPATH,ZNARY,ZXIDX,ZXPARY,ZNUM,ZREDUX) ; DO NEXT SIB
62 . E D DOMO(GNXT,ZPATH,ZNARY,ZXIDX,ZXPARY,$S(ZNUM>0:ZNUM+1,1:""),ZREDUX) ; SIB
63 Q
64 ;
65ADDNARY(ZXP,ZVALUE) ; ADD AN NHIN ARRAY VALUE TO ZNARY
66 ;
67 ; IF ZATT=1 THE ARRAY IS ADDED AS ATTRIBUTES
68 ;
69 N ZZI,ZZJ,ZZN
70 S ZZI=$P(ZXP,"/",1) ; FIRST PIECE OF XPATH ARRAY
71 I ZZI="" Q ; DON'T ADD THIS ONE .. PROBABLY THE //results NODE
72 S ZZJ=$P(ZXP,ZZI_"/",2) ; REST OF XPATH ARRAY
73 S ZZJ=$TR(ZZJ,"/",".") ; REPLACE / WITH .
74 I ZZI'["]" D ; A SINGLETON
75 . S ZZN=1
76 E D ; THERE IS AN [x] OCCURANCE
77 . S ZZN=$P($P(ZZI,"[",2),"]",1) ; PULL OUT THE OCCURANCE
78 . S ZZI=$P(ZZI,"[",1) ; TAKE OUT THE [X]
79 I ZZJ'="" D ; TIME TO ADD THE VALUE
80 . S @ZNARY@(ZZI,ZZN,ZZJ)=ZVALUE
81 Q
82 ;
83PARSE(INXML,INDOC) ;CALL THE MXML PARSER ON INXML, PASSED BY NAME
84 ; INDOC IS PASSED AS THE DOCUMENT NAME - DON'T KNOW WHERE TO STORE THIS NOW
85 ; EXTRINSIC WHICH RETURNS THE DOCID ASSIGNED BY MXML
86 ;Q $$EN^MXMLDOM(INXML)
87 Q $$EN^MXMLDOM(INXML,"W")
88 ;
89ISMULT(ZOID) ; RETURN TRUE IF ZOID IS ONE OF A MULTIPLE
90 N ZN
91 ;I $$TAG(ZOID)["entry" B
92 S ZN=$$NXTSIB(ZOID)
93 I ZN'="" Q $$TAG(ZOID)=$$TAG(ZN) ; IF TAG IS THE SAME AS NEXT SIB TAG
94 Q 0
95 ;
96FIRST(ZOID) ;RETURNS THE OID OF THE FIRST CHILD OF ZOID
97 Q $$CHILD^MXMLDOM(C0CDOCID,ZOID)
98 ;
99PARENT(ZOID) ;RETURNS THE OID OF THE PARENT OF ZOID
100 Q $$PARENT^MXMLDOM(C0CDOCID,ZOID)
101 ;
102ATT(RTN,NODE) ;GET ATTRIBUTES FOR ZOID
103 S HANDLE=C0CDOCID
104 K @RTN
105 D GETTXT^MXMLDOM("A")
106 Q
107 ;
108TAG(ZOID) ; RETURNS THE XML TAG FOR THE NODE
109 ;I ZOID=149 B ;GPLTEST
110 N X,Y
111 S Y=""
112 S X=$G(C0CCBK("TAG")) ;IS THERE A CALLBACK FOR THIS ROUTINE
113 I X'="" X X ; EXECUTE THE CALLBACK, SHOULD SET Y
114 I Y="" S Y=$$NAME^MXMLDOM(C0CDOCID,ZOID)
115 Q Y
116 ;
117NXTSIB(ZOID) ; RETURNS THE NEXT SIBLING
118 Q $$SIBLING^MXMLDOM(C0CDOCID,ZOID)
119 ;
120DATA(ZT,ZOID) ; RETURNS DATA FOR THE NODE
121 ;N ZT,ZN S ZT=""
122 ;S C0CDOM=$NA(^TMP("MXMLDOM",$J,C0CDOCID))
123 ;Q $G(@C0CDOM@(ZOID,"T",1))
124 S ZN=$$TEXT^MXMLDOM(C0CDOCID,ZOID,ZT)
125 Q
126 ;
127OUTXML(ZRTN,INID,NO1ST) ; USES C0CMXMLB (MXMLBLD) TO OUTPUT XML FROM AN MXMLDOM
128 ;
129 S C0CDOCID=INID
130 I '$D(NO1ST) S NO1ST=0 ; DO NOT SURPRESS THE <?xml tag generation
131 D START^C0CMXMLB($$TAG(1),,"G",NO1ST)
132 D NDOUT($$FIRST(1))
133 D END^C0CMXMLB ;END THE DOCUMENT
134 M @ZRTN=^TMP("MXMLBLD",$J)
135 K ^TMP("MXMLBLD",$J)
136 Q
137 ;
138NDOUT(ZOID) ;CALLBACK ROUTINE - IT IS RECURSIVE
139 N ZI S ZI=$$FIRST(ZOID)
140 I ZI'=0 D ; THERE IS A CHILD
141 . N ZATT D ATT("ZATT",ZOID) ; THESE ARE THE ATTRIBUTES MOVED TO ZATT
142 . D MULTI^C0CMXMLB("",$$TAG(ZOID),.ZATT,"NDOUT^C0CMXML(ZI)") ;HAVE CHILDREN
143 E D ; NO CHILD - IF NO CHILDREN, A NODE HAS DATA, IS AN ENDPOINT
144 . ;W "DOING",ZOID,!
145 . N ZD D DATA("ZD",ZOID) ;NODES WITHOUT CHILDREN HAVE DATA
146 . N ZATT D ATT("ZATT",ZOID) ;ATTRIBUTES
147 . D ITEM^C0CMXMLB("",$$TAG(ZOID),.ZATT,$G(ZD(1))) ;NO CHILDREN
148 I $$NXTSIB(ZOID)'=0 D ; THERE IS A SIBLING
149 . D NDOUT($$NXTSIB(ZOID)) ;RECURSE FOR SIBLINGS
150 Q
151 ;
152WNHIN(ZDFN) ; WRITES THE XML OUTPUT OF GET^NHINV TO AN XML FILE
153 ;
154 N GN,GN2
155 D GET^NHINV(.GN,ZDFN) ; EXTRACT THE XML
156 S GN2=$NA(@GN@(1))
157 W $$OUTPUT^C0CXPATH(GN2,"nhin_"_ZDFN_".xml","/home/wvehr3-09/")
158 Q
159 ;
160NARY2XML(ZGOUT,ZGIN) ; CREATE XML FROM AN NHIN ARRAY
161 ; ZGOUT AND ZGIN ARE PASSED BY NAME
162 N C0CDOCID
163 W !,ZGOUT," ",ZGIN
164 S C0CDOCID=$$DOMI(ZGIN) ; PUT IT INTO THE DOM
165 D OUTXML(ZGOUT,C0CDOCID)
166 Q
167 ;
168 ; EXAMPLE OF NHIN ARRAY FORMAT - THIS IS AN OUTPUT OF DOMO ABOVE WHEN RUN
169 ; AGAINST THE OUTPUT OF THE GET^NHINV ROUTINE. (THIS IS NOT REAL PATIENT DATA)
170 ;
171 ;GNARY("med",1,"doses.dose@dose")=10
172 ;GNARY("med",1,"doses.dose@noun")="TABLET"
173 ;GNARY("med",1,"doses.dose@route")="PO"
174 ;GNARY("med",1,"doses.dose@schedule")="QD"
175 ;GNARY("med",1,"doses.dose@units")="MG"
176 ;GNARY("med",1,"doses.dose@unitsPerDose")=1
177 ;GNARY("med",1,"facility@code")=100
178 ;GNARY("med",1,"facility@name")="VOE OFFICE INSTITUTION"
179 ;GNARY("med",1,"form@value")="TAB"
180 ;GNARY("med",1,"id@value")="1N;O"
181 ;GNARY("med",1,"location@code")=5
182 ;GNARY("med",1,"location@name")="3 WEST"
183 ;GNARY("med",1,"name@value")="LISINOPRIL TAB"
184 ;GNARY("med",1,"orderID@value")=294
185 ;GNARY("med",1,"ordered@value")=3110531.001233
186 ;GNARY("med",1,"orderingProvider@code")=63
187 ;GNARY("med",1,"orderingProvider@name")="KING,MATTHEW MICHAEL"
188 ;GNARY("med",1,"products.product.class@code")="ACE INHIBITORS"
189 ;GNARY("med",1,"products.product.vaGeneric@code")=1990
190 ;GNARY("med",1,"products.product.vaGeneric@name")="LISINOPRIL"
191 ;GNARY("med",1,"products.product.vaGeneric@vuid")=4019380
192 ;GNARY("med",1,"products.product.vaProduct@code")=8118
193 ;GNARY("med",1,"products.product.vaProduct@name")="LISINOPRIL 10MG TAB"
194 ;GNARY("med",1,"products.product.vaProduct@vuid")=4008593
195 ;GNARY("med",1,"products.product@code")=6174
196 ;GNARY("med",1,"products.product@name")="LISINOPRIL 10MG U/D"
197 ;GNARY("med",1,"products.product@role")="D"
198 ;GNARY("med",1,"sig")="10MG BY MOUTH EVERY DAY"
199 ;GNARY("med",1,"sig@xml:space")="preserve"
200 ;GNARY("med",1,"status@value")="active"
201 ;GNARY("med",1,"type@value")="OTC"
202 ;GNARY("med",1,"vaType@value")="N"
203 ;
204 ; DOMI is an extrinsic to insert NHIN ARRAY FORMAT arrays into the DOM
205 ; it returns 0 or 1 based on success.
206 ;
207 ; INARY is passed by name and has the format shown above
208 ; HANDLE is the document number in the DOM (both MXML and EWD DOMs will
209 ; be supported eventually - initial implementation is for MXML
210 ;
211 ; PARENT is the node id or tag of the parent under which the DOM will
212 ; be populated. If it is numeric, it is a node. If it is a string, the DOM
213 ; will be searched to find the tag. If not found and there is no root,
214 ; it will be inserted as the root. If not found and there is a root, it
215 ; will be inserted under the root.
216 ;
217 ; For the above example the call would be OK=$$DOMI("GNARY",0,"results")
218 ; because "results" is the root tag. Use OUTXML to render the xml from
219 ; the DOM.
220 ;
221DOMI(INARY,HANDLE,PARENT) ; EXTRINSIC TO INSERT NHIN ARRAYS TO A DOM
222 ;
223 N ZPARNODE
224 S (SUCCESS,LEVEL,LEVEL(0),NODE)=0
225 I '$D(INARY) Q 0 ; NO ARRAY PASSED
226 I '$D(HANDLE) S HANDLE=$$NEWDOM() ; MAKE A NEW DOM
227 ;I PARENT="" S PARENT="root"
228 I +$G(PARENT)>0 S ZPARNODE=PARENT ; WE HAVE BEEN PASSED A PARENT NODE ID
229 E I $L($G(PARENT))>0 D ; TBD FIND THE PARENT IN THE DOM AND SET LEVEL
230 . D STARTELE^MXMLDOM(PARENT) ; INSERT THE PARENT NODE
231 . S ZPARNODE=1 ;
232 ; WE NOW HAVE A HANDLE AND A PARENT NODE AND LEVEL HAS BEEN SET
233 N ZEXARY
234 D EXPAND("ZEXARY",INARY) ; EXPAND THE NHIN ARRAY
235 D MAJOR("ZEXARY") ; PROCESS ALL THE NODES TO BE ADDED
236 I $L($G(PARENT))>0 D ENDELE^MXMLDOM(PARENT) ; CLOSE OUT THE PARENT NODE
237 Q HANDLE ; SUCCESS
238 ;
239MAJOR(ZARY) ; RECURSIVE ROUTINE FOR INTERMEDIATE NODES
240 N ZI S ZI=""
241 N ZTAG
242 F S ZI=$O(@ZARY@(ZI)) Q:ZI="" D ; FOR EACH SECTION
243 . N ZELEADD S ZELEADD=0
244 . I ZI["@" D ; END NODE HAS NO VALUE, ONLY ATTRIBUTES
245 . . S ZTAG=$P(ZI,"@",1) ; PULL OUT THE TAG
246 . . K ZATT ; CLEAR OUT LAST ONE
247 . . M ZATT=@ZARY@(ZI,1) ; GET ATTRIBUTE ARRAY
248 . . D STARTELE^MXMLDOM(ZTAG,.ZATT) ; ADD THE NODE
249 . . S ZELEADD=1 ; FLAG TO NOT ADD THE ELEMENT TWICE
250 . I $O(@ZARY@(ZI,""))="" D ;END NODE
251 . . S ZTAG=ZI ; USE ZI FOR THE TAG
252 . . I 'ZELEADD D STARTELE^MXMLDOM(ZTAG) ; ADD ELEMENT IF NOT THERE
253 . . S ZELEADD=1 ; ADDED AN ELEMENT
254 . . D CHAR^MXMLDOM($G(@ZARY@(ZI))) ; INSERT THE VALUE
255 . I ZELEADD D Q ; NO MORE TO DO ON THIS LEVEL
256 . . D ENDELE^MXMLDOM(ZTAG) ; CLOSE THE ELEMENT BEFORE LEAVING
257 . N NEWARY ; INDENTED ARRAY
258 . N ZN S ZN=0
259 . F S ZN=$O(@ZARY@(ZI,ZN)) Q:ZN="" D ; FOR EACH MULTIPLE
260 . . D STARTELE^MXMLDOM(ZI) ; ADD THE INTERMEDIATE TAG
261 . . S NEWARY=$NA(@ZARY@(ZI,ZN)) ; INDENT THE ARRAY
262 . . D MAJOR(NEWARY) ; RECURSE FOR INDENTED ARRAY
263 . . D ENDELE^MXMLDOM(ZI) ; END THE INTERMEDIATE TAG
264 Q
265 ;
266EXPAND(ZZOUT,ZZIN) ; EXPANDS NHIN ARRAY FORMAT TO AN EXPANDED
267 ; CONSISTENT FORMAT
268 ; GNARY("patient",1,"facilities[2].facility@code")="050"
269 ; becomes G2ARY("patient",1,"facilities",2,"facility@",1,"code")="050"
270 ; for easier processing (this is fileman format genius)
271 ; basically removes the dot notation from the strings
272 ;
273 N ZZI
274 S ZZI=""
275 F S ZZI=$O(@ZZIN@(ZZI)) Q:ZZI="" D ;
276 . N ZZN S ZZN=0
277 . F S ZZN=$O(@ZZIN@(ZZI,ZZN)) Q:ZZN="" D ;
278 . . N ZZS S ZZS=""
279 . . N GA ;PUSH STACK
280 . . F S ZZS=$O(@ZZIN@(ZZI,ZZN,ZZS)) Q:ZZS="" D ;
281 . . . K GA ; NEW STACK
282 . . . D PUSH^C0CXPATH("GA",ZZI_"^"_ZZN) ; PUSH PARENT
283 . . . N ZZV ; PLACE TO STASH THE VALUE
284 . . . S ZZV=@ZZIN@(ZZI,ZZN,ZZS) ; VALUE
285 . . . W !,"VALUE:",ZZV
286 . . . N GK ; COUNTER
287 . . . F GK=1:1:$L(ZZS,".") D ; FOR EACH INTERMEDIATE NODE
288 . . . . N ZZN2 S ZZN2=1 ; DEFAULT IF NO [X]
289 . . . . N GM S GM=$P(ZZS,".",GK) ; TAG
290 . . . . I GM["[" D ; IT'S A MULTIPLE
291 . . . . . S ZZN2=$P($P(GM,"[",2),"]",1) ; PULL OUT THE NUMBER
292 . . . . . S GM=$P(GM,"[",1) ; PULL OUT THE TAG
293 . . . . I GM["@" D ; IT'S GOT ATTRIBUTES
294 . . . . . N GM2 S GM2=$P(GM,"@",2) ; PULLOUT THE ATTRIBUTE NAME
295 . . . . . D PUSH^C0CXPATH("GA",$P(GM,"@",1)_"@"_"^"_ZZN2) ; PUSH THE TAG
296 . . . . . D PUSH^C0CXPATH("GA",GM2_"^"_ZZN2)
297 . . . . E D PUSH^C0CXPATH("GA",GM_"^"_ZZN2) ;
298 . . . S GA(GA(0))=$P(GA(GA(0)),"^",1)_"^" ; GET RID OF THE LAST "1"
299 . . . N GZI S GZI="" ; STRING FOR THE INDEX
300 . . . F GK=1:1:GA(0) D ; TIME TO REVERSE POP THE TAGS
301 . . . . S GM=$P(GA(GK),"^",1) ; THE TAG
302 . . . . S ZZN2=$P(GA(GK),"^",2) ; THE NUMBER IF ANY
303 . . . . I ZZN2="" S GZI=GZI_""""_GM_"""" ; FOR THE LAST ONE
304 . . . . E S GZI=GZI_""""_GM_""""_","_ZZN2_"," ; FOR THE REST
305 . . . S GZI2=ZZOUT_"("_GZI_")" ; INCLUDE THE ARRAY NAME
306 . . . W !,GZI
307 . . . S @GZI2=ZZV ; REMEMBER THE VALUE?
308 Q
309 ;
310NEWDOM() ; extrinsic which creates a new DOM and returns the HANDLE
311 N CBK,SUCCESS,LEVEL,NODE,HANDLE
312 K ^TMP("MXMLERR",$J)
313 L +^TMP("MXMLDOM",$J):5
314 E Q 0
315 S HANDLE=$O(^TMP("MXMLDOM",$J,""),-1)+1,^(HANDLE)=""
316 L -^TMP("MXMLDOM",$J)
317 Q HANDLE
318 ;
Note: See TracBrowser for help on using the repository browser.