View Javadoc

1   /*
2    * Created on Sep 13, 2004
3    *
4    * TODO To change the template for this generated file go to
5    * Window - Preferences - Java - Code Style - Code Templates
6    */
7   package ch.ledcom.hephaistos.abstractActions;
8   
9   import java.io.IOException;
10  
11  import javax.servlet.ServletContext;
12  import javax.servlet.ServletException;
13  import javax.servlet.http.HttpServletRequest;
14  import javax.servlet.http.HttpServletResponse;
15  
16  import net.sf.hibernate.HibernateException;
17  import net.sf.hibernate.Session;
18  import net.sf.hibernate.SessionFactory;
19  import net.sf.hibernate.Transaction;
20  
21  import org.apache.log4j.Logger;
22  import org.apache.struts.action.Action;
23  import org.apache.struts.action.ActionError;
24  import org.apache.struts.action.ActionErrors;
25  import org.apache.struts.action.ActionForm;
26  import org.apache.struts.action.ActionForward;
27  import org.apache.struts.action.ActionMapping;
28  
29  import ch.ledcom.hephaistos.dao.HibernatePlugIn;
30  
31  /***
32   * @author gehel
33   *
34   * TODO To change the template for this generated type comment go to
35   * Window - Preferences - Java - Code Style - Code Templates
36   */
37  public abstract class HibernateAddAction extends Action {
38    private static final Logger logger = Logger
39    .getLogger(HibernateAddAction.class);
40  
41    protected abstract void doAdd (ActionForm form, Session session) throws HibernateException;
42    
43    public ActionForward execute(ActionMapping mapping, ActionForm form,
44        HttpServletRequest request, HttpServletResponse response)
45        throws IOException, ServletException {
46  
47      if (logger.isDebugEnabled()) {
48        logger.debug("HibernateAction - start");
49      }
50  
51      String target = "failure";
52      ActionErrors errors = new ActionErrors();
53  
54      if (form != null) {
55        Session hibSession = null;
56  
57        try { // getting session
58          ServletContext servletContext = servlet.getServletContext();
59          logger.debug("Getting session factory with key : " + HibernatePlugIn.SESSION_FACTORY_KEY);
60          SessionFactory sf = (SessionFactory) servletContext.getAttribute (HibernatePlugIn.SESSION_FACTORY_KEY);
61          hibSession = sf.openSession();
62  
63          Transaction tx = null;
64          try { // saving data
65            logger.debug("Create a transaction");
66            tx = hibSession.beginTransaction();
67  
68            doAdd(form, hibSession);
69            
70            tx.commit();
71            target = "success";
72  
73          } catch (HibernateException e) {
74            logger.error("Problem saving the data", e);
75            errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
76                "errors.hibernate.save"));
77            if (tx != null) {
78              tx.rollback();
79            }
80          } // saving data
81  
82        } catch (HibernateException he) {
83          logger.error("Hibernate session could not be opened", he);
84          errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
85              "errors.hibernate.sessionFactory.openSession"));
86        } finally {
87          try { // closing session
88            if (hibSession != null) {
89              hibSession.close();
90            }
91          } catch (HibernateException he) {
92            logger.warn("Hibernate session could not be closed", he);
93          } // closing session
94        } // getting session
95      } // form != null
96  
97      // Notify errors if any
98      if (!errors.isEmpty()) {
99        saveErrors(request, errors);
100     }
101     // Forward
102     return mapping.findForward(target);
103   }
104 
105 }