Java tutorial
/** * Copyright (c) 2012-2014 http://www.eryansky.com * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.afeng.common.dao.orm; import org.apache.commons.beanutils.PropertyUtils; import org.springframework.util.Assert; import org.springframework.web.util.WebUtils; import com.afeng.common.dao.orm.PropertyFilter; import com.afeng.common.utils.StringUtils; import com.afeng.common.utils.SysConstants; import com.afeng.common.utils.reflection.ReflectionUtils; import javax.servlet.http.HttpServletRequest; import java.util.*; /** * HibernateWeb. * @author &Eryan eryanwcp@gmail.com * @date 2013-3-21 ?12:30:01 * */ public class HibernateWebUtils { public static final String FILTERPREFIX = "filter_"; private static Properties sqlfilterProperties; static { sqlfilterProperties = SysConstants.getSqlfilter().getProperties(); } private HibernateWebUtils() { } /** * ?ID?,???. * <p/> * ????"id". * * @see #mergeByCheckedIds(java.util.Collection, java.util.Collection, Class, String) */ public static <T, ID> void mergeByCheckedIds(final Collection<T> srcObjects, final Collection<ID> checkedIds, final Class<T> clazz) { mergeByCheckedIds(srcObjects, checkedIds, clazz, "id"); } /** * ?ID?,???. * <p/> * ??????id,??????id?????. * ???id??,??id??. * ?ID, ??cascade-save-or-update. * * @param srcObjects ??,. * @param checkedIds ?,ID. * @param clazz ? * @param idName ?? */ public static <T, ID> void mergeByCheckedIds(final Collection<T> srcObjects, final Collection<ID> checkedIds, final Class<T> clazz, final String idName) { //? Assert.notNull(srcObjects, "scrObjects?"); Assert.hasText(idName, "idName?"); Assert.notNull(clazz, "clazz?"); //?,???. if (checkedIds == null) { srcObjects.clear(); return; } //????,id?ID?,. //?,???id,?id???id. Iterator<T> srcIterator = srcObjects.iterator(); try { while (srcIterator.hasNext()) { T element = srcIterator.next(); Object id; id = PropertyUtils.getProperty(element, idName); if (!checkedIds.contains(id)) { srcIterator.remove(); } else { checkedIds.remove(id); } } //ID??id????,,id??. for (ID id : checkedIds) { T obj = clazz.newInstance(); PropertyUtils.setProperty(obj, idName, id); srcObjects.add(obj); } } catch (Exception e) { throw ReflectionUtils.convertReflectionExceptionToUnchecked(e); } } /** * ?PropertyFilter??Request?,PropertyFilter. * Filter???filter_. * * @see #buildPropertyFilters(HttpServletRequest, String) */ public static List<PropertyFilter> buildPropertyFilters(final HttpServletRequest request) { return buildPropertyFilters(request, FILTERPREFIX); } /** * ?PropertyFilter??Request?,PropertyFilter. * PropertyFilter??Filter?__??. * <p/> * eg. * filter_EQS_name * filter_LIKES_name_OR_email */ public static List<PropertyFilter> buildPropertyFilters(final HttpServletRequest request, final String filterPrefix) { return HibernateWebUtils.buildPropertyFilters(request, FILTERPREFIX, true); } /** * ?PropertyFilter??Request?,PropertyFilter. * PropertyFilter??Filter?__??. * <p/> * eg. * filter_EQS_name * filter_LIKES_name_OR_email */ public static List<PropertyFilter> buildPropertyFilters(final HttpServletRequest request, final String filterPrefix, boolean filterSQL) { List<PropertyFilter> filterList = new ArrayList<PropertyFilter>(); //request??????,?????Map. Map<String, Object> filterParamMap = WebUtils.getParametersStartingWith(request, filterPrefix); //??Map,PropertyFilter for (Map.Entry<String, Object> entry : filterParamMap.entrySet()) { String filterName = entry.getKey(); String value = (String) entry.getValue(); //value,filter. if (StringUtils.isNotEmpty(value)) { if (filterSQL) { for (Object oj : sqlfilterProperties.keySet()) { String key = (String) oj; value = value.replaceAll(key, sqlfilterProperties.getProperty(key)); } } PropertyFilter filter = new PropertyFilter(filterName.replaceAll("__", "."), value); filterList.add(filter); } } return filterList; } }