View Javadoc

1   /***
2    * Copyright (c) 2003 held jointly by the individual authors. This library is
3    * free software; you can redistribute it and/or modify it under the terms of
4    * the GNU Lesser General Public License as published by the Free Software
5    * Foundation; either version 2.1 of the License, or (at your option) any later
6    * version. This library is distributed in the hope that it will be useful, but
7    * WITHOUT ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY
8    * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
9    * License for more details. You should have received a copy of the GNU Lesser
10   * General Public License along with this library; if not, write to the Free
11   * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
12   * USA. > http://www.gnu.org/copyleft/lesser.html >
13   * http://www.opensource.org/licenses/lgpl-license.php
14   */
15  
16  package net.mlw.vlh.web.mvc;
17  
18  import java.util.Map;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpSession;
22  
23  import net.mlw.vlh.ValueList;
24  import net.mlw.vlh.ValueListHandler;
25  import net.mlw.vlh.ValueListInfo;
26  import net.mlw.vlh.web.ValueListRequestUtil;
27  import net.mlw.vlh.web.tag.TableInfo;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /***
33   * This package is used in mvc controlers to better handle with the
34   * ValueListHandler. It allow you to
35   * <li>backup ValueList <b>info </b> to the session before redirect!</li>
36   * <li>modify paging possition, focusing property and values, sorting order,
37   * etc.</li>
38   * <h5>Usage</h5>
39   * Please, before use, set a ValueListHandler property with a reference to your
40   * valueListHandler!</h4>
41   * 
42   * @author Andrej Zachar
43   * @version $Revision: 1.6 $ $Date: 2005/11/23 14:19:45 $
44   */
45  public class ValueListHandlerHelper
46  {
47     /***
48      * Logger for this class
49      */
50     private static final Log LOGGER = LogFactory.getLog(ValueListHandlerHelper.class);
51  
52     private ValueListHandler _valueListHandler = null;
53  
54     /***
55      * @param valueListHandler The valueListHandler to set.
56      */
57     public void setValueListHandler(ValueListHandler valueListHandler)
58     {
59        _valueListHandler = valueListHandler;
60     }
61  
62     /***
63      * Get a bean of the ValueListHandler.
64      * 
65      * @return ValueListHandler
66      */
67     public ValueListHandler getValueListHandler()
68     {
69        return _valueListHandler;
70     }
71  
72     /***
73      * Retrieve valueList from ValueListHadler bean.
74      * 
75      * @param name the name of the Adapter
76      * @param info
77      * @return ValueList
78      */
79     public ValueList getValueList(String name, ValueListInfo info)
80     {
81        return getValueListHandler().getValueList(name, info);
82     }
83  
84     /***
85      * To get ValueListInfo it use the TableInfo.DEFAULT_ID as the tableId.
86      * 
87      * @see ValueListHandlerHelper#getValueListInfo(HttpServletRequest, String)
88      * @see TableInfo#DEFAULT_ID
89      * @param request
90      * @return ValueListInfo
91      */
92     public ValueListInfo getValueListInfo(HttpServletRequest request)
93     {
94        LOGGER.warn("Gettting a valueListInfo for a table with the default id!");
95        return getValueListInfo(request, TableInfo.DEFAULT_ID);
96     }
97  
98     /***
99      * Return ValueListInfo from a <b>request </b> (which is preferr to session)
100     * or a session or new ValueListInfo().
101     * <li>If request contains any parameters, which belongs to a table with
102     * the <b>tableId </b>, return the ValueListInfo from the <b>request. </b>
103     * </li>
104     * <li>Otherwise if the session <b>contains attribute tableId </b>, then
105     * return it from the <b>session </b>.</li>
106     * <li>If the request or the session <b>dosn't </b> contain the
107     * ValueListInfo, return <b>new </b> ValueListInfo.</li>
108     * 
109     * @see ValueListHandlerHelper#getValueListInfo(HttpServletRequest)
110     * @param request
111     * @param tableId
112     * @return ValueListInfo
113     */
114 
115    public ValueListInfo getValueListInfo(HttpServletRequest request, String tableId)
116    {
117       if (request == null)
118       {
119          final String message = "getValueListInfo - request is null!";
120          LOGGER.error(message);
121          throw new NullPointerException(message);
122       }
123       if (tableId == null)
124       {
125          LOGGER.error("TableId is null!");
126          tableId = TableInfo.DEFAULT_ID;
127       }
128 
129       if (LOGGER.isDebugEnabled())
130       {
131          LOGGER.debug("Start to getting ValuelistInfo for the tableId '" + tableId + "'.");
132       }
133 
134       Map requestParamsMap = ValueListRequestUtil.getRequestParameterMap(request, tableId);
135 
136       ValueListInfo info = null;
137 
138       if (requestParamsMap.isEmpty())
139       {
140 
141          if (LOGGER.isDebugEnabled())
142          {
143             LOGGER.debug("Try to get backup of an info from the session for the tableId '" + tableId + "'.");
144          }
145 
146          HttpSession session = request.getSession();
147          if (session != null)
148          {
149             info = (ValueListInfo) session.getAttribute(tableId);
150          }
151          else
152          {
153             LOGGER.warn("ValueListInfo for tableId '" + tableId + "'  was not found in the sesion due to session is null!");
154          }
155 
156          if (LOGGER.isDebugEnabled())
157          {
158             if (info == null)
159             {
160                LOGGER.debug("Backup of the ValueListInfo for the tableId '" + tableId + "'was NOT found in the session.");
161             }
162             else
163             {
164                LOGGER.debug("Backup of the ValueListInfo for the tableId '" + tableId + "' was FOUND in the session");
165             }
166          }
167 
168       }
169       else
170       {
171          info = ValueListRequestUtil.buildValueListInfo(request, tableId);
172          if (LOGGER.isDebugEnabled())
173          {
174             LOGGER.debug("ValueListInfo for the tableId '" + tableId + "' was build from the request's params.");
175          }
176       }
177 
178       if (info == null)
179       {
180          if (LOGGER.isInfoEnabled())
181          {
182             LOGGER.info("Creating a default ValueListInfo for the tableId '" + tableId + "'");
183          }
184          info = new ValueListInfo();
185       }
186       return info;
187 
188    }
189 
190    /***
191     * Do 2 things:
192     * <li>Backup the ValueList <b>Info to the session </b> with the
193     * attribute's name <b>tableId </b> and</li>
194     * <li>store Value <b>List to the request </b> with the attribute key's
195     * name <b>valueListName </b></li>
196     * 
197     * @see ValueListHandlerHelper#backupInfoFor(HttpServletRequest,
198     *      ValueListInfo, String)
199     * @see ValueListHandlerHelper#setValueListTo(HttpServletRequest, ValueList,
200     *      String)
201     * @param request
202     * @param valueList null -> info will be removed from session and storing
203     *            will be skipped.
204     * @param valueListName the name used in the &lt;vlh:root
205     *            value="valueListName" ... to retrieve
206     * @param tableId unique id in the session
207     */
208    public void backupAndSet(HttpServletRequest request, ValueList valueList, String valueListName, String tableId)
209    {
210 
211       backupInfoFor(request, valueList == null ? null : valueList.getValueListInfo(), tableId);
212       setValueListTo(request, valueList, valueListName);
213 
214    }
215 
216    /***
217     * Store only ValueList to request as an attribute with the name
218     * valueListName.
219     * 
220     * @param request
221     * @param valueList ValueList to store
222     * @param valueListName request attribute's
223     */
224    public void setValueListTo(HttpServletRequest request, ValueList valueList, String valueListName)
225    {
226       if (valueListName != null && valueListName.length() > 0)
227       {
228          request.setAttribute(valueListName, valueList);
229          if (LOGGER.isDebugEnabled())
230          {
231             LOGGER.debug("ValueList '" + valueListName + "' is stored in the request.");
232          }
233       }
234       else
235       {
236          LOGGER.error("Skiped storing of the ValueList to the request, valueListName is null or empty!");
237       }
238    }
239 
240    /***
241     * Save ValueListInfo to the session with key of TableInfo.DEFAULT_ID. You
242     * should prevent of use this method.
243     * 
244     * @see TableInfo#DEFAULT_ID
245     * @see ValueListHandlerHelper#backupInfoFor(HttpServletRequest,
246     *      ValueListInfo, String)
247     * @param request HttpServletRequest
248     * @param info ValueListInfo
249     */
250    public void backupInfoFor(HttpServletRequest request, ValueListInfo info)
251    {
252       LOGGER.warn("Backing Up with the default table id ");
253       backupInfoFor(request, info, TableInfo.DEFAULT_ID);
254 
255    }
256 
257    /***
258     * Save ValueListInfo to session with key of tableId if info is null, remove
259     * the tableId attribute from session.
260     * 
261     * @param request HttpServletRequest
262     * @param info ValueListInfo to back up to session
263     * @param tableId String session's attributes name
264     * @see TableInfo#DEFAULT_ID
265     * <h4> during accesing session, we are not using any synchronization.</h4>
266     */
267    public void backupInfoFor(HttpServletRequest request, ValueListInfo info, String tableId)
268    {
269 
270       if (tableId != null && tableId.length() > 0)
271       {
272          if (info == null)
273          {
274             request.getSession().removeAttribute(tableId);
275 
276             if (LOGGER.isWarnEnabled())
277             {
278                LOGGER.warn("ValueListInfo to back up is null! ValueListInfo for the tableId '" + tableId
279                      + "' was removed from the session.");
280             }
281          }
282          else
283          {
284             request.getSession().setAttribute(tableId, info);
285             if (LOGGER.isDebugEnabled())
286             {
287                LOGGER.debug("ValueListInfo for tableId '" + tableId + "' was saved in session.");
288             }
289          }
290       }
291       else
292       {
293          throw new NullPointerException(
294                "The session unique attribute tableId is null or empty, skipped backUp of valueListInfo into the session!");
295       }
296    }
297 
298    /***
299     * Return the name of parameter with ActionTag.ACTION_TEMP_PARAM_PREFIX
300     * Example: param <b>id </b> return <b>ACTid </b>
301     * 
302     * @see net.mlw.vlh.web.tag.ActionTag#ACTION_TEMP_PARAM_PREFIX
303     * @param request
304     * @param name
305     * @return String
306     */
307    public String getActionTempParam(HttpServletRequest request, String name)
308    {
309       return request.getParameter(ValueListRequestUtil.getActionTempParamName(name));
310    }
311 
312    /***
313     * Return the name of parameter without ActionTag.ACTION_TEMP_PARAM_PREFIX
314     * Example: param <b>id </b> return <b>id </b>
315     * 
316     * @see net.mlw.vlh.web.tag.ActionTag#ACTION_TEMP_PARAM_PREFIX
317     * @param request
318     * @param name
319     * @return String
320     */
321    public String getActionParam(HttpServletRequest request, String name)
322    {
323       return request.getParameter(name);
324    }
325 
326    /***
327     * If delete first and at that time also the last entry on the last page,
328     * you sets actual last page! You should call this method in a action after
329     * delete is done.
330     * 
331     * @param info ValueListInfo
332     * @return int Last page, or last page -1 of last retrieved valueListInfo.
333     * @see ValueListInfo#getTotalNumberOfEntries()
334     * @see ValueListInfo#getPagingNumberPer()
335     * @see ValueListInfo#getPagingPer()
336     */
337    public int getLastPageAfterDelete(ValueListInfo info)
338    {
339       int lastPage = info.getPagingPage();
340 
341       int lastCount = info.getTotalNumberOfEntries() % info.getPagingNumberPer();
342 
343       if (lastCount == 1 && lastPage > 1)
344       {
345          lastPage--;
346       }
347       return lastPage;
348    }
349 }