<#--
   ******************************************************
   *
   *                       NOTICE
   *
   *  THIS SOFTWARE IS THE PROPERTY OF AND CONTAINS
   *  CONFIDENTIAL INFORMATION OF INFOR, AND SHALL NOT BE
   *  COPIED, USED, NOR DISCLOSED WITHOUT EXPRESS WRITTEN
   *  AUTHORIZATION.  ALTHOUGH PUBLICATION IS NOT
   *  INTENDED, IN THE EVENT OF PUBLICATION, THE
   *  FOLLOWING NOTICE IS APPLICABLE:
   *
   *  Copyright © 2009 Infor. All rights reserved.
   *  The word and design marks set forth herein are
   *  trademarks and/or registered trademarks of Infor
   *  and/or related affiliates and subsidiaries.
   *  All rights reserved. All other trademarks listed
   *  herein are the property of their respective owners.
   *  www.infor.com.
   *
   ******************************************************
  -->

<#-- Check op standard methods en identifiers, doen we niet, iStudio -->

<#list protected.listProtectedComponents() as c>
	<#list c.componentImplementation.attributeImplementation as attributeImplementation>
		<#assign attributeName=common.getAttributeName(attributeImplementation)>
 		<#assign datatype=common.getDatatype(attributeImplementation)>
	<#-- External Attr domains become extern, we do not generate this -->
	    <#assign dimensions=common.getDimensionsForDataType(common.getMandatoryDatatype(attributeImplementation))/>
   		<#if (dimensions?number > 1)>
	domain ${common.getDatatypeName(datatype)} bl.${c.componentName}.${attributeName}(${dimensions})
	boolean bl.${c.componentName}.${attributeName}.set(${dimensions})
    boolean bl.${c.componentName}.${attributeName}.get(${dimensions})    
 		<#else>
	domain ${common.getDatatypeName(datatype)} bl.${c.componentName}.${attributeName}
    boolean bl.${c.componentName}.${attributeName}.set
    boolean bl.${c.componentName}.${attributeName}.get
		</#if><#--if dimensions?number > 1-->
	<#-- Public.Attributes? -->
	</#list><#--list c.componentImplementation.attributeImplementation as attributeImplementation-->
</#list><#--list protected.listProtectedComponents() as c-->
<#list protected.listProtectedComponents() as c>
	<#if public.hasCustomerDataLocation(c.componentImplementation.component.idref)>
	boolean	bl.${c.componentName}.custom.output
	</#if><#--if public.hasCustomerDataLocation(c.componentImplementation.component.idref))-->
</#list><#--list protected.listProtectedComponents() as c-->
	boolean bl.permission.for.setting.readonly.attributes

	<#-- Reset.Table.List, something of the old generator internals -->
<#assign tables=[]>
<#list protected.listProtectedComponents() as c>
	<#list c.componentImplementation.componentImplementationTable as componentImplementationTable>
		<#assign tableName=componentImplementationTable.table.idref.name>
		<#assign found=false>
		<#list tables as t>
			<#if t.tableName==tableName || componentImplementationTable.table.idref.type == "form">
				<#assign found=true>
				<#break>
			</#if>
		</#list>
		<#if !found>
			<#assign tables=[{"tableName":tableName}]+tables>
		<#-- make sure table is unique -->
			<#if componentImplementationTable.table.idref.type != "form">
	table t${tableName}
			</#if>
		<#-- No Reference table = always true-->
			<#if !componentImplementationTable.isReferentialIntegrityTable>
	boolean bl.${tableName}.set
			</#if>
		</#if>
	</#list><#--list c.componentImplementation.componentImplementationTable as componentImplementationTable-->
</#list><#--list protected.listProtectedComponents() as c-->

<#list protected.listProtectedComponents() as c>
	<#list c.componentImplementation.componentImplementationTable as componentImplementationTable>
		<#if componentImplementationTable.table.idref.type == "form">
			<#assign table=componentImplementationTable.table.idref>
			<#list table.column as tc>
				<#assign first=true>
				<#list tc.name?split(".") as tn>
					<#if first>
						<#assign tableName=tn>
					<#else>
						<#assign fieldMame=tn>
					</#if>
					<#assign first=false>
				</#list>
				<#assign found=false>
				<#list tables as t>
					<#if t.tableName==tableName>
						<#assign found=true>
						<#break>
					</#if>
				</#list>
				<#if !found>
					<#-- Check if this is really a table field -->
					<#-- Length of tableName must be 8. -->
					<#-- Note: The 'matches' operator has been disabled, because it
						gives problems sometimes when generating. Note that the old
						check (including 'matches') was not complete: names like
						"aaaaa000" and "abcde123" were considered as valid table
						names.
					-->
					<#if tableName?length != 8>
						<#assign tables=[{"tableName":tableName}]+tables>
						<#assign found=true><#-- Do not insert-->
					</#if>
				</#if>
				<#if !found>
					<#assign tables=[{"tableName":tableName}]+tables>
		<#-- make sure table is unique -->
	table t${tableName}
		<#-- No Reference table = always true-->
				</#if>
			</#list>
		</#if>
	</#list><#--list c.componentImplementation.componentImplementationTable as componentImplementationTable-->
