#set ($db = $table.Database) #if ($db.Package) #set ($package = $db.Package) #else #set ($package = $targetPackage) #end package ${package}; import java.util.*; import java.math.*; import java.sql.*; import com.workingdogs.village.*; import org.apache.torque.Torque; import org.apache.torque.util.BasePeer; import org.apache.torque.om.*; import org.apache.torque.util.*; import org.apache.torque.map.*; import org.apache.torque.TorqueException; // Local classes import ${package}.map.*; /** #if ($addTimeStamp) * This class was autogenerated by Torque on: * * [$now] * #end */ public abstract class $basePrefix${table.JavaName}Peer extends $table.BasePeer { #if (!$table.isAlias()) /** the default database name for this class */ public static final String DATABASE_NAME = "$table.Database.Name"; /** the table name for this class */ public static final String TABLE_NAME = "$table.Name"; /** * @returns the map builder for this peer */ public static MapBuilder getMapBuilder() throws Exception { return getMapBuilder(${table.JavaName}MapBuilder.CLASS_NAME); } #foreach ($col in $table.Columns) #set ( $tfc=$table.JavaName ) #set ( $cfc=$col.JavaName ) #set ( $cup=$col.Name.toUpperCase() ) /** the column name for the $cup field */ public static final String $cup; #end static { #foreach ($col in $table.Columns) #set ( $tfc=$table.JavaName ) #set ( $cfc=$col.JavaName ) #set ( $cup=$col.Name.toUpperCase() ) $cup = "${table.Name}.$cup"; #end if (Torque.isInit()) { try { getMapBuilder(); } catch (Exception e) { category.error("Could not initialize Peer", e); } } else { Torque.registerMapBuilder(${table.JavaName}MapBuilder.CLASS_NAME); } } #end ## ends if (!$table.isAlias()) /** number of columns for this peer */ public static final int numColumns = $table.NumColumns; /** A class that can be returned by this peer. */ protected static final String CLASSNAME_DEFAULT = "${package}.$table.JavaName"; /** A class that can be returned by this peer. */ protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); /** Class object initialization method. */ private static Class initClass(String className) { Class c = null; try { c = Class.forName(className); } catch (Throwable t) { category.error("A FATAL ERROR has occurred which should not " + "have happened under any circumstance. Please notify " + "the Turbine developers " + "and give as many details as possible (including the error " + "stack trace).", t); // Error objects should always be propogated. if (t instanceof Error) { throw (Error) t.fillInStackTrace(); } } return c; } #if (!$table.isAlias()) /** * Get the list of objects for a ResultSet. Please not that your * resultset MUST return columns in the right order. You can use * getFieldNames() in BaseObject to get the correct sequence. */ public static Vector resultSet2Objects (java.sql.ResultSet results) throws Exception { QueryDataSet qds = null; Vector rows = null; try { qds = new QueryDataSet( results ); rows = getSelectResults( qds ); } finally { if (qds != null) qds.close(); } return populateObjects (rows); } #if ($table.ChildrenColumn) #set ($col = $table.ChildrenColumn) #set ( $tfc=$table.JavaName ) #set ( $cfc=$col.JavaName ) #set ( $cup=$col.Name.toUpperCase() ) #if ($col.isEnumeratedClasses()) ## NOTE: this hack requires a class type definition column to ## be a primitive type or a String. Should not be a bad assumption #if ($col.isPrimitive()) #set ($quote = "") #else #set ($quote = '"') #end #foreach ($child in $col.Children) /** A key representing a particular subclass */ public static final $col.JavaNative CLASSKEY_$child.Key.toUpperCase() = $quote$child.Key$quote; /** A class that can be returned by this peer. */ public static final String CLASSNAME_$child.Key.toUpperCase() = "${package}.$child.ClassName"; /** A class that can be returned by this peer. */ public static final Class CLASS_$child.Key.toUpperCase() = initClass(CLASSNAME_$child.Key.toUpperCase()); #end #end #end /** * Method to do inserts */ public static ObjectKey doInsert( Criteria criteria ) throws Exception { return $basePrefix${table.JavaName}Peer .doInsert( criteria, (Connection) null ); } /** * Method to do inserts. This method is to be used during a transaction, * otherwise use the doInsert(Criteria) method. It will take care of * the connection details internally. */ public static ObjectKey doInsert( Criteria criteria, Connection con ) throws Exception { #foreach ($col in $table.Columns) #set ( $cup=$col.Name.toUpperCase() ) #if($col.isBooleanInt()) // check for conversion from boolean to int if ( criteria.containsKey($cup) ) { Object possibleBoolean = criteria.get($cup); if ( possibleBoolean instanceof Boolean ) { if ( ((Boolean)possibleBoolean).booleanValue() ) { criteria.add($cup, 1); } else { criteria.add($cup, 0); } } } #elseif ($col.isBooleanChar()) // check for conversion from boolean to Y/N if ( criteria.containsKey($cup) ) { Object possibleBoolean = criteria.get($cup); if ( possibleBoolean instanceof Boolean ) { if ( ((Boolean)possibleBoolean).booleanValue() ) { criteria.add($cup, "Y"); } else { criteria.add($cup, "N"); } } } #end #end if ( con == null ) { // Set the correct dbName if it has not been overridden // criteria.getDbName will return the same object if not set to // another value so == check is okay and faster if ( criteria.getDbName() == Torque.getDefaultDB() ) { criteria.setDbName(DATABASE_NAME); } return BasePeer.doInsert( criteria ); } else { return BasePeer.doInsert( criteria, con ); } } /** Add all the columns needed to create a new object */ public static void addSelectColumns (Criteria criteria) throws Exception { #foreach ($col in $table.Columns) #set ( $cup=$col.Name.toUpperCase() ) criteria.addSelectColumn( $cup ); #end } /** * Create a new object of type cls from a resultset row starting * from a specified offset. This is done so that you can select * other rows than just those needed for this object. You may * for example want to create two objects from the same row. */ public static $table.JavaName row2Object (Record row, int offset, Class cls ) throws Exception { $table.JavaName obj = ($table.JavaName)cls.newInstance(); populateObject(row, offset, obj); #if ($addSaveMethod) obj.setModified(false); #end obj.setNew(false); return obj; } /** * Populates an object from a resultset row starting * from a specified offset. This is done so that you can select * other rows than just those needed for this object. You may * for example want to create two objects from the same row. */ public static void populateObject (Record row, int offset, $table.JavaName obj ) throws Exception { #set ( $n=0 ) #foreach ($col in $table.Columns) #if ($col.isBooleanChar()) obj.set${col.JavaName} ("Y".equals(row.getValue(offset+$n).$col.VillageMethod)); #elseif ($col.isBooleanInt() && $col.isUsePrimitive()) obj.set${col.JavaName} (1 == row.getValue(offset+$n).$col.VillageMethod); #elseif ($col.isBooleanInt()) obj.set${col.JavaName}(new Boolean( 1 == row.getValue(offset+$n).${col.VillageMethod}.intValue())); #else #if ($col.isPrimaryKey() || $col.isForeignKey() ) obj.set${col.JavaName}( new ${col.JavaNative}(row.getValue(offset+$n).$col.VillageMethod)); #else obj.set${col.JavaName}(row.getValue(offset+$n).$col.VillageMethod); #end #end #set ( $n = $n + 1 ) #end } /** Method to do selects */ public static Vector doSelect( Criteria criteria ) throws Exception { return populateObjects( doSelectVillageRecords(criteria) ); } /** Method to do selects within a transaction */ public static Vector doSelect( Criteria criteria, Connection con ) throws Exception { return populateObjects( doSelectVillageRecords(criteria, con) ); } /** * Grabs the raw Village records to be formed into objects. * This method handles connections internally. The Record objects * returned by this method should be considered readonly. Do not * alter the data and call save(), your results may vary, but are * certainly likely to result in hard to track MT bugs. */ public static Vector doSelectVillageRecords( Criteria criteria ) throws Exception { return $basePrefix${table.JavaName}Peer .doSelectVillageRecords(criteria, (Connection) null); } /** * Grabs the raw Village records to be formed into objects. * This method should be used for transactions */ public static Vector doSelectVillageRecords( Criteria criteria, Connection con ) throws Exception { #if ($targetDatabase == "postgresql" && $table.requiresTransactionInPostgres()) // stuff for postgresql problem..... criteria.setBlobFlag(); #end if (criteria.getSelectColumns().size() == 0) { addSelectColumns ( criteria ); } #foreach ($col in $table.Columns) #set ( $cup=$col.Name.toUpperCase() ) #if($col.isBooleanInt()) // check for conversion from boolean to int if ( criteria.containsKey($cup) ) { Object possibleBoolean = criteria.get($cup); if ( possibleBoolean instanceof Boolean ) { if ( ((Boolean)possibleBoolean).booleanValue() ) { criteria.add($cup, 1); } else { criteria.add($cup, 0); } } } #elseif ($col.isBooleanChar()) // check for conversion from boolean to Y/N if ( criteria.containsKey($cup) ) { Object possibleBoolean = criteria.get($cup); if ( possibleBoolean instanceof Boolean ) { if ( ((Boolean)possibleBoolean).booleanValue() ) { criteria.add($cup, "Y"); } else { criteria.add($cup, "N"); } } } #end #end // BasePeer returns a Vector of Value (Village) arrays. The array // order follows the order columns were placed in the Select clause. if ( con == null) { // Set the correct dbName if it has not been overridden // criteria.getDbName will return the same object if not set to // another value so == check is okay and faster if ( criteria.getDbName() == Torque.getDefaultDB() ) { criteria.setDbName(DATABASE_NAME); } return BasePeer.doSelect(criteria); } else { return BasePeer.doSelect(criteria, con); } } /** * The returned vector will contain objects of the default type or * objects that inherit from the default. */ public static Vector populateObjects(Vector records) throws Exception { Vector results = new Vector(records.size()); // populate the object(s) for ( int i=0; i 1) #set ( $comma = false ) /** * retrieve object using using pk values. * #foreach ($col in $table.PrimaryKeys) #set ( $clo=$col.Name.toLowerCase() ) #set ( $cjtype= $col.JavaNative ) * @param $cjtype $clo #end */ public static $table.JavaName ${retrieveMethod}( #foreach ($col in $table.PrimaryKeys) #set ( $clo=$col.Name.toLowerCase() ) #set ( $cjtype = $col.JavaNative ) #if ($comma),#end $cjtype $clo #set ( $comma = true ) #end ) throws Exception { Connection db = null; $table.JavaName retVal = null; try { db = getConnection( DATABASE_NAME ); retVal = retrieveByPK( #set ( $comma = false ) #foreach ($col in $table.PrimaryKeys) #set ( $clo=$col.Name.toLowerCase() ) #if ($comma),#end $clo #set ( $comma = true ) #end , db); } finally { if (db != null) closeConnection(db); } return(retVal); } #set ( $comma = false ) /** * retrieve object using using pk values. * #foreach ($col in $table.PrimaryKeys) #set ( $clo=$col.Name.toLowerCase() ) #set ( $cjtype= $col.JavaNative ) * @param $cjtype $clo #end * @param Connection con */ public static $table.JavaName ${retrieveMethod}( #foreach ($col in $table.PrimaryKeys) #set ( $clo=$col.Name.toLowerCase() ) #set ( $cjtype = $col.JavaNative ) #if ($comma),#end $cjtype $clo #set ( $comma = true ) #end ,Connection con ) throws Exception { Criteria criteria = new Criteria(5); #foreach ($col in $table.PrimaryKeys) #set ( $cup=$col.Name.toUpperCase() ) #set ( $clo=$col.Name.toLowerCase() ) criteria.add( $cup, $clo ); #end Vector v = doSelect(criteria, con); if ( v.size() != 1) { throw new Exception("Failed to select one and only one row."); } else { return ($table.JavaName) v.firstElement(); } } #end #if ($complexObjectModel) ## ## setup joins ## #set ( $className = $table.JavaName ) #set ( $countFK = 0 ) #foreach ($dummyFK in $table.ForeignKeys) #set ( $countFK = $countFK + 1 ) #end #if ($countFK >= 1) #foreach ($fk in $table.ForeignKeys) ## want to cover this case, but the code is not there yet. #if ( !($fk.ForeignTableName.equals($table.Name)) ) #set ( $partJoinName = "" ) #foreach ($columnName in $fk.LocalColumns) #set ( $column = $table.getColumn($columnName) ) #if ($column.isMultipleFK() || $fk.ForeignTableName.equals($table.Name)) #set ( $partJoinName = "$partJoinName$column.JavaName" ) #end #end #set ( $joinTable = $table.Database.getTable($fk.ForeignTableName) ) #set ( $joinClassName = $joinTable.JavaName ) #if ($partJoinName == "") #set ( $joinColumnId = $joinClassName ) #set ( $collThisTable = "${className}s" ) #set ( $collThisTableMs = $className ) #else #set ( $joinColumnId= "${joinClassName}RelatedBy$partJoinName" ) #set ( $collThisTable="${className}sRelatedBy$partJoinName" ) #set ( $collThisTableMs="${className}RelatedBy$partJoinName" ) #end ## ------------------------------------------------------------ /** * selects a collection of $className objects pre-filled with their * $joinClassName objects. * * This method is protected by default in order to keep the public * api reasonable. You can provide public methods for those you * actually need in ${table.JavaName}Peer. */ protected static Vector doSelectJoin${joinColumnId}(Criteria c) throws Exception { #if ($targetDatabase == "postgresql" && $table.requiresTransactionInPostgres()) // stuff for postgresql problem..... c.setBlobFlag(); #end // Set the correct dbName if it has not been overridden // c.getDbName will return the same object if not set to // another value so == check is okay and faster if ( c.getDbName() == Torque.getDefaultDB() ) { c.setDbName(DATABASE_NAME); } ${table.JavaName}Peer.addSelectColumns(c); int offset = numColumns + 1; ${joinClassName}Peer.addSelectColumns(c); #set ( $lfMap = $fk.LocalForeignMapping ) #foreach ($columnName in $fk.LocalColumns) #set ( $column = $table.getColumn($columnName) ) #set ( $columnFk = $joinTable.getColumn( $lfMap.get($columnName) ) ) c.addJoin(${table.JavaName}Peer.$column.Name.toUpperCase(), ${joinClassName}Peer.$columnFk.Name.toUpperCase()); #end #foreach ($col in $table.Columns) #set ( $cup=$col.Name.toUpperCase() ) #if($col.isBooleanInt()) // check for conversion from boolean to int if ( c.containsKey($cup) ) { Object possibleBoolean = c.get($cup); if ( possibleBoolean instanceof Boolean ) { if ( ((Boolean)possibleBoolean).booleanValue() ) { c.add($cup, 1); } else { c.add($cup, 0); } } } #elseif ($col.isBooleanChar()) // check for conversion from boolean to Y/N if ( c.containsKey($cup) ) { Object possibleBoolean = c.get($cup); if ( possibleBoolean instanceof Boolean ) { if ( ((Boolean)possibleBoolean).booleanValue() ) { c.add($cup, "Y"); } else { c.add($cup, "N"); } } } #end #end Vector rows = BasePeer.doSelect(c); Vector results = new Vector(); for (int i=0; i 2) #foreach ($fk in $table.ForeignKeys) #set ( $excludeTable = $table.Database.getTable($fk.ForeignTableName) ) #set ( $excludeClassName = $excludeTable.JavaName ) #set ( $relatedByCol = "" ) #foreach ($columnName in $fk.LocalColumns) #set ( $column = $table.getColumn($columnName) ) #if ($column.isMultipleFK()) #set ($relatedByCol = "$relatedByCol$column.JavaName") #end #end #if ($relatedByCol == "") #set ( $excludeString = $excludeClassName ) #set ( $collThisTable = "${className}s" ) #set ( $collThisTableMs = $className ) #else #set ( $excludeString="${excludeClassName}RelatedBy$relatedByCol" ) #set ( $collThisTable="${className}sRelatedBy$relatedByCol" ) #set ( $collThisTableMs="${className}RelatedBy$relatedByCol" ) #end /** * selects a collection of $className objects pre-filled with * all related objects. * * This method is protected by default in order to keep the public * api reasonable. You can provide public methods for those you * actually need in ${table.JavaName}Peer. */ protected static Vector doSelectJoinAllExcept${excludeString}(Criteria c) throws Exception { #if ($targetDatabase == "postgresql" && $table.requiresTransactionInPostgres()) // stuff for postgresql problem..... c.setBlobFlag(); #end // Set the correct dbName if it has not been overridden // c.getDbName will return the same object if not set to another value // so == check is okay and faster if ( c.getDbName() == Torque.getDefaultDB() ) { c.setDbName(DATABASE_NAME); } addSelectColumns(c); int offset2 = numColumns + 1; #set ( $index = 2 ) #foreach ($fk in $table.ForeignKeys) ## want to cover this case, but the code is not there yet. #if ( !($fk.ForeignTableName.equals($table.Name)) ) #set ( $joinTable = $table.Database.getTable($fk.ForeignTableName) ) #set ( $joinClassName = $joinTable.JavaName ) #if (!$joinClassName.equals($excludeClassName)) #set ( $new_index = $index + 1 ) ${joinClassName}Peer.addSelectColumns(c); int offset$new_index = offset$index + ${joinClassName}Peer.numColumns; #set ( $index = $new_index ) #end #end #end #foreach ($col in $table.Columns) #set ( $cup=$col.Name.toUpperCase() ) #if($col.isBooleanInt()) // check for conversion from boolean to int if ( c.containsKey($cup) ) { Object possibleBoolean = c.get($cup); if ( possibleBoolean instanceof Boolean ) { if ( ((Boolean)possibleBoolean).booleanValue() ) { c.add($cup, 1); } else { c.add($cup, 0); } } } #elseif ($col.isBooleanChar()) // check for conversion from boolean to Y/N if ( c.containsKey($cup) ) { Object possibleBoolean = c.get($cup); if ( possibleBoolean instanceof Boolean ) { if ( ((Boolean)possibleBoolean).booleanValue() ) { c.add($cup, "Y"); } else { c.add($cup, "N"); } } } #end #end Vector rows = BasePeer.doSelect(c); Vector results = new Vector(); for (int i=0; i