## 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.
package ${package};

import java.util.Collection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.ojb.broker.*;
import org.apache.ojb.broker.query.*;

/**
 * ${basePrefix}${table.JavaName}Manager.
#if ($addTimeStamp)
 * This class was autogenerated by Torque on:
 *
 * [$now]
 *
#end
 */
public class ${basePrefix}${table.JavaName}Manager
{
    /** Logging class from commons.logging */
    private static Log log = LogFactory.getLog(${basePrefix}${table.JavaName}Manager.class);

    protected PersistenceBroker getBroker() {
        return PersistenceBrokerFactory.defaultPersistenceBroker();
    }

    protected ${table.JavaName} getObjectByCriteria(Criteria crit) {
        PersistenceBroker broker = getBroker();
        Query query = new QueryByCriteria(${table.JavaName}.class, crit);
        ${table.JavaName} object = (${table.JavaName}) broker.getObjectByQuery(query);
        broker.close();
        return object;
    }

#set ($params = "")
#foreach ($pk in $table.PrimaryKey)
  #set ($params = "$params ${pk.JavaNative} ${pk.uncapitalisedJavaName}, ")
#end
    public ${table.JavaName} getObjectByPk($strings.chop($params,2))
    {
        Criteria crit = new Criteria();
#foreach ($pk in $table.PrimaryKey)
        crit.addEqualTo("${pk.uncapitalisedJavaName}", ${pk.uncapitalisedJavaName});
#end
        return (${table.JavaName}) getObjectByCriteria(crit);
    }

    protected Collection getCollectionByCriteria(Criteria crit) {
        PersistenceBroker broker = getBroker();
        Query query = new QueryByCriteria(${table.JavaName}.class, crit);
        Collection coll = broker.getCollectionByQuery(query);
        broker.close();
        return coll;
    }

    public void save(${table.JavaName} object) {
        PersistenceBroker broker = getBroker();
        try {
            broker.beginTransaction();
            broker.store(object);
            broker.commitTransaction();
        } catch (PersistenceBrokerException ex) {
            broker.abortTransaction();
            log.error("Error saving object", ex);
        } finally {
            broker.close();
        }
    }

    protected void save(PersistenceBroker broker, ${table.JavaName} object) {
        broker.store(object);
    }

    public void delete(${table.JavaName} object) {
        PersistenceBroker broker = getBroker();
        try {
            broker.beginTransaction();
            delete(broker, object);
            broker.commitTransaction();
        } catch (Throwable t) {
            broker.abortTransaction();
            log.error("could not delete object", t);
        } finally {
            broker.close();
        }
    }

    protected void delete(PersistenceBroker broker, ${table.JavaName} object) {
        broker.delete(object);
    }

}
