View Javadoc
1   /*
2    * 
3    * $Revision: 1.8 $
4    * $Date: 2005/03/15 21:16:23 $
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.context;
27  
28  import net.sf.hibernate.Session;
29  
30  import org.strutsit.architecture.context.StrutsitContext;
31  import org.strutsit.architecture.hibernate.HibernateContext;
32  import org.strutsit.architecture.struts.StrutsitAction;
33  import org.strutsit.architecture.struts.StrutsitActionBase;
34  import org.strutsit.architecture.util.HibernateUtil;
35  
36  /***
37   * <p>
38   * Example of a pre-processed action in case of using the struts-it architecture
39   * framework. This pre-processed action creates a Hibernate session for this thread.
40   * </p>
41   * 
42   * <p>
43   * <b>Responsibilities:</b>
44   * <ul>
45   * <li>Providing the hibernate session for this thread.</li>
46   * </ul>
47   * </p>
48   * 
49   * @author <a href="mailto:wolff@struts-it.de">Manfred Wolff</a>
50   * @since JDK 1.4
51   * @version $Revision: 1.8 $
52   */
53  
54  public class PrepareContext extends StrutsitActionBase implements
55          StrutsitAction {
56  
57      // Nothing return value for the use-case driven approuch.
58      static final String nothing = "";
59  
60      /***
61       * Execute-Methods for Struts-It Actions. This Action prepares the context
62       * in that way, that the hibernate session of this session is always set.
63       * 
64       * @param  context The Context for this usecase
65       * @return True, if the request is fulfilled. In this case the context has
66       *         to return the next step. False, if the request is not fulfilled
67       *         yet.
68       * @throws Exception
69       */
70      public boolean execute(StrutsitContext context) throws Exception {
71  
72          // This must be a hibernate context
73          if (!(context instanceof HibernateContext)) {
74              throw new Exception("wrong context");
75          }
76  
77          // Sets the correct hibernate session into the context.
78          Session session = HibernateUtil.currentSession();
79          ((HibernateContext) context).setHibSession(session);
80  
81          return false;
82      }
83  
84      /***
85       * Gets the corresponded use case for this action. In this case it returns
86       * nothing.
87       * 
88       * @return An empty String
89       */
90      public String getUseCase() {
91          return nothing;
92      }
93  }