KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > sort > SortAction


1 package fr.improve.struts.taglib.layout.sort;
2
3 import javax.servlet.http.HttpServletRequest JavaDoc;
4 import javax.servlet.http.HttpServletResponse JavaDoc;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.apache.struts.action.ActionForm;
9 import org.apache.struts.action.ActionForward;
10 import org.apache.struts.action.ActionMapping;
11 /**
12  * This action is used to sort a specified collection
13  * @author: Jean-NoŽl Ribette
14  */

15 public class SortAction extends org.apache.struts.action.Action {
16     /**
17      * Action the response is forwarded to in case of a sort exception.
18      */

19     private static final String JavaDoc SORT_ERROR_URL = "sortError";
20     
21     /**
22      * Commons Logging instance.
23      */

24     protected static Log log = LogFactory.getLog(SortAction.class);
25     
26 /**
27  * Returns the sort util in the session.
28  */

29 protected SortUtil getSortUtil(HttpServletRequest JavaDoc in_request) {
30     return (SortUtil) in_request.getSession().getAttribute(SortUtil.SORTUTIL_KEY);
31 }
32 public ActionForward execute(
33     ActionMapping in_mapping,
34     ActionForm in_form,
35     HttpServletRequest JavaDoc in_request,
36     HttpServletResponse JavaDoc in_response) {
37
38     SortUtil lc_sortUtil = getSortUtil(in_request);
39     String JavaDoc lc_url = SORT_ERROR_URL;
40     
41     // Be careful, a session timeout causes the SortUtil to be null.
42
if (lc_sortUtil!=null) {
43         try {
44             lc_url = lc_sortUtil.sort(in_request);
45         } catch (SortException e) {
46             log.error("Sort failed", e);
47             lc_url = SORT_ERROR_URL;
48         }
49     } else {
50         log.error("Unable to sort : SortUtil is null, maybe because there has been a session timeout");
51     }
52     
53     if (lc_url.startsWith(in_request.getContextPath() + '/')) {
54         lc_url = lc_url.substring(in_request.getContextPath().length());
55     }
56     if (lc_url.indexOf('.')!=-1 && lc_url.indexOf('?')!=-1) {
57         lc_url = lc_url.substring(0, lc_url.indexOf('?'));
58     }
59     
60     ActionForward lc_forward = null;
61     if (SORT_ERROR_URL.equals(lc_url)) {
62         lc_forward = in_mapping.findForward(SORT_ERROR_URL);
63         if (lc_forward==null) {
64             log.error("Sort failed, but forward sortError is not defined, so no error page can be displayed");
65         }
66     } else {
67         lc_forward = new ActionForward(lc_url);
68     }
69     
70     return lc_forward;
71
72 }
73 }
74
Popular Tags