## ============================================================================ ## Filename : WebSaveCmdTemplate.vm ## Note(s) : This template is used to generate a SaveCommand Java class. ## ## 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 ## $objectName ## $objectVar ## $pkAttr ## $objectCaption ## $className ## $packageName ## $prjPkgName ## $prjClassPrefix ## $superClassName ## $dataClass ## $date ## $codeGen ## $attributes ## #if ($packageName) package $packageName; #end #parse( "ClassHeaderInclude.vm" ) import java.util.ArrayList; import thinkui.command.CommandContext; import thinkui.db.DataObject; import thinkui.${prjPkgName}.objects.${objectName}; import thinkui.${prjPkgName}.proxies.${prjClassPrefix}Proxy; import thinkui.${prjPkgName}.web.${prjClassPrefix}SessionManager; import thinkui.validator.DataObjectValidator; import thinkui.web.${superClassName}; import thinkui.web.AbstractWebCommand; import thinkui.web.WebCommandContext; /** * Implements a command to save the ${objectName} object. */ public class ${className} extends ${superClassName} { /** * ${className} constructor. */ public ${className}() { super(); } /* public final ApplicationPermission getPermission() { return Permissions.ADMINISTRATOR_ROLE; } */ /** * @return whether or not the precondition for the command has been met. */ public final boolean isPrecondition(CommandContext context) { WebCommandContext ctx = (WebCommandContext) context; ${prjClassPrefix}SessionManager sessionMgr = new ${prjClassPrefix}SessionManager(ctx); // Ensure the user session is synchronized (protect from back button and direct access). return (sessionMgr.get${objectName}() != null); } /** * Set the data object attribute values from the request into the data object. * @return the data object set with the corresponding values from the request. * @throws Exception */ protected final DataObject setValues(WebCommandContext ctx) throws Exception { ${prjClassPrefix}SessionManager sessionMgr = new ${prjClassPrefix}SessionManager(ctx); ${objectName} ${objectVar} = sessionMgr.get${objectName}(); ctx.setDataObjectValues(${objectVar}); return ${objectVar}; } /** * Validate the data object attribute values based on the required business rules * and put any errors into the command context. * @return whether or not the save command should be performed if the validation passed. */ protected final boolean validateValues( WebCommandContext ctx, DataObject dataObject) throws Exception { ${objectName} ${objectVar} = (${objectName}) dataObject; DataObjectValidator validator = DataObjectValidator.getValidator(${objectVar}); java.util.Collection validateAttrNames = new ArrayList(); #foreach( $attr in $attributes ) #if ($codeGen.isIncludeAttrName($attr.getName()) && !$codeGen.isSyncAttrName($dataClass, $attr.getName())) validateAttrNames.add(${objectName}.${attr.getName().toUpperCase()}); #end #end validator.setValidateAttrNames(validateAttrNames); validator.validate(ctx); return true; } /** * Save the data object attribute values to the database. At this stage, the save * command should be expected to be successful since all necessary validation has been * performed. If however if there are any potential exceptions that can occur and * special handling is required, it will need to be trapped and handled here. Otherwise, * any uncaught exceptions will be trapped in the command dispatcher and a generic error * screen will be displayed. */ protected final void saveValues( WebCommandContext ctx, DataObject dataObject) throws Exception { ${objectName} ${objectVar} = (${objectName}) dataObject; ${prjClassPrefix}SessionManager sessionMgr = new ${prjClassPrefix}SessionManager(ctx); ${prjClassPrefix}Proxy proxy = (${prjClassPrefix}Proxy) ctx.lookupProxy(${prjClassPrefix}Proxy.NAME); try { ${objectVar} = proxy.save${objectName}(${objectVar}); sessionMgr.set${objectName}(${objectVar}); } finally { proxy.remove(); } // Clear cached data from the user session (if any). sessionMgr.set${objectName}EnumType(null); String message = "${objectCaption} information for " + ${objectVar}.getValue(${objectName}.ID) + " successfully saved."; ctx.addMessage(message); } /** * Return the command that should be forwarded to upon a successful save. Typically, the * command that is returned is the command used to display the module menu or search command. * * @return the command that should be forwarded to upon a successful save. */ protected final AbstractWebCommand getSuccessCommand( WebCommandContext ctx, DataObject dataObject) throws Exception { ShowSearch${objectName}Command cmd = new ShowSearch${objectName}Command(); cmd.setClearSearchCriteria(false); return cmd; /* TODO -- Uncomment if applicable, otherwise remove generated code. String previousCmd = getPreviousCommand(); AbstractWebCommand cmd; if ("${objectVar}Details".equals(previousCmd)) { cmd = new ShowView${objectName}DetailsCommand(); cmd.setValue( ShowView${objectName}DetailsCommand.${objectName.toUpperCase()}_ID, dataObject.getValue(${objectName}.ID)); } else { ShowSearch${objectName}Command showSearchCmd = new ShowSearch${objectName}Command(); showSearchCmd.setClearSearchCriteria(false); cmd = showSearchCmd; } return cmd; */ } /** * Return the command that should be forwarded to upon an unsuccessful save. Typically, the * command that is returned is the command used to display the show view screen command. * * @return the command that should be forwarded to upon an unsuccessful save. */ protected final AbstractWebCommand getFailureCommand( WebCommandContext ctx, DataObject dataObject) throws Exception { ShowView${objectName}Command cmd = new ShowView${objectName}Command(); return cmd; } }