## ============================================================================ ## Filename : WebShowViewCmdTemplate.vm ## Note(s) : This template is used to generate a ShowViewScreenCommand 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 ## $date ## $fkRelationships ## $codeGen ## #if ($packageName) package $packageName; #end #parse( "ClassHeaderInclude.vm" ) import thinkui.command.Command; import thinkui.command.CommandContext; import thinkui.command.Parameter; import thinkui.common.Utils; import thinkui.db.DataClass; import thinkui.db.DefaultDataClass; import thinkui.db.attribute.Attribute; import thinkui.db.attribute.AttributeType; import thinkui.${prjPkgName}.objects.${objectName}; import thinkui.${prjPkgName}.proxies.${prjClassPrefix}Proxy; import thinkui.${prjPkgName}.web.${prjClassPrefix}SessionManager; import thinkui.${prjPkgName}.web.screens.View${objectName}Screen; import thinkui.web.${superClassName}; import thinkui.web.JSPAction; import thinkui.web.JSPScreen; import thinkui.web.WebCommandContext; /** * Extends ${superClassName} to implement a command to show the View${objectName}Screen. * The ${objectName}.ID parameter is used to retrieve the desired object to display. * If the ${objectName}.ID parameter was not specified, the ${objectName} object from the session is * used. If no object is in the session, a new empty ${objectName} object is created and * placed in the session. */ public class ${className} extends ${superClassName} { public static final String ID = ${objectName}.ID; private static final Attribute[] attrs = new Attribute[] { new Parameter(${pkAttr.getName().toUpperCase()}, "${pkAttr.getCaption()}", AttributeType.${codeGen.getAttributeTypeConstant($pkAttr.getType())})}; public static final DataClass DATA_CLASS = new DefaultDataClass("${className}", attrs).addAttributes( ${superClassName}.DATA_CLASS); /** * ${className} constructor. */ public ${className}() { super(); } public final DataClass getDataClass() { return DATA_CLASS; } 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 (getValue(ID) != null || sessionMgr.get${objectName}() != null); } protected final void createActions( WebCommandContext ctx, JSPScreen screen, ${objectName} ${objectVar}) throws Exception { // Get the previous command (if any) and determine what actions are // available based on it. String previousCmd = getPreviousCommand(); // Create the default actions. Command saveCmd = new Save${objectName}Command(); saveCmd.copyFrom(this); screen.addAction(new JSPAction("save", " Save ", saveCmd)); screen.addAction( new JSPAction("resetForm", " Reset ", JSPAction.RESET)); if (!${objectVar}.isNew()) { Delete${objectName}Command deleteCmd = new Delete${objectName}Command(); deleteCmd.copyFrom(this); Object ${objectVar}Id = ${objectVar}.getValue(${objectName}.ID); deleteCmd.setObjectId(${objectVar}Id); JSPAction deleteAction = new JSPAction("delete", "Delete", deleteCmd); deleteAction.setConfirmRequired(true); deleteAction.setDescription( "delete ${objectCaption.toLowerCase()} " + ${objectVar}Id + "."); screen.addAction(deleteAction); } Command cancelCmd; if ("search".equals(previousCmd)) { cancelCmd = new ShowSearch${objectName}Command(); } else { cancelCmd = new ShowHomeCommand(); } cancelCmd.copyFrom(this); screen.addAction(new JSPAction("cancel", "Cancel", cancelCmd)); } public final Object execute(CommandContext commandContext) throws Exception { WebCommandContext ctx = (WebCommandContext) commandContext; ${prjClassPrefix}SessionManager sessionMgr = new ${prjClassPrefix}SessionManager(ctx); Object ${objectVar}Id = getValue(ID); ${objectName} ${objectVar}; if (${objectVar}Id != null || (${objectVar} = sessionMgr.get${objectName}()) == null) { ${prjClassPrefix}Proxy proxy = (${prjClassPrefix}Proxy) ctx.lookupProxy(${prjClassPrefix}Proxy.NAME); try { ${objectVar} = proxy.get${objectName}(${objectVar}Id); } finally { proxy.remove(); } sessionMgr.set${objectName}(${objectVar}); } // Register dynamic attribute types with the command context. /* TODO -- Uncomment if applicable, otherwise remove generated code. #foreach( $relationship in $fkRelationships ) ctx.overrideAttribute( ${objectVar}, ${objectName}.${codeGen.toString($relationship.getFromAttrNames(), null, null, "_")}, sessionMgr.create${codeGen.toClassName($relationship.getToDataClassName())}EnumType()); #end */ View${objectName}Screen screen = new View${objectName}Screen(); screen.setObject("${objectVar}", ${objectVar}); setScreenTitle(screen, ${objectVar}, "${objectCaption}"); // Create the actions available in this screen createActions(ctx, screen, ${objectVar}); return screen; } }