KickJava   Java API By Example, From Geeks To Geeks.

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


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.webapp.form.core.SetFilterForm;
19 import com.blandware.atleap.webapp.taglib.core.grid.util.Grid;
20 import com.blandware.atleap.webapp.taglib.core.grid.util.SetFilter;
21 import com.blandware.atleap.webapp.util.core.WebappUtil;
22 import org.apache.commons.beanutils.PropertyUtils;
23 import org.apache.struts.action.ActionForm;
24 import org.apache.struts.action.ActionForward;
25 import org.apache.struts.action.ActionMapping;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29 import javax.servlet.http.HttpSession JavaDoc;
30 import javax.servlet.jsp.JspException JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.util.Arrays JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Map JavaDoc;
37
38 /**
39  * <p>Calls set filter JSP and sets form bean properties according to filter stored in session
40  * </p>
41  * <p><a HREF="CallSetFilterAction.java.htm"><i>View Source</i></a></p>
42  * <p/>
43  *
44  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
45  * @version $Revision: 1.12 $ $Date: 2006/03/25 15:12:49 $
46  * @struts.action path="/grid/callSetFilter"
47  * name="setFilterForm"
48  * scope="request"
49  * @struts.action-forward name="showFilterPage"
50  * path=".setFilter"
51  * @struts.action-forward name="filterError"
52  * path=".filterError"
53  */

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

65     public ActionForward execute(ActionMapping mapping, ActionForm form,
66                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
67
68         HttpSession JavaDoc session = request.getSession();
69
70         SetFilterForm setFilterForm = (SetFilterForm) form;
71         String JavaDoc fieldName = setFilterForm.getFieldName();
72         String JavaDoc gridName = setFilterForm.getGridName();
73         String JavaDoc beanId = setFilterForm.getBeanId();
74         String JavaDoc methodName = setFilterForm.getMethod();
75
76         Grid grid = getGridByName(gridName, session);
77
78         if ( grid == null ) {
79             if ( log.isErrorEnabled() ) {
80                 String JavaDoc errorMessage = "No grid with name " + gridName + " could be found in session";
81                 log.error(errorMessage);
82             }
83             return mapping.findForward("filterError");
84         }
85         boolean beanIdSpecified = beanId != null && beanId.length() != 0;
86         boolean methodNameSpecified = methodName != null && methodName.length() != 0;
87
88         Object JavaDoc c = null;
89         Collection JavaDoc collection = null;
90         Map JavaDoc availableElements = new HashMap JavaDoc();
91         boolean isMap = false;
92         if ( beanIdSpecified && methodNameSpecified ) {
93
94             // Find bean in application context and specified method on it
95

96             Object JavaDoc bean = getBean(beanId);
97             if ( bean == null ) {
98                 String JavaDoc errorMessage = "No bean with ID=" + beanId + " could be found in application context";
99                 if ( log.isErrorEnabled() ) {
100                     log.error(errorMessage);
101                 }
102                 JspException JavaDoc e = new JspException JavaDoc(errorMessage);
103                 throw e;
104             }
105
106             Method JavaDoc method = null;
107             try {
108                 method = bean.getClass().getMethod(methodName, (Class JavaDoc[]) null);
109             } catch ( NoSuchMethodException JavaDoc e ) {
110                 if ( log.isErrorEnabled() ) {
111                     log.error(e.getMessage());
112                 }
113                 throw new JspException JavaDoc(e);
114             }
115
116             // Invoke method in order to get collection for displaying on set filter page
117

118             try {
119                 collection = (Collection JavaDoc) method.invoke(bean, (Object JavaDoc[]) null);
120             } catch ( Exception JavaDoc e ) {
121                 if ( log.isErrorEnabled() ) {
122                     log.error("Exception: " + e.getClass().getName() + ": " + e.getMessage());
123                 }
124                 throw new JspException JavaDoc(e);
125             }
126
127         } else {
128             c = session.getAttribute(SetFilter.AVAILABLE_ELEMENTS);
129             if ( c instanceof Map JavaDoc ) {
130                 availableElements = (Map JavaDoc) c;
131                 isMap = true;
132             }
133         }
134
135         // if there was attribute in session and it was not a map, fill map from collection
136
if ( !isMap ) {
137             // check how collection was acquired: from persistence layer or from session atribute
138
if ( collection == null ) {
139                 // collection was saved in session
140
if ( c instanceof Collection JavaDoc ) {
141                     collection = (Collection JavaDoc) c;
142                 } else if ( c instanceof Object JavaDoc[] ) {
143                     collection = Arrays.asList((Object JavaDoc[]) c);
144                 }
145             }
146
147             String JavaDoc labelProperty = setFilterForm.getLabel();
148             String JavaDoc valueProperty = setFilterForm.getValue();
149
150             // create map of elements from collection
151
for ( Iterator JavaDoc i = collection.iterator(); i.hasNext(); ) {
152                 Object JavaDoc nextElement = i.next();
153                 String JavaDoc label = new String JavaDoc();
154                 String JavaDoc value = new String JavaDoc();
155                 if ( valueProperty != null && valueProperty.length() > 0 ) {
156                     // value is specified
157
value = String.valueOf(PropertyUtils.getProperty(nextElement, valueProperty));
158                     if ( labelProperty != null && labelProperty.length() > 0 ) {
159                         label = String.valueOf(PropertyUtils.getProperty(nextElement, labelProperty));
160                     } else {
161                         label = value;
162                     }
163                 } else {
164                     label = String.valueOf(nextElement);
165                     value = String.valueOf(nextElement);
166                 }
167                 availableElements.put(value, label);
168             }
169         }
170
171
172         session.setAttribute(SetFilter.AVAILABLE_ELEMENTS, availableElements);
173
174         SetFilter filter = null;
175         try {
176             filter = (SetFilter) grid.getFilterByFieldName(fieldName);
177         } catch ( ClassCastException JavaDoc e ) {
178             if ( log.isErrorEnabled() ) {
179                 String JavaDoc errorMessage = "Filter for field \'" + fieldName + "\' in grid \'" + gridName + "\' is not instance of com.blandware.atleap.webapp.taglib.core.grid.util.SetFilter";
180                 log.error(errorMessage);
181             }
182             return mapping.findForward("filterError");
183         }
184
185         if ( filter != null ) {
186             try {
187                 String JavaDoc rowIterators = setFilterForm.getRowIterators();
188                 WebappUtil.copyProperties(setFilterForm, filter, request);
189                 setFilterForm.setRowIterators(rowIterators);
190             } catch ( Exception JavaDoc e ) {
191                 if ( log.isErrorEnabled() ) {
192                     log.error(e);
193                 }
194                 throw new JspException JavaDoc(e);
195             }
196         }
197
198         if ( log.isDebugEnabled() ) {
199             log.debug("Returning filter page: " + mapping.findForward("showFilterPage").getPath());
200         }
201
202         return mapping.findForward("showFilterPage");
203     }
204 }
Popular Tags