</#list><#--list protected.listProtectedComponents() as c-->

<#list protected.listProtectedComponents() as c>
	boolean bl.${c.componentName}.initialize
</#list><#--list protected.listProtectedComponents() as c-->

	string  bl.object(99)
	string  bl.bodesc(99)
	string  bl.component(99)
	long    bl.dal.nr.messages
	string  bl.method(${common.MethodLen()})

	long    bl.result
	long    bl.before.initialize
	long    bl.method.result
	long	bl.component.result
	long    bl.ret
<#if common.referenceMethodPresent()>
	string 	bl.refKey(40)
	boolean bl.refKey.set
</#if><#--if common.referenceMethodPresent()-->

	xmlNode	bl.RequestArgument
	xmlNode	bl.ResponseArgument
	xmlNode	bl.ResultArgument

	boolean	bl.SortingExpressionSet

	xmlNode	bl.Filter
	xmlNode	bl.FilteredAttributes
	string	bl.FilterExpression(1024)
	boolean	bl.PostFilter

<#if protected.generateFunctionServer()>
	string	bl.afs.err.mess(256)	mb
	string	bl.afs.session(13)
	<#list protected.listProtectedComponents() as c>
		<#if protected.isFunctionServerComponent(c.componentImplementation)>
	string	bl.${c.componentName}.main.session(13)
	string	bl.${c.componentName}.sync.session(13)
		</#if>
	</#list>
</#if>

#include <bic_dam>
#pragma used dll otlbctdll1000

<#if protected.generateFunctionServer()>
#pragma used dll ottstpapihand
</#if><#--if protected.generateFunctionServer()-->

<#if common.referenceMethodPresent()>
#pragma used dll ottdlldb.extend
</#if><#--if common.referenceMethodPresent()-->

function extern void <@names.ST_DLLCode/>.set.xmlArguments(
					const xmlNode i.RequestArgument,
					const xmlNode i.ResponseArgument,
					const xmlNode i.ResultArgument)
{
	bl.RequestArgument  = i.RequestArgument
	bl.ResponseArgument = i.ResponseArgument
	bl.ResultArgument   = i.ResultArgument
}

<#list protected.listProtectedComponents() as c>
function extern 
void <@names.ST_DLLCode/>.${c.componentName}.initialize()
{
	DllUsage
	EndDllUsage

	long	i
	i = 0

	<#list c.componentImplementation.attributeImplementation as attributeImplementation>
		<#assign attributeName=common.getAttributeName(attributeImplementation)>
 	   <#assign aiDatatype=common.getMandatoryDatatype(attributeImplementation)/>
 	   <#assign dimensions=common.getDimensionsForDataType(aiDatatype)/>
    	<#if (dimensions?number > 1)>
	for i = 1 to ${dimensions?number}
		bl.${c.componentName}.${attributeName}.set(i) = false
		bl.${c.componentName}.${attributeName}.get(i) = false
			<#if common.get_BaanDatatype_from_XMLSchemaDatatype(aiDatatype)=="DB.STRING">
		bl.${c.componentName}.${attributeName}(1,i) = ${protected.getBaanDataInitializers(attributeImplementation)} 
			<#else>
		bl.${c.componentName}.${attributeName}(i) = ${protected.getBaanDataInitializers(attributeImplementation)} 
			</#if><#--if common.get_BaanDatatype_from_XMLSchemaDatatype(aiDatatype)=="DB.STRING"-->
	endfor
		<#else>
	bl.${c.componentName}.${attributeName}.set = false
	bl.${c.componentName}.${attributeName}.get = false
	bl.${c.componentName}.${attributeName} = ${protected.getBaanDataInitializers(attributeImplementation)} 
		</#if><#--if dimensions?number > 1-->
	</#list>
<#list protected.listProtectedComponents() as c>
	<#if public.hasCustomerDataLocation(c.componentImplementation.component.idref)>
	bl.${c.componentName}.custom.output = true
	</#if><#--if public.hasCustomerDataLocation(c.componentImplementation.component.idref))-->
</#list><#--list protected.listProtectedComponents() as c-->

	bl.${c.componentName}.initialize = true
	<#list c.componentImplementation.componentImplementationTable as componentImplementationTable>
		<#if !componentImplementationTable.isReferentialIntegrityTable>
	bl.${componentImplementationTable.table.idref.name}.set = false
		</#if><#--if !componentImplementationTable.isReferentialIntegrityTable-->
	</#list><#--list c.componentImplementation.componentImplementationTable as componentImplementationTable-->
	set.permission.for.readonly.attributes(false)

	<#list protected.listProtectedComponents(c.componentName) as sc>
	<@names.ST_DLLCode/>.${sc.componentName}.initialize()
	</#list>
}

</#list><#--list protected.listProtectedComponents() as c-->

function extern boolean <@names.ST_DLLCode/>.is.business.studio.supported()
{
	| Checks if the needed functionality is present.
	static	boolean	initialized
	static	boolean	supported
	long		dll.id
	
	if not initialized then
		if get.tools.tiv() < 1400 then
			| A check is done if DLL tlbctdll1000 has external function
			| tlbct.dll1000.remove.nillable.attributes().
<#if names.applicationBackendRelease == "BaanIV">
<#-- The DLL_SILENT_ERR parameter for load_dll is not available on Baan IV -->
			dll.id = load_dll("otlbctdll1000")
<#else>
			dll.id = load_dll("otlbctdll1000", DLL_SILENT_ERR)
</#if>
			if dll.id = 0 then
				supported = false
			else
				supported = (get_function(dll.id, "tlbct.dll1000.remove.nillable.attributes") > 0)
			endif
		else
			| ES 8.4.2 or later is used
			supported = true
		endif
		initialized = true
	endif
	return(supported)
}

<#assign tableContainer=[]><#-- containing list of processed tables -->
<#list protected.listProtectedComponents() as c>
	<#list c.componentImplementation.componentImplementationTable as componentImplementationTable>
		<#assign table=componentImplementationTable.table.idref>
	 	<#if !componentImplementationTable.isReferentialIntegrityTable>
			<#if table.type == "table"><#-- versioning is unsupported for "form" (AFS) -->
				<#assign tableName=table.name>
				<#list table.column as col>
					<#assign a = protected.getAttributeImplementationLinkedToTableColumnByCit(c, componentImplementationTable, col.name)>
					<#if a?has_content>
						<#-- column is used, so generate 'exists' function -->
						<#assign tableContainerEntry=tableName+"."+col.name>
						<#if tableContainer?seq_contains(tableContainerEntry)>
							<#-- skip this one; table already processed for a(nother) component -->
						<#else>
							<#assign tableContainer=tableContainer+[tableContainerEntry]>
						<#assign columnDataType = col.datatype.idref>
						<#if columnDataType.dimensions?exists && columnDataType.dimensions?has_content && !(columnDataType.dimensions?number == 0)>
							<#assign isArray = true>
				    		<#assign dimensions=columnDataType.dimensions>
						<#else>
							<#assign isArray = false>
				    		<#assign dimensions=0/>
						</#if><#-- columnDataType.dimensions?exists && columnDataType.dimensions?has_content && !(columnDataType.dimensions?number == 0) -->

							<#if isArray>
function boolean column.${tableName}.${col.name}.element.exists(long i.element.nr)
							<#else>
function boolean column.${tableName}.${col.name}.exists()
							</#if>
{
	static	boolean	initialized	| whether this method was called before
							<#if isArray>
	static	long	existing.dimensions		| the number of existing dimensions for the column
							| in the current application version; 0 if column does not exist
							<#else>
	static	boolean	exists		| whether the column exists in the current application version
							</#if>
			long	depth	| depth for array column
			long	retl	| return value to be checked
			long	duml	| dummy variable
			string	dums(1)	| dummy variable
	
	if not initialized then
		retl = rdi.column("${tableName}.${col.name}",
					dums,  | domain
					duml,  | offset
					duml,  | size
					depth,
					duml,  | type
					duml,  | flags
					dums)  | default value
							<#if isArray>
		if retl = 0 then
			existing.dimensions = min(${dimensions}, depth)	
		else
			existing.dimensions = 0
		endif
							<#else>
		exists = (retl = 0)
							</#if>
		<#-- #VERS.#TODO temporary 'logging' START
		write.log.file("${tableName}.${col.name} - exists check initialized")
		<#-- #VERS.#TODO temporary 'logging' END -->

		initialized = true
	endif
							<#if isArray>
	return(i.element.nr > 0 and i.element.nr <= existing.dimensions)
							<#else>
		<#-- #VERS.#TODO temporary 'logging' START
		if exists then
			write.log.file("${tableName}.${col.name} - exists")
		else
			write.log.file("${tableName}.${col.name} - does not exist")
		endif
		<#-- #VERS.#TODO temporary 'logging' END -->
	return(exists)
							</#if>
}
						</#if><#-- container?seq_contains(table) -->
					</#if><#--if a?has_content-->
				</#list><#--list componentImplementationTable.table.idref.column as col-->
			</#if><#-- table.type == "table" -->
	 	</#if><#-- componentImplementationTable.isReferentialIntegrityTable -->
	</#list><#-- c.componentImplementation.componentImplementationTable as componentImplementationTable -->
</#list><#-- protected.listProtectedComponents() as c -->
