<#import "common.ftl" as common/>  
<#--
     Generate the type-safe Java proxy for a BusinessInterfaceDefinition
     - object is an instance of BusinessInterfaceDefinition
     - properties is a HashMap generated by PropertiesHelper.getInstance() - it provides access to the
       named GeneratorProperty elements in ApplicationControlData.
  -->
<@common.copyright/>
 *  Component Name: ${object.name?cap_first}
 * <#if object.versionID?has_content> Component Version: ${object.versionID}</#if>
 * <#if object.description?has_content> Component Description: ${object.description}</#if>
 *  BID: ${common.getBIDName(object)}
 *****************************************************/
<#assign bdeName>
	<@common.toName name="${object.name?cap_first}"/>
</#assign>
<#assign noOfAttrs=0>
<#assign noOfPKeys=0>
<#list object.attribute as attribute>
	<#if !attribute.isHidden && attribute.isIdentifier>
		<#assign noOfPKeys = noOfPKeys + 1>
	</#if>
	<#if !attribute.isHidden && !common.isUsedInComplexOrGroup(object, attribute)>	
		<#assign noOfAttrs = noOfAttrs + 1>
	</#if>		
</#list>
<#if properties.javaPackage?has_content>package ${properties.javaPackage};</#if>

import com.ssaglobal.bdeimpl.java.BusinessDataEntity;
import com.ssaglobal.bdeimpl.java.BdeIterator;
import com.ssaglobal.bde.BdeException;
import com.ssaglobal.bde.Date;
import com.ssaglobal.bde.DateTime;
import com.ssaglobal.bde.Time;
import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * Generated typesafe BDE component proxy ${object.name?cap_first}.
 * <br>This ${object.name?cap_first} Bde Proxy extends com.ssaglobal.bdeimpl.java.BusinessDataEntity.<br>
 * Identifying attributes: ${common.identifyingAttributes(object)}<br>
 * Mandatory attributes  : ${common.mandatoryAttributes(object)}<br>
 * Component Name: ${object.name?cap_first}<br>
 * <#if object.versionID?has_content>Component Version: ${object.versionID}<br></#if>
 * <#if object.description?has_content>Component Description: ${object.description}<br></#if>
 */
public class ${bdeName} extends BusinessDataEntity
{
<#if common.getBIDName(object)=object.name>
	/**
	 * Constructor for creating a 'empty' ${bdeName}.
	 * <br>Depending on the action you want to perform identifying or mandatory attributes may be required.
	 * This can be used if a ${bdeName} can be created without identifying attributes (created by backend)
	 */
	public ${bdeName}()
	{
		super(s_metaData[0][ITEM_NAME]);
	}
<#else>
	/* package private */ ${bdeName}()
	{
		super(s_metaData[0][ITEM_NAME]);
	}
</#if>

<#if common.countPrimKeys(object) != 0 >	
	/** 
	 * Create ${bdeName} with identifying attributes.
	 */
	public ${bdeName}(${common.primkeys_types_and_names(object)}) 
		throws BdeException
	{
		super(s_metaData[0][ITEM_NAME]);	
		<#list object.attribute as attribute>		
			<#if attribute.isIdentifier && !attribute.isHidden && !common.isUsedInComplexOrGroup(object, attribute)>
		set${attribute.name?cap_first}(p_${attribute.name});
			</#if>
		</#list>
	}
	
</#if>
<#if object.relationship?has_content>
<#list object.relationship as relations>	
	<#assign subComponent = common.getSubComponent(relations)>
	<#assign relationComponentName = subComponent.name?cap_first >	
	<#if relations.type = "aggregation">
		<#if !subComponent.isHidden>
	/**
	 * List ${relationComponentName} currently present in this local ${bdeName}.
	 * @return ${relationComponentName}Iterator
	 * @throws BdeException If an error occurs	 
	 */
	public ${relationComponentName}Iterator list${relationComponentName}() throws BdeException		
	{
		return (${relationComponentName}Iterator) listComponents(${noOfAttrs+relations_index+1});
	}

	/**
	 * Add a new ${relationComponentName} to this local ${bdeName} for create.
	 * <br>The method arguments are the identifying attributes for this new ${relationComponentName}.
	 * The mandatory attributes are not filled.
	 * Executing the Change on the ${common.getBIDName(object)} does the actual create of this ${relationComponentName} on the backend.
	 * @return ${bdeName} proxy object, which can be used to set more (mandatory) attributes.
	 * @throws BdeException If an error occurs	 
	 */
	public ${relationComponentName} create${relationComponentName}(${common.primkeys_types_and_names(subComponent)}) throws BdeException		
	{
		final ${relationComponentName} newComponent = new ${relationComponentName}(${common.primkeys_names(subComponent)});
		super.createComponent((BusinessDataEntity) newComponent);
		return newComponent;
	}
	
	/**
	 * Add a new ${relationComponentName} to this local ${bdeName} for create.
	 * <br>The identifying and mandatory attributes are not filled.
	 * Executing the Change on the ${common.getBIDName(object)} does the actual create of this ${relationComponentName} on the backend.
	 * @return ${bdeName} proxy object, which can be used to set mandatory or identifying attributes.
	 * @throws BdeException If an error occurs	 
	 */
	public ${relationComponentName} create${relationComponentName}() throws BdeException		
	{
		final ${relationComponentName} newComponent = new ${relationComponentName}();
		super.createComponent((BusinessDataEntity) newComponent);
		return newComponent;
	}
	
	/**
	 * Add a new ${relationComponentName} to this local ${bdeName} for change.
	 * <br>Executing the Change on the ${common.getBIDName(object)} does the actual change of this ${relationComponentName} on the backend.
	 * @return ${bdeName} proxy object, which can be used to set more (mandatory or identifying) attributes.
	 * @throws BdeException If an error occurs	 
	 */
	public ${relationComponentName} change${relationComponentName}(${common.primkeys_types_and_names(subComponent)}) throws BdeException		
	{
		final ${relationComponentName} newComponent = new ${relationComponentName}(${common.primkeys_names(subComponent)});
		super.changeComponent((BusinessDataEntity) newComponent);
		return newComponent;
	}
	
	/**
	 * Add a new ${relationComponentName} to this local ${bdeName} for delete.
	 * <br>The new ${relationComponentName} is based on the identifying attributes.
	 * Executing the Change on the ${common.getBIDName(object)} does the actual delete of this ${relationComponentName} on the backend.	 
	 * @throws BdeException If an error occurs	 
	 */
	public void delete${relationComponentName}(${common.primkeys_types_and_names(subComponent)}) throws BdeException		
	{
		final ${relationComponentName} newComponent = new ${relationComponentName}(${common.primkeys_names(subComponent)});
		super.deleteComponent((BusinessDataEntity) newComponent);
	}

	/**
	 * Add a ${relationComponentName} to this local ${bdeName} for delete.
	 * <br>Executing the Change on the ${common.getBIDName(object)} does the actual delete of this ${relationComponentName} on the backend.	 
	 * @throws BdeException If an error occurs	 
	 */
	public void delete${relationComponentName}(${relationComponentName} componentToDelete) throws BdeException		
	{
		super.deleteExistingComponent(componentToDelete);
	}

		</#if>
	<#else>
	// found associated component '${relationComponentName}' not generating code for associations.
	
