PK.VITLKEY,
"HDR",@1,
"VITALSET",@4,
"PATS",@3,
"TRLR",@2,
END;

[1]::HDR
"<?xml version="_D(34)_1.0_D(34)_" ?>"^!,
"<ReplyMessage>"^!,
"<TransactionId>0</TransactionId>"^!,
"<Status>Success</Status>"^!,
"<StatusDescription>Step returned a Success status.</StatusDescription>"^!,
"<Payload>"^!,
"<CodeSets>"^!;

[2]::TRLR
"</CodeSets>"^!,
"</Payload>"^!,
"</ReplyMessage>"^!;

[3]::PATS
/ARGV[1]^DT,
2^nr,
DO{nr</ARGC 1,
   /ARGV[nr]^ACT,
   ?(A)AAAI[ACT]^aa,
   "VITALS",@6,
   nr+1^nr};

[4]::VITALSET
""^ggv,
DO{+/CONF[.vitals.queries][ggv]^ggv "got one",
   "VITAL.DEF",@5};

[5]::VITAL.DEF
ggv^/V.QRY[ggv];

[6]::VITALS
$(A)AA[aa]|3^MRN,
^$(N1)NPCW[aa,"I"]^NUR.DATA,
""^URN^SEQ^TD^TM^EDITNUM,
DO{<([NUR.DATA,URN,"O",SEQ,"TD",TD,TM,XXX],YYY)^XXX "got one",
      XXX#"0S"^CTR,
      IF{EDITNUM_.=. CTR^EDITNUM},
      L(EDITNUM,.)'=L(EDITNUM)^HASDOT,
      IF{(HASDOT)&(CTR'=EDITNUM) 1,
            "had edit, doesnt match most recent edit, dont show";
         ('HASDOT)&([NUR.DATA,URN,"O",SEQ,"TD",TD,TM,CTR]_.=.) 1,
            "no edits, but no root record, means deleted, dont show";
         XXX#"1S"="Q" "regular result",
            XXX#"2S"^ggv,
            IF{/V.QRY[ggv]_.=.;
               YYY|0_.=. "no value, dont display";
               "Q"^keyprefix,
               "SEND.DATA",@7};
         XXX#"1S"="R" "repeat result",
            XXX#"4S"^ggv,
            IF{/V.QRY[ggv]_.=.;
               YYY|0_.=. "no value, dont display";
               "R"_(XXX#"2S")^keyprefix,
               "SEND.DATA",@7}};
   ""^EDITNUM,-[NUR.DATA,URN,"O",SEQ,"TD",TD,TM]^TM;
   -[NUR.DATA,URN,"O",SEQ,"TD",TD]^TD'<DT;
   ""^TD,-[NUR.DATA,URN,"O",SEQ]^SEQ;
   -[NUR.DATA,URN]^URN};

[7]::SEND.DATA
"GET.VALUE",@9,
IF{ggv=/CONF[.vitals.bp.query] "blood pressure gets special handling",
   "split bp query into systolic and diastolic",
      ggv^tmpggv,
      val^tmpval,
      tmpggv_":S"^ggv,tmpval$L(tmpval,"/")^val,"SEND.VITAL",@8,
      tmpggv_":D"^ggv,tmpval%L(tmpval,"/")^val,"SEND.VITAL",@8,
      tmpggv^ggv;
   "otherwise, just emit this query",
      "SEND.VITAL",@8};

[8]::SEND.VITAL
"<Vital>"^!,
"<VitalMnemonic>"_aa_URN_SEQ_TD_(TM)_(XXX#"0S")_(keyprefix)_(ggv)_"</VitalMnemonic>"^!,
"<Key>"_(TD)_(TM)_"</Key>"^!,
"<PersonMnemonic>"_(MRN)_"</PersonMnemonic>"^!,
"<EncounterMnemonic>"_(ACT)_"</EncounterMnemonic>"^!,
"<Value><![CDATA["_(val)_"]]></Value>"^!,
"<DateTime>"_(TD)_(TM)_"</DateTime>"^!,
"<EventCodeMnemonic>"_(ggv)_"</EventCodeMnemonic>"^!,
"</Vital>"^!;

[9]::GET.VALUE
YYY|0^val,
&(G)GGV[ggv]|2^fldtype,
IF{(fldtype="G")!(fldtype="O") "special handling",
   &(G)GGV[ggv]|4^ggl,
   &(G)GGL[ggl]E[val]|0^val};

