<#--
   ******************************************************
   *
   *                       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.
   *
   ******************************************************
 -->
<#--
     Generate the public layer implementation file
     - object is an instance of BusinessInterfaceImplementation
 -->
<@common.checkCancel/>
<@source.query_statuses/>
#define RETIFNOK2_LIST_NEW(...)
^               bl.result = ...
^               if bl.result <> 0 then
^                       handle.messages(o.responseArgument,
^					o.resultArgument,
^					true)
^                       bl.ret = xmlDelete(o.ResponseArgument)
^                       o.ResponseArgument = 0
^                       return(bl.result)
^               endif

#define	RETIFNOK5_LIST(...)
^		bl.result = ...
^		if bl.result <> 0 then
^			return(bl.result)
^		endif

<#function attributeImplementedInQuery1 iComponent iAttribute>
	<#list SelectionList as sl>
		<#if sl.component==iComponent && sl.attribute == iAttribute>
			<#return true>
		</#if>
	</#list>

	<#return false>
</#function>

<#macro selecting_attributes iattr comp compName>
	<#assign level=level+1>
	<#if attributeImplementedInQuery1(compName, iattr.name)>
		<#assign attr_ok=true>
	<#else>
		<#assign attr_ok=false>
	</#if>
	<#if level==1>
		<#assign groupSel=public.getAttributeGroupForSelection(comp, iattr)>
		<#assign attrPath=iattr.name?replace("_", ".")>
		<#if iattr.type=="group" || iattr.type=="complex" >
		<#-- Only ..* -->
			<#assign star=".*">
		<#else>
			<#assign star="">
		</#if>
		<#if attr_ok || iattr.type=="group" || iattr.type=="complex">
			<#assign case_for_attr=true>
	case "${iattr.name}${star}":|*attr(${iattr.type})
			<#if groupSel != iattr.name>
	case "${groupSel}${star}":|*group(${iattr.type})
			</#if>
			<#if attrPath != iattr.name && attrPath != groupSel>
	case "${attrPath}${star}":|*attr_path(${iattr.type})
			</#if>
		</#if>
		<#assign attrList="">
	</#if>
	<#if !attrList?contains(":"+iattr.name+":")>
		<#assign attrList=attrList+":"+iattr.name+":">
	<#if iattr.type!="group">
		<#if attr_ok>
		${compName}.${iattr.name}.select = true |* LVL:${level}
			<#if iattr.type == "qualifier" && iattr.parentAttribute?exists && 
			     !attrList?contains(":"+iattr.parentAttribute.value+":") && attributeImplementedInQuery1(compName, iattr.parentAttribute.value)>
				<#assign attrList=attrList+":"+iattr.name+":">
		${compName}.${iattr.parentAttribute.value}.select = true  |* Parentattribute
			</#if>
			<#if level==1>
				<#list parentIdentifiers as pi>
		${pi.component}.${pi.attribute}.select = true  |* Parent Identifier ${compName}
				</#list>
			</#if>
		</#if>
	</#if>
	<#list iattr.attributeRelationship as ar>
		<#if !ar.attribute.idref.isHidden>
<@selecting_attributes iattr=ar.attribute.idref comp=comp compName=compName/>
		</#if>
	</#list>
	</#if>
	<#assign level=level-1>
</#macro>

<@source.performance_point description="InitSelection"/>
function void InitSelection(boolean i.settrue)
{
	FunctionUsage
	EndFunctionUsage

<#list public.listAllPublicAttributes() as a>
	${a.componentName}.${a.attributeName}.select = i.settrue
</#list><#--list public.listAllPublicAttributes() as a-->
}

<@source.performance_point description="SetSelectionVariable_new1"/>
function long SetSelectionVariable_new1(const string SelecVar)
{
	<#-- Note:
		This generated function uses in public.sb.common 
		the function generateDuplicatesSetSelectionVariable. When
		changing the case structure of this function
		always implement these changes also in the
		generateDuplicatesSetSelectionVariable function.
		-->
	long	end_pos_comp
	boolean	correct_selection

	correct_selection = false
	|* Strip component.
	end_pos_comp = pos(SelecVar, ".")

	<#-- make duplicate array -->
	<#assign dupl_comp="">
	<#assign dupl_comp_done="">
	<#list DuplicateComponents as dc>
		<#assign dupl_comp=dupl_comp+":"+dc+":">
	</#list>
	if end_pos_comp > 0 then
		on case SelecVar(1;end_pos_comp-1)
	<#list common.listComponentImplementations("", iobject) as c>
		<#-- Check for duplicate components -->
		<#if dupl_comp?contains(":"+c.componentPublicName+":")> 
			<#if !dupl_comp_done?contains(":"+c.componentPublicName+":")>
		  		<#assign dupl_comp_done=dupl_comp_done+":"+c.componentPublicName+":">
		case "${c.componentPublicName}":
		   	  	<#list common.listComponentImplementations("", iobject) as d1>
		     		<#if c.componentPublicName == d1.componentPublicName>
		    SetSelectionVariable_New1_${d1.componentName}(
				SelecVar(end_pos_comp+1), correct_selection)
		     		</#if>
		     	</#list>
			break
		     </#if>
		<#else>
		case "${c.componentPublicName}":
			SetSelectionVariable_New1_${c.componentName}(
				SelecVar(end_pos_comp+1), correct_selection)
			break
		</#if>
	</#list>
		endcase
	else
		if SelecVar = "*" then
			InitSelection(true)
			correct_selection = true
		endif
	endif
	
	if not correct_selection then
			dal.set.error.message("tlbctsb0042", SelecVar)
			|* No valid selectionAttributes received
			|* (unknown selectionAttribute received "%1$s").
			return(DALHOOKERROR)
	endif
	return (0)
}
	<#list common.listComponentImplementations("", iobject) as c>

