View Javadoc

1   /*
2    * Copyright 2004 Carlos Sanchez.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.oness.common.webapp.controller.action;
17  
18  import javax.servlet.http.HttpServletRequest;
19  
20  import net.sf.oness.common.model.util.PaginatedList;
21  
22  import org.displaytag.tags.TableTagParameters;
23  import org.displaytag.util.ParamEncoder;
24  
25  /***
26   * Utility class with action common methods.
27   *
28   * @author Carlos Sanchez
29   * @version $Revision: 1.1 $
30   */
31  public class ActionUtils {
32  
33      public static final String RESULTS_ATTRIBUTE_NAME = "results";
34      public static final String MAX_ELEMENTS_ATTRIBUTE_NAME = "maxElements";
35  
36      private ActionUtils() {
37      }
38  
39      /***
40       * <p>Set the common needed parameters for a page-by-page iterator</p>
41       * 
42       * <p>Sets a PaginatedList (called RESULTS_ATTRIBUTE_NAME by default)
43       * and parameters for previous and next links</p>
44       * 
45       * @param list The current page
46       * @param attributeName list will be saved in the request
47       *        under this name, if null the RESULTS_ATTRIBUTE_NAME
48       *        will be used
49       * @param request
50       */
51      public static void setIteratorPageParameters(
52          PaginatedList list,
53          String attributeName,
54          HttpServletRequest request) {
55  
56          if (attributeName == null) {
57              request.setAttribute(RESULTS_ATTRIBUTE_NAME, list);
58          } else {
59              request.setAttribute(attributeName, list);
60          }
61  
62      }
63  
64      /***
65       * Set the common needed parameters for a page-by-page iterator.
66       * Equivalent to call
67       * setIteratorPageParameters(list, null, request)
68       * 
69       * @see setIteratorPageParameters(PaginatedList, String, Map, HttpServletRequest)
70       * 
71       * @param list The current page
72       * @param request
73       */
74      public static void setIteratorPageParameters(
75          PaginatedList list,
76          HttpServletRequest request) {
77          setIteratorPageParameters(list, null, request);
78      }
79  
80      /***
81       * Get the first element using displaytag page parameter from the request
82       * @todo Create the param encoder with value from table.id than null
83       * @param request
84       * @return
85       */
86      public static int getFirstElement(HttpServletRequest request) {
87          ParamEncoder paramEncoder = new ParamEncoder(null);
88  
89          String pageParameter =
90              paramEncoder.encodeParameterName(TableTagParameters.PARAMETER_PAGE);
91          String page = request.getParameter(pageParameter);
92  
93          return (page == null)
94              ? 0
95              : getMaxElements(request) * (Integer.parseInt(page) - 1);
96      }
97  
98      /***
99       * Get the max number of elements using MAX_ELEMENTS_ATTRIBUTE_NAME
100      * parameter from the request
101      * @param request
102      * @return
103      */
104     public static int getMaxElements(HttpServletRequest request) {
105         String s =
106             request.getParameter(ActionUtils.MAX_ELEMENTS_ATTRIBUTE_NAME);
107         return (s == null) ? Integer.MAX_VALUE : Integer.parseInt(s);
108     }
109 
110 }