COMPILE SUBROUTINE EXPORT_INTERFACE_DICTS(Dummy)

/*
	Routine to export dictionary information to text file for work with SQL interface
	
	July 2, 2012 - John C. Henry  - J.C. Henry, Inc.
	
*/

DECLARE SUBROUTINE Utility
DECLARE FUNCTION Utility

$INSERT DICT_EQUATES

equ CRLF$       to char(13):char(10)
equ TAB$		TO CHAR(9)

EQU COL_TABLENAME$		TO 1
EQU COL_FIELD_NO$		TO 2
EQU COL_COL_NAME$		TO 3
EQU COL_COL_HEAD$		TO 4
EQU COL_COL_DESC$		TO 5
EQU COL_BTREE$			TO 6
EQU COL_XREF$			TO 7
EQU COL_CASE_SENSITIVE$	TO 8
EQU COL_RELATED_TO$		TO 9
EQU COL_RELATED_FROM$	TO 10

TableNames  = 'WO_LOG':@VM
TableNames := 'WO_MAT':@VM
TableNames := 'REACT_RUN':@VM
TableNames := 'RDS_LAYER':@VM
TableNames := 'RDS_TEST':@VM
TableNames := 'TW_USE':@VM
TableNames := 'COC':@VM
TableNames := 'CLEAN_INSP':@VM
TableNames := 'NCR':@VM
TableNames := 'PROD_VER':@VM
TableNames := 'PROD_SPEC':@VM
TableNames := 'REACT_UTIL':@VM
TableNames := 'REACTOR_LOG':@VM
TableNames := 'REACTOR_PM'

TableCnt = COUNT(TableNames,@VM) + (TableNames NE '')

CurrPath = DRIVE()	;* Returns OI directory so backup one level
CurrPath = FIELD(CurrPath,'\',1,COUNT(CurrPath,'\')):'\ACADTEXT'



DirPath = CurrPath
DirPath = UTILITY('CHOOSEDIR',@WINDOW,'Directory for Acad Text':@FM:CurrPath)

Output = ''

Output  = 'TableName':@VM
Output := 'Field No':@VM
Output := 'Field Name':@VM
Output := 'Heading':@VM
Output := 'Description':@VM
Output := 'Btree':@VM
Output := 'XRef':@VM
Output := 'Case Sense':@VM
Output := 'Related To':@VM
Output := 'Related From':@FM


FOR N = 1 TO TableCnt

	TableName = TableNames<1,N>
	
	FieldData = XLATE('DICT.':TableName,'%FIELDS%','','X')	;* Get the %FIELD% record from the dictionary

	MaxFields = COUNT(FieldData<FIELDS_NAME$>,@VM) + (FieldData<FIELDS_NAME$> NE '')
	PrintLines = ''
	
	TextLines		= ''
	SymTextLines	= ''
	Symbolics		= ''
	SymSort			= ''
	FieldSort		= ''

	LineCnt = 0
	FOR I = 1 TO MaxFields
		
		FieldName = FieldData<FIELDS_NAME$,I>
		IF FieldName[1,1] NE '%' THEN
			DictRec = XLATE('DICT.':TableName,FieldName,'','X')
			
			FMC				= DictRec<DICT_COLUMN_NO$>
			Part			= DictRec<DICT_PART$>
			Type			= DictRec<DICT_TYPE$>
			Master			= DictRec<DICT_MASTER_FLAG$>
			S_M				= DictRec<DICT_SM$>
			DataType		= DictRec<DICT_GENERIC_TYPE$>
			Head			= DictRec<DICT_DISPLAY$>
			Desc			= DictRec<DICT_DESC$>
			Conv			= DictRec<DICT_CONV$>
			Length			= DictRec<DICT_LENGTH$>
			Justification	= DictRec<DICT_JUST$>
			
			Formula			= DictRec<DICT_FORMULA$>
			SWAP @VM WITH CRLF$ IN Formula
			
			Btree			= DictRec<DICT_INDEX_FLAG$>
			XrefField		= DictRec<DICT_XREF$>
			RelatedTo		= DictRec<DICT_RELATIONAL$>
			RelatedFrom		= DictRec<DICT_RELATED$>
			Protected		= DictRec<DICT_PROTECT$>
			CaseSensitive	= DictRec<DICT_LOWERCASE$>
			
			DataLine = ''
			
			
			IF Btree = 1			THEN DataLine<1,COL_BTREE$> = 'Yes'
			IF XrefField = 1		THEN DataLine<1,COL_XREF$>= 'Yes'
			IF CaseSensitive = 1	THEN DataLine<1,COL_CASE_SENSITIVE$>= 'Yes'
			
			IF RelatedTo NE '' 		THEN DataLine<1,COL_RELATED_TO$>= RelatedTo
			IF RelatedFrom NE ''	THEN DataLine<1,COL_RELATED_FROM$>= RelatedFrom
			
			IF FieldName[-1,'B4'] NE '_ORG' THEN
			
				IF S_M = 'M' THEN
					FieldName := '(s)'
				END
			
				IF Type = 'S' THEN
				
					DataLine<1,COL_TABLENAME$>	= TableName
					DataLine<1,COL_FIELD_NO$> 	= ''
					DataLine<1,COL_COL_NAME$>	= FieldName
					DataLine<1,COL_COL_HEAD$>	= Head
					DataLine<1,COL_COL_DESC$>	= Desc
				
				END
				
				IF Type = 'F' THEN
					
					DataLine<1,COL_TABLENAME$>	= TableName
					DataLine<1,COL_FIELD_NO$> 	= FMC
					DataLine<1,COL_COL_NAME$>	= FieldName
					DataLine<1,COL_COL_HEAD$>	= Head
					DataLine<1,COL_COL_DESC$>	= Desc
					
				END
			
					
				IF FMC NE '' THEN
					
					IF FMC = 0 AND Part NE '' THEN FMC := '.':Part
					LOCATE FMC IN FieldSort BY 'AR' USING @FM SETTING POS THEN
						FieldSort = INSERT(FieldSort,POS,0,0,FMC)
					END ELSE
						FieldSort = INSERT(FieldSort,POS,0,0,FMC)
					END
					
					TextLines = INSERT(TextLines,POS,0,0,DataLine)
					
				END ELSE
					
					LOCATE FieldName IN SymSort USING @FM SETTING POS ELSE
						SymSort = INSERT(SymSort,POS,0,0,FieldName)
					END
					
					SymTextLines = INSERT(SymTextLines,POS,0,0,DataLine)
					
				END ;* End of sorting routine	
			END	;* End of check for "_ORG" 

		END ; * 

	NEXT I
	
	Output := TextLines:@FM:SymTextLines:@FM
	TestLines = ''
	SymTextLines = ''
	
	
NEXT N

SWAP @FM WITH CRLF$ IN Output
CONVERT @VM TO TAB$ IN Output

FileName = 'InterfaceDicts.TXT'
OSWRITE Output ON DirPath:'\':FileName

RETURN