<@source.performance_point description="SetSelectionVariable_new1_"+c.componentName/>
function SetSelectionVariable_New1_${c.componentName}(const string SelecVar,
			ref boolean	o.correct_selection)
{
	<#assign first=true>
	<#list common.listComponentImplementations(c.componentName, iobject) as d>
		<#if first>
	long	end_pos_comp

	|* Strip component.
	
	end_pos_comp = pos(SelecVar, ".")
	if end_pos_comp > 0 then
		on case (SelecVar(1;end_pos_comp-1))
			<#assign first=false>
		</#if>
		case "${d.componentPublicName}":
			SetSelectionVariable_New1_${d.componentName}(
				SelecVar(end_pos_comp+1), o.correct_selection)
			return
			break
	</#list>
	<#if !first>
		endcase
	endif
	</#if>
	
	on case (SelecVar)
	case "*": |* ${c.componentName}.*
		<#list SelectionList as sl>
			<#if c.componentName == sl.component>
		${sl.component}.${sl.attribute}.select = true
			</#if>
		</#list>
		<#list public.listAllParentIndentifiers(c.componentName) as i>
		${i.parentComponentName}.${i.attributeName}.select = true  |* Parent Identifier ${c.componentName}
		</#list>
		o.correct_selection = true
		break
		<#assign parentIdentifiers=[]>
		<#list public.listAllParentIndentifiers(c.componentName) as i>
			<#assign parentIdentifiers=parentIdentifiers + [{
				"component":i.parentComponentName,
				"attribute":i.attributeName}]>
		</#list>
		<#-- build a list with relations for associations -->
		<#assign attribute_associations=[]>
		<#list public.listAllPublicAttributes() as at>
			<#if at.componentName == c.componentName && at.attributeImplementation.type == "association">
				<#list protected.listProtectedAssociatedAttributes(at.componentImplementation) as aa>
					<#if aa.fromAttributeImplementation == at.attributeImplementation>
						<#list common.listRelationshipDetails(aa.fromComponentImplementation,aa.relationshipImplementation) as rd>
							<#if attributeImplementedInQuery1(aa.fromComponentName, rd.fromAttributeName)>
								<#assign attribute_associations=attribute_associations + [{
									"attribute":at.attributeName,
									"related_component":aa.fromComponentName,
									"related_attribute":rd.fromAttributeName}]>
							</#if>
						</#list>
					</#if>
				</#list>
			</#if>
		</#list>
	<#assign level=0>
	<#list c.componentImplementation.component.idref.attribute as attr>
		<#if !attr.isHidden>
			<#assign case_for_attr=false>
<@selecting_attributes iattr=attr comp=c.componentImplementation.component.idref compName=c.componentName/>
			<#if case_for_attr>
				<#list attribute_associations as aa>
					<#if attr.name == aa.attribute>
		${aa.related_component}.${aa.related_attribute}.select = true  |* Related Identifier
					</#if>
				</#list>
		o.correct_selection = true
		break
			</#if>
		</#if>
	</#list>
	endcase	
}
	</#list>

<@source.performance_point description="SetSelection_new1"/>
function long SetSelection_new1(xmlNode SelectionNode)
{
	long	ret
	xmlNode	AttributeNode
	string	SelectionAttribute(256)
	boolean	postfilter
	xmlnode filteredAttributes
	xmlnode	filter.attribute

	AttributeNode = xmlGetFirstChild(SelectionNode)
	while AttributeNode > 0
		ret = xmlGetData(AttributeNode, SelectionAttribute)
		RETIFNOK(SetSelectionVariable_new1(strip$(SelectionAttribute)))
		AttributeNode = xmlGetRightSibling(AttributeNode)
	endwhile
	
	|* Set selected for postfilter attributes
	RETIFNOK(<@names.ST_DLLCode/>.get.postfilter(postfilter))
	if postfilter then
		RETIFNOK(<@names.ST_DLLCode/>.get.filteredAttributes(
					filteredAttributes))
		filter.attribute = xmlGetFirstChild(filteredAttributes)
		while filter.attribute <> 0
			on case xmlData$(filter.attribute)
		<#list public.listAllPublicAttributes() as a>
			case "${a.componentName}.${common.getAttributeName(a.attributeImplementation)}":
				${a.componentName}.${a.attributeName}.select = true
				break
		</#list>
			default:			
				break
			endcase
			filter.attribute = xmlGetRightSibling(filter.attribute)
		endwhile
	endif

	return(0)
}

