templates.velocity.java5g.pertable.manager.vm Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sql2java-generator Show documentation
Show all versions of sql2java-generator Show documentation
executable jar of sql2java generator
#parse( "table.include.vm" )
#parse( "header.include.vm" )
######################
#set ( $managerClassNSP = $table.asManagerClassNSP() )
#set ( $nativeClass = "${table.package}.$table.asBeanClass()" )
#set ( $beanClass = $table.asBeanClassNSP() )
#set ( $listenerClass = $table.asListenerClassNSP() )
#set ( $nativeListener = $table.asListenerClass() )
$codewriter.setCurrentJavaFilename("${pkg}.manager", "${managerClassNSP}.java")
package ${pkg}.manager;
import ${pkg}.Constant;
import ${pkg}.${beanClass};
import ${pkg}.$table.asManagerInterfaceNSP();
#importLinkedTable()
import gu.sql2java.*;
import gu.sql2java.exception.*;
/**
* Handles database calls (save, load, count, etc...) for the $tablename table.
* all {@link DaoException} be wrapped as {@link RuntimeDaoException} to throw.
#if ( $table.hasRemarks() )
* Remarks: $table.getRemarks()
#end
* @author guyadong
*/
public class $managerClassNSP extends BaseTableManager<$beanClass> implements $table.asManagerInterfaceNSP(),Constant
{
public $managerClassNSP(){
super("$tablename");
}
#if ( $table.countPrimaryKeys() > 0 )
//////////////////////////////////////
// PRIMARY KEY METHODS
//////////////////////////////////////
//1 override $table.asManagerInterfaceNSP()
@Override
public $beanClass loadByPrimaryKey(#join($primaryKeys "${e.javaType} ${e.varName}" ','))
{
return loadByPrimaryKey(#join($primaryKeys "(Object)${e.varName}" ','));
}
//1.1 override $table.asManagerInterfaceNSP()
@Override
public $beanClass loadByPrimaryKeyChecked(#join($primaryKeys "${e.javaType} ${e.varName}" ',')) throws ObjectRetrievalException
{
return loadByPrimaryKeyChecked(#join($primaryKeys "(Object)${e.varName}" ','));
}
@Override
public boolean existsPrimaryKey(#join($primaryKeys "${e.javaType} ${e.varName}" ','))
{
return existsPrimaryKey(#join($primaryKeys "(Object)${e.varName}" ','));
}
#if ( 1 == $table.countPrimaryKeys() )
#set ($pk = $table.getPrimaryKey())
//1.4.1 override $table.asManagerInterfaceNSP()
@Override
public ${pk.javaType} checkDuplicate(${pk.javaType} $pk.getVarName())throws ObjectRetrievalException{
return checkDuplicateByPk($pk.getVarName());
}
//1.8 override $table.asManagerInterfaceNSP()
@Override
public java.util.List<$beanClass> loadByPrimaryKey(${pk.javaType}... keys){
return loadByPks(keys);
}
//1.9 override $table.asManagerInterfaceNSP()
@Override
public java.util.List<$beanClass> loadByPrimaryKey(java.util.Collection<${pk.javaType}> keys){
return loadByPks(keys);
}
#end##1 == $table.countPrimaryKeys()
//2 override $table.asManagerInterfaceNSP()
@Override
public int deleteByPrimaryKey(#join($primaryKeys "${e.javaType} ${e.varName}" ','))
{
return this.deleteByPrimaryKey(#join($primaryKeys "(Object)${e.varName}" ','));
}
#if ( 1 == $table.countPrimaryKeys() )
#set ($pk = $table.getPrimaryKey())
//2.2 override $table.asManagerInterfaceNSP()
@Override
public int deleteByPrimaryKey(${pk.javaType}... keys){
return deleteByPks(keys);
}
//2.3 override $table.asManagerInterfaceNSP()
@Override
public int deleteByPrimaryKey(java.util.Collection<${pk.javaType}> keys){
return deleteByPks(keys);
}
#end##1 == $table.countPrimaryKeys()
#end ## $table.countPrimaryKeys() > 0
## ======================================================
## xImportedKeys template
## ======================================================
#foreach ($importedTable in $importedTables)
#if ( $velocityCount == 1 )
//////////////////////////////////////
// GET/SET IMPORTED KEY BEAN METHOD
//////////////////////////////////////
#end
#set ( $importedClass = "$importedTable.asBeanClassNSP()" )
#set ( $importedNative = "$importedTable.asBeanClass()" )
#set ( $importedClassManager = "$importedTable.asManagerClassNSP()" )
#foreach( $fkName in $importedTable.getFkMapNames($table.name) )
#set ( $columnsOfFk = $importedTable.getForeignKeysByFkName($fkName) )
//3.1 GET IMPORTED override $table.asManagerInterfaceNSP()
@Override
public ${importedClass}[] ${importedTable.getImportedBeansGetMethod($fkName)}($beanClass bean)
{
return getImportedBeans(bean,"$fkName");
}
//3.1.2 GET IMPORTED override $table.asManagerInterfaceNSP()
@Override
public ${importedClass}[] ${importedTable.getImportedBeansGetMethod($fkName)}(#join($primaryKeys "${e.javaType} $e.getFullVarName()" ','))
{
return getImportedBeans("$fkName",#join($primaryKeys "$e.getFullVarName()" ','));
}
//3.2 GET IMPORTED override $table.asManagerInterfaceNSP()
@Override
public java.util.List<$importedClass> ${importedTable.getImportedBeansGetMethod($fkName)}AsList($beanClass bean)
{
return getImportedBeansAsList(bean, "$fkName");
}
//3.2.2 GET IMPORTED override $table.asManagerInterfaceNSP()
@Override
public java.util.List<$importedClass> ${importedTable.getImportedBeansGetMethod($fkName)}AsList(#join($primaryKeys "${e.javaType} $e.getFullVarName()" ','))
{
return getImportedBeansAsList("$fkName",#join($primaryKeys "$e.getFullVarName()" ','));
}
//3.2.3 DELETE IMPORTED override $table.asManagerInterfaceNSP()
@Override
public int ${importedTable.getImportedBeansDelMethod($fkName)}(#join($primaryKeys "${e.javaType} $e.getFullVarName()" ','))
{
return deleteImportedBeans("$fkName",#join($primaryKeys "$e.getFullVarName()" ','));
}
//3.2.4 GET IMPORTED override $table.asManagerInterfaceNSP()
@Override
public java.util.List<$importedClass> ${importedTable.getImportedBeansGetMethod($fkName)}AsList($beanClass bean,int startRow, int numRows)
{
return instanceOf(${importedTable.asManagerClassNSP()}.class).loadByForeignKeyAsList(bean,"$fkName",startRow,numRows);
}
//3.3 SET IMPORTED override $table.asManagerInterfaceNSP()
@Override
public ${importedClass}[] ${importedTable.getImportedBeansSetMethod($fkName)}($beanClass bean , ${importedClass}[] importedBeans)
{
return setImportedBeans(bean, importedBeans, "$fkName");
}
//3.4 SET IMPORTED override $table.asManagerInterfaceNSP()
@Override
public > C ${importedTable.getImportedBeansSetMethod($fkName)}($beanClass bean , C importedBeans)
{
return setImportedBeans(bean, importedBeans, "$fkName");
}
#end
#end
## ======================================================
## only xImportedKeys template
## ======================================================
#if( $table.hasForeignTables() || $table.hasImportedTables() )
//3.5 SYNC SAVE override $table.asManagerInterfaceNSP()
@Override
public $beanClass save($beanClass bean
#foreachForeignTables('native' $table ", $m_foreignClass $table.asRefArg($m_fkName) " '' '')
#foreachImportedTables('native' $table ", ${m_importedClass}[] $m_importedTable.asImpArg($m_fkName) " '' ''))
{
java.util.HashMap referenced = new java.util.HashMap<>();
#foreach($foreignTable in $foreignTables)
#foreach ( $fkName in $table.getFkMapNames($foreignTable.name) )
referenced.put("$fkName",$table.asRefArg($fkName));
#end
#end
java.util.HashMap> imported = new java.util.HashMap<>();
#foreach($importedTable in $importedTables)
#foreach ( $fkName in $importedTable.getFkMapNames($table.name) )
#set ( $impVar = $importedTable.asImpArg($fkName) )
imported.put("$fkName",null == $impVar ? null : java.util.Arrays.asList($impVar));
#end
#end
return save(bean,referenced,imported);
}
//3.6 SYNC SAVE AS TRANSACTION override $table.asManagerInterfaceNSP()
@Override
public $beanClass saveAsTransaction(final $beanClass bean
#foreachForeignTables('native' $table ",final $m_foreignClass $table.asRefArg($m_fkName) " '' '')
#foreachImportedTables('native' $table ",final ${m_importedClass}[] $m_importedTable.asImpArg($m_fkName) " '' ''))
{
return this.runAsTransaction(new java.util.concurrent.Callable<$beanClass>(){
@Override
public $beanClass call() throws Exception {
return save(bean #foreachForeignTables('native' $table ", $table.asRefArg($m_fkName) " '' '')#foreachImportedTables('native' $table ", $m_importedTable.asImpArg($m_fkName) " '' ''));
}});
}
#if( $table.hasImportedTables() )
//3.7 SYNC SAVE override $table.asManagerInterfaceNSP()
@Override
public $beanClass save($beanClass bean
#foreachForeignTables('native' $table ", $m_foreignClass $table.asRefArg($m_fkName) " '' '')
#foreachImportedTables('native' $table ", java.util.Collection<$m_importedClass> $m_importedTable.asImpArg($m_fkName) " '' ''))
{
java.util.HashMap referenced = new java.util.HashMap<>();
#foreach($foreignTable in $foreignTables)
#foreach ( $fkName in $table.getFkMapNames($foreignTable.name) )
referenced.put("$fkName",$table.asRefArg($fkName));
#end
#end
java.util.HashMap> imported = new java.util.HashMap<>();
#foreach($importedTable in $importedTables)
#foreach ( $fkName in $importedTable.getFkMapNames($table.name) )
#set ( $impVar = $importedTable.asImpArg($fkName) )
imported.put("$fkName",null == $impVar ? null : new java.util.ArrayList($impVar));
#end
#end
return save(bean,referenced,imported);
}
//3.8 SYNC SAVE AS TRANSACTION override $table.asManagerInterfaceNSP()
@Override
public $beanClass saveAsTransaction(final $beanClass bean
#foreachForeignTables('native' $table ",final $m_foreignClass $table.asRefArg($m_fkName) " '' '')
#foreachImportedTables('native' $table ",final java.util.Collection<$m_importedClass> $m_importedTable.asImpArg($m_fkName) " '' ''))
{
return this.runAsTransaction(new java.util.concurrent.Callable<$beanClass>(){
@Override
public $beanClass call() throws Exception {
return save(bean #foreachForeignTables('native' $table ", $table.asRefArg($m_fkName) " '' '')#foreachImportedTables('native' $table ", $m_importedTable.asImpArg($m_fkName) " '' ''));
}});
}
#end ## $table.hasImportedTables()
#end ## $table.hasForeignTables() || $table.hasImportedTables()
## ======================================================
## xForeignKeys template
## ======================================================
#foreach ($foreignTable in $foreignTables)
#if ( $velocityCount == 1 )
//////////////////////////////////////
// GET/SET FOREIGN KEY BEAN METHOD
//////////////////////////////////////
#end
#set ( $foreignClass = "$foreignTable.asBeanClassNSP()" )
#set ( $foreignNative = "$foreignTable.asBeanClass()" )
#set ( $foreignClassManager = "$foreignTable.asManagerClass()" )
#foreach( $fkName in $table.getFkMapNames($foreignTable.name) )
#set ( $columnsOfFk = $table.getForeignKeysByFkName($fkName) )
//5.1 GET REFERENCED VALUE override $table.asManagerInterfaceNSP()
@Override
public $foreignClass $table.getReferencedVarGetMethod($fkName)($beanClass bean)
{
return getReferencedBean(bean,"$fkName");
}
//5.2 SET REFERENCED override $table.asManagerInterfaceNSP()
@Override
public $foreignClass $table.getReferencedVarSetMethod($fkName)($beanClass bean, $foreignClass beanToSet)
{
return setReferencedBean(bean, beanToSet, "$fkName");
}
#end
#end
//_____________________________________________________________________
//
// SAVE
//_____________________________________________________________________
#if ( $table.hasVersionColumn() )
#set ($lockColumn = $table.getVersionColumn() )
@Override
protected Class> getLockColumnType(){
return ${lockColumn.javaType}.class;
}
@Override
protected String getLockColumnName(){
return "${lockColumn.name}";
}
#end
#if ($table.countIndices() > 0)
//_____________________________________________________________________
//
// USING INDICES
//_____________________________________________________________________
#foreach ( $index in $table.getIndices() )
#set( $indexColumns = $index.getIndexColumnsList())
#if($index.isUnique())
// override $table.asManagerInterfaceNSP()
@Override
public $beanClass loadBy${index.asCamelCaseName()}(#join( $indexColumns "${e.javaType} ${e.getVarName()}" ',')){
return loadUniqueByIndex("$index.name",#join( $indexColumns "${e.getVarName()}" ','));
}
// override $table.asManagerInterfaceNSP()
@Override
public $beanClass loadBy${index.asCamelCaseName()}Checked(#join( $indexColumns "${e.javaType} ${e.getVarName()}" ','))throws ObjectRetrievalException{
return loadUniqueByIndexChecked("$index.name",#join( $indexColumns "${e.getVarName()}" ','));
}
#if( 1 == $indexColumns.size())
#set ($indexColumn = $indexColumns.get(0))
// override $table.asManagerInterfaceNSP()
@Override
public java.util.List<$beanClass> loadBy${index.asCamelCaseName()}(${indexColumn.javaType}... indexs)
{
return loadByIndexForIndices("$index.name", indexs);
}
// override $table.asManagerInterfaceNSP()
@Override
public java.util.List<$beanClass> loadBy${index.asCamelCaseName()}(java.util.Collection<${indexColumn.javaType}> indexs)
{
return loadByIndexForIndices("$index.name",indexs);
}
// override $table.asManagerInterfaceNSP()
@Override
public int deleteBy${index.asCamelCaseName()}(${indexColumn.javaType}... indexs)
{
return deleteByIndexForIndices("$index.name", indexs);
}
// override $table.asManagerInterfaceNSP()
@Override
public int deleteBy${index.asCamelCaseName()}(java.util.Collection<${indexColumn.javaType}> indexs)
{
return deleteByIndexForIndices("$index.name",indexs);
}
#end##1 == $indexColumns.size()
#else ## $index.isUnique()
// override $table.asManagerInterfaceNSP()
@Override
public ${beanClass}[] loadBy${index.asCamelCaseName()}(#join( $indexColumns "${e.javaType} ${e.getVarName()}" ','))
{
return this.loadBy${index.asCamelCaseName()}AsList(#join( $indexColumns "${e.getVarName()}" ',')).toArray(new ${beanClass}[0]);
}
// override $table.asManagerInterfaceNSP()
@Override
public java.util.List<$beanClass> loadBy${index.asCamelCaseName()}AsList(#join( $indexColumns "${e.javaType} ${e.getVarName()}" ','))
{
return loadByIndexAsList("$index.name",#join( $indexColumns "${e.getVarName()}" ','));
}
#end## $index.isUnique()
// override $table.asManagerInterfaceNSP()
@Override
public int deleteBy${index.asCamelCaseName()}(#join( $indexColumns "${e.javaType} ${e.getVarName()}" ','))
{
return deleteByIndex("$index.name",#join( $indexColumns "${e.getVarName()}" ','));
}
#end
#end##($table.countIndices() > 0)
#set ( $jTables = $table.getJunctionTables() )
#foreach ( $jTable in $jTables )
#set ( $linkedTable = $jTable.tableOfJunction( $table) )
#if ( $velocityCount == 1)
//_____________________________________________________________________
//
// MANY TO MANY: LOAD OTHER BEAN VIA JUNCTION TABLE
//_____________________________________________________________________
#end
#set ( $junctionBeanClass = $jTable.asBeanClass($isGeneral))
#set ( $junctionManagerClass = ${jTable.asManagerClass($isGeneral)})
#set ( $linkedBean = "${linkedTable.asBeanClass($isGeneral)}" )
#set ( $localFk = $jTable.getForeignKeys($table)[0])
#set ( $linkedFk = $jTable.getForeignKeys($linkedTable)[0])
#set ( $linkManagerClass = ${linkedTable.asManagerClass($isGeneral)})
//22 MANY TO MANY override $table.asManagerInterfaceNSP()
@Override
public java.util.List<${beanClass}> $jTable.getLoadMethodOfJunction()AsList($linkedBean bean)
{
return this.$jTable.getLoadMethodOfJunction()AsList(bean, 1, -1);
}
//23 MANY TO MANY override $table.asManagerInterfaceNSP()
@Override
public java.util.List<${beanClass}> $jTable.getLoadMethodOfJunction()AsList($linkedBean bean, int startRow, int numRows)
{
return instanceOf(${jTable.asManagerClassNSP()}.class).loadViaJunctionTableAsList(bean, ${beanClass}.class, startRow, numRows);
}
#define($junctionParam)
#join($jTable.primaryKeys '#set( $fColumn = $e.foreignColumn)#if($fColumn.table == $table)bean#{else}linked#{end}.$fColumn.getGetMethod()()' ',')
#end
//23.2 MANY TO MANY override $table.asManagerInterfaceNSP()
@Override
public void addJunction($beanClass bean,$linkedBean linked){
instanceOf(${jTable.asManagerClassNSP()}.class).addJunction(bean,linked);
}
//23.3 MANY TO MANY override $table.asManagerInterfaceNSP()
@Override
public int deleteJunction($beanClass bean,$linkedBean linked){
return instanceOf(${jTable.asManagerClassNSP()}.class).deleteJunction(bean,linked);
}
//23.4 MANY TO MANY override $table.asManagerInterfaceNSP()
@Override
public void addJunction($beanClass bean,$linkedBean... linkedBeans){
instanceOf(${jTable.asManagerClassNSP()}.class).addJunction(bean,linkedBeans);
}
//23.5 MANY TO MANY override $table.asManagerInterfaceNSP()
@Override
public void addJunction($beanClass bean,java.util.Collection<$linkedBean> linkedBeans){
instanceOf(${jTable.asManagerClassNSP()}.class).addJunction(bean,linkedBeans);
}
//23.6 MANY TO MANY override $table.asManagerInterfaceNSP()
@Override
public int deleteJunction($beanClass bean,$linkedBean... linkedBeans){
return instanceOf(${jTable.asManagerClassNSP()}.class).deleteJunction(bean,linkedBeans);
}
//23.7 MANY TO MANY override $table.asManagerInterfaceNSP()
@Override
public int deleteJunction($beanClass bean,java.util.Collection<$linkedBean> linkedBeans){
return instanceOf(${jTable.asManagerClassNSP()}.class).deleteJunction(bean,linkedBeans);
}
#end
#if ( 1 == $table.countPrimaryKeys() )
#set ($pk = $table.getPrimaryKey())
//45 override $table.asManagerInterfaceNSP()
@Override
public java.util.List<${pk.javaType}> toPrimaryKeyList($beanClass... beans){
return toPrimaryKeyList(${pk.javaType}.class,beans);
}
//46 override $table.asManagerInterfaceNSP()
@Override
public java.util.List<${pk.javaType}> toPrimaryKeyList(java.util.Collection<$beanClass> beans){
return toPrimaryKeyList(${pk.javaType}.class,beans);
}
#end##1 == $table.countPrimaryKeys()
################### method for self-reference field#################
#foreach($selfFk in $table.selfRefKeys)
#if($velocityCount == 1)
//_____________________________________________________________________
//
// SELF-REFERENCE
//_____________________________________________________________________
#end
//47 $table.asManagerInterfaceNSP()
@Override
public java.util.List<$beanClass> $table.getListMethod($selfFk)(#join($primaryKeys '${e.javaType} ${e.varName}' ',')){
return listOfSelfRef("$selfFk.fkName",#join($primaryKeys '${e.varName}' ','));
}
//48 $table.asManagerInterfaceNSP()
@Override
public java.util.List<$beanClass> $table.getListMethod($selfFk)($beanClass bean){
return bean == null ? java.util.Collections.<$beanClass>emptyList() : listOfSelfRef("$selfFk.fkName",bean.primaryValues());
}
//49 $table.asManagerInterfaceNSP()
@Override
public int $table.getLevelMethod($selfFk)(#join($primaryKeys '${e.javaType} ${e.varName}' ',')){
return levelOfSelfRef("$selfFk.fkName",#join($primaryKeys '${e.varName}' ','));
}
//50 $table.asManagerInterfaceNSP()
@Override
public int $table.getLevelMethod($selfFk)($beanClass bean){
return bean == null ? 0 : levelOfSelfRef("$selfFk.fkName",bean.primaryValues());
}
//51 $table.asManagerInterfaceNSP()
@Override
public boolean $table.getCyeleTestMethod($selfFk)(#join($primaryKeys '${e.javaType} ${e.varName}' ',')){
return $table.getLevelMethod($selfFk)(#join($primaryKeys '${e.varName}' ',')) < 0;
}
//52 $table.asManagerInterfaceNSP()
@Override
public boolean $table.getCyeleTestMethod($selfFk)($beanClass bean){
return $table.getLevelMethod($selfFk)(bean) < 0;
}
//53 $table.asManagerInterfaceNSP()
@Override
public $beanClass $table.getTopMethod($selfFk)(#join($primaryKeys '${e.javaType} ${e.varName}' ',')){
return topOfSelfRef("$selfFk.fkName",#join($primaryKeys '${e.varName}' ','));
}
//54 $table.asManagerInterfaceNSP()
@Override
public $beanClass $table.getTopMethod($selfFk)($beanClass bean){
if(null == bean){
throw new NullPointerException("bean is null");
}
return topOfSelfRef("$selfFk.fkName",bean.primaryValues());
}
#if($table.countPrimaryKeys() == 1)
//55 $table.asManagerInterfaceNSP()
@Override
public ${table.primaryKey.javaType} $table.getCheckNotCycleMethod($selfFk)(${table.primaryKey.javaType} $table.primaryKey.getVarName()){
if($table.getCyeleTestMethod($selfFk)($table.primaryKey.getVarName())){
throw new IllegalStateException("cycle on field: " + #join($selfFk.columns '"$e.name"' '+'));
}
return $table.primaryKey.getVarName();
}
#end
//56 $table.asManagerInterfaceNSP()
@Override
public $beanClass $table.getCheckNotCycleMethod($selfFk)($beanClass bean){
if($table.getCyeleTestMethod($selfFk)(bean)){
throw new IllegalStateException("cycle on field: " + #join($selfFk.columns '"$e.name"' '+'));
}
return bean;
}
//57 $table.asManagerInterfaceNSP()
@Override
public java.util.List<$beanClass> $table.getChildListMethod($selfFk)(#join($primaryKeys '${e.javaType} ${e.varName}' ',')){
return $table.getChildListMethod($selfFk)(createBean(#join($primaryKeys '${e.varName}' ',')));
}
//58 $table.asManagerInterfaceNSP()
@Override
public java.util.List<$beanClass> $table.getChildListMethod($selfFk)($beanClass bean){
java.util.LinkedHashSet<$beanClass> set = new java.util.LinkedHashSet<>();
return new java.util.ArrayList<$beanClass>(doListOfChild(bean,set,"${selfFk.fkName}"));
}
#end
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy