## ============================================================================
## Filename : DAOTemplate.vm
## Note(s) : This template is used to generate a Data Access Object class
## based on a database table/view.
##
## Note: The generated code is dependent on the ThinkUI framework.
## Please refer to the SQL Client documentation for details.
##
## Copyright (c) 2007 ThinkUI Software Inc. All rights reserved.
## ============================================================================
##
## ****************************************************************************
## The following variables are available in this template. For more
## information on any of the following variable, please refer to the user guide.
## ****************************************************************************
## $projectName
## $authorName
## $headerText
## $tableName
## $className
## $objectName
## $objectVar
## $pkAttr
## $captionAttrName
## $packageName
## $subPackageName
## $prjPkgName
## $prjClassPrefix
## $superClassName
## $date
## $auditTrailColumns
## $codeGen
##
#if ($packageName)
package $packageName;
#end
#parse( "ClassHeaderInclude.vm" )
import java.util.ArrayList;
import java.util.Iterator;
import thinkui.common.SearchResultsData;
import thinkui.db.DataClass;
import thinkui.db.OrderBy;
import thinkui.db.PrimaryKey;
import thinkui.db.attribute.AttributeType;
import thinkui.db.attribute.EnumType;
import thinkui.db.criteria.SearchCriteria;
import thinkui.db.criteria.SearchCriteriaFactory;
import thinkui.db.criteria.SearchCriteriaInSet;
import thinkui.db.dao.DataObjectManager;
import thinkui.db.dao.QueryData;
import ${subPackageName}.criteria.${objectName}SearchData;
import ${subPackageName}.objects.${objectName};
/**
* Implements the $className Data Access Object.
*/
public class $className extends $superClassName {
/**
* $className constructor.
*/
public $className(DataObjectManager dataObjectManager) {
super(dataObjectManager);
}
/**
* @return enum attribute type of all ${objectName} objects.
*/
public final AttributeType create${objectName}EnumType() throws Exception {
DataObjectManager objectMgr = getDataObjectManager();
String captionAttrName = ${objectName}.${captionAttrName.toUpperCase()};
java.util.List ${objectVar}s =
objectMgr.select(
${objectName}.DATA_CLASS,
SearchCriteria.EMPTY,
new OrderBy(captionAttrName));
return EnumType.create(
${objectVar}s,
AttributeType.${codeGen.getAttributeTypeConstant($pkAttr.getType())},
${objectName}.${pkAttr.getName().toUpperCase()},
captionAttrName);
}
/**
* Find all ${objectName} records matching the specified criteria.
*
* @return the results matching the specified criteria.
*/
public final SearchResultsData find${objectName}(${objectName}SearchData searchData)
throws Exception {
// Build search criteria expression (use default behaviour).
SearchCriteria criteria = SearchCriteriaFactory.create(searchData);
#if ($auditTrailColumns)
if (searchData.isValueEmpty(${objectName}SearchData.${pkAttr.getName().toUpperCase()})) {
criteria = addFromToCreateDateTimeCriteria(searchData, criteria);
}
#end
// Perform the requested query.
DataObjectManager objectMgr = getDataObjectManager();
DataClass dataClass = ${objectName}.DATA_CLASS;
SearchResultsData searchResults = new SearchResultsData();
if (searchData.isPerformCountOnly()) {
count${objectName}(
objectMgr,
dataClass,
criteria,
searchData,
searchResults);
} else {
java.util.List objects = new ArrayList();
QueryData queryData = new QueryData();
queryData.setDataClass(dataClass);
queryData.setStartIndex(searchData.getStartIndex());
queryData.setMaxObjectCount(searchData.getMaxObjectCount());
queryData.setEnforceMaxObjectCount(false);
queryData.setSelectedObjects(objects);
//queryData.setQuery();
queryData.setSearchCriteria(criteria);
queryData.setOrderBy(new OrderBy(${objectName}.${pkAttr.getName().toUpperCase()}));
objectMgr.select(queryData);
// Set back information to the client (proxy).
copyResults(searchResults, queryData);
// If the count is not known from above query,
// then perform a separate select count(*).
if (searchResults.getCount() < 0) {
count${objectName}(
objectMgr,
dataClass,
criteria,
searchData,
searchResults);
}
}
return searchResults;
}
/**
* Helper method to count all ${objectName} records matching the specified criteria.
*/
private final void count${objectName}(
DataObjectManager objectMgr,
DataClass dataClass,
SearchCriteria criteria,
${objectName}SearchData searchData,
SearchResultsData searchResults)
throws Exception {
// Check if the count was passed in the search criteria
// (i.e. known from a previous query). Otherwise, perform a
// separate select count(*) query to determine the search count.
int count = searchData.getObjectCount();
if (count < 0) {
QueryData queryData = new QueryData();
//queryData.setQuery();
queryData.setDataClass(dataClass);
queryData.setSearchCriteria(criteria);
count = objectMgr.count(queryData);
}
// Set back information to the client (proxy).
searchResults.setCount(count);
}
/**
* Get the ${objectName} object for the specified object id. If the object id is null,
* simply create and return a new empty Project object.
*
* @return the ${objectName} object for the specified object id or a new empty ${objectName} object if the id is null.
*/
public final ${objectName} get${objectName}(Object ${objectVar}Id) throws Exception {
DataObjectManager objectMgr = getDataObjectManager();
${objectName} ${objectVar};
if (${objectVar}Id != null) {
${objectVar} =
(${objectName}) objectMgr.get(
${objectName}.DATA_CLASS,
new PrimaryKey(${objectName}.${pkAttr.getName().toUpperCase()}, ${objectVar}Id));
} else {
// Create a new ${objectVar} object and initalize default values (if any).
${objectVar} = new ${objectName}();
}
return ${objectVar};
}
/**
* Save the specified ${objectName} object to the database.
*
* @return the ${objectName} object saved.
*/
public final ${objectName} save${objectName}(${objectName} ${objectVar}) throws Exception {
DataObjectManager objectMgr = getDataObjectManager();
#if ($auditTrailColumns)
populatePseudoColumns(${objectVar});
#end
objectMgr.save(${objectVar});
return ${objectVar};
}
/**
* Insert the specified ${objectName} object into the database. Any defaulted
* columns will be automatically populated and returned.
*/
public final ${objectName} insert${objectName}(${objectName} ${objectVar}) throws Exception {
DataObjectManager objectMgr = getDataObjectManager();
#if ($auditTrailColumns)
populatePseudoColumns(${objectVar});
#end
objectMgr.insert(${objectVar});
return ${objectVar};
}
/**
* Insert the specified ${objectName} object to the database.
*/
public final void insert${objectName}NoSync(${objectName} ${objectVar}) throws Exception {
DataObjectManager objectMgr = getDataObjectManager();
#if ($auditTrailColumns)
populatePseudoColumns(${objectVar});
#end
objectMgr.insertNoSync(${objectVar});
}
/**
* Delete the specified ${objectName} object from the database.
*/
public final void delete${objectName}(Object ${objectVar}Id) throws Exception {
DataObjectManager objectMgr = getDataObjectManager();
// Delete all dependent objects first.
/*
objectMgr.deleteAll(
ChildObject.DATA_CLASS,
SearchCriteriaFactory.create(
ChildObject.${tableName.toUpperCase()}_ID,
SearchCriteria.EQ,
${objectVar}Id));*/
// Finally delete the ${objectName} object.
objectMgr.delete(
${objectName}.DATA_CLASS,
new PrimaryKey(${objectName}.${pkAttr.getName().toUpperCase()}, ${objectVar}Id));
}
/**
* Delete the specified ${objectName} objects from the database.
*/
public final int delete${codeGen.toPlural($objectName)}(java.util.Set ${objectVar}Ids) throws Exception {
for (Iterator iter = ${objectVar}Ids.iterator(); iter.hasNext();) {
delete${objectName}(iter.next());
}
return ${objectVar}Ids.size();
}
/**
* Update all ${objectName} objects matching the specified search criteria.
* The number of records that was actually updated is returned.
*
* @return the number of records that was updated.
*/
public final int update${objectName}(${objectName} ${objectVar}, ${objectName}SearchData searchData)
throws Exception {
// Build search criteria expression (use default behaviour).
SearchCriteria criteria = SearchCriteriaFactory.create(searchData);
#if ($auditTrailColumns)
if (searchData.isValueEmpty(${objectName}SearchData.${pkAttr.getName().toUpperCase()})) {
criteria = addFromToCreateDateTimeCriteria(searchData, criteria);
}
#end
// Perform the requested query.
DataObjectManager objectMgr = getDataObjectManager();
return objectMgr.updateAll(${objectVar}, criteria);
}
/**
* Updated the specified ${objectName} objects with the values in the specified ${objectVar} object.
*/
public final int update${codeGen.toPlural($objectName)}(java.util.Set ${objectVar}Ids, ${objectName} ${objectVar})
throws Exception {
int count = 0;
java.util.Set modifiedAttrNames = ${objectVar}.getModifiedAttrNames();
if (!modifiedAttrNames.isEmpty()) {
DataObjectManager objectMgr = getDataObjectManager();
SearchCriteriaInSet inSet = new SearchCriteriaInSet(${objectVar}Ids);
SearchCriteria criteria =
SearchCriteriaFactory.create(
${objectName}.${pkAttr.getName().toUpperCase()},
SearchCriteria.IN,
inSet);
java.util.List objects =
objectMgr.select(${objectName}.DATA_CLASS, criteria);
// For each matching data object, copy the modified attribute values
// from ${objectVar} and update the data object.
${objectName} object;
String[] attrNames =
(String[]) modifiedAttrNames.toArray(
new String[modifiedAttrNames.size()]);
for (Iterator iter = objects.iterator(); iter.hasNext();) {
object = (${objectName}) iter.next();
object.copyFrom(${objectVar}, attrNames);
objectMgr.update(object);
}
count = objects.size();
}
return count;
}
}