KickJava   Java API By Example, From Geeks To Geeks.

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


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.StringFilterForm;
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.StringFilter;
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
32 /**
33  * <p>Action for string filter
34  * </p>
35  * <p><a HREF="StringFilterAction.java.htm"><i>View Source</i></a></p>
36  * <p/>
37  *
38  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
39  * @version $Revision: 1.5 $ $Date: 2005/02/24 19:50:49 $
40  * @struts.action path="/grid/stringFilter"
41  * input="showFilterPage"
42  * scope="request"
43  * validate="true"
44  * name="stringFilterForm"
45  * @struts.action-forward name="showFilterPage"
46  * path=".stringFilter"
47  * @struts.action-forward name="filterError"
48  * path=".filterError"
49  */

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

61     public ActionForward execute(ActionMapping mapping, ActionForm form,
62                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
63
64         HttpSession JavaDoc session = request.getSession();
65         StringFilterForm stringFilterForm = (StringFilterForm) form;
66
67         String JavaDoc fieldName = stringFilterForm.getFieldName();
68         String JavaDoc rowIterators = stringFilterForm.getRowIterators();
69         String JavaDoc gridName = stringFilterForm.getGridName();
70         Grid grid = getGridByName(gridName, session);
71
72         if ( grid == null ) {
73             if ( log.isErrorEnabled() ) {
74                 String JavaDoc errorMessage = "No grid with name " + gridName + " could be found in session";
75                 log.error(errorMessage);
76             }
77             return mapping.findForward("filterError");
78         }
79
80         StringFilter filter = new StringFilter(fieldName);
81         filter.setRowIterators(ConvertUtil.convertStringToList(rowIterators, ",", true));
82
83         String JavaDoc firstCondition = stringFilterForm.getFirstCondition();
84         String JavaDoc firstValue = stringFilterForm.getFirstValue();
85         String JavaDoc secondCondition = stringFilterForm.getSecondCondition();
86         String JavaDoc secondValue = stringFilterForm.getSecondValue();
87         String JavaDoc logicCopula = stringFilterForm.getLogicCopula();
88
89         // Remove filter if 'clear' button was pressed else create clauses
90
// according to specified values
91

92         if ( stringFilterForm.getClearFilter() != null ) {
93             grid.removeFilter(filter);
94             if ( log.isDebugEnabled() ) {
95                 log.debug("Filter for field '" + fieldName + "' was removed from grid");
96             }
97         } else {
98             if ( firstCondition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NULL) || firstCondition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NOT_NULL) ) {
99                 filter.createFirstClause(firstCondition, null);
100             } else if ( firstValue != null || firstValue.length() != 0 ) {
101                 filter.createFirstClause(firstCondition, firstValue);
102             } else {
103                 if ( secondCondition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NULL) || secondCondition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NOT_NULL) ) {
104                     filter.createFirstClause(secondCondition, null);
105                 } else if ( secondValue != null || secondValue.length() != 0 ) {
106                     filter.createFirstClause(secondCondition, secondValue);
107                 }
108             }
109
110             if ( !secondCondition.equalsIgnoreCase(FilterConditions.CONDITION_NOT_SET) ) {
111                 if ( secondCondition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NULL) || secondCondition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NOT_NULL) ) {
112                     filter.createSecondClause(secondCondition, null);
113                 } else if ( secondValue != null && secondValue.length() != 0 ) {
114                     filter.createSecondClause(secondCondition, secondValue, logicCopula);
115                 }
116             }
117             grid.addFilter(filter);
118             if ( log.isDebugEnabled() ) {
119                 log.debug("New StringFilter added to grid '" + gridName + "' for field '" + fieldName + "'. Clause is: " + filter.getClause());
120             }
121
122         }
123
124         saveGrid(grid, session);
125         request.setAttribute(TaglibConstants.PAGE_URL, stringFilterForm.getPageUrl());
126         return mapping.findForward("filterReturn");
127     }
128 }
Popular Tags