source: smart/trunk/p/C0SDOM.m

Last change on this file was 1591, checked in by Sam Habiel, 12 years ago

Updated license for routines

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