	</#if>
</#list>
</#if>
<#--
	This #list loop below generates the following methods: 
	get<Component>
	set<Component>
	has<Component>
	unset<Component>

-->
<#assign attributeIndex=0>
<#list object.attribute as attribute>
	<#if !attribute.isHidden && !common.isUsedInComplexOrGroup(object, attribute)>
		<#assign attributeIndex	= attributeIndex+1/>
		<#switch attribute.type>
		  	<#case "simple">
				<#assign dataType = common.getjavadt(attribute.datatype.idref.primitiveDatatype)>		  	
			<#break>
			<#case "group">
				<#assign dataType = attribute.name?cap_first>
			<#break>
			<#case "complex">
				<#assign dataType = attribute.datatype.idref.name?cap_first>			
			<#break>	
		</#switch>  
	/** 
	 * Get <@common.printAttributeInfo _attr=attribute/>
	 * <@common.printAttributeDescription _attr=attribute/>
	 */	
	public ${dataType} get${attribute.name?cap_first}() throws BdeException	
   	{
		<#if attribute.type = "simple">
		return super.get${dataType?cap_first}Value(${attributeIndex});
		<#else>
		${dataType} new${dataType} = new ${dataType}();
		super.getComplexValue(${attributeIndex}, new${dataType});
		return new${dataType};
		</#if>
   	}
    
		<#if attribute.isIdentifier>
   	/*package private */ void set${attribute.name?cap_first}(${dataType} value) throws BdeException		
   	{
		<#if attribute.type = "simple">
		super.set${dataType?cap_first}Value(${attributeIndex}, value);
		<#else>
		super.setComplexValue(${attributeIndex}, value);
		</#if>
   	}

		<#else>
	/** 
	 * Set <@common.printAttributeInfo _attr=attribute/>
	 * <@common.printAttributeDescription _attr=attribute/>
	 */	
	public void set${attribute.name?cap_first}(${dataType} value) throws BdeException		
   	{
		<#if attribute.type = "simple">
		super.set${dataType?cap_first}Value(${attributeIndex}, value);
		<#else>
		super.setComplexValue(${attributeIndex}, value);
		</#if>
   	}
   		
	/** 
	 * Has <@common.printAttributeInfo _attr=attribute/>
	 * <@common.printAttributeDescription _attr=attribute/>
	 */	
	public boolean has${attribute.name?cap_first}()
   	{
		return super.hasValue(${attributeIndex});
   	}	
   	
	/** 
	 * Unset <@common.printAttributeInfo _attr=attribute/>
	 * <@common.printAttributeDescription _attr=attribute/>
	 */	
	public void unset${attribute.name?cap_first}()
   	{
       	super.unsetValue(${attributeIndex});
   	}	  

