From: <jm...@us...> - 2005-07-11 07:13:26
|
Update of /cvsroot/struts/dialogs/src/net/jspcontrols/dialogs/samples/wizardaction/wizardsimple In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12963/src/net/jspcontrols/dialogs/samples/wizardaction/wizardsimple Added Files: SimpleSignupAction.java SimpleSignupForm.java Log Message: --- NEW FILE: SimpleSignupAction.java --- /* * Copyright 2004-2005 Michael Jouravlev * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.jspcontrols.dialogs.samples.wizardaction.wizardsimple; import net.jspcontrols.dialogs.actions.wizard.WizardAction; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForm; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * A concrete wizard action class. Does nothing more than a regular * WizardAction, but setting response as non-cachable. * * @author Michael Jouravlev */ public class SimpleSignupAction extends WizardAction { /** * Returns an <code>ActionForward</code> instance describing the View * for current dialog state, usually a forward to a JSP page. * * @param mapping The ActionMapping used to select this instance * @param form The optional ActionForm bean for this request (if any) * @param request The HTTP request we are processing * @param response The HTTP response we are creating * * @exception Exception if an exception occurs * @return ActionForward instance describing the View for dialog state */ public ActionForward getDialogView(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { setNoCache(response); return super.getDialogView(mapping, form, request, response); } } --- NEW FILE: SimpleSignupForm.java --- /* * Copyright 2004-2005 Michael Jouravlev * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.jspcontrols.dialogs.samples.wizardaction.wizardsimple; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.util.LabelValueBean; import net.jspcontrols.wizard.intf.IWizard; import net.jspcontrols.wizard.intf.IWizardListener; import net.jspcontrols.dialogs.actions.wizard.IWizardManager; import net.jspcontrols.dialogs.actions.wizard.WizardConstants; import net.jspcontrols.dialogs.samples.wizardaction.rules.SignupWizard; import net.jspcontrols.dialogs.samples.wizardaction.accounts.UserAccounts; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.util.Map; import java.util.HashMap; import java.util.ArrayList; /** * Wizard Manager for New User Signup wizard. For simplicity, this action form * does not use base WizardForm. Uses standard WizardAction as dispatching action. * * @author Michael Jouravlev */ public class SimpleSignupForm extends ActionForm implements IWizardManager, IWizardListener { /************************************************************************** * Wizard Controller **************************************************************************/ /** * Aggregated wizard ontroller; must be set prior to populate phase */ private IWizard wizard; /** * Returns aggregated wizard controller, typecast to concrete type. * Used to access properties of this from JSP page. */ public SignupWizard getSignupWizard() { return (SignupWizard) wizard; } /** * Returns aggregated wizard controller as generic wizard. This method * is not used in this simple action form, it comes handy when * base WizardForm class is utilized. */ public IWizard getWizard() { return wizard; } /** * Returns errors accumulated by wizard during processing of input data. */ public Map getWizardErrors() { return wizard != null ? wizard.getWizardErrors() : new HashMap(); } /************************************************************************** * Session object is used as login storage and to access user accounts **************************************************************************/ /** * This demo uses session to store login info and to access user accounts. */ HttpSession session; /** * Returns current session. Used to access user accounts. */ public HttpSession getSession() { return session; } /************************************************************************** * Wizard Navigation **************************************************************************/ /** * Cancels wizard. * @return string mapping describing the after-cancel View */ public String wizardCancel() { disposeWizard(); return WizardConstants.MAPPING_ON_CANCEL; } /** * Tries to go one step back. Wizard does not allow to move past first * step. * @return string mapping describing the after-back View. * If successfully moved one step back, MAPPING_ON_BACK_SUCCESS * is returned. Otherwise, MAPPING_ON_BACK_FAILURE is returned. * @see WizardConstants#MAPPING_ON_BACK_SUCCESS * @see WizardConstants#MAPPING_ON_BACK_FAILURE */ public String wizardBack() { return wizard != null && wizard.back() ? WizardConstants.MAPPING_ON_BACK_SUCCESS : WizardConstants.MAPPING_ON_BACK_FAILURE; } /** * Tries to go one step forward. * @return string mapping describing the after-forward View * If successfully moved one step forward, and wizard is not finished * yet, MAPPING_ON_NEXT_SUCCESS is returned. If wizard was finished, * MAPPING_ON_DONE is returned. Otherwise, MAPPING_ON_NEXT_FAILURE * is returned. * @see WizardConstants#MAPPING_ON_NEXT_SUCCESS * @see WizardConstants#MAPPING_ON_NEXT_FAILURE * @see WizardConstants#MAPPING_ON_DONE */ public String wizardNext() { if (wizard != null && wizard.forward()) { if (wizard.isCompleted()) { disposeWizard(); return WizardConstants.MAPPING_ON_DONE; } else { return WizardConstants.MAPPING_ON_NEXT_SUCCESS; } } return WizardConstants.MAPPING_ON_NEXT_FAILURE; } /** * Disposes wizard and performs housekeeping tasks like removing messages, * event listeneres and other objects created by wizard. */ public void disposeWizard() { // Cleanup wizard if (wizard != null) { wizard.removeAllListeners(); wizard = null; } // Remove combobox values session.removeAttribute("simple-signup-questions"); } /** * Returns true if wizard was completed or was never instantiated; * after wizard completes, it should not be accessed anymore. */ public boolean isCompleted() { return wizard != null ? wizard.isCompleted() : true; } /** * Returns string mapping of the wizard page, corresponding to * current wizard state. By convention, uses the name of wizard step. * Always use unique names for wizard steps. */ public String getWizardView() { return wizard != null ? wizard.getCurrentStepName() : null; } /************************************************************************** * Implementing IWizardListener **************************************************************************/ /** * Fires event when wizard is about to move forward from current step. * * @param event IWizardListener.STEP_REGULAR if about to leave "middle" step, * IWizardListener.STEP_LAST if about to leave the final wizard * step, effectively finishing the wizard. * @return true if it is allowed to move to the next step, false if step * must not be changed. * @see IWizardListener#STEP_REGULAR * @see IWizardListener#STEP_LAST */ public boolean onTransition(int event) { // Not finishing wizard ==> not interested if (event != IWizardListener.STEP_LAST) return true; // Wizard is about to complete ==> store new user account if (UserAccounts.addUser( session, getSignupWizard().getStepSignup().getName(), getSignupWizard().getStepSignup().getPassword(), getSignupWizard().getStepDetails().getSecurityAnswerId(), getSignupWizard().getStepDetails().getSecurityAnswer(), true) ) return true; // Account was not stored ==> generate error, do not dispose wizard getWizardErrors().put("loginsignupcontrol.loginexists", new String[] {getSignupWizard().getStepSignup().getName()}); return false; } /************************************************************************** * ActionForm methods **************************************************************************/ /** * Resets action form. This method is called each time request is received. * Initialize wizard here if needed, and clear checkboxes. * * @param mapping The ActionMapping used to select this instance * @param request The HTTP request we are processing */ public void reset(ActionMapping mapping, HttpServletRequest request) { super.reset(mapping, request); // This form must have session scope to store CRUD data if (!"session".equalsIgnoreCase(mapping.getScope())) { throw new IllegalStateException("Action " + mapping.getPath() + "should have session scope"); } // Session is used to store login name session = request.getSession(); // This wizard is not a control, it does not have stub pages. // So, initialize wizard each time user navigates to this action // and wizard controller does not exist yet. if (wizard == null) { // Create new Wizard Controller instance; // Use internal to Rule Engine object for error messages. wizard = new SignupWizard(null); // Use this action form as wizard event listener; // it stores account info in the account database when // wizard is about to finish. wizard.addListener(this); // Build combobox data fro wizard array data; // rule engine does not deal with types specific to Struts, // it returns a simple string array. ArrayList questions = new ArrayList(); String[] wizardQuestions = getSignupWizard().getStepDetails().getSecurityQuestions(); for (int i = 0; i < wizardQuestions.length; i++) { questions.add(new LabelValueBean(wizardQuestions[i], Integer.toString(i))); } // This is a combobox object for security question on second step. session.setAttribute( "simple-signup-questions", questions ); } // Important: clear checkboxes for POST requests only! if ("POST".equalsIgnoreCase(request.getMethod())) { wizard.wizardReset(); } } } |