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.Table;
28  import jdbcadmin.core.exceptions.DataNotFoundException;
29  import jdbcadmin.web.ContexteManager;
30  import jdbcadmin.web.forms.OtherDataForm;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.apache.struts.action.ActionError;
35  import org.apache.struts.action.ActionErrors;
36  import org.apache.struts.action.ActionForm;
37  import org.apache.struts.action.ActionForward;
38  import org.apache.struts.action.ActionMapping;
39  
40  /***
41   * Retrieves more data
42   * @struts.action
43   * name="otherDataForm"
44   * path="/otherData"
45   * @struts.action-forward
46   * name="main"
47   * path="/select.jsp"
48   * @author Thomas Recloux (trecloux@norsys.fr)
49   */
50  public class OtherDataAction extends IndexedAction {
51  
52      /*** Logger */
53      private static Log logger = LogFactory.getLog(OtherDataAction.class);
54  
55      /*** {@inheritDoc} */
56      public ActionForward indexedExec(
57          ActionMapping mapping,
58          ActionForm form,
59          HttpServletRequest request,
60          HttpServletResponse response)
61          throws Exception {
62  
63  
64          if (logger.isDebugEnabled()) {
65              logger.debug("BEGIN OtherDataAction");
66          }
67          ActionErrors errors = new ActionErrors();
68          ActionForward forward = new ActionForward();
69          String strFwd = "main";
70          // return value
71          OtherDataForm autresDonneesForm = (OtherDataForm) form;
72          int idxOriginal = 0;
73  
74          try {
75              idxOriginal = ContexteManager.getIdxDonnees(request);
76              int idx = idxOriginal;
77              if (OtherDataForm.DIR_AFTER.equals(autresDonneesForm.getDirection())) {
78                  idx = idx + Constants.NB_LINES_PER_PAGE;
79              } else {
80                  idx = idx - Constants.NB_LINES_PER_PAGE;
81                  if (idx < 0) {
82                      idx = 0;
83                  }
84              }
85              if (logger.isDebugEnabled()) {
86                  logger.debug("Start index: " + idx);
87              }
88              Table tab = ContexteManager.getCurrentTable(request);
89              if (logger.isDebugEnabled()) {
90                  logger.debug("Table  : " + tab.getCompleteName());
91              }
92              List lignes = ContexteManager.getManager(request).select(tab
93                      , idx, Constants.NB_LINES_PER_PAGE);
94              if (logger.isDebugEnabled()) {
95                  logger.debug(lignes.size() + " lignes retrieved");
96              }
97              ContexteManager.setIdxDonnees(request, idx);
98              request.setAttribute("lines", lignes);
99          } catch (DataNotFoundException dnfe) {
100 
101             try {
102                 Table tab = ContexteManager.getCurrentTable(request);
103                 List lignes = ContexteManager.getManager(request).select(tab
104                         , idxOriginal, Constants.NB_LINES_PER_PAGE);
105                 ContexteManager.setIdxDonnees(request, idxOriginal);
106                 request.setAttribute("lines", lignes);
107                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.endtable"));
108                 strFwd = "main";
109             } catch (Exception e) {
110                 if (logger.isErrorEnabled()) {
111                     logger.error("Erreur non prévue lors l'action de recuperation d'autres données", e);
112                 }
113                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("erreur.generique", e));
114                 strFwd = "erreur";
115             }
116         } catch (Exception e) {
117             if (logger.isErrorEnabled()) {
118                 logger.error("Unexexted error retrieving other data", e);
119             }
120             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.generic", e));
121             strFwd = "error";
122         }
123 
124         // If a message is required, save the specified key(s)
125         // into the request for use by the <struts:errors> tag.
126 
127         if (!errors.isEmpty()) {
128             saveErrors(request, errors);
129         }
130         // Write logic determining how the user should be forwarded.
131         forward = mapping.findForward(strFwd);
132 
133         if (logger.isDebugEnabled()) {
134             logger.debug("END OtherDataAction");
135         }
136         // Finish with
137         return (forward);
138 
139     }
140 }