1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
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      
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          
73          if (!(context instanceof HibernateContext)) {
74              throw new Exception("wrong context");
75          }
76  
77          
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  }