<@source.performance_point description="check.iteratorID_new"/>
function long check.iteratorID_new(long i.iteratorID, boolean	i.reset)
{
	static	long	ProtectedIteratorID

	if i.reset then
		ProtectedIteratorID = 0
		return(0)
	endif
	
	if i.iteratorID <> 0 then
		if ProtectedIteratorID = 0 then
			dal.set.error.message("tlbctsb0017", i.iteratorID)
			|* iteratorID %1$d is not known. No iterator is present
			return(DALHOOKERROR)
		endif

		|* An iterator request for a none existing process: so error
		if i.iteratorID <> pid then
			dal.set.error.message("tlbctsb0016", i.iteratorID)
			|* iteratorID %1$d is not known.
			return(DALHOOKERROR)
		endif
	endif

	ProtectedIteratorID = pid
	
	return(0)
}
<#list object.methodImplementation as methodImplementation>
	<#assign methodName=common.getNameFromMethodImplementation(methodImplementation)>
	<#if (methodName=="Show" || methodName=="List") &&
	     protected.hasMethodAlternativeImplementation(methodName)=="">

<@source.performance_point description=methodName+"_new"/>
function long <@names.SB_DLLCode/>.${methodName}_new(
					const xmlnode i.RequestArgument,
		<#if object.businessInterfaceDefinition.idref.type!="bod">
					    boolean i.suppressNillableAttributes,
		</#if>
					ref xmlnode o.ResponseArgument,
					ref xmlnode o.ResultArgument)
{
	xmlNode	Response${names.businessInterfaceName}Node
	boolean	doFetch
	boolean	${names.businessInterfaceName}_cancel
	long	bl.retval
		<#if methodName!="Show">
	boolean	iterator_active
		</#if>
	long	duml

	duml = 0
	bl.current.method = "${methodName}"
	bl.processingScope = REQUEST
		<#if methodName!="Show">
	iterator_active = false
		</#if>
		
	RETIFNOK2(createResponseObject(o.ResponseArgument, "${methodName}"))
	RETIFNOK2(createResultObject(o.ResultArgument))
	RETIFNOK2(selectResponseNode(o.ResponseArgument, Response${names.businessInterfaceName}Node))

	make.xmlArguments.global(i.RequestArgument,
				 o.ResponseArgument,
				 o.ResultArgument)


	doFetch = true
	bl.result = Handle.Request.${methodName}.${names.businessInterfaceName}_new(i.RequestArgument, 
		<#if object.businessInterfaceDefinition.idref.type!="bod">
				i.suppressNillableAttributes,
		</#if>
				o.ResponseArgument, o.ResultArgument,
				Response${names.businessInterfaceName}Node, <#if methodName!="Show">iterator_active, </#if>${names.businessInterfaceName}_cancel)

	bl.retval = bl.result
	if bl.result <> 0 then
		|* Add used query to response.
		add.executed.queries.to.response.${methodName}.${names.businessInterfaceName}(o.resultArgument, true)
		handle.messages(o.responseArgument,
		o.resultArgument,
		true)
        bl.ret = xmlDelete(o.ResponseArgument)
        o.ResponseArgument = 0
	else
	 	if log.query then
			|* Add used query to response.
			add.executed.queries.to.response.${methodName}.${names.businessInterfaceName}(o.ResponseArgument, false)
		endif
		handle.messages(o.ResponseArgument,
			o.ResultArgument, false) |* put mess in response
	endif
		<#if methodName!="Show">
	if not iterator_active then
		</#if>
		<#list public.listPublicComponents(methodName) as c>
		RETIFNOK2_LIST_NEW(<@names.ST_DLLCode/>.finalize.${c.componentName}.${methodName}())
		</#list>
		<#if methodName!="Show">
		duml = check.iteratorID_new(0, true)
		</#if>
		
		RETIFNOK2_LIST_NEW(<@names.ST_DLLCode/>.finalize.request.${methodName}())
		<#if methodName!="Show">
	endif
		</#if>

	return(bl.retval)
}

		<#list public.listPublicComponents(methodName) as c>
<@source.performance_point description="add.executed.queries.to.response"/>
function add.executed.queries.to.response.${methodName}.${c.componentName}(xmlnode	io.response,
					boolean		i.error)
{
	xmlnode	log.node
	xmlnode mess.node
	xmlnode	curr.node
	long	i, ret
	xmlnode	bnode
	string hookname(128)
			<#if c.componentName == names.businessInterfaceName>
	string filterExpression(1024)
	boolean	postFilter
	xmlnode	filter
			</#if>

	if <@names.ST_DLLCode/>.return.query.status.${c.componentName}() = QUERY.INACTIVE then
		return
	endif
			<#if c.componentName == names.businessInterfaceName>
	if i.error then
		log.node = xmlNewNode("MessageDetails", XML_ELEMENT, io.response)
	else
		log.node = xmlNewNode("InformationArea", XML_ELEMENT, io.response)
	endif
	mess.node = xmlNewNode("Message", XML_ELEMENT, log.node)

	curr.node = xmlNewDataElement("messageCode", "QueryLog", mess.node)
	curr.node = xmlNewDataElement("messageType", "information", mess.node)
	curr.node = xmlNewDataElement("messageText", "Executed Queries for method ${methodName}.",
				mess.node)
	curr.node = xmlNewDataElement("messageSource","SQL Query",
				mess.node)
	curr.node = xmlNewDataElement("messageReferenceType", "logEntry", mess.node)
	curr.node = xmlNewNode("messageReferenceInfo", XML_ELEMENT, mess.node)
			<#-- Show table Aliases -->
	for i = 1 to <@names.ST_DLLCode/>.retrieve.table_alias.count()
		if i = 1 then
			mess.node = xmlNewNode("Table_Aliases",XML_ELEMENT, curr.node)
		endif
		bnode = xmlNewDataElement("t" & str$(i),
			strip$(<@names.ST_DLLCode/>.retrieve.table_alias(i)), mess.node)
	endfor
	mess.node = xmlNewNode("ComponentFilterHook", XML_ELEMENT, curr.node)
				<#list protected.listProtectedComponents() as pc>
					<#if pc.componentImplementation.componentFilterHookExpression?exists && pc.componentImplementation.componentFilterHookExpression?has_content>
	bnode = xmlNewDataElement("${pc.componentName}", <@names.ST_DLLCode/>.retrieve.component.filter.hook.${pc.componentName}(), mess.node)
					</#if>
				</#list>
	mess.node = xmlNewNode("Filter", XML_ELEMENT, curr.node)
	ret = <@names.ST_DLLCode/>.get.filter.expression(filterExpression)
	bnode = xmlNewDataElement("Expression", filterExpression, mess.node)
	mess.node = xmlNewNode("PostFilter", XML_ELEMENT, curr.node)
	ret = <@names.ST_DLLCode/>.get.postfilter(postfilter)
	if postfilter then
		|* for postfiltering the original filter is used
		ret = <@names.ST_DLLCode/>.get.filter(filter)
		ret = xmlDuplicateAndAppendToChilds(mess.node, filter)
	endif
	ret = xmlSetAttribute(mess.node, "postfiltering", postfilter?"true":"false")
			<#else>
	curr.node = io.response
			</#if>

	log.node = xmlNewNode("${c.componentName}",XML_ELEMENT, curr.node)

	if <@names.ST_DLLCode/>.query.on.hook.${c.componentName}.${methodName}(hookname) then
	mess.node = xmlNewDataElement("HOOK", 
		"Query is executed in hook " & strip$(hookname), log.node)
	else
	|* Read and Add query.
	mess.node = xmlNewDataElement("select", 
		<@names.ST_DLLCode/>.retrieve.select.clause.${c.componentName}(), log.node)
	mess.node = xmlNewDataElement("from", 
		<@names.ST_DLLCode/>.retrieve.from.clause.${c.componentName}(), log.node)
	mess.node = xmlNewDataElement("where", 
		<@names.ST_DLLCode/>.retrieve.where.clause.${c.componentName}(), log.node)
	mess.node = xmlNewDataElement("orderby", 
		<@names.ST_DLLCode/>.retrieve.orderby.clause.${c.componentName}(), log.node)
	for i = 1 to <@names.ST_DLLCode/>.retrieve.selectbind.count.${c.componentName}()
		if i = 1 then
			mess.node = xmlNewNode("selectbind",XML_ELEMENT, log.node)
		endif
		bnode = xmlNewDataElement("_" & str$(i),
			strip$(<@names.ST_DLLCode/>.retrieve.selectbind.clause.${c.componentName}(i)), mess.node)
	endfor
	for i = 1 to <@names.ST_DLLCode/>.retrieve.wherebind.count.${c.componentName}()
		if i = 1 then
			mess.node = xmlNewNode("wherebind",XML_ELEMENT, log.node)
		endif
		bnode = xmlNewDataElement("_"& str$(i),
			strip$(<@names.ST_DLLCode/>.retrieve.wherebind.clause.${c.componentName}(i)), mess.node)
	endfor
	endif
	if <@names.ST_DLLCode/>.return.query.status.${c.componentName}() = QUERY.ERROR then
		ret = xmlsetAttribute(log.node, "Error_status", "Error")
		return
	endif
	ret = xmlsetAttribute(log.node, "fetches", <@names.ST_DLLCode/>.retrieve.fetch.count.${c.componentName}())
	
	|* Add sub components.
			<#assign listOfPublicSubComponents = public.listPublicSubComponents(methodName, c.componentName)>
			<#list listOfPublicSubComponents as s>
	add.executed.queries.to.response.${methodName}.${s.componentName}(log.node, i.error)
			</#list><#--list listOfPublicSubComponents as s-->
}

<@source.performance_point description="Handle.Request."+methodName+"."+c.componentName+"_new"/>
function long Handle.Request.${methodName}.${c.componentName}_new(
			const		xmlNode	i.Request${c.componentName}Node,
			<#if object.businessInterfaceDefinition.idref.type!="bod">
					boolean	i.suppressNillableAttributes,
			</#if>
			ref			xmlNode	io_${methodName}.ResponseArgument, 
			ref			xmlNode	io_${methodName}.ResultArgument,
			<#if c.componentName == names.businessInterfaceName>
			const		xmlNode	i.Response${names.businessInterfaceName}Node,
				<#if methodName!="Show">
			ref			boolean	o.iter_act,
				</#if><#--if methodName!="Show"-->
			<#else><#--if c.componentName == names.businessInterfaceName-->
			const		xmlNode	i.Response${c.parentComponentName}Node,
			</#if><#--if c.componentName == names.businessInterfaceName-->		
			ref		boolean	o.cancel)
{
	long	ret
	boolean	fetching
	xmlNode	Response.${c.componentName}Node
	boolean	selection.present
			<#if c.componentName == names.businessInterfaceName>
	xmlnode	ControlAreaNode
				<#if c.componentName == names.businessInterfaceName && methodName!="Show">
	long	iteratorID
	long	iteratorFetchSize
	xmlnode ControlArea
				</#if><#--if c.componentName == names.businessInterfaceName && methodName!="Show"-->
	long	fetch.count
				<#if c.componentName == names.businessInterfaceName && methodName!="Show">
	long	max.number.objects, max.batch.count
	static	long	total.object.count
	boolean	cont.iteration
				</#if><#--if c.componentName == names.businessInterfaceName && methodName!="Show"-->
	boolean sortingExpressionSet
	boolean	TagFound
	string  Value(1024)
	xmlnode	filter
	boolean	postfilter
	xmlnode	metaDataForFilter
	boolean	postfilter.valid
			</#if><#--if c.componentName == names.businessInterfaceName-->

			<#if c.componentName != names.businessInterfaceName || methodName=="Show">
	ret = 0
			</#if><#--if c.componentName != names.businessInterfaceName || methodName=="Show"-->
	bl.result = 0
	Response.${c.componentName}Node = 0

			<#if c.componentName == names.businessInterfaceName>
	ControlAreaNode = xmlFindFirst("ControlArea", i.Request${c.componentName}Node)
			</#if><#--if c.componentName == names.businessInterfaceName-->
			<#if c.componentName == names.businessInterfaceName && methodName!="Show" >
	|* Get maxNumberOfObjects from the ControlAreaNode Node
	RETIFNOK5_LIST(getValue(ControlAreaNode, 
			"ControlArea.maxNumberOfObjects",
			"<ControlArea>.<maxNumberOfObjects>",
			false, Value, TagFound))
	max.number.objects = lval(Value)
	max.batch.count = max.number.objects
	
	|* Get iteratorID from the ControlAreaNode Node
	RETIFNOK5_LIST(getValue(ControlAreaNode, 
			"ControlArea.iteratorID",
			"<ControlArea>.<iteratorID>",
			false, Value, TagFound))
	iteratorID  = lval(Value)
	RETIFNOK5_LIST(check.iteratorID_new(iteratorID, false))
	
	|* Get iteratorFetchSize from the ControlAreaNode Node
	RETIFNOK5_LIST(getValue(ControlAreaNode, 
			"ControlArea.iteratorFetchSize",
			"<ControlArea>.<iteratorFetchSize>",
			false, Value, TagFound))
	iteratorFetchSize = lval(Value)

	cont.iteration = false
	if iteratorFetchSize <> 0 then
		if IteratorID <> 0 then
			|* Busy with iteration
			cont.iteration = true
		else
	 		iteratorID = pid
			total.object.count = 0
		endif
		max.batch.count = iteratorFetchSize
	else
		|* No iteration.
		total.object.count = 0
	endif

			</#if><#--if c.componentName == names.businessInterfaceName && methodName!="Show" -->
			<#if c.componentName == names.businessInterfaceName && methodName!="Show" >
	if not cont.iteration then
			</#if><#--if c.componentName == names.businessInterfaceName && methodName!="Show" -->
			<#if c.componentName == names.businessInterfaceName>
	if <@names.ST_DLLCode/>.return.query.status.${c.componentName}() <> QUERY.FETCH then
		RETIFNOK5_LIST(<@names.ST_DLLCode/>.initialize.request.${methodName}())

		|* Get sortingExpression from the ControlAreaNode Node
		RETIFNOK5_LIST(getValue(ControlAreaNode, 
				"ControlArea.sortingExpression",
				"<ControlArea>.<sortingExpression>",
				false, Value, TagFound))
		if Tagfound then
			if not isspace(Value) then
				dal.set.error.message("tlbctsb0050")
				|* No valid sortingExpression received
				|* (a non-empty sortingExpression is not supported).
				return(DALHOOKERROR)					
			endif
			sortingExpressionSet = true
		else
			sortingExpressionSet = false
		endif
		
		|* Set sortingExpression to protected layer
		RETIFNOK5_LIST(<@names.ST_DLLCode/>.set.sorting.expression(sortingExpressionSet))

		|* Set filter to protected layer and determine postfiltering
		filter = xmlFindFirst("Filter", ControlAreaNode)
		RETIFNOK5_LIST(<@names.ST_DLLCode/>.set.request.filter(filter))

		|* Get list of components with parent components
		RETIFNOK5_LIST(<@names.ST_DLLCode/>.get.postfilter(postfilter))								
		if postfilter then	
			metaDataForFilter = ${methodName}.getMetaDataForFilter()
		endif
	endif

			</#if><#--if c.componentName == names.businessInterfaceName-->
	CONTINUEIFNOK(${methodName},
			io_${methodName}.ResponseArgument, io_${methodName}.ResultArgument,
			"${names.businessInterfaceName}", "${c.componentName}", false,
			${methodName}.handle.input.${c.componentName}_new(i.Request${c.componentName}Node,
			io_${methodName}.ResponseArgument, io_${methodName}.ResultArgument,
			selection.present))
	if not selection.present then	|* No valid selection attributes found
		return(0)
	endif
	if <@names.ST_DLLCode/>.return.query.status.${c.componentName}() <> QUERY.END then
		RETIFNOK5_LIST(<@names.ST_DLLCode/>.initialize.${c.componentName}.${methodName}())
	endif

	CONTINUEIFNOK(${methodName},
			io_${methodName}.ResponseArgument, io_${methodName}.ResultArgument,
			"${names.businessInterfaceName}", "${c.componentName}", false,
			<@names.ST_DLLCode/>.initialize.set.${c.componentName}.${methodName}())
			<#if c.componentName == names.businessInterfaceName && methodName!="Show" >
	endif
			</#if><#--if c.componentName == names.businessInterfaceName && methodName!="Show" -->
	fetching = true
			<#if c.componentName == names.businessInterfaceName>
	fetch.count = 0
			</#if><#--if c.componentName == names.businessInterfaceName-->
			<#if c.componentName == names.businessInterfaceName && methodName!="Show" >
	while fetching and (fetch.count < max.batch.count or max.batch.count = 0) and
		  (total.object.count < max.number.objects or max.number.objects = 0)
			<#else><#--if c.componentName == names.businessInterfaceName && methodName!="Show" -->
	while fetching
			</#if><#--if c.componentName == names.businessInterfaceName && methodName!="Show" -->
		RETIFNOK5_LIST(
			<@names.ST_DLLCode/>.before.execute.${c.componentName}.${methodName}(o.cancel))
		if o.cancel then
			return(0)
		endif

		RETIFNOK5_LIST(
			<@names.ST_DLLCode/>.execute.${c.componentName}.${methodName}())

		on case <@names.ST_DLLCode/>.return.query.status.${c.componentName}()
		case QUERY.FETCH:
			RETIFNOK5_LIST(
			<#if c.componentName == names.businessInterfaceName>
				${methodName}.handle.output.${c.componentName}_new(0,
			<#else><#--if c.componentName == names.businessInterfaceName-->
				${methodName}.handle.output.${c.componentName}_new(i.Response${c.parentComponentName}Node,
			</#if><#--if c.componentName == names.businessInterfaceName-->
			<#if object.businessInterfaceDefinition.idref.type!="bod" && (methodName == "List" || methodName == "Show")>
				i.suppressNillableAttributes,
			</#if>
				Response.${c.componentName}Node))
	
			<#assign notInitialized = true>
			<#list public.listPublicSubComponents(methodName, c.componentName) as s>
				<#if notInitialized>
			long	tmp.node
			long	positionNode
			tmp.node = xmlNewNode("TempNode")
					<#assign notInitialized = false>
				</#if>
			RETIFNOK5_LIST(Handle.Request.${methodName}.${s.componentName}_new(
				i.Request${c.componentName}Node,
				<#if object.businessInterfaceDefinition.idref.type!="bod" && (methodName == "List" || methodName == "Show")>
				i.suppressNillableAttributes,
				</#if>
				io_${methodName}.ResponseArgument, io_${methodName}.ResultArgument,
				tmp.node,
				o.cancel))
				<@public.generateAnchorReplacement iComponentName=c.componentName iSubComponentName=s.componentName iIndent=false/>
			</#list><#--public.listPublicSubComponents(c.componentName) as s-->
			<#if !notInitialized>
			ret = xmlDelete(tmp.node)
			</#if>
			<#if c.componentName == names.businessInterfaceName>

			RETIFNOK5_LIST(tlbct.dll1000.ApplyPostFilter(
				postfilter,
				filter,
				metaDataForFilter,
				i.Response${names.businessInterfaceName}Node,
				Response.${c.componentName}Node,
				postfilter.valid))
			</#if><#--if c.componentName == names.businessInterfaceName-->
			break
		case QUERY.END:
			fetching = false
			break
		case QUERY.ERROR:
				return (bl.result)
		default:
			|* Should not come here.
			fetching = false
			break
		endcase

		RETIFNOK5_LIST(
			<@names.ST_DLLCode/>.after.execute.${c.componentName}.${methodName}())
		<#if c.componentName == names.businessInterfaceName>
			<#if methodName!="Show" >
		if fetching and postfilter.valid then
			fetch.count = fetch.count + 1
			total.object.count = total.object.count + 1
		endif
			<#else><#--if methodName!="Show" -->
		if fetching then
			fetch.count = fetch.count + 1
		endif
		fetching = false
			</#if><#--if methodName!="Show" -->
		</#if><#--if c.componentName == names.businessInterfaceName-->
	endwhile
	
		<#if c.componentName == names.businessInterfaceName>
			<#if methodName!="Show" >
	if total.object.count = 0 then
		dal.set.warning.message("tlbctsb0006")
		|* No objects found.
	endif
	if iteratorID <> 0 and fetch.count = max.batch.count then
		|* Create ListResponse > ControlArea

		NEW.NODE("ControlArea", 0, ControlArea)
		|* Create the iteratorID element
		ret = xmlRewriteDataElement(ControlArea, "iteratorID",
								 pid)
		|* Insert ControlArea as first child element in the Response
		ret = xmlInsertInChilds(io_${methodName}.responseArgument, ControlArea)
		o.iter_act = true
	else
		o.iter_act = false
			<#else><#--if methodName!="Show" -->
	if fetch.count = 0 then
		| No objects lefts!
		dal.set.error.message("tlbctsb0026")
		|* Object not found.
		return(DALHOOKERROR)
	endif
			</#if><#--if methodName!="Show" -->
		</#if><#--if c.componentName == names.businessInterfaceName-->
		CONTINUEIFNOK(${methodName},
			io_${methodName}.ResponseArgument, io_${methodName}.ResultArgument,
			"${names.businessInterfaceName}", "${c.componentName}", false,
			<@names.ST_DLLCode/>.finalize.set.${c.componentName}.${methodName}())
		<#if c.componentName == names.businessInterfaceName && methodName!="Show" >
	endif
		</#if><#--if c.componentName == names.businessInterfaceName && methodName!="Show" -->
			
	return(0)
}

<@source.performance_point description=methodName+".handle.input."+c.componentName+"_new"/>
function long ${methodName}.handle.input.${c.componentName}_new(
		const xmlNode 	i.Node,
		ref	  xmlNode	o.ResponseArgument,
		ref	  xmlNode	o.resultArgument,
		ref	  boolean	o.selected)
{
	string	value(1024)
	boolean	value.isSet
	boolean tagfound
		<#if c.componentName==names.businessInterfaceName>
	xmlnode BuildNode
	xmlnode FilterNode
	xmlnode	SelectionNode
	xmlnode ControlAreaNode
		</#if><#--if c.componentName==names.businessInterfaceName-->
	xmlNode	l.node
	long	retl
		<#assign AttributeList=[]>
		<#if c.componentName!=names.businessInterfaceName>
			<#list common.listRelationshipDetails(c.parentComponentImplementation, c.relationshipImplementation) as rd>
				<#assign datatype=common.getDatatype(rd.fromAttributeImplementation)>
				<#assign AttributeList=AttributeList+[{
					"componentImplementation":rd.toComponentImplementation, 
					"componentName":rd.toComponentName,
					"attributeImplementation":rd.toAttributeImplementation,
					"attributeName":rd.toAttributeName
					}]>
	domain	${common.getDatatypeName(datatype)} ${rd.fromComponentName}.${rd.fromAttributeName}.value
			</#list><#--list common.listRelationshipDetails(c.parentComponentImplementation, c.relationshipImplementation) as rd-->
		</#if><#-- c.componentName!=names.businessInterfaceName-->

	l.node = i.Node
	value = ""
	value.isSet = false
	tagfound = false
	retl = 0

		<#if methodName == "Show" && c.componentName==names.businessInterfaceName>
			<#list public.listPublicInputAttributes(methodName) as a>
				<#assign AttributeList=AttributeList+[{
					"componentImplementation":a.componentImplementation, 
					"componentName":a.componentName,
					"attributeImplementation":a.attributeImplementation, 
					"attributeName":a.attributeName 
					}]>
	RETIFNOK(getValue(l.node, 
		"${a.componentName}.${a.selectionPath}",
		"<>.<DataArea>.<${a.componentName}>.${a.path}",
				<#if a.componentName==names.businessInterfaceName && a.isMandatory>
		TRUE,
				<#else><#--if a.componentName==names.businessInterfaceName && a.isMandatory-->
		FALSE,
				</#if><#--if a.componentName==names.businessInterfaceName && a.isMandatory-->
		value, tagfound))
	if tagfound then
		RETIFNOK(<@names.SB_DLLCode/>.set.${a.componentName}.${a.attributeName}.Mapper(value))
	endif
			</#list><#--list public.listPublicInputAttributes(methodName) as a-->
		</#if><#--if methodName == "Show" && c.componentName==names.businessInterfaceName-->
		<#if c.componentName=names.businessInterfaceName>

	|* Which attributes have been selected.
	ControlAreaNode = xmlFindFirst("ControlArea", i.Node)
	BuildNode = xmlFindFirst("Selection", ControlAreaNode)
<#--#leakfix.sn-->
	if BuildNode <> 0 then
		|* Avoid modification of Request XML
		BuildNode = xmlDuplicate(BuildNode)
	endif
<#--#leakfix.en-->
	FilterNode = xmlFindFirst("Filter", ControlAreaNode)
	if must.build.selection.node(BuildNode) then
		build.selectionnode.for.public.attributes(BuildNode)
	endif
	InitSelection(false)
	RETIFNOK2_LIST(SetSelection_new1(BuildNode))
<#--#leakfix.sn-->
	retl = xmlDelete(BuildNode)
<#--#leakfix.en-->
		</#if><#--if c.componentName=names.businessInterfaceName-->
		<#list public.listPublicAttributes(methodName, c.componentName) as a>
	if ${a.componentName}.${a.attributeName}.select then
		<@names.ST_DLLCode/>.select.${a.componentName}.${a.attributeName}()
	endif
		</#list><#--list public.listPublicAttributes(methodName, c.componentName) as a-->
	
		<#assign tmp_list=[]>
		<#list AttributeList as at>
			<#list protected.listAttributesUsingAttribute(methodName, at.componentImplementation, at.attributeName, false)?reverse as p1>
				<#assign found=false>
				<#list tmp_list as l>
					<#if l.attributeName==p1.attributeName>
						<#assign found=true>
						<#break>
					</#if>
				</#list>
				<#if !found>
					<#assign tmp_list=[p1]+tmp_list>
				</#if>
			</#list>
		</#list><#--list AttributeList as at-->
		<#list tmp_list as p>
		<#assign dimensions=common.getDimensionsForDataType(common.getMandatoryDatatype(p.attributeImplementation))/>
		<#if (dimensions?number > 1)>
	long	${p.attributeName}.counter
	domain ${common.getDatatypeFromAttributeImplementation(p.attributeImplementation).nativeDatatype}	set.${p.attributeName}(${dimensions})
	boolean	set.${p.attributeName}isSet(${dimensions})
	for ${p.attributeName}.counter = 1 to ${dimensions}
		set.${p.attributeName}(${p.attributeName}.counter) = ${protected.getBaanDataInitializers(p.attributeImplementation)}
		set.${p.attributeName}(${p.attributeName}.counter) = false
	endfor
	retl = <@names.ST_DLLCode/>.set.${p.componentName}.${p.attributeName}(set.${p.attributeName}, set.${p.attributeName}isSet)
		<#else>
	domain ${common.getDatatypeFromAttributeImplementation(p.attributeImplementation).nativeDatatype} set.${p.attributeName}
	set.${p.attributeName} = ${protected.getBaanDataInitializers(p.attributeImplementation)}
	retl = <@names.ST_DLLCode/>.set.${p.componentName}.${(p.attributeName)}(set.${p.attributeName})
		</#if><#--if (dimensions?number > 1)-->
		</#list>

	o.selected = false
		<#list public.listPublicAttributes(methodName, c.componentName) as a>
		or ${a.componentName}.${a.attributeName}.select
		</#list><#--list public.listPublicAttributes(methodName, c.componentName) as a-->

	if not o.selected then
		return(0)
	endif

		<#if c.componentName!=names.businessInterfaceName>
			<#list common.listRelationshipDetails(c.parentComponentImplementation, c.relationshipImplementation) as rd>
	RETIFNOK(<@names.ST_DLLCode/>.get.${rd.fromComponentName}.${rd.fromAttributeName}(
			${rd.fromComponentName}.${rd.fromAttributeName}.value))
	RETIFNOK(<@names.ST_DLLCode/>.set.${rd.toComponentName}.${rd.toAttributeName}(
			${rd.fromComponentName}.${rd.fromAttributeName}.value))
	<@names.ST_DLLCode/>.select.${rd.toComponentName}.${rd.toAttributeName}()
			</#list><#--list common.listRelationshipDetails(c.parentComponentImplementation, c.relationshipImplementation) as rd-->
		</#if><#-- c.componentName!=names.businessInterfaceName-->
		<#list public.listIdentifiers(methodName, c.componentName) as a>
	${a.componentName}.${a.attributeName}.select = true
	<@names.ST_DLLCode/>.select.${a.componentName}.${a.attributeName}()
		</#list><#--list public.listPublicAttributes(methodName, c.componentName) as a-->
		<#list protected.listInternalIdentifiers(c.componentName) as pi>
	<@names.ST_DLLCode/>.select.${pi.componentName}.${pi.attributeName}()
		</#list>
		
		<#-- add identifiers -->
