## Copyright 2001-2005 The Apache Software Foundation.
##
## Licensed under the Apache License, Version 2.0 (the "License")
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
##     http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.

##
## convert object into bean and vice versa
##
  #set ( $beanClassName = "${table.JavaName}${beanSuffix}" )
    /**
     * Creates a $beanClassName with the contents of this object
  #if ($complexObjectModel)
     * This also creates beans for cached related objects, if they exist
  #end
     * @return a $beanClassName with the contents of this object
     */
    public $beanClassName getBean()
    {
        return getBean(new IdentityMap());
    }

    /**
     * Creates a $beanClassName with the contents of this object
     * intended for internal use only
     * @param createdBeans a IdentityMap which maps objects
     *        to already created beans
     * @return a $beanClassName with the contents of this object
     */
    public $beanClassName getBean(IdentityMap createdBeans)
    {
        ${beanClassName} result = ($beanClassName) createdBeans.get(this);
        if (result != null ) {
            // we have already created a bean for this object, return it
            return result;
        }
        // no bean exists for this object; create a new one
        result = new ${beanClassName}();
        createdBeans.put(this, result);

  #foreach ($col in $table.Columns)
        result.${col.SetterName}(${col.GetterName}());
  #end

  #if ($complexObjectModel)
    ## foreign references to this Object
    #if ($objectIsCaching)
      #foreach ($fk in $table.Referrers)
        #set ( $tblFK = $fk.Table )
        #if ( !($tblFK.Name.equals($table.Name)) )
          #set ( $relatedClassName = ${tblFK.JavaName} )
          #set ( $relatedBeanClassName = "${relatedClassName}${beanSuffix}" )
          #set ( $relatedByCol = "" )
          #foreach ($columnName in $fk.LocalColumns)
            #set ( $column = $tblFK.getColumn($columnName) )
            #if ($column.isMultipleFK())
              #set ($relatedByCol= "$relatedByCol$column.JavaName")
            #end
          #end

          #if ($relatedByCol == "")
            #set ( $relCol = "${relatedClassName}s" )
            #set ( $beanRelCol = "${relatedBeanClassName}s" )
          #else
            #set ( $relCol= "${relatedClassName}sRelatedBy$relatedByCol" )
            #set ( $beanRelCol = "${relatedBeanClassName}sRelatedBy$relatedByCol" )
          #end

          #set ( $collName = "coll$relCol" )
        if ($collName != null)
        {
            List<${relatedBeanClassName}> relatedBeans = new ArrayList<${relatedBeanClassName}>(${collName}.size());
            for (Iterator ${collName}It = ${collName}.iterator(); ${collName}It.hasNext(); )
            {
                $relatedClassName related = ($relatedClassName) ${collName}It.next();
                ${relatedBeanClassName} relatedBean = related.getBean(createdBeans);
                relatedBeans.add(relatedBean);
            }
            result.set${beanRelCol}(relatedBeans);
        }
        #end
      #end
    #end

    ## foreign keys in this object
    #foreach ($fk in $table.ForeignKeys)

      #set ( $tblFK = $table.Database.getTable($fk.ForeignTableName) )
      #set ( $relatedClassName = "${tblFK.JavaName}" )
      #set ( $relatedBeanClassName = "${relatedClassName}${beanSuffix}" )

      #set ( $relCol = "" )
      #foreach ($columnName in $fk.LocalColumns)
        #set ( $column = $table.getColumn($columnName) )
        #if ($column.isMultipleFK() || $fk.ForeignTableName.equals($table.Name))
          #set ( $relCol = "$relCol$column.JavaName" )
        #end
      #end
      #if ($relCol != "")
        #set ( $relCol = "RelatedBy$relCol" )
      #end

      #set ( $beanMethodSuffix = "${relatedBeanClassName}${relCol}" )
      #set ( $objectMethodSuffix = "${relatedClassName}${relCol}" )
      #set ( $varName = "a$relatedClassName$relCol" )
        if ($varName != null)
        {
            ${relatedBeanClassName} relatedBean = ${varName}.getBean(createdBeans);
            result.set${beanMethodSuffix}(relatedBean);
        }
    #end
  #end
        result.setModified(isModified());
        result.setNew(isNew());
        return result;
    }

    /**
     * Creates an instance of ${table.JavaName} with the contents
     * of a $beanClassName.
     * This behaviour could have also been achieved using a constructor,
     * however as this class is abstract no constructors are allowed
     * @param bean the $beanClassName which contents are used to create
     *        the resulting class
     * @return an instance of ${table.JavaName} with the contents of bean
     */
    public static ${table.JavaName} create${table.JavaName}(${beanClassName} bean)
        throws TorqueException
    {
        return create${table.JavaName}(bean, new IdentityMap());
    }

    /**
     * Creates an instance of ${table.JavaName} with the contents
     * of a $beanClassName.
     * This behaviour could have also been achieved using a constructor,
     * however as this class is abstract no constructors are allowed.
     *
     * This method is intended for internal use only.
     * @param bean the $beanClassName which contents are used to create
     *        the resulting class
     * @param createdObjects a IdentityMap which maps beans
     *        to already created objects
     * @return an instance of ${table.JavaName} with the contents of bean
     */

    public static ${table.JavaName} create${table.JavaName}(${beanClassName} bean, IdentityMap createdObjects)
        throws TorqueException
    {
        ${table.JavaName} result = (${table.JavaName}) createdObjects.get(bean);
        if (result != null)
        {
            // we already have an object for the bean, return it
            return result;
        }
        result = new ${table.JavaName}();
        createdObjects.put(bean, result);

  #foreach ($col in $table.Columns)
        result.${col.SetterName}(bean.${col.GetterName}());
  #end

  #if ($complexObjectModel)
    ## foreign references to this Object
    #if ($objectIsCaching)
      #foreach ($fk in $table.Referrers)
        #set ( $tblFK = $fk.Table )
        #if ( !($tblFK.Name.equals($table.Name)) )
          #set ( $relatedClassName = ${tblFK.JavaName} )
          #set ( $relatedBeanClassName = "${relatedClassName}${beanSuffix}" )
          #set ( $relatedByCol = "" )
          #foreach ($columnName in $fk.LocalColumns)
            #set ( $column = $tblFK.getColumn($columnName) )
            #if ($column.isMultipleFK())
              #set ($relatedByCol= "$relatedByCol$column.JavaName")
            #end
          #end

          #if ($relatedByCol == "")
            #set ( $relCol = "${relatedClassName}s" )
            #set ( $beanRelCol = "${relatedBeanClassName}s" )
          #else
            #set ( $relCol= "${relatedClassName}sRelatedBy$relatedByCol" )
            #set ( $beanRelCol = "${relatedBeanClassName}sRelatedBy$relatedByCol" )
          #end

        {
            List relatedBeans = bean.get${beanRelCol}();
            if (relatedBeans != null)
            {
                for (Iterator relatedBeansIt = relatedBeans.iterator(); relatedBeansIt.hasNext(); )
                {
                    $relatedBeanClassName relatedBean = ($relatedBeanClassName) relatedBeansIt.next();
                    ${relatedClassName} related = ${relatedClassName}.create${relatedClassName}(relatedBean, createdObjects);
                    result.add${relatedClassName}FromBean(related);
                }
            }
        }
        #end
      #end
    #end

    ## foreign keys in this object
    #foreach ($fk in $table.ForeignKeys)

      #set ( $tblFK = $table.Database.getTable($fk.ForeignTableName) )
      #set ( $className = "${tblFK.JavaName}" )
      #set ( $beanClassName = "${className}${beanSuffix}" )

      #set ( $relCol = "" )
      #foreach ($columnName in $fk.LocalColumns)
        #set ( $column = $table.getColumn($columnName) )
        #if ($column.isMultipleFK() || $fk.ForeignTableName.equals($table.Name))
          #set ( $relCol = "$relCol$column.JavaName" )
        #end
      #end
      #if ($relCol != "")
        #set ( $relCol = "RelatedBy$relCol" )
      #end

      #set ( $beanMethodSuffix = "${beanClassName}${relCol}" )
      #set ( $objectMethodSuffix = "${className}${relCol}" )
        {
            ${beanClassName} relatedBean = bean.get${beanMethodSuffix}();
            if (relatedBean != null)
            {
                ${className} relatedObject = ${className}.create${className}(relatedBean, createdObjects);
                result.set${objectMethodSuffix}(relatedObject);
            }
        }
    #end
  #end
    result.setModified(bean.isModified());
    result.setNew(bean.isNew());
  	return result;
    }

  #if ($complexObjectModel)
    ## foreign references to this Object
    #if ($objectIsCaching)
      #foreach ($fk in $table.Referrers)
        #set ( $tblFK = $fk.Table )
        #if ( !($tblFK.Name.equals($table.Name)) )
          #set ( $className = $tblFK.JavaName )
          #set ( $relatedByCol = "" )
          #foreach ($columnName in $fk.LocalColumns)
            #set ( $column = $tblFK.getColumn($columnName) )
            #if ($column.isMultipleFK())
              #set ($relatedByCol= "$relatedByCol$column.JavaName")
            #end
          #end

          #if ($relatedByCol == "")
            #set ( $relCol = "${className}s" )
            #set ( $relColMs = $className )
          #else
            #set ( $relCol= "${className}sRelatedBy$relatedByCol" )
            #set ( $relColMs= "${className}RelatedBy$relatedByCol" )
          #end
          #set ( $collName = "coll$relCol" )

    /**
     * Method called to associate a $tblFK.JavaName object to this object.
     * through the $className foreign key attribute
     *
     * @param toAdd $className
     */
    protected void add${relColMs}FromBean($className toAdd)
    {
        init${relCol}();
        ${collName}.add(toAdd);
    }
        #end
      #end
    #end
  #end
