<#--
   ******************************************************
   *
   *                       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.
   *
   ******************************************************
  -->
<#if protected.generateFunctionServer()>
|******************************** AFS ****************************
|******************************** Set & Get for AFS ****************************
	<#list protected.listProtectedComponents() as c>
		<#if protected.isFunctionServerComponent(c.componentImplementation)>
			<#list c.componentImplementation.attributeImplementation as attributeImplementation>
				<#if attributeImplementation.column?exists>
				<#assign domain = common.getDatatypeName(common.getDatatype(attributeImplementation))>
				<#if attributeImplementation.componentImplementationTable.idref.table.idref.type == "form">
					<#assign mapping = attributeImplementation.column.value>
				<#else>
					<#assign mapping = attributeImplementation.componentImplementationTable.idref.table.idref.name + 
										"." + attributeImplementation.column.value>
				</#if>
				<#assign AFS_setval = "str$(i.value)">
				<#assign AFS_retval = "lval(value)">
				<#switch common.get_BaanDatatype_from_XMLSchemaDatatype(common.getDatatype(attributeImplementation))>
				<#case "DB.ENUM">	
					<#assign AFS_setval = "str$(etol(i.value))">
					<#assign AFS_retval = "ltoe(lval(value))">
					<#break>
				<#case "DB.STRING">
				<#case "DB.MULTIBYTE">
					<#assign AFS_setval = "i.value">
					<#assign AFS_retval = "value">
					<#break>
				</#switch>
				<#assign datatype=common.getDatatype(attributeImplementation)>
				<#if datatype.dimensions?exists>
	<#-- Yes an array -->
					<#assign ARRAY=true>
function void AFS.set.${c.componentName}.${attributeImplementation.attribute.value}(
			const domain ${domain} i.value,
			      long	i.element)
{
					<#if !common.isTextDatatype(datatype)>
	stpapi.put.field(bl.afs.session, "${mapping}", ${AFS_setval},
						i.element)
					</#if>
}
function domain ${domain} AFS.get.${c.componentName}.${attributeImplementation.attribute.value}(
			      long i.element)
{
	string value(1024)
					<#if !common.isTextDatatype(datatype)>
	stpapi.get.field(bl.afs.session, "${mapping}", value, i.element)
					</#if>
	bl.${c.componentName}.${attributeImplementation.attribute.value}.set = true
	return(AFS_retval)

}
				<#else>
	<#-- Not an array -->
						<#assign ARRAY=false>
	|* No array
function void AFS.set.${c.componentName}.${attributeImplementation.attribute.value}(
			const domain ${domain} i.value)
{
<#-- <<-!SegmentedField>>   not supported-->
						<#if !common.isTextDatatype(datatype)>
	stpapi.put.field(bl.afs.session, "${mapping}", ${AFS_setval})
						</#if>
}

function domain ${domain} AFS.get.${c.componentName}.${attributeImplementation.attribute.value}()
{
	string value(1024)
						<#if !common.isTextDatatype(datatype)>
	stpapi.get.field(bl.afs.session, "${mapping}", value)
						</#if>
	bl.${c.componentName}.${attributeImplementation.attribute.value}.set = true
	return(${AFS_retval})
}
					</#if>
				</#if>
			</#list>
		</#if>
	</#list>

function long handle.result.AFS(long i.result)
{
	string  e.message(256) mb
	string  mscode(12)

	long	result
	long	no.of.mess

	no.of.mess = 0
	repeat
		e.message = ""
		mscode = stpapi.get.mess.code(bl.afs.session, e.message)
		if not isspace(e.message) then
			dal.set.error.message("@" & e.message)
			no.of.mess = no.of.mess + 1
		endif
	until isspace(e.message)

	if no.of.mess = 0 then
		if i.result = ENOREC then
			|* get errormessage by messcode tlbctrerr111
			mscode = "tlbcterr" & str$(i.result)
			e.message = sprintf$(form.text$(mscode),
					strip$(bl.method), strip$(bl.object))
			dal.set.error.message("@" & e.message)
		else
			if not isspace(bl.afs.err.mess) then
				dal.set.error.message("@" & bl.afs.err.mess)
			endif
		endif
	endif

	|* Set general message that BO - Method failed
			dal.set.info.message("tlbctstgen",
				strip$(bl.method), strip$(bl.component),
				strip$(bl.bodesc))
			|* Error occurred in Method '%1$s' on component '2$s'
			|* of Object '%3$s'
	result = stpapi.recover(bl.afs.session, e.message)
	if result = 0 and not isspace(e.message) then
		dal.set.error.message("@" & e.message)
		no.of.mess = no.of.mess + 1
	endif
	return(i.result)
}
</#if>

