package ${entity.getPackageName()};

import java.io.*;
import java.util.*;

import org.apache.commons.lang.builder.*;
import org.asyrinx.joey.entity.Orphanable;

#foreach ($importFqn in ${entity.getImports()})
import ${importFqn};
#end

/** 
 * ${entityBasePrefix}${entity.name}${entityBaseSuffix} [ ${entity.getLabel()} ]
#if ($addTimeStamp)
 * generated by joey-gen on $now
#end
 */
public class ${entityBasePrefix}${entity.name}${entityBaseSuffix} 
#if( ${entity.getSuperClass()} )
	extends ${entity.getSuperClass().getName()} {
#else
	implements Serializable, Orphanable {
#end

$generator.parse("om/entity/entity_base_constructor.vm", "")
	
    /* --------------------Properties -------------------- */
$generator.parse("om/entity/entity_base_properties.vm", "")

    /* --------------------Referring Objects -------------------- */
## ---------------------- Referring Object ( own foreignKey ) ---------------------- 
#foreach( $reference in ${entity.getReferences().toList()})
$generator.parse("om/entity/entity_base_referring.vm", "", "reference", $reference)
#end

    /* --------------------Referred Objects -------------------- */
## ---------------------- Referred Object ( own foreignKey ) ---------------------- 
#foreach( $reference in ${entity.getReferreds()})
$generator.parse("om/entity/entity_base_referred.vm", "", "reference", $reference)
#end

    /**
	 * cut links to other entities
	 */
	public void beOrphaned() {
        if (!(this instanceof ${entity.name}))
            return;
#foreach( $reference in ${entity.getReferences().toList()})
#set( $foreignClass = ${reference.getReferenceClass()} )
#set( $refPropName = ${reference.getPropertyNameInLocal()} )
#set( $paramName = ${reference.getPropertyNameInReferred( false )} )
#set( $paramNameCapitalized = ${stringUtils.capitalize( $paramName )} )
        if (this.${refPropName} != null)
            this.${refPropName}.remove${paramNameCapitalized}((${entity.name}) this);
#end			
#foreach( $reference in ${entity.getReferences().toList()})
#set( $foreignClass = ${reference.getReferenceClass()} )
#set( $refPropName = ${reference.getPropertyNameInLocal()} )
#set( $refPropNameCapitalized = ${stringUtils.capitalize( $refPropName )} )
	    this.set${refPropNameCapitalized}(null);
#end
    }
## ---------------------- primaryKey ---------------------- 
    /**
     * returns an id that differentiates this object from others
     * of its class.
     */
    public Serializable getPrimaryKey() {
#if (${entity.PrimaryKey.size()} == 1)
	#set( $pkProperty = ${entity.getPrimaryKey().getEntry(0).getProperty()} )
	#if (${pkProperty.type.isPrimitive()})
        return new ${pkProperty.getType().toClass().getName()}( get${pkProperty.getCapitalizedName()}() );
	#else
		return get${pkProperty.getCapitalizedName()}();
	#end
#elseif (${entity.getPrimaryKey().size()} > 1)
        return this;
#else
        return null;
#end
    }

## ---------------------- methods ---------------------- 

    public boolean equals(Object other) {
        if ( !(other instanceof ${entity.getName()}) ) 
        	return false;
        ${entity.getName()} castOther = (${entity.getName()}) other;
        return new EqualsBuilder()
#foreach ( $property in ${entity.getProperties().iterator()} )
            .append( this.get${property.getCapitalizedName()}(), castOther.get${property.getCapitalizedName()}() )
#end
            .isEquals();
    }

    public int hashCode() {
        return new HashCodeBuilder()
#foreach ( $property in ${entity.getProperties().iterator()} )
            .append( this.get${property.getCapitalizedName()}() )
#end
            .toHashCode();
    }

    public String toString() {
        return new ToStringBuilder(this)
#foreach ( $property in ${entity.getProperties().iterator()} )
            .append( "${property.getName()}", this.get${property.getCapitalizedName()}() )
#end
            .toString();
    }

#if( ! $entity.getSuperClass() )
	/**
	 * @see org.asyrinx.brownie.core.util.Captionable#getCaption()
	 */
	public String getCaption() {
		return String.valueOf(getPrimaryKey());
	}
#end
	
	public static String getEntityCaption(){
		return "${entity.getLabel()}";
	}

}
