/*
 * Copyright (c) 2002-2008, Mairie de Paris
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice
 *     and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright notice
 *     and the following disclaimer in the documentation and/or other materials
 *     provided with the distribution.
 *
 *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
 *     contributors may be used to endorse or promote products derived from
 *     this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * License 1.0
 */


package ${businessObject.packageName};

import fr.paris.lutece.util.sql.DAOUtil;

import java.util.ArrayList;
import java.util.Collection;


/**
 * This class provides Data Access methods for ${businessObject.className} objects
 */

public final class ${businessObject.className}DAO implements I${businessObject.className}DAO
{
	
	// Constants
	
	private static final String SQL_QUERY_NEW_PK = "SELECT max( ${businessObject.idColumnName} ) FROM ${businessObject.table}";
	private static final String SQL_QUERY_SELECT = "SELECT <#list businessObject.attributes as attribute>${attribute.columnName}<#if attribute_has_next>,  FROM ${businessObject.table} WHERE ${businessObject.idColumnName} = ?";
	private static final String SQL_QUERY_INSERT = "INSERT INTO ${businessObject.table} ( <#list businessObject.attributes as attribute>${attribute.columnName}<#if attribute_has_next>,  ) VALUES ( <#list businessObject.attributes as attribute>?<#if attribute_has_next>,  ) ";
	private static final String SQL_QUERY_DELETE = "DELETE FROM ${businessObject.table} WHERE ${businessObject.idColumnName} = ? ";
	private static final String SQL_QUERY_UPDATE = "UPDATE ${businessObject.table} SET <#list businessObject.attributes as attribute>${attribute.columnName} = ?<#if attribute_has_next>,  WHERE ${businessObject.idColumnName} = ?";
	private static final String SQL_QUERY_SELECTALL = "SELECT <#list businessObject.attributes as attribute>${attribute.columnName}<#if attribute_has_next>,  FROM ${businessObject.table}";


	
	/**
	 * Generates a new primary key
	 * @return The new primary key
	 */
    
	public int newPrimaryKey()
	{
		DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK  );
		daoUtil.executeQuery();

		int nKey;

		if( !daoUtil.next() )
		{
			// if the table is empty
			nKey = 1;
		}

		nKey = daoUtil.getInt( 1 ) + 1;
		daoUtil.free();

		return nKey;
	}




	/**
	 * Insert a new record in the table.
	 * @param ${businessObject.instanceName} instance of the ${businessObject.className} object to insert
	 */

	public void insert( ${businessObject.className} ${businessObject.instanceName} )
	{
		DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
                
		${businessObject.instanceName}.setId( newPrimaryKey() );
                
        	<#list businessObject.attributes as attribute>
                daoUtil.set${attribute.type?cap_first} ( ${attribute_index + 1}, ${businessObject.instanceName}.get${attribute.name} ( ) );
            

		daoUtil.executeUpdate();
		daoUtil.free();
	}


	/**
	 * Load the data of the ${businessObject.instanceName} from the table
	 * @param nId The identifier of the ${businessObject.instanceName}
	 * @return the instance of the ${businessObject.className}
	 */


        public ${businessObject.className} load( int nId )
	{
		DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
		daoUtil.setInt( 1 , nId );
		daoUtil.executeQuery();

		${businessObject.className} ${businessObject.instanceName} = null;

		if ( daoUtil.next() )
		{
			${businessObject.instanceName} = new ${businessObject.className}();

                <#list businessObject.attributes as attribute>
                    ${businessObject.instanceName}.set${attribute.name}( daoUtil.get${attribute.type?cap_first}(  ${attribute_index + 1} ) );
                
		}

		daoUtil.free();
		return ${businessObject.instanceName};
	}


	/**
	 * Delete a record from the table
	 * @param n${businessObject.className}Id The identifier of the ${businessObject.instanceName}
	 */

	public void delete( int n${businessObject.className}Id )
	{
		DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
		daoUtil.setInt( 1 , n${businessObject.className}Id );
		daoUtil.executeUpdate();
		daoUtil.free();
	}


	/**
	 * Update the record in the table
	 * @param ${businessObject.instanceName} The reference of the ${businessObject.instanceName}
	 */

	public void store( ${businessObject.className} ${businessObject.instanceName} )
	{
		DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
                
             <#list businessObject.attributes as attribute>
             <#assign nIndex=attribute_index+1 >
                daoUtil.set${attribute.type?cap_first}( ${nIndex}, ${businessObject.instanceName}.get${attribute.name}( ) );
             
             daoUtil.setInt( ${nIndex+1}, ${businessObject.instanceName}.getId( ) );
                
		daoUtil.executeUpdate( );
		daoUtil.free( );
	}



	/**
	 * Load the data of all the ${businessObject.instanceName}s and returns them as a collection
	 * @return The Collection which contains the data of all the ${businessObject.instanceName}s
	 */

        public Collection<${businessObject.className}> select${businessObject.className}sList()
	{
		Collection<${businessObject.className}> ${businessObject.instanceName}List = new ArrayList<${businessObject.className}>(  );
		DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
		daoUtil.executeQuery(  );

		while ( daoUtil.next(  ) )
		{
                ${businessObject.className} ${businessObject.instanceName} = new ${businessObject.className}(  );

                <#list businessObject.attributes as attribute>
                    ${businessObject.instanceName}.set${attribute.name}( daoUtil.get${attribute.type?cap_first}( ${attribute_index+1} ) );
                

                ${businessObject.instanceName}List.add( ${businessObject.instanceName} );
		}

		daoUtil.free();
		return ${businessObject.instanceName}List;
	}

}