LEXSRC ; ISL/KER - Classification Code Source ; 02/02/2006
 ;;2.0;LEXICON UTILITY;**7,25,26,38**;Sep 23, 1996
 ;
 ; External References
 ;   None
 ;                     
ONE(LEXI,LEXS,LEXVDT) ; Return a single primary code of a source
 S LEXI=+($G(LEXI)),LEXS=$G(LEXS) S LEXI=$$CODE(LEXI,LEXS,$G(LEXVDT)) Q LEXI
ALL(LEXI,LEXS,LEXVDT) ; Return all codes of a source
 S LEXI=+($G(LEXI)),LEXS=$G(LEXS)
 D CODES(LEXI,LEXS,$G(LEXVDT))
 Q
CODE(LEXI,LEXS,LEXVDT) ; Return a single primary code
 N LEXSRC D CODES(LEXI,LEXS,$G(LEXVDT)) S LEXI=$G(LEXSRC(1)) Q LEXI
CODES(LEXI,LEXS,LEXVDT) ; Build an array LEXSRC of codes
 S LEXI=+($G(LEXI)) Q:LEXI=0  Q:'$D(^LEX(757.01,LEXI))
 S LEXS=$G(LEXS) Q:'$D(^LEX(757.03,"ASAB",LEXS))
 N LEXMC S LEXMC=+($G(^LEX(757.01,LEXI,1))) Q:'$D(^LEX(757,LEXMC,0))
 N LEXMCE S LEXMCE=+($G(^LEX(757,LEXMC,0))) Q:'$D(^LEX(757.01,LEXMCE,0))
 N LEXUNI,LEXSA,LEXN,LEXSAB,LEXSTA,LEXPRI,LEXNOM,LEXCC,LEXX S LEXSA=0
 F  S LEXSA=$O(^LEX(757.02,"AMC",LEXMC,LEXSA)) Q:+LEXSA=0  D
 . S LEXN=$G(^LEX(757.02,LEXSA,0)) N LEXLD,LEXLS
 . S LEXCC=$P(LEXN,"^",2) Q:LEXCC=""
 . S LEXSTA=+($$STATCHK^LEXSRC2(LEXCC,$G(LEXVDT))) Q:+LEXSTA'>0
 . S LEXSAB=+($P(LEXN,"^",3)),LEXSAB=$E($G(^LEX(757.03,LEXSAB,0)),1,3) Q:LEXSAB'=LEXS
 . S LEXPRI=+($P(LEXN,"^",7)),LEXCC=$P(LEXN,"^",2) Q:LEXCC=""
 . D:LEXPRI>0 PRI(LEXCC) D:LEXPRI=0 NOM(LEXCC)
 D COMP
 Q
PRI(LEXX) ; Primary Code
 N LEXCC S LEXCC=$G(LEXX) Q:LEXCC=""  S LEXX=+($G(LEXPRI(0))),LEXX=LEXX+1
 S LEXPRI(LEXX)=LEXCC,LEXPRI(0)=LEXX Q
NOM(LEXX) ; Normal Code
 N LEXCC S LEXCC=$G(LEXX) Q:LEXCC=""  S LEXX=+($G(LEXNOM(0))),LEXX=LEXX+1
 S LEXNOM(LEXX)=LEXCC,LEXNOM(0)=LEXX Q
COMP ; Compile array from Primary and Normal Codes
 N LEXUNI,LEXCT,LEXNT S (LEXCT,LEXNT)=0
 I $L($G(LEXPRI(1))) D
 . S LEXCT=LEXCT+1,LEXSRC(LEXCT)=LEXPRI(1)
 . S LEXSRC(0)=LEXCT,LEXUNI(LEXPRI(1))=""
 F  S LEXNT=$O(LEXNOM(LEXNT)) Q:+LEXNT=0  D
 . Q:$D(LEXUNI(LEXNOM(LEXNT)))
 . I $L($G(LEXNOM(LEXNT))) D
 . . S LEXCT=LEXCT+1,LEXSRC(LEXCT)=LEXNOM(LEXNT),LEXSRC(0)=LEXCT,LEXUNI(LEXNOM(LEXNT))=""
 K LEXPRI,LEXNOM,LEXUNI Q
