1 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 ; 28 import javax.servlet.http.HttpServletResponse ; 29 import javax.servlet.http.HttpSession ; 30 import javax.servlet.jsp.JspException ; 31 import java.lang.reflect.Method ; 32 import java.util.Arrays ; 33 import java.util.Collection ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.Map ; 37 38 54 public final class CallSetFilterAction extends BaseGridAction { 55 56 65 public ActionForward execute(ActionMapping mapping, ActionForm form, 66 HttpServletRequest request, HttpServletResponse response) throws Exception { 67 68 HttpSession session = request.getSession(); 69 70 SetFilterForm setFilterForm = (SetFilterForm) form; 71 String fieldName = setFilterForm.getFieldName(); 72 String gridName = setFilterForm.getGridName(); 73 String beanId = setFilterForm.getBeanId(); 74 String methodName = setFilterForm.getMethod(); 75 76 Grid grid = getGridByName(gridName, session); 77 78 if ( grid == null ) { 79 if ( log.isErrorEnabled() ) { 80 String 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 c = null; 89 Collection collection = null; 90 Map availableElements = new HashMap (); 91 boolean isMap = false; 92 if ( beanIdSpecified && methodNameSpecified ) { 93 94 96 Object bean = getBean(beanId); 97 if ( bean == null ) { 98 String errorMessage = "No bean with ID=" + beanId + " could be found in application context"; 99 if ( log.isErrorEnabled() ) { 100 log.error(errorMessage); 101 } 102 JspException e = new JspException (errorMessage); 103 throw e; 104 } 105 106 Method method = null; 107 try { 108 method = bean.getClass().getMethod(methodName, (Class []) null); 109 } catch ( NoSuchMethodException e ) { 110 if ( log.isErrorEnabled() ) { 111 log.error(e.getMessage()); 112 } 113 throw new JspException (e); 114 } 115 116 118 try { 119 collection = (Collection ) method.invoke(bean, (Object []) null); 120 } catch ( Exception e ) { 121 if ( log.isErrorEnabled() ) { 122 log.error("Exception: " + e.getClass().getName() + ": " + e.getMessage()); 123 } 124 throw new JspException (e); 125 } 126 127 } else { 128 c = session.getAttribute(SetFilter.AVAILABLE_ELEMENTS); 129 if ( c instanceof Map ) { 130 availableElements = (Map ) c; 131 isMap = true; 132 } 133 } 134 135 if ( !isMap ) { 137 if ( collection == null ) { 139 if ( c instanceof Collection ) { 141 collection = (Collection ) c; 142 } else if ( c instanceof Object [] ) { 143 collection = Arrays.asList((Object []) c); 144 } 145 } 146 147 String labelProperty = setFilterForm.getLabel(); 148 String valueProperty = setFilterForm.getValue(); 149 150 for ( Iterator i = collection.iterator(); i.hasNext(); ) { 152 Object nextElement = i.next(); 153 String label = new String (); 154 String value = new String (); 155 if ( valueProperty != null && valueProperty.length() > 0 ) { 156 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 e ) { 178 if ( log.isErrorEnabled() ) { 179 String 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 rowIterators = setFilterForm.getRowIterators(); 188 WebappUtil.copyProperties(setFilterForm, filter, request); 189 setFilterForm.setRowIterators(rowIterators); 190 } catch ( Exception e ) { 191 if ( log.isErrorEnabled() ) { 192 log.error(e); 193 } 194 throw new JspException (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 |