		</#if>       
	</#if>       
</#list>
	private static final String [][] s_metaData = 
	{
<#--
	This #list loop below creates the following:
	{"<BDEName>"},
	,{"<FieldName>", TYPE_<dataTypeUPPERCASE>,"<FacetInformation>"}
	etc...
-->		
		{"${object.name}"}
<#list object.attribute as attribute>		
	<#if !attribute.isHidden && !common.isUsedInComplexOrGroup(object, attribute)>
		<#switch attribute.type>
		  	<#case "simple">
		,{"${attribute.name}", TYPE_${attribute.datatype.idref.primitiveDatatype?upper_case}<#list attribute.datatype.idref.facet as facet><#if facet_index = 0>,</#if><#break></#list><#list attribute.datatype.idref.facet as facet>"${facet.type?j_string}:${facet.value?j_string}"<#if facet_has_next>,</#if></#list>}
			<#break>
			<#case "group">
		,{"${attribute.name}", TYPE_GROUP}
			<#break>
			<#case "complex">
		,{"${attribute.name}", TYPE_COMPLEX}
			<#break>	
		</#switch>  
	</#if>
</#list>
	<#if object.relationship?has_content>
		<#list object.relationship as relations>
			<#if relations.type = "aggregation">
				<#assign subComponent = common.getSubComponent(relations)>
				<#if !subComponent.isHidden>
		,{"${subComponent.name}", TYPE_COMPONENT}
				</#if>
			</#if>
		</#list>
	</#if>
	};
	
	protected String getMetaData(int ItemIndex, int ProperyIndex)
	{
		try
        {
            return s_metaData[ItemIndex][ProperyIndex];
        }
        catch (Exception e) 
        {
            return null;
       	}
	}

<#assign attributeIndex=0>
	<@GenerateAttributeIdentifier sName="_ALL_"  sValue="${object.name}.*"/>
<#list object.attribute as attribute>	
	<#if !attribute.isHidden && !common.isUsedInComplexOrGroup(object, attribute)>
		<#switch attribute.type>
			<#case "simple">
			<@GenerateAttributeIdentifier sName="${attribute.name}"  sValue="${object.name}.${attribute.name}"/>
				<#break>
			<#case "group">			
			<#case "complex">
			<@createComplexAttributeIdentifiers iCompleteName="" iAttribute=attribute iAttributeRelation=""/>
				<#break>
		</#switch>  
	</#if>
</#list>

	protected BdeIterator _createComponentIterator(int index)
	{
		<#if object.relationship?has_content>
		switch (index)
		{
		<#list object.relationship as relations>
			<#if relations.type = "aggregation">
				<#assign subComponent = common.getSubComponent(relations)>
				<#if !subComponent.isHidden>
			case(${noOfAttrs+1+relations_index}):
				return new ${subComponent.name}Iterator();
				</#if>
			</#if>		
		</#list>
		}
		</#if>
		return null;
	}
	
	/*package private */ static String _getPrimaryKeys(String sParentName)
	{
		final StringBuffer sb = new StringBuffer();

<#list object.attribute as attribute>
	<#if !attribute.isHidden && attribute.isIdentifier>
		<#switch attribute.type>
			<#case "simple">
		sb.append(sParentName + ${attribute.name?upper_case} + ";");
				<#break>
		</#switch>  
	</#if>
</#list>		
<#if object.relationship?has_content>
	<#list object.relationship as relations>
		<#if relations.type = "aggregation">
			<#assign subComponent = common.getSubComponent(relations)>
			<#if !subComponent.isHidden>
		sb.append(${subComponent.name}._getPrimaryKeys(s_metaData[0][ITEM_NAME] + "."));
			</#if>
		</#if>		
	</#list>
</#if>
		return sb.toString();
	}
}
<#macro createComplexAttributeIdentifiers iCompleteName iAttribute iAttributeRelation>
	<#if !iAttribute.isHidden>
	<#switch iAttribute.type>
		<#case "simple">
		<@GenerateAttributeIdentifier sName = "${iCompleteName}${iAttributeRelation.name}" sValue="${object.name}.${iCompleteName}${iAttributeRelation.name}"/>
			<#break>
		<#case "group">
		<@GenerateAttributeIdentifier sName = "${iCompleteName}${iAttribute.name}._ALL_" sValue="${object.name}${iCompleteName}.${iAttribute.name}.*"/>		
			<#list iAttribute.attributeRelationship as ar>
 				<@createComplexAttributeIdentifiers iCompleteName = iCompleteName+iAttribute.name+"." iAttribute=ar.attribute.idref iAttributeRelation=ar/>
			</#list>
			<#break>
		<#case "complex">		
				<#if iAttributeRelation?has_content>
		<@GenerateAttributeIdentifier sName = "${iCompleteName}${iAttributeRelation.name}._ALL_" sValue="${object.name}.${iCompleteName}${iAttributeRelation.name}.*"/>						
				<#else>
		<@GenerateAttributeIdentifier sName = "${iCompleteName}${iAttribute.name}._ALL_" sValue="${object.name}.${iCompleteName}${iAttribute.name}.*"/>		
				</#if>
			<#list iAttribute.attributeRelationship as ar>
				<#if iAttributeRelation?has_content>
					<@createComplexAttributeIdentifiers iCompleteName = iCompleteName+iAttributeRelation.name+"." iAttribute=ar.attribute.idref iAttributeRelation=ar/>
				<#else>
					<@createComplexAttributeIdentifiers iCompleteName = iCompleteName+iAttribute.name+"." iAttribute=ar.attribute.idref iAttributeRelation=ar/>				
				</#if>
			</#list>
			<#break>
	</#switch>
	</#if>
</#macro>
<#macro GenerateAttributeIdentifier sName sValue>
	public static final String ${sName?upper_case?replace(".","_")}  = "${sValue}";
</#macro>