1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package jdbcadmin.web.actions;
21
22 import java.util.List;
23
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 import jdbcadmin.core.data.Line;
28 import jdbcadmin.core.data.Table;
29 import jdbcadmin.core.exceptions.ConvertionException;
30 import jdbcadmin.core.exceptions.DangerousOperationException;
31 import jdbcadmin.web.ContexteManager;
32 import jdbcadmin.web.util.HttpWrapper;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.apache.struts.action.ActionError;
37 import org.apache.struts.action.ActionErrors;
38 import org.apache.struts.action.ActionForm;
39 import org.apache.struts.action.ActionForward;
40 import org.apache.struts.action.ActionMapping;
41
42 /***
43 * @struts.action name="actionForm" path="/persist"
44 * @struts.action-forward name="main" path="/select.jsp"
45 * @struts.action-forward name="insertFailure" path="/insert.jsp"
46 * @struts.action-forward name="updateFailure" path="/update.jsp"
47 * @author Thomas Recloux (trecloux@norsys.fr)
48 */
49 public class PersistAction extends IndexedAction {
50
51 /*** Logger */
52 private static Log logger = LogFactory.getLog(PersistAction.class);
53
54 /***
55 * Constructor
56 */
57 public PersistAction() {
58 super();
59 }
60
61 /***
62 * {@inheritDoc}
63 */
64 public ActionForward indexedExec(
65 ActionMapping mapping,
66 ActionForm form,
67 HttpServletRequest request,
68 HttpServletResponse response)
69 throws Exception {
70
71 ActionErrors errors = new ActionErrors();
72 jdbcadmin.web.forms.ActionForm actionForm = (jdbcadmin.web.forms.ActionForm) form;
73 String strForward = "main";
74 ActionForward forward;
75
76
77 try {
78 Table tab = ContexteManager.getCurrentTable(request);
79 Line line = new Line(tab);
80 HttpWrapper.wrapLine(request, line);
81
82 request.setAttribute("line", line);
83
84 if (actionForm.getTypeAction().equals("update")) {
85 try {
86 ContexteManager.getManager(request).update(line);
87 request.setAttribute("messageInfo", getResources(request).getMessage("message.update.ok"));
88 } catch (DangerousOperationException doe) {
89 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.dangerousOp"));
90 strForward = "updateFailure";
91 } catch (ConvertionException ce) {
92 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.convertion"
93 , ce.getPropertyName(), ce.getSampleValue()));
94 strForward = "updateFailure";
95 }
96
97 } else if (actionForm.getTypeAction().equals("insert")) {
98 try {
99 ContexteManager.getManager(request).insert(line);
100 request.setAttribute("messageInfo", getResources(request).getMessage("message.insert.ok"));
101 } catch (ConvertionException ce) {
102 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.convertion"
103 , ce.getPropertyName(), ce.getSampleValue()));
104 strForward = "insertFailure";
105 }
106 }
107
108 List lignes = ContexteManager.getManager(request).select(tab,
109 ContexteManager.getIdxDonnees(request),
110 Constants.NB_LINES_PER_PAGE);
111 request.setAttribute("lines", lignes);
112 } catch (Exception e) {
113 if (logger.isErrorEnabled()) {
114 logger.error("Unexpected error ", e);
115 }
116 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.generic", e.getMessage()));
117 strForward = "error";
118 }
119
120
121
122
123 if (!errors.isEmpty()) {
124 saveErrors(request, errors);
125 }
126
127 forward = mapping.findForward(strForward);
128
129
130 return (forward);
131
132 }
133 }