All Downloads are FREE. Search and download functionalities are using the official Maven repository.

vlc.coxb-bind-choice.vm Maven / Gradle / Ivy

The newest version!
#**
Choice binding class Velocity Template.
@author Fady
@version 1.2
*#
## ==================================================================
## Choice element binding code
##
#parse("vlc/coxb-bind-common-imports.vm")
package $coxbContext.getCoxbPackageName();

import com.legstar.coxb.ICobolBinding;
import com.legstar.coxb.common.CChoiceBinding;
#foreach($importType in $importTypes)
import ${importType};
#end
#if (!$importTypes.contains("com.legstar.coxb.ICobolComplexBinding"))
import com.legstar.coxb.ICobolComplexBinding;
#end
import com.legstar.coxb.host.HostException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

## Determine the value Object type
#set ($parentObjectType = $coxbHelper.getBoundTypeName($binding))
import ${binding.getParentBinding().getValueObjectClassName()};

/**
 * LegStar Binding for Choice element:
#foreach ($child in $binding.alternativesList)
 *   ${child.jaxbName}
#end
 * 
 * Represents a choice between 2 or more elements. A choice maps to a cobol
 * REDEFINES clause exposed as an xs:choice in the corresponding XML schema
 * 
 * This class was generated by ${generatorName}.
 */
public class $binding-class-name 
             extends CChoiceBinding {

#foreach ($child in $binding.alternativesList)
#set ($childObjectType = $coxbHelper.getBoundTypeName($child))
#if($child.isBound())
    /** Alternative binding bound to value object property ${child.jaxbName}($childObjectType). */
#else
    /** Unbound Alternative. */
#end
    public $coxbHelper.getBindingInterfaceName($child) $coxbHelper.getFieldName($child);
#if($coxbHelper.getGenericType($child).equals("complexArray"))
    public ICobolComplexBinding $coxbHelper.getFieldName($child)Item;
#end
#end

    /** Logger. */
    private final Log _log = LogFactory.getLog(getClass());

#if(${hasSimpleChilds} == true)
    /** Binding factory. */
    private static final ICobolBindingFactory BF
        = CobolBindingFactory.getBindingFactory();
#end    
    
    /**
     * Constructor for a Choice element.
     * 
     * @param bindingName the identifier for this binding
     * @param parentBinding a reference to the parent binding
     */
    public ${binding-class-name}(
            final String bindingName,
            final ICobolComplexBinding parentBinding) {
        
        super(bindingName, null, parentBinding);
#if(${helper.isEmpty("${binding.getMarshalChoiceStrategyClassName()}")} == false)
        setMarshalChoiceStrategyClassName(
            "${binding.getMarshalChoiceStrategyClassName()}");
#end
#if(${helper.isEmpty("${binding.getUnmarshalChoiceStrategyClassName()}")} == false)
        setUnmarshalChoiceStrategyClassName(
            "${binding.getUnmarshalChoiceStrategyClassName()}");
#end
        initAlternatives();
    }

    /** Creates a binding property for each alternative. */
    private void initAlternatives() {
        if (_log.isDebugEnabled()) {
            _log.debug("Initializing started");
        }
        /* Create binding alternatives instances */
#set($parent-binding = "getParentBinding()")
#set($childrenList = $binding.alternativesList)
#parse("vlc/coxb-bind-init-children.vm")

        /* Add alternatives to alternatives list */
#foreach ($child in $binding.alternativesList)
        addAlternative(${coxbHelper.getFieldName($child)});
#end ##foreach

        if (_log.isDebugEnabled()) {
            _log.debug("Initializing successful");
        }
    }
 
    /** {@inheritDoc} */
    public void setAlternativesValues() throws HostException {
        Object value;
#foreach ($child in $binding.alternativesList)
#if($child.isBound())
        value = get${parentObjectType}().get${child.jaxbName}();
        if (_log.isDebugEnabled()) {
            _log.debug("Getting value from Value object property "
                    + "${child.jaxbName}"
                    + " value=" + value);
        }
        ${coxbHelper.getFieldName($child)}.setObjectValue(value);
#end
#end

    }
    /** {@inheritDoc} */
    public void setPropertyValue(
            final int index) throws HostException {
        /* Set the Value object property value from binding object */
        ICobolBinding alt = getAlternativesList().get(index);

      /* Alternatives that are not bound to a value object property are ignored.
       * This includes Choices and dynamically generated counters
       * for instance.  */
        if (!alt.isBound()) {
            return;
        }

        Object bindingValue = alt.getObjectValue(alt.getJaxbType());
        if (_log.isDebugEnabled()) {
            _log.debug("Setting value of Value object property "
                    + alt.getJaxbName()
                    + " value=" + bindingValue);
        }
        switch (index) {
#set($counter = 0)
#foreach ($child in $binding.alternativesList)
#set ($childObjectType = $coxbHelper.getBoundTypeName($child))
        case ${counter}:
#if($child.isBound())
#if(${coxbHelper.isArray($child)})
            get${parentObjectType}().${coxbHelper.getterMethodName($child)}().clear();
            get${parentObjectType}().${coxbHelper.getterMethodName($child)}().addAll(
                (List < $childObjectType >) bindingValue);
#else
            get${parentObjectType}().${coxbHelper.setterMethodName($child)}(
                ($childObjectType) bindingValue);
#end
#end
            break;
#set($counter = $counter + 1)
#end
        default:
            break;
        }
    }
    
    /** {@inheritDoc} */
    public Object getObjectValue(
            final Class < ? >  type) throws HostException {
        throw new HostException("Attempt to get value from choice binding "
                + getCobolName());
    }

    /** {@inheritDoc} */
    public void setObjectValue(final Object value) throws HostException {
        throw new HostException("Attempt to set value for choice binding "
                + getCobolName());
    }
    
    /** {@inheritDoc} */
    public boolean isSet() {
        /* A Choice is considered set if at least one of its alternatives
         * is set. */
        for (ICobolBinding alt : getAlternativesList()) {
            if (alt.isSet()) {
                return true;
            }
        }
        return false;
    }

    /** {@inheritDoc} */
    public Object getParentValueObject() throws HostException {
        return (($coxbHelper.getCoxbTypeName($binding.getParentBinding())) getParentBinding()).get${parentObjectType}();
    }

    /**
     * @return the Value object bound to the parent of this choice
     * @throws HostException if parent bound Value object cannot be retrieved
     */
    public ${parentObjectType} get${parentObjectType}() throws HostException {
        return (${parentObjectType}) getParentValueObject();
    }
    
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy