XBCDIC3	; IHS/ADC/GTH - CHECK ^DD ; [ 02/07/97   3:02 PM ]
	;;4.0;XB;;Jul 20, 2009;Build 2
	;
	; Part of XBCDIC
	;
START	;
	W !!,"Now checking ^DD entries."
	S U="^",XBCDFILE=""
	F XBCDL=0:0 S XBCDFILE=$O(^UTILITY("XBDSET",$J,XBCDFILE)) Q:XBCDFILE=""  W !?5,"Checking ",XBCDFILE D XBCDDDC
	KILL XBCDANS,XBCDFILE,XBCDL
	Q
	;
XBCDDDC	; CHECK ^DD ENTRY
	D CHKDD0 ; CHECK ^DD 0TH NODE
	D CHKPT ; CHECK "PT" NODE
	D CHKTRB ; CHECK "TRB" NODE
	D CHKACOMP ; CHECK "ACOMP" NODE
	D SBTRACE ; CHECK "SB" NODE
	Q
	;
CHKDD0	; CHECK 0TH NODE
	I '($D(^DD(XBCDFILE,.01,0))#2) W !,"File ",XBCDFILE," has no ^DD(",XBCDFILE,",.01,0) entry."
	I '$D(^DD(XBCDFILE,0,"NM")) W !,"File ",XBCDFILE," has no ^DD(",XBCDFILE,",0,""NM"") entry."
	E  S XBCDX=$O(^DD(XBCDFILE,0,"NM","")),XBCDX=$O(^(XBCDX)) I XBCDX]"" W !,"File ",XBCDFILE," has multiple names."
	Q
	;
CHKPT	; CHECK "PT" NODE
	S XBCDPFLE=""
	F XBCDL=0:0 S XBCDPFLE=$O(^DD(XBCDFILE,0,"PT",XBCDPFLE)) Q:XBCDPFLE=""  S XBCDPFLD="" F XBCDL=0:0 S XBCDPFLD=$O(^DD(XBCDFILE,0,"PT",XBCDPFLE,XBCDPFLD)) Q:XBCDPFLD=""  D PT
	KILL XBCDPFLE,XBCDPFLD,XBCDX
	Q
