1
2
3
4
5
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 HibernateGetAction extends Action {
38 private static final Logger logger = Logger
39 .getLogger(HibernateGetAction.class);
40
41 protected abstract ActionForm doGet (Long id, 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 {
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 {
65 logger.debug("Create a transaction");
66 tx = hibSession.beginTransaction();
67
68 Long id = new Long(request.getParameter("id"));
69
70 form = doGet(id, hibSession);
71
72 tx.commit();
73
74 if (form == null) {
75 logger.info("form is null");
76 }
77
78 logger.debug("saving the form in the session with key : " + mapping.getAttribute());
79 request.setAttribute(mapping.getAttribute(), form);
80
81 logger.debug("form : " + form.toString());
82
83 target = "success";
84
85 } catch (HibernateException e) {
86 logger.error("Problem saving the data", e);
87 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
88 "errors.hibernate.save"));
89 if (tx != null) {
90 tx.rollback();
91 }
92 }
93
94 } catch (HibernateException he) {
95 logger.error("Hibernate session could not be opened", he);
96 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
97 "errors.hibernate.sessionFactory.openSession"));
98 } finally {
99 try {
100 if (hibSession != null) {
101 hibSession.close();
102 }
103 } catch (HibernateException he) {
104 logger.warn("Hibernate session could not be closed", he);
105 }
106 }
107 }
108
109
110 if (!errors.isEmpty()) {
111 saveErrors(request, errors);
112 }
113
114 return mapping.findForward(target);
115 }
116
117 }