<#list public.listPublicAttributes(methodName, c.componentName) as a>
	<#if a.attributeImplementation.attribute.idref.type == "qualifier" && 
		 !a.attributeImplementation.attribute.idref.parentAttribute?exists>
	${a.componentName}.${a.attributeName}.select = true
	<@names.ST_DLLCode/>.select.${a.componentName}.${a.attributeName}()
	</#if>
</#list>
	return(0)
}

<@source.performance_point description=methodName+".handle.output."+c.componentName+"_new"/>
function long ${methodName}.handle.output.${c.componentName}_new(
				xmlnode	i.Node,
		<#if object.businessInterfaceDefinition.idref.type!="bod" && (methodName == "List" || methodName == "Show")>
				boolean	i.suppressNillableAttributes,
		</#if>
			ref	xmlnode	o.Node)
{
	string	value(1024)
	boolean	value.isSet
	long	ret
	<#-- used in macro create_output -->
	long	g.node
		<#if public.hasPublicOutputTextArgumentsByComponent(methodName, c.componentName)>
	xmlnode	text.Node
		</#if><#--if public.hasPublicOutputTextArgumentsByComponent(methodName, c.componentName) -->

	value = ""
	value.isSet = false
	ret = 0
	<#-- used in macro create_output -->
	g.node = 0
	NEW.NODE("${c.componentPublicName}", i.Node, o.Node)

		<#assign attrList="">
		<#assign level=0>
		<@generate_component_anchors iMethodName=methodName iLocation="before" iComponent=c.componentImplementation.component.idref/>
		<#list c.componentImplementation.component.idref.attribute as attr>
			<#if !attr.isHidden>
<@create_output iattr=attr comp=c.componentImplementation.component.idref compName=c.componentName methodName=methodName isQuery=true/>
			<#else>
				<@create_output_anchors_only iattr=attr comp=c.componentImplementation.component.idref/>
			</#if>
		</#list>
		<@generate_component_anchors iMethodName=methodName iLocation="after" iComponent=c.componentImplementation.component.idref/>

	return(0)
}

		</#list>
	</#if>
</#list><#-- object.methodImplementation as methodImplementation-->