PT	;
	W "."
	I '$D(^DD(XBCDPFLE)) W "|" KILL ^DD(XBCDFILE,0,"PT",XBCDPFLE) Q
	I '$D(^DD(XBCDPFLE,XBCDPFLD)) W "|" KILL ^DD(XBCDFILE,0,"PT",XBCDPFLE,XBCDPFLD) Q
	S XBCDX=$P(^DD(XBCDPFLE,XBCDPFLD,0),U,2)
	I XBCDX["V",$D(^DD(XBCDPFLE,XBCDPFLD,"V","B",XBCDFILE)) Q
	I XBCDX["P",XBCDX[XBCDFILE Q
	W "|" KILL ^DD(XBCDFILE,0,"PT",XBCDPFLE,XBCDPFLD)
	Q
	;
CHKTRB	; CHECK "TRB" NODE
	Q:'$D(^DD(XBCDFILE,"TRB"))
	S XBCDTFLE=""
	F XBCDL=0:0 S XBCDTFLE=$O(^DD(XBCDFILE,"TRB",XBCDTFLE)) Q:XBCDTFLE=""  S XBCDTFLD="" F XBCDL=0:0 S XBCDTFLD=$O(^DD(XBCDFILE,"TRB",XBCDTFLE,XBCDTFLD)) Q:XBCDTFLD=""  D TRB
	KILL XBCDTFLE,XBCDTFLD,XBCDX
	Q
	;
TRB	; THIS CAN CHECK MORE THAN IT DOES ***
	W "."
	I '$D(^DD(XBCDTFLE)) W "|" KILL ^DD(XBCDFILE,"TRB",XBCDTFLE) Q
	I '$D(^DD(XBCDTFLE,XBCDTFLD)) W "|" KILL ^DD(XBCDFILE,"TRB",XBCDTFLE,XBCDTFLD) Q
	Q
	;
CHKACOMP	; CHECK "ACOMP" ENTRIES
	Q:'$D(^DD("ACOMP",XBCDFILE))
	S XBCDFLD=""
	F XBCDL=0:0 S XBCDFLD=$O(^DD("ACOMP",XBCDFILE,XBCDFLD)) Q:XBCDFLD'=+XBCDFLD  D CHKFIELD
	KILL XBCDFLD
	Q
	;
CHKFIELD	;
	S XBCDAFLE=""
	F XBCDL=0:0 S XBCDAFLE=$O(^DD("ACOMP",XBCDFILE,XBCDFLD,XBCDAFLE)) Q:XBCDAFLE=""  S XBCDAFLD="" F XBCDL=0:0 S XBCDAFLD=$O(^DD("ACOMP",XBCDFILE,XBCDFLD,XBCDAFLE,XBCDAFLD)) Q:XBCDAFLD=""  D ACOMP
	KILL XBCDAFLE,XBCDAFLD,XBCDX
	Q
	;
ACOMP	;
	W "."
	I '$D(^DD(XBCDAFLE)) W "|" KILL ^DD("ACOMP",XBCDFILE,XBCDFLD,XBCDAFLE) Q
	I '$D(^DD(XBCDAFLE,XBCDAFLD)) W "|" KILL ^DD("ACOMP",XBCDFILE,XBCDFLD,XBCDAFLE,XBCDAFLD) Q
	I '($D(^DD(XBCDAFLE,XBCDAFLD,0))#2) W "|" KILL ^DD("ACOMP",XBCDFILE,XBCDFLD,XBCDAFLE,XBCDAFLD) Q
	S XBCDX=$P(^DD(XBCDAFLE,XBCDAFLD,0),U,2)
	I XBCDX'["C" W "|" KILL ^DD("ACOMP",XBCDFILE,XBCDFLD,XBCDAFLE,XBCDAFLD)
	Q
	;
SBTRACE	; CHECK ALL SUB-FILES
	KILL XBCDSFL
	S XBCDC=1,XBCDSFL="",XBCDSFL(XBCDC)=XBCDFILE
	F XBCDL=0:0 S XBCDI=$O(XBCDSFL("")) Q:XBCDI=""  S XBCDSF=XBCDSFL(XBCDI) D SBTRACE2 S XBCDI=$O(XBCDSFL("")) W "." KILL XBCDSFL(XBCDI)
	KILL XBCDC,XBCDI,XBCDSF,XBCDSFL,XBCDY,XBCDZ
	Q
	;
SBTRACE2	;
	S XBCDI=0
	F XBCDL=0:0 S XBCDI=$O(^DD(XBCDSF,"SB",XBCDI)) Q:XBCDI=""  W "." S XBCDC=XBCDC+1,XBCDSFL(XBCDC)=XBCDI D SBCHECK
	Q
	;
SBCHECK	;
	I '$D(^DD(XBCDI)) S X=$O(^DD(XBCDSF,"SB",XBCDI,0)),Y=$P(^DD(XBCDSF,X,0),U) D  Q
	. W !?10,"Subfile ",XBCDI," for field ",X," does not exists.",!?12,"Deleting field ",X," from file ",XBCDSF
	. KILL ^DD(XBCDSF,X),^DD(XBCDSF,"SB",XBCDI),^DD(XBCDSF,"B",Y,X)
	. Q
	D SBTRACE3,SBTRACE4
	Q
	;
SBTRACE3	;
	I '$D(^DD(XBCDI,0,"UP")),$D(^DIC(XBCDI)) W !?10,XBCDI," is a primary file.  Deleting ^DD(",XBCDSF,",""SB"",",XBCDI,")" KILL ^DD(XBCDSF,"SB",XBCDI) Q
	I $D(^DD(XBCDI,0,"PT")) W !?10,XBCDI," sub-file has ""PT"" node.  Deleting." KILL ^DD(XBCDI,0,"PT")
	I '$D(^DD(XBCDI,0,"UP")) W !?10,XBCDI," has no ""UP"" node.  Creating ^DD(",XBCDI,",0,""UP"")=",XBCDSF S ^DD(XBCDI,0,"UP")=XBCDSF
	Q:^DD(XBCDI,0,"UP")=XBCDSF
	I ^DD(XBCDI,0,"UP")="" W !?10,XBCDI," ""UP"" node is NULL.  Setting ^DD(",XBCDI,",0,""UP"")=",XBCDSF S ^DD(XBCDI,0,"UP")=XBCDSF Q
	W !?10,XBCDSF," lists ",XBCDI," as a sub-file.  The ""UP"" node in ",!?10+$L(XBCDSF)+1,XBCDI," is ",^DD(XBCDI,0,"UP"),"."
	I $D(^DD(^DD(XBCDI,0,"UP"),"SB",XBCDSF)) W !?12,"The ""SB"" in ",^DD(XBCDI,0,"UP")," agrees.  Fixing." KILL ^DD(XBCDSF,"SB",XBCDI) Q
	E  W !?12,"The ""SB"" in ",^DD(XBCDI,0,"UP")," disagrees.  Fixing." S ^DD(XBCDI,0,"UP")=XBCDSF
	Q
	;
SBTRACE4	;
	I '$D(^DD(XBCDI,0,"NM")) W !?10,"Sub-file ",XBCDI," has no ^DD(",XBCDI,",0,""NM"") entry.  Fixing." D SBTFIX I 1
	E  S XBCDX=$O(^DD(XBCDI,0,"NM","")),XBCDX=$O(^(XBCDX)) I XBCDX]"" W !?10,"Sub-file ",XBCDI," has multiple names.  Fixing." D SBTFIX
	Q
	;
SBTFIX	; FIX "NM"
	KILL ^DD(XBCDI,0,"NM")
	I '$D(^DD(XBCDI,0,"UP")) W !?12,"Can't fix.  No ""UP"" node." Q
	S XBCDX=^DD(XBCDI,0,"UP")
	I XBCDX="" W !?12,"Can't fix.  ""UP"" node is NULL." Q
	I '$D(^DD(XBCDX,"SB",XBCDI)) W !?12,"Can't fix because can't locate parent field." Q
	S XBCDY=$O(^DD(XBCDX,"SB",XBCDI,"")),XBCDZ=$P(^DD(XBCDX,XBCDY,0),U,1)
	S ^DD(XBCDI,0,"NM",XBCDZ)=""
	Q
	;
