BMXADOVJ	; IHS/CIHA/GIS - RPC CALL: GENERATE DATA FOR AN ADO DATASET ;
	;;2.1;BMX;;Jul 26, 2009
	; THIS ROUTINE MANAGES THE JOINS
	; 
	; 
	; 
	; THE FIFTH PARAMETER OF SS^BMXADO CONTAINS THE JOIN INSTRUCTIONS
	;  SYNTAX: DESCENDANT SCHEMA IEN (DETAILS FILE), JOIN FIELD FROM MASTER FILE
	;   THE FIRST "," PIECE STATES THAT THE MASTER FILE IS JOINED BY ITS .02 FIELD TO THE DETAILS FILE
	;   THE SECOND "," PIECE STARTES THAT THE DETAILS FILE IS DEFINED BY SCHEMA #6
	;   AN OPTIONAL 3RD "," PIECE MAY CONTAIN A SECONDARY VSTG TO MORE PRECISELY DEFINE JOIN ITERATION 
	;    E.G., "...~6.,.02,AA~1/1/2004~2/1/2004~~~~~|WT|R"
	;    IN THIS CASE, THE SECONDARY VSTG SPECIFIES THAT THE AA INDEX BE USED TO CONTROL THE ITERATOR
	;    THE START AND STOP DATES ARE IN EFFECT BUT MAX IS IGNORED/IRRELEVANT
	;    THE 1ST "|" PIECE OF THE PARAM SECTION WILL BE AUTOMATICALLY STUFFED WITH PATEINT DFN(S) DURING ITERATION
	;  IF MULTIPLE JOINS ARE REQUESTED, THEY ARE SPARATED BY THE '@JOIN@' DELIMTER
	;   "E.G., 6,.02@JOIN@1,.03@JOIN@2,.02@JOIN@9,SUB"
	;   IN THIS EXAPLE THE MASTER FILE IS JOIND TO THE DETAILS FILES ASSOCIATED WITH SCHEMAS 6, 1, AND 9
	;   NOTE THAT THE 3RD JOIN DEINED IN THE STRING SPECIFIES A SUBFILE REALTION RATHER THAN A "POINTER" RELATION
	; IF A SECOND RECORD SET IS CREATED TO FULFILL A JOIN REQUEST, IT WILL ONLY CONTAIN THE ROWS NECESSARY TO  COMPLETE THE JOIN
	;
JOIN(SMASTER,JSTG)	;EP - APPEND ADDITIONAL ANRS TO FULFILL JOIN REQUESTS
	N TMP,JOIN,JINST,FMASTER
	I '$L($G(JSTG)) Q  ; JOIN STRING MUST NOT BE NULL
	S FMASTER=$P($G(^BMXADO(+$G(SMASTER),0)),U,2) I 'FMASTER Q  ; MASTER SCHEMA & FILE MUST EXIST
	S TMP=$NA(^TMP("BMX JOIN",$J)) K @TMP ; JOIN INFO TEMP STORAGE ARRAY
	S @TMP@(0,SMASTER)=$$RANGE ; GET DATA NODE RANGE FOR THE MASTER ANR
	I '$D(@TMP@(0)) Q  ; DATA MUST EXIST IN THE MASTER FILE OR QUIT
	F JOIN=1:1 S JINST=$P(JSTG,"@JOIN@",JOIN) Q:JINST=""  D J(SMASTER,JINST) ; MAIN LOOP FOR DOING JOINS
	K @TMP
	Q
	;
RANGE()	; GET DATA NODE RANGE FOR LAST SCHEMA ENTERED
	N X,FIRST,LAST,Y
	S (X,LAST)=$O(@OUT@(999999999),-1)
	F  S X=$O(@OUT@(X),-1) Q:'X  S Y=@OUT@(X) Q:Y'[$C(30)  S FIRST=X
	I '$G(FIRST) Q ""
	S FIRST=FIRST+1
	Q (FIRST_U_LAST)
	; 
J(SMASTER,JSTG)	; JOIN DETAILS FILE TO MASTER FILE
	; SMASTER=MASTER SCHMA IEN, SDETAIL=DETAILS SCHEMA IEN
	N JARR,SEC,ERR,JIEN,SUB,IX,PARENT,JFLD,DFLD,NODE,X,STOP,VSTG2,SDETAIL,JFLD
	S SDETAIL=$P(JSTG,C),JFLD=$P(JSTG,C,2),DFLD=$P(JSTG,C,3),VSTG2=$P(JSTG,C,4,999)
	I JFLD="SUB" S JFLD=.001,DFLD=.0001
	I JFLD=.001,DFLD=.0001 S SUB=1,VSTG2="~~~~~SIT~BMXADOVJ~" ; MAKE SUBFILE ITERATOR VSTG
	D IEN(SMASTER,SDETAIL,JFLD) ; GET A LIST OF JOIN IENS FROM THE MASTER FILE
	I '$D(@TMP@(1)) Q  ; NO MASTER FILE IENS FOR JOINS, SO QUIT
	N FIEN,DAS,SIEN,VSTG,JSTG
	S DAS="",SIEN=SDETAIL,VSTG=VSTG2
	S FIEN=$P($G(^BMXADO(SIEN,0)),U,2) I 'FIEN Q
	D JEP^BMXADO ; BUILD THE JOIN ANR
	Q
	;
IEN(SMASTER,SDETAIL,JFLD)	; GET THE MASTER FILE IENS FOR BUILDING THE JOIN DATA SET
	N FIEN,%,FIRST,LAST,NODE,DA,IEN
	I JFLD["IEN" S JFLD=+JFLD
	S FIEN=$P($G(^BMXADO(SMASTER,0)),U,2) I 'FIEN Q
	S %=$G(@TMP@(0,SMASTER)) I '$L(%) Q
	S FIRST=+%,LAST=$P(%,U,2),NODE=FIRST-.1
	F  S NODE=$O(@OUT@(NODE))  Q:'NODE  Q:NODE>LAST  D
	. S DA=+@OUT@(NODE)
	. I 'DA Q
	. I JFLD=.001 S @TMP@(1,SDETAIL,DA)="" Q
	. S IEN=$$GET1^DIQ(FIEN,(DA_C),JFLD,"I") I 'IEN Q
	. S @TMP@(1,SDETAIL,IEN)=""
	. Q
	Q
	;
JFLD	; EP-STUFF JOIN FIELD IDS INTO THE INTRO SEGMENT OF THE SCHEMA
	N NODE,%
	S NODE=999999999999
	F  S NODE=$O(@OUT@(NODE),-1) Q:'NODE  I ^(NODE)["@@@meta@@@" Q
	I 'NODE Q
	S %=$P(@OUT@(NODE),U),$P(%,"|",4)=$G(JFLD),$P(%,"|",5)=$G(DFLD)
	S @OUT@(NODE)=%_U
	Q
	; 