<#macro generate_methods_create_object_code c compTable>
	<#assign main = "">
	<#assign sync = "">
	<#list compTable.table.value?split("_") as session>
		<#if main?length == 0 >
			<#assign main = session>
		<#else>
			<#if sync?length == 0 >
				<#assign sync = session>
			</#if>
		</#if>
	</#list>
	bl.${c.name}.main.session = "${main}"
	bl.${c.name}.sync.session = "${sync}"

	SET.AFS.SESSION(bl.${c.name}.main.session)
	<#-- Read View fields -->
	<#assign ViewFieldList=[]>
	<#list compTable.table.idref.index as i>
		<#if i.name=="view">
			<#list i.indexColumn as ic>
				<#assign ViewFieldList=ViewFieldList+[{
				"field":ic.column.value
				}]>
			</#list>
		</#if>
	</#list>
	<#-- Read index fields -->
	<#assign IndexFieldList=[]>
	<#list compTable.table.idref.index as i>
		<#if i.name=="index1" && i.isPrimaryKey >
			<#list i.indexColumn as ic>
				<#assign IndexFieldList=IndexFieldList+[{
				"field":ic.column.value
				}]>
			</#list>
		</#if>
	</#list>
	<#assign viewfieldPresent=false>
	<#list protected.listProtectedAttributes("Create", c.name) as a>
		<#assign datatype=common.getDatatype(a.attributeImplementation)>
		<#assign viewfield=false>
		<#if a.attributeImplementation.column?exists>
			<#list ViewFieldList as vfl>
				<#if vfl.field == a.attributeImplementation.column.value>
					<#assign viewfield=true>
				</#if>
			</#list>
		</#if>
		<#if viewfield>
			<#assign viewfieldPresent=true>
			<#if datatype.dimensions?exists>
	SET.AFS.FIELD.ARRAY(${c.name}.${a.attributeName}, ${datatype.dimensions})
			<#else>
	SET.AFS.FIELD(${c.name}.${a.attributeName})
			</#if>
		</#if>
	</#list>

	<#if viewfieldPresent>
	result = stpapi.change.view(bl.${c.name}.main.session, bl.afs.err.mess)
	if result <> 1 and result <> 2 and result <> 0 then
		RETIFNOK(handle.result(AFS_ERROR))
	endif
	</#if>
	
	SYNCHRONYZE.SESSION(${c.name}, add)
	<#list protected.listProtectedAttributes("Create", c.name) as a>
		<#if a.attributeImplementation.column?exists &&
		 (a.scope=="in" || a.scope=="inout")>
			<#assign datatype=common.getDatatype(a.attributeImplementation)>
			<#assign viewfield=false>
			<#list ViewFieldList as vfl>
				<#if vfl.field == a.attributeImplementation.column.value>
					<#assign viewfield=true>
				</#if>
			</#list>
			<#if !viewfield>
				<#if datatype.dimensions?exists>
	SET.AFS.FIELDARRAY(${c.name}.${a.attributeName}, ${datatype.dimensions})
				<#else>
	SET.AFS.FIELD(${c.name}.${a.attributeName})
				</#if>
			</#if>
		</#if>
	</#list>

	<#list protected.listProtectedAttributes("Create", c.name) as a>
		<#if a.attributeImplementation.interactionPoint?exists && a.scope=="in">
	result = stpapi.enum.answer(bl.afs.session, "${a.attributeImplementation.interactionPoint.idref.name}",
		bl.${c.name}.${a.attributeName})
	if result <> 1 then
		bl.afs.err.mess = sprintf$(form.text$("tlbcts0036"),
			bl.afs.session, "${a.attributeImplementation.interactionPoint.idref.name}",
			bl.${c.name}.${a.attributeName})
		|* Error in Question handling in session '%1$s';
		|* Question '%2$s' with answer '%3$s'.
		RETIFNOK(handle.result(AFS_ERROR))
	endif
		</#if>
	</#list>

	result = stpapi.insert(bl.${c.name}.main.session, true, bl.afs.err.mess)
	if result <> 1 then
		RETIFNOK(handle.result(AFS_ERROR))
	endif

	<#assign OutputFieldList=[]>
				
	<#list protected.listProtectedAttributes("Create", c.name) as a>
		<#assign OutputFieldList=OutputFieldList+[{
				"field":a.attributeName
				}]>
		<#if a.attributeImplementation.interactionPoint?exists && a.scope=="out">
			${logger.warn(object, "Interaction Point [" + a.attributeName +
						"] in method 'Create' may not have scope with 'out' value")}
		</#if>
			<#if a.attributeImplementation.column?exists>
				<#assign datatype=common.getDatatype(a.attributeImplementation)>
				<#if datatype.dimensions?exists>
	GET.AFS.FIELDARRAY(${c.name}.${a.attributeName}, ${datatype.dimensions})
				<#else>
	GET.AFS.FIELD(${c.name}.${a.attributeName})
				</#if>
			<#else>
				<#assign defaultVal=protected.getDefaultValueFromMethodArgumentImplementation(a.methodArgumentImplementation)>
				<#if defaultVal.present>
	|* Found in ${defaultVal.where}
	bl.${c.name}.${a.attributeName} = ${defaultVal.value}
	bl.${c.name}.${a.attributeName}.set = true
				</#if>
			</#if>
	</#list>
	<#list c.attributeImplementation as a>
		<#assign aDone=false>
		<#list OutputFieldList as afl>
			<#if afl.field == a.name>
				<#assign aDone=true>
			</#if>
		</#list>
		<#if !aDone && !a.interactionPoint?exists>
			<#if a.column?exists>
				<#assign datatype=common.getDatatype(a)>
				<#if datatype.dimensions?exists>
	GET.AFS.FIELD.ARRAY(${c.name}.${a.name}, ${datatype.dimensions})
				<#else>
	GET.AFS.FIELD(${c.name}.${a.name})
				</#if>
			<#elseif a.defaultValue?exists> <#-- fill default -->
				<#assign defaultVal=protected.getDefaultFromAttributeImplementation(a)>
				<#if defaultVal.dimensions==0 && defaultVal.present> <#-- no defaults for arrays-->
	bl.${c.name}.${a.name} = ${defaultVal.value}
	bl.${c.name}.${a.name}.set = true
				</#if>
			</#if>
		</#if>
	</#list>
</#macro>

<#macro generate_methods_read_object_part comp compTable>
 	<#assign c=comp.componentImplementation>
