KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > grid > SetFilterAction


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
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 com.blandware.atleap.webapp.action.core.grid;
17
18 import com.blandware.atleap.common.util.ConvertUtil;
19 import com.blandware.atleap.webapp.form.core.SetFilterForm;
20 import com.blandware.atleap.webapp.taglib.core.grid.util.FilterConditions;
21 import com.blandware.atleap.webapp.taglib.core.grid.util.Grid;
22 import com.blandware.atleap.webapp.taglib.core.grid.util.SetFilter;
23 import com.blandware.atleap.webapp.taglib.core.util.TaglibConstants;
24 import org.apache.struts.action.ActionForm;
25 import org.apache.struts.action.ActionForward;
26 import org.apache.struts.action.ActionMapping;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30 import javax.servlet.http.HttpSession JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33
34 /**
35  * <p>Action for set filter
36  * </p>
37  * <p><a HREF="SetFilterAction.java.htm"><i>View Source</i></a></p>
38  * <p/>
39  *
40  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
41  * @version $Revision: 1.10 $ $Date: 2005/02/24 19:50:49 $
42  * @struts.action path="/grid/setFilter"
43  * input="showFilterPage"
44  * scope="request"
45  * validate="true"
46  * name="setFilterForm"
47  * @struts.action-forward name="showFilterPage"
48  * path=".setFilter"
49  * @struts.action-forward name="filterError"
50  * path=".filterError"
51  */

52 public final class SetFilterAction extends com.blandware.atleap.webapp.action.core.grid.BaseGridAction {
53
54     /**
55      * @param mapping The ActionMapping used to select this instance
56      * @param form The optional ActionForm bean for this request (if any)
57      * @param request The HTTP request we are proceeding
58      * @param response The HTTP response we are creating
59      * @return an ActionForward instance describing where and how
60      * control should be forwarded, or null if response
61      * has already been completed
62      */

63     public ActionForward execute(ActionMapping mapping, ActionForm form,
64                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
65
66
67         HttpSession JavaDoc session = request.getSession();
68         SetFilterForm setFilterForm = (SetFilterForm) form;
69         String JavaDoc fieldName = setFilterForm.getFieldName();
70         String JavaDoc rowIterators = setFilterForm.getRowIterators();
71         String JavaDoc gridName = setFilterForm.getGridName();
72         Grid grid = getGridByName(gridName, session);
73
74         if ( grid == null ) {
75             if ( log.isErrorEnabled() ) {
76                 String JavaDoc errorMessage = "No grid with name " + gridName + " could be found in session";
77                 log.error(errorMessage);
78             }
79             return mapping.findForward("filterError");
80         }
81
82         SetFilter filter = new SetFilter(fieldName);
83         filter.setRowIterators(ConvertUtil.convertStringToList(rowIterators, ",", true));
84         filter.setAvailableElements((Map JavaDoc) session.getAttribute(SetFilter.AVAILABLE_ELEMENTS));
85
86         // Remove filter if 'clear' button was pressed else create clauses
87
// according to specified values
88

89         if ( setFilterForm.getClearFilter() != null || (setFilterForm.getSelectedElements().size() == 0 && !setFilterForm.getFirstCondition().equalsIgnoreCase(FilterConditions.OBJECT_IS_NULL) && !setFilterForm.getFirstCondition().equalsIgnoreCase(FilterConditions.OBJECT_IS_NOT_NULL)) ) {
90             grid.removeFilter(filter);
91             if ( log.isDebugEnabled() ) {
92                 log.debug("Filter for field '" + fieldName + "' was removed from grid");
93             }
94         } else {
95             String JavaDoc firstCondition = setFilterForm.getFirstCondition();
96             if ( !firstCondition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NULL) && !firstCondition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NOT_NULL) ) {
97                 filter.setSelectedElements(setFilterForm.getSelectedElements());
98             } else {
99                 filter.setSelectedElements(new HashMap JavaDoc());
100             }
101             filter.createFirstClause(setFilterForm.getFirstCondition());
102             grid.addFilter(filter);
103             if ( log.isDebugEnabled() ) {
104                 log.debug("New SetFilter added to grid '" + gridName + "' for field '" + fieldName + "'. Clause is: " + filter.getClause());
105             }
106         }
107
108         saveGrid(grid, session);
109         request.setAttribute(TaglibConstants.PAGE_URL, setFilterForm.getPageUrl());
110         return mapping.findForward("filterReturn");
111     }
112 }
Popular Tags