View Javadoc
1   /*
2    * 
3    * $Revision: 1.15 $
4    * $Date: 2005/03/15 21:16:21 $
5    *
6    * ====================================================================
7    * struts-it
8    * Copyright (C) 2004-2005 - Manfred Wolff and the strutsit community
9    * 
10   * Licensed under the Apache License, Version 2.0 (the "License");
11   * you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   * 
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   * 
16   * Unless required by applicable law or agreed to in writing, software
17   * distributed under the License is distributed on an "AS IS" BASIS,
18   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   * See the License for the specific language governing permissions and
20   * limitations under the License.
21   *
22   * Projekt       : struts-it solutions for webbased projects
23   * Part          : demo application for the struts-it architecture
24   * Created       : 26.02.2005 mwolff
25   */
26  package org.struts.velocity.demo.login;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.struts.action.ActionMessage;
31  import org.apache.struts.action.ActionMessages;
32  import org.struts.velocity.demo.util.Constants;
33  import org.strutsit.architecture.context.StrutsitContext;
34  import org.strutsit.architecture.hibernate.HibernateContext;
35  import org.strutsit.architecture.service.Service;
36  import org.strutsit.architecture.struts.StrutsitAction;
37  import org.strutsit.architecture.struts.StrutsitActionBase;
38  
39  /***
40   * <p>
41   * The accountaction is called, when the user will create a new account for
42   * himself. First it looks if the account already exists. In this case an error
43   * message will provide. In the other case a new account will be created and a
44   * message will be sendet to the new user.
45   * </p>
46   * <p>
47   * Demonstrates:
48   * <ul>
49   * <li>How to use Action-Messages under Velocity.</li>
50   * <li>How to go back to the input form.</li>
51   * </ul>
52   * </p>
53   * 
54   * @author <a href="mailto:wolff@struts-it.de">Manfred Wolff</a>
55   * @since JDK 1.4
56   * @version $Revision: 1.15 $
57   */
58  public class AccountAction extends StrutsitActionBase implements StrutsitAction {
59  
60      /// the logger for this class
61      private static Log log = LogFactory.getLog(AccountAction.class);
62  
63      /***
64       * Execute-Methods for Struts-It Actions.
65       * 
66       * @param context The Context for this usecase
67       * @return true, if the request is fulfilled. In this case the context has
68       *         to return the next step. False, if the request is not fulfilled
69       *         yet.
70       */
71      public boolean execute(StrutsitContext context) throws Exception {
72  
73          if (log.isTraceEnabled()) {
74              log.trace("execute -> START");
75          }
76  
77          HibernateContext hibContext = (HibernateContext) context;
78  
79          // Gets the service from the hivemind registry.
80          Service service = getService("org.struts.velocity.demo.service.LoginService");
81  
82          // Puts an action into the context.
83          hibContext.setAction(Constants.LOGIN_ACOUNT_EXISTS);
84  
85          // executes the servcie
86          service.execute(hibContext);
87  
88          boolean result = hibContext.getResult();
89  
90          // error handling, if there went something wrong.
91          if (result == true) {
92              ActionMessages errors = new ActionMessages();
93              errors.add("", new ActionMessage("account.error.exists"));
94  
95              saveMessages(hibContext.getRequest(), errors);
96              setInput(context);
97  
98              if (log.isTraceEnabled()) {
99                  log.trace("execute with input -> END");
100             }
101             return true;
102         }
103 
104         // Puts another action into the context an executes the service again.
105         hibContext.setAction(Constants.LOGIN_NEW_ACOUNT);
106         service.execute(hibContext);
107 
108         // clears ressources
109         hibContext.resetForm();
110         hibContext.release();
111 
112         setSucceeded(context);
113 
114         if (log.isTraceEnabled()) {
115             log.trace("execute with success -> END");
116         }
117         return true;
118     }
119 
120     /***
121      * The use-case for the usecase driven approuch.
122      */
123     public String getUseCase() {
124         return "login";
125     }
126 }