function long Read.Object.${c.name}.AFS()
{
	long	result

	result = 0
	bl.method = "Read"
	bl.object = "<@names.BDE_Code/> ${c.name}"

	bl.bodesc = "<@names.BDE_Code/> ${names.businessInterfaceName}"
	bl.component = "${c.name}"
	<#assign main = "">
	<#assign sync = "">
	<#list compTable.table.value?split("_") as session>
		<#if main?length == 0 >
			<#assign main = session>
		<#else>
			<#if sync?length == 0 >
				<#assign sync = session>
			</#if>
		</#if>
	</#list>
	bl.${c.name}.main.session = "${main}"
	bl.${c.name}.sync.session = "${sync}"

	SET.AFS.SESSION(bl.${c.name}.main.session)
	<#list c.attributeImplementation as a>
		<#if a.column?exists && a.isIdentifier>
			<#assign datatype=common.getDatatype(a)>
			<#assign viewfieldPresent=true>
			<#if datatype.dimensions?exists>
	SET.AFS.FIELD.ARRAY(${c.name}.${a.name}, ${datatype.dimensions})
			<#else>
	SET.AFS.FIELD(${c.name}.${a.name})
			</#if>
		</#if>
	</#list>

	result = stpapi.find(bl.${c.name}.main.session, bl.afs.err.mess)
	if result <> 1 then
		AFS.set.error.message.${c.name}.${main}()
		bl.result = handle.result(AFS_ERROR)
		if bl.result <> 0 then
			bl.${c.name}.initialize = false
			bl.method.result = bl.result
			bl.ret = <@names.ST_DLLCode/>.finalize.Read(bl.result)
			return(bl.result)
		endif		
	endif

	if not isspace(bl.${c.name}.sync.session) then
<#if names.applicationBackendRelease != "BaanIV">
		result = stpapi.synchronize.dialog(
			bl.${c.name}.main.session, "display",
			bl.afs.err.mess)
		if result <> 1 then
			bl.result = handle.result(AFS_ERROR)
			if bl.result <> 0 then
				bl.${c.name}.initialize = false
				bl.method.result = bl.result
				bl.ret = <@names.ST_DLLCode/>.finalize.Read(bl.result)
				return(bl.result)
			endif		
		endif
</#if><#--if names.applicationBackendRelease != "BaanIV"-->
		SET.AFS.SESSION(bl.${c.name}.sync.session)
	else
		SET.AFS.SESSION(bl.${c.name}.main.session)
	endif

	<#list c.attributeImplementation as a>
		<#if !a.interactionPoint?exists>
			<#if a.column?exists>
				<#assign datatype=common.getDatatype(a)>
				<#if datatype.dimensions?exists>
	GET.AFS.FIELD.ARRAY(${c.name}.${a.name}, ${datatype.dimensions})
				<#else>
	GET.AFS.FIELD(${c.name}.${a.name})
				</#if>
			</#if>
		</#if>
	</#list>
	return(0)
}

</#macro>	

<#macro generate_methods_change_object_part c compTable>
	<#assign main = "">
	<#assign sync = "">
	<#list compTable.table.value?split("_") as session>
		<#if main?length == 0 >
			<#assign main = session>
		<#else>
			<#if sync?length == 0 >
				<#assign sync = session>
			</#if>
		</#if>
	</#list>
	bl.${c.name}.main.session = "${main}"
	bl.${c.name}.sync.session = "${sync}"

	SET.AFS.SESSION(bl.${c.name}.main.session)
	
	<#list protected.listProtectedAttributes("Change", c.name) as a>
		<#if a.attributeImplementation.column?exists &&
		  a.attributeImplementation.isIdentifier>
			<#assign datatype=common.getDatatype(a.attributeImplementation)>
			<#if datatype.dimensions?exists>
	SET.AFS.FIELDARRAY(${c.name}.${a.attributeName}, ${datatype.dimensions})
			<#else>
	SET.AFS.FIELD(${c.name}.${a.attributeName})
			</#if>
		</#if>
	</#list>
	
	result = stpapi.find(bl.${c.name}.main.session, bl.afs.err.mess)
	if result <> 1 then
		AFS.set.error.message.${c.name}.${main}()
		RETIFNOK(handle.result(AFS_ERROR))
	endif

	SYNCHRONYZE.SESSION(${c.name}, modify)

	<#assign AFS_question=false>
	<#list protected.listProtectedAttributes("Change", c.name) as a>
		<#if a.attributeImplementation.column?exists &&
		  !a.attributeImplementation.isIdentifier>
			<#assign datatype=common.getDatatype(a.attributeImplementation)>
			<#if datatype.dimensions?exists>
	SET.AFS.FIELDARRAY(${c.name}.${a.attributeName}, ${datatype.dimensions})
			<#else>
	SET.AFS.FIELD(${c.name}.${a.attributeName})
			</#if>
		</#if>
	</#list>

	<#list protected.listProtectedAttributes("Change", c.name) as a>
		<#if a.attributeImplementation.interactionPoint?exists && a.scope=="in">
	result = stpapi.enum.answer(bl.afs.session, "${a.attributeImplementation.interactionPoint.idref.name}",
			bl.${c.name}.${a.attributeName})
	if result <> 1 then
		bl.afs.err.mess = sprintf$(form.text$("tlbcts0036"),
			bl.afs.session, "${a.attributeImplementation.interactionPoint.idref.name}",
			bl.${c.name}.${a.attributeName})
		|* Error in Question handling in session '%1$s';
		|* Question '%2$s' with answer '%3$s'.
		RETIFNOK(handle.result(AFS_ERROR))
	endif
		</#if>
	</#list>

	result = stpapi.update(bl.${c.name}.main.session, true, bl.afs.err.mess)
	if result <> 1 then
		RETIFNOK(handle.result(AFS_ERROR))
	endif

	<#assign OutputFieldList=[]>
	<#list protected.listProtectedAttributes("Change", c.name) as a>
		<#assign OutputFieldList=OutputFieldList+[{
				"field":a.attributeName
				}]>
		<#if a.attributeImplementation.interactionPoint?exists && a.scope=="out">
			${logger.warn(object, "Interaction Point [" + a.attributeName +
						"] in method 'Change' may not have scope with 'out' value")}
		</#if>
			<#if a.attributeImplementation.column?exists>
				<#assign datatype=common.getDatatype(a.attributeImplementation)>
				<#if datatype.dimensions?exists>
	GET.AFS.FIELDARRAY(${c.name}.${a.attributeName}, ${datatype.dimensions})
				<#else>
	GET.AFS.FIELD(${c.name}.${a.attributeName})
				</#if>
			<#else>
				<#assign defaultVal=protected.getDefaultValueFromMethodArgumentImplementation(a.methodArgumentImplementation)>
				<#if defaultVal.present>
	|* Found in ${defaultVal.where}
	bl.${c.name}.${a.attributeName} = ${defaultVal.value}
	bl.${c.name}.${a.attributeName}.set = true
				</#if>
			</#if>
	</#list>
	<#list c.attributeImplementation as a>
		<#assign aDone=false>
		<#list OutputFieldList as afl>
			<#if afl.field == a.name>
				<#assign aDone=true>
			</#if>
		</#list>
		<#if !aDone && !a.interactionPoint?exists>
			<#if a.column?exists>
				<#assign datatype=common.getDatatype(a)>
				<#if datatype.dimensions?exists>
	GET.AFS.FIELD.ARRAY(${c.name}.${a.name}, ${datatype.dimensions})
				<#else>
	GET.AFS.FIELD(${c.name}.${a.name})
				</#if>
			<#elseif a.defaultValue?exists> <#-- fill default -->
				<#assign defaultVal=protected.getDefaultFromAttributeImplementation(a)>
				<#if defaultVal.dimensions==0 && defaultVal.present> <#-- no defaults for arrays-->
	bl.${c.name}.${a.name} = ${defaultVal.value}
	bl.${c.name}.${a.name}.set = true
				</#if>
			</#if>
		</#if>
	</#list>
</#macro>


<#macro generate_methods_delete_object_part c compTable>
	<#assign main = "">
	<#list compTable.table.value?split("_") as session>
		<#if main?length == 0 >
			<#assign main = session>
		</#if>
	</#list>
	bl.${c.name}.main.session = "${main}"

	SET.AFS.SESSION(bl.${c.name}.main.session)

	<#assign AFS_question=false>
	<#list protected.listProtectedAttributes("Delete", c.name) as a>
		<#if a.attributeImplementation.column?exists &&
		  a.attributeImplementation.isIdentifier>
			<#assign datatype=common.getDatatype(a.attributeImplementation)>
			<#if datatype.dimensions?exists>
	SET.AFS.FIELDARRAY(${c.name}.${a.attributeName}, ${datatype.dimensions})
			<#else>
	SET.AFS.FIELD(${c.name}.${a.attributeName})
			</#if>
		</#if>
	</#list>
	result = stpapi.find(bl.${c.name}.main.session, bl.afs.err.mess)
	if result <> 1 then
		AFS.set.error.message.${c.name}.${main}()
		RETIFNOK(handle.result(AFS_ERROR))
	endif

	<#list protected.listProtectedAttributes("Delete", c.name) as a>
		<#if a.attributeImplementation.interactionPoint?exists && a.scope=="in">
	result = stpapi.enum.answer(bl.afs.session, "${a.attributeImplementation.interactionPoint.idref.name}",
			bl.${c.name}.${a.attributeName})
	if result <> 1 then
		bl.afs.err.mess = sprintf$(form.text$("tlbcts0036"),
			bl.afs.session, "${a.attributeImplementation.interactionPoint.idref.name}",
			bl.${c.name}.${a.attributeName})
		|* Error in Question handling in session '%1$s';
		|* Question '%2$s' with answer '%3$s'.
		RETIFNOK(handle.result(AFS_ERROR))
	endif
		</#if>
	</#list>

	result = stpapi.delete(bl.${c.name}.main.session, true, bl.afs.err.mess)
	if result <> 1 then
		RETIFNOK(handle.result(AFS_ERROR))
	endif
</#macro>



<#macro set_error_message>
	<#list protected.listProtectedComponents() as c>
		<#if protected.isFunctionServerComponent(c.componentImplementation)>
			<#list c.componentImplementation.componentImplementationTable as cit>
				<#assign table=cit.table.idref>
				<#assign main = "">
				<#list cit.table.value?split("_") as session>
					<#if main?length == 0 >
						<#assign main = session>
					</#if>
				</#list>
function AFS.set.error.message.${c.componentName}.${main}()
{
	string	session.desc(256) mb

	if not isspace(bl.afs.err.mess) then
		return
	endif
	tt.session.desc("${main}", session.desc)
	bl.afs.err.mess = sprintf$(form.text$("tlbctst0013"),
				<#assign prefix="        ">
				<#assign rel_attribute="">
				<#list table.index as ti>
					<#if ti.isPrimaryKey>
						<#list ti.indexColumn as tii>
							<#assign tc=tii.column.idref>
							<#-- search cooresponding attribute-->
							<#list protected.listProtectedAttributes("", c.componentName) as a>
								<#if a.attributeImplementation.column?exists && 
								     a.attributeImplementation.column.value == tc.name>
										<#assign rel_attribute=c.componentName + "." + a.attributeName>
								</#if>
							</#list>
							<#switch common.get_BaanDatatype_from_XMLSchemaDatatype(tc.datatype.idref)>
	  							<#case "DB.LONG">
							  	<#case "DB.DATE">
							  	<#case "DB.TIME">
							  	<#case "DB.TEXT">
							  	<#case "DB.DOUBLE">
		${prefix}str$(bl.${rel_attribute})
									<#break>
					  			<#case "DB.STRING">
		${prefix}strip$(bl.${rel_attribute})
									<#break>
							  	<#case "DB.ENUM">
		${prefix}enum.descr$("${tc.datatype.value}", bl.${rel_attribute})	  
									<#break>	
							</#switch>  
							<#assign prefix="& \",\" & ">
						</#list>
					</#if>
				</#list>
		, session.desc, get.compnr())
	|* Record [%1$s] cannot be found in session %2$s in company %3$d.
}
			</#list>
		</#if>
	</#list>
</#macro>	



