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 Window -
35 * Preferences - Java - Code Style - Code Templates
36 */
37 public abstract class HibernateDeleteAction extends Action {
38 private static final Logger logger = Logger
39 .getLogger(HibernateDeleteAction.class);
40
41 protected abstract ActionForm doDelete(Long id, Session session)
42 throws HibernateException;
43
44 public ActionForward execute(ActionMapping mapping, ActionForm form,
45 HttpServletRequest request, HttpServletResponse response)
46 throws IOException, ServletException {
47
48 if (logger.isDebugEnabled()) {
49 logger.debug("HibernateDeleteAction - start");
50 }
51
52 String target = "failure";
53 ActionErrors errors = new ActionErrors();
54
55 Session hibSession = null;
56
57 try {
58 ServletContext servletContext = servlet.getServletContext();
59 logger.debug("Getting session factory with key : "
60 + HibernatePlugIn.SESSION_FACTORY_KEY);
61 SessionFactory sf = (SessionFactory) servletContext
62 .getAttribute(HibernatePlugIn.SESSION_FACTORY_KEY);
63 hibSession = sf.openSession();
64
65 Transaction tx = null;
66 try {
67 logger.debug("Create a transaction");
68 tx = hibSession.beginTransaction();
69
70 Long id = new Long(request.getParameter("id"));
71
72 doDelete(id, hibSession);
73
74 tx.commit();
75
76 target = "success";
77
78 } catch (HibernateException e) {
79 logger.error("Problem saving the data", e);
80 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
81 "errors.hibernate.save"));
82 if (tx != null) {
83 tx.rollback();
84 }
85 }
86
87 } catch (HibernateException he) {
88 logger.error("Hibernate session could not be opened", he);
89 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
90 "errors.hibernate.sessionFactory.openSession"));
91 } finally {
92 try {
93 if (hibSession != null) {
94 hibSession.close();
95 }
96 } catch (HibernateException he) {
97 logger.warn("Hibernate session could not be closed", he);
98 }
99 }
100
101
102 if (!errors.isEmpty()) {
103 saveErrors(request, errors);
104 }
105
106 return mapping.findForward(target);
107 }
108
109 }