📄 webapputils.java
字号:
package net.java.workeffort.webapp.support;import java.beans.BeanInfo;import java.beans.IntrospectionException;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.Method;import java.text.FieldPosition;import java.text.ParsePosition;import java.text.SimpleDateFormat;import java.util.Collection;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import net.java.workeffort.infrastructure.Constants;import net.java.workeffort.infrastructure.utils.DateRangePredicate;import net.java.workeffort.infrastructure.utils.DateRangeValidationUtils;import org.apache.commons.beanutils.PropertyUtils;import org.apache.commons.collections.CollectionUtils;import org.apache.commons.collections.Predicate;import org.apache.commons.lang.Validate;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * @author Antony Joseph */public class WebappUtils { protected static final Log log = LogFactory.getLog(WebappUtils.class); public static Map getLovMethods(List lovList, Object listOfValuesService) { Map map = new HashMap(); try { if (lovList != null) { Iterator it = lovList.iterator(); while (it.hasNext()) { String lovName = (String) it.next(); map.put(lovName, listOfValuesService.getClass().getMethod( lovName, null)); } } } catch (NoSuchMethodException e) { throw new RuntimeException(e); } return map; } public static Map referenceLovData(Map lovMethodCache, Object lovService) { Map map = new HashMap(); if (lovMethodCache != null && lovService != null) { Iterator it = lovMethodCache.keySet().iterator(); while (it.hasNext()) { String key = (String) it.next(); try { map.put(key, ((Method) lovMethodCache.get(key)).invoke( lovService, null)); } catch (Exception e) { throw new RuntimeException(e); } } } return map; } public static Class getPropertyType(String propertyName, Class clazz) { Validate.notEmpty(propertyName, "propertyName is null"); Validate.notNull(clazz, "clazz is null"); try { BeanInfo beanInfo = Introspector.getBeanInfo(clazz); PropertyDescriptor[] descriptors = beanInfo .getPropertyDescriptors(); for (int i = 0; i < descriptors.length; i++) { PropertyDescriptor descriptor = descriptors[i]; if (descriptor.getName().equals(propertyName)) return descriptor.getPropertyType(); } } catch (IntrospectionException e) { throw new RuntimeException(e); } // Code reaches here no property found throw new RuntimeException("Could not find property '" + propertyName + "' in class " + clazz); } public static Object getSimpleProperty(Object obj, String propertyName) { try { return PropertyUtils.getSimpleProperty(obj, propertyName); } catch (Exception e) { throw new RuntimeException("Error while getting property:" + propertyName + " from object:" + obj, e); } } public static PropertyDescriptor getPropertyDescriptor(Object obj, String propertyName) { try { return PropertyUtils.getPropertyDescriptor(obj, propertyName); } catch (Exception e) { throw new RuntimeException( "Error while getting property descriptor:" + propertyName + " from object:" + obj, e); } } public static String convertDateToString(Date date, String dateFormat) { if (date == null) return null; StringBuffer buf = new StringBuffer(); SimpleDateFormat formatter = new SimpleDateFormat(dateFormat); formatter.setLenient(false); formatter.format(date, buf, new FieldPosition(0)); return buf.toString(); } public static Date convertStringToDate(String dateStr, String dateFormat) { if (dateStr == null) return null; StringBuffer buf = new StringBuffer(); SimpleDateFormat formatter = new SimpleDateFormat(dateFormat); formatter.setLenient(false); return formatter.parse(dateStr, new ParsePosition(0)); } /** * Validate the date ranges against other rows. Generally used within the * context of a master detail form, where the date ranges of multiple * details must be validated against each other to prevent conflict * @param request The request * @param rows The list of rows * @param filterPropertyNames The property names which will be used to * filter the rows. * @param fromDtPropertyName The from date property name * @param thruDtPropertyName The thru date property name * @return Whether the date ranges are valid */ public static void validateMultiRowDateRange(HttpServletRequest request, List rows, List filterPropertyNames, String fromDtPropertyName, String thruDtPropertyName) throws InvalidDateRangeException, ConflictingMultiRowDateRangeException { if (log.isDebugEnabled()) log.debug("validateDateRange() invoked."); for (int i = 0; i < rows.size(); i++) { Object row = rows.get(i); String processType = (String) getSimpleProperty(row, Constants.PROCESS_TYPE); if ("insert".equals(processType) || "update".equals(processType)) { Predicate predicate = new DateRangePredicate(row, filterPropertyNames); Collection filteredCollection = CollectionUtils.select(rows, predicate); if (log.isDebugEnabled()) log.debug("filteredCollection=" + filteredCollection); // first make sure that the thru dt is >= from date Date fromDt = (Date) getSimpleProperty(row, fromDtPropertyName); Validate.notNull(fromDt, "fromDt cannot be null"); //if (fromDt == null) // fromDt = DateRangeValidationUtils.getDefaultFromDate(); Date thruDt = (Date) getSimpleProperty(row, thruDtPropertyName); if (thruDt == null) thruDt = DateRangeValidationUtils.getDefaultThruDate(); if (fromDt.after(thruDt)) { InvalidDateRangeException idte = new InvalidDateRangeException( fromDtPropertyName + "," + thruDtPropertyName); idte.setIndex(new Integer(i)); throw idte; } if (!DateRangeValidationUtils.validateMultiRowDateRanges( filteredCollection, fromDt, thruDt, fromDtPropertyName, thruDtPropertyName)) { ConflictingMultiRowDateRangeException ce = new ConflictingMultiRowDateRangeException( fromDtPropertyName + "," + thruDtPropertyName); ce.setIndex(new Integer(i)); throw ce; } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -