#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.turbine.om.*; import org.apache.turbine.om.peer.*; import org.apache.turbine.util.*; import org.apache.turbine.util.db.*; import org.apache.turbine.util.db.map.*; import org.apache.turbine.util.db.pool.DBConnection; import org.apache.turbine.services.db.TurbineDB; import org.apache.turbine.util.TurbineException; // 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 mapbuilder for this class */ private static final ${table.JavaName}MapBuilder mapBuilder = (${table.JavaName}MapBuilder)getMapBuilder(${table.JavaName}MapBuilder.CLASS_NAME); /** the table name for this class */ public static final String TABLE_NAME = mapBuilder.getTable(); /** * @returns the map builder for this peer */ public static MapBuilder getMapBuilder() { return(mapBuilder); } #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 = mapBuilder.get${tfc}_${cfc}(); #end #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(); /** Initialization method for static CLASS_DEFAULT attribute */ private static Class initClass() { Class c = null; try { c = Class.forName(CLASSNAME_DEFAULT); } catch (Exception e) { Log.error("A FATAL ERROR has occurred which should not" + "have happened under any circumstance. Please notify" + "Turbine and give as many details as possible including the " + "error stacktrace.", e); } 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() = init${child.Key.toUpperCase()}Class(); /** Initialization method for static CLASS_$child.Key.toUpperCase() attribute */ private static Class init${child.Key.toUpperCase()}Class() { Class c = null; try { c = Class.forName(CLASSNAME_$child.Key.toUpperCase()); } catch (Exception e) { Log.error("A FATAL ERROR has occurred which should not" + "have happened under any circumstance. Please notify " + "Turbine and give as many details as possible including the " + "error stacktrace.", e); } return c; } #end #end #end /** Method to do inserts */ public static ObjectKey doInsert( Criteria criteria ) throws Exception { #if ($table.Database.Name != "default") criteria.setDbName(mapBuilder.getDatabaseMap().getName()); #end #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 return BasePeer.doInsert( criteria ); } /** * 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, DBConnection dbCon ) throws Exception { #if ($table.Database.Name != "default") criteria.setDbName(mapBuilder.getDatabaseMap().getName()); #end #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 return BasePeer.doInsert( criteria, dbCon ); } /** 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()) obj.set${col.JavaName} (1 == row.getValue(offset+$n).$col.VillageMethod); #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, DBConnection dbCon ) throws Exception { return populateObjects( doSelectVillageRecords(criteria, dbCon) ); } /** * 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 { #if ($table.Database.Name != "default") criteria.setDbName(mapBuilder.getDatabaseMap().getName()); #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. return BasePeer.doSelect(criteria); } /** * Grabs the raw Village records to be formed into objects. * This method should be used for transactions */ public static Vector doSelectVillageRecords( Criteria criteria, DBConnection dbCon ) throws Exception { 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. return BasePeer.doSelect(criteria, dbCon); } /** * 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 retrieveByPK( #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 { DBConnection db = null; $table.JavaName retVal = null; try { db = TurbineDB.getConnection( mapBuilder.getDatabaseMap().getName() ); 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) TurbineDB.releaseConnection(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 DBConnection dbcon */ public static $table.JavaName retrieveByPK( #foreach ($col in $table.PrimaryKeys) #set ( $clo=$col.Name.toLowerCase() ) #set ( $cjtype = $col.JavaNative ) #if ($comma),#end $cjtype $clo #set ( $comma = true ) #end ,DBConnection dbcon ) 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, dbcon); if ( v.size() != 1) { throw new Exception("Failed to select one and only one row."); } else { return ($table.JavaName) v.firstElement(); } } #end #end ## ends if (!$table.isAlias()) #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()) #set ( $partJoinName=$strings.concat([$partJoinName,$column.JavaName]) ) #elseif ( $fk.ForeignTableName.equals($table.Name) ) #set ( $partJoinName=$strings.concat([$partJoinName,$column.JavaName]) ) #end #end #set ( $joinTable = $table.Database.getTable($fk.ForeignTableName) ) #set ( $joinClassName = $joinTable.JavaName ) #if ($partJoinName == "") #set ( $joinColumnId = $joinClassName ) #set ( $collThisTable = $strings.concat([$className, "s"]) ) #set ( $collThisTableMs = $className ) #else #set ( $joinColumnId=$strings.concat([$joinClassName,"RelatedBy",$partJoinName]) ) #set ( $collThisTable=$strings.concat([$className,"sRelatedBy",$partJoinName]) ) #set ( $collThisTableMs=$strings.concat([$className,"RelatedBy",$partJoinName]) ) #end ## ------------------------------------------------------------ /** * selects a collection of $className objects pre-filled with their * $joinClassName objects. */ protected static Vector doSelectJoin${joinColumnId}(Criteria c) throws Exception { c.setDbName(mapBuilder.getDatabaseMap().getName()); ${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=$strings.concat([$relatedByCol,$column.JavaName])) #end #end #if ($relatedByCol == "") #set ( $excludeString = $excludeClassName ) #set ( $collThisTable = $strings.concat([$className, "s"]) ) #set ( $collThisTableMs = $className ) #else #set ( $excludeString=$strings.concat([$excludeClassName,"RelatedBy",$relatedByCol]) ) #set ( $collThisTable=$strings.concat([$className,"sRelatedBy",$relatedByCol]) ) #set ( $collThisTableMs=$strings.concat([$className,"RelatedBy",$relatedByCol]) ) #end /** * selects a collection of $className objects pre-filled with * all related objects. */ protected static Vector doSelectJoinAllExcept${excludeString}(Criteria c) throws Exception { c.setDbName(mapBuilder.getDatabaseMap().getName()); 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