package ${omServiceSimpleImplPackage};

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import ${omPojoPackage}.*;
import ${omDaoPackage}.*;
import ${omServiceSimplePackage}.*;
import org.asyrinx.joey.om.Entity;
import org.asyrinx.joey.om.SearchCondition;

/**
 * @author akima
 */
public class ${table.getJavaName()}${omServiceSimpleImplSuffix} implements ${table.getJavaName()}${omServiceSimpleSuffix} {

#set( $mainDaoClassName = "${table.getJavaName()}${omDaoSuffix}" )
#set( $mainDaoObjName = "$stringUtils.uncapitalize( $mainDaoClassName )" )
	/**
	 *  
	 */
	public ${table.getJavaName()}${omServiceSimpleImplSuffix}(
#set( $usedForeignTable = $helper.newInstance("java.util.HashSet")  )
// $table.getName() $usedForeignTable.add( $table )
#foreach( $fk in $table.getForeignKeys() )
	#set( $fkTable = $fk.getForeignTableObject() )
	#set($fkDaoClassName = "${fkTable.getJavaName()}${omDaoSuffix}" )
	#set($fkDaoObjName = "$stringUtils.uncapitalize($fkDaoClassName)" )
	#if( ! $usedForeignTable.contains( $fkTable ) )
		$fkDaoClassName $fkDaoObjName, // $fk.getName() $usedForeignTable.add( $fkTable )
	#end
#end
		 $mainDaoClassName $mainDaoObjName
		) {
		super();
#set( $usedForeignTable = $helper.newInstance("java.util.HashSet")  )
// $table.getName() $usedForeignTable.add( $table )
#foreach( $fk in $table.getForeignKeys() )
	#set( $fkTable = $fk.getForeignTableObject() )
	#set($fkDaoClassName = "${fkTable.getJavaName()}${omDaoSuffix}" )
	#set($fkDaoObjName = "$stringUtils.uncapitalize($fkDaoClassName)" )
	#if( ! $usedForeignTable.contains( $fkTable ) )
		this.$fkDaoObjName = $fkDaoObjName; // $fk.getName() $usedForeignTable.add( $fkTable )
	#end
#end
		 this.$mainDaoObjName = $mainDaoObjName;
	}
	private final $mainDaoClassName $mainDaoObjName;

#set( $usedForeignTable = $helper.newInstance("java.util.HashSet")  )
// $table.getName() $usedForeignTable.add( $table )
#foreach( $fk in $table.getForeignKeys() )
	#set( $fkTable = $fk.getForeignTableObject() )
	#set($fkDaoClassName = "${fkTable.getJavaName()}${omDaoSuffix}" )
	#set($fkDaoObjName = "$stringUtils.uncapitalize($fkDaoClassName)" )
	#if( ! $usedForeignTable.contains( $fkTable ) )
	// $fk.getName() $usedForeignTable.add( $fkTable )
	private final $fkDaoClassName $fkDaoObjName;
	#end
#end

	/* (non-Javadoc)
	 * @see org.asyrinx.joey.om.EntityService#newEntity()
	 */
	public Entity newEntity() {
		return ${mainDaoObjName}.newEntity();
	}
	
	/* (non-Javadoc)
	 * @see org.asyrinx.joey.om.EntityService#restoreBeforeSave(org.asyrinx.joey.om.Entity)
	 */
	public void restoreBeforeSave(Entity entity) {
		${mainDaoObjName}.restoreBeforeSave(entity);
	}
	
	/**
	 * @param obj
	 */
	public void delete(${table.getJavaName()} obj) {
		${mainDaoObjName}.delete(obj);
	}

	/**
	 * @param key
	 * @return
	 */
	public ${table.getJavaName()} load(Serializable key) {
		if (key == null)
			return null;
		final ${table.getJavaName()} result = ${mainDaoObjName}.load(key);
		restore(result);
		return result;
	}

	/**
	 * @param key
	 * @return
	 */
	public Entity loadEntity(Serializable key){
		return this.load(key);
	}

	public void deleteEntity(Serializable key) {
	}

	public void saveEntity(Object entity) {
		if (entity instanceof ${table.getJavaName()})
			save((${table.getJavaName()}) entity);
		else
			throw new RuntimeException("entity is not a Area.");
	}

	/**
	 * @param obj
	 */
	public void save(${table.getJavaName()} obj) {
		${mainDaoObjName}.save(obj);
	}

	/**
	 * @param condition
	 * @return
	 */
	public List select(SearchCondition condition) {
		final List results = ${mainDaoObjName}.select(condition);
		for (Iterator iter = results.iterator(); iter.hasNext();) {
			final ${table.getJavaName()} obj = (${table.getJavaName()})iter.next();
			restore( obj );
		}
		return results;
	}
	
	public void restore(${table.getJavaName()} obj){
		if (obj == null)
			return ;
#foreach( $fk in $table.getForeignKeys() )
	#set( $fkTable = $fk.getForeignTableObject() )
	#set( $fkPropNameLow = $fk.getPropertyNameInLocal() )
	#set( $fkPropName = ${stringUtils.capitalize( $fkPropNameLow )} )
	#set($fkDaoClassName = "${fkTable.getJavaName()}${omDaoSuffix}" )
	#set($fkDaoObjName = "$stringUtils.uncapitalize($fkDaoClassName)" )
		obj.set${fkPropName}( ${fkDaoObjName}.load( obj.get${fkPropName}Key() ) );
#end
	}
}
