View Javadoc

1   /* ==============================================================================
2    *   JDBCAdmin, data management software.
3    *   Copyright (C) 2005  Norsys S.A
4    *
5    *   This library is free software; you can redistribute it and/or
6    *   modify it under the terms of the GNU Lesser General Public
7    *   License as published by the Free Software Foundation; either
8    *   version 2.1 of the License, or (at your option) any later version.
9    *
10   *   This library is distributed in the hope that it will be useful,
11   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *   Lesser General Public License for more details.
14   *
15   *   You should have received a copy of the GNU Lesser General Public
16   *   License along with this library; if not, write to the Free Software
17   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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          // return value
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         // If a message is required, save the specified key(s)
121         // into the request for use by the <struts:errors> tag.
122 
123         if (!errors.isEmpty()) {
124             saveErrors(request, errors);
125         }
126         // Write logic determining how the user should be forwarded.
127         forward = mapping.findForward(strForward);
128 
129         // Finish with
130         return (forward);
131 
132     }
133 }