Java tutorial
/** * Copyright (c) 2005-2009 springside.org.cn * * Licensed under the Apache License, Version 2.0 (the "License"); * * $Id: HibernateUtils.java 1141 2010-07-31 17:54:22Z calvinxiu $ */ package com.framework.infrastructure.utils; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.lang.StringUtils; import org.springframework.util.Assert; import com.framework.infrastructure.persistence.PropertyFilter; /** * HibernateWeb. * * @author calvin */ public class HibernateUtils { /** * ID,. * * "id". * * @see #mergeByCheckedIds(Collection, 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,. * * 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; } // ,idID,. // ,id,idid. 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); } } // IDid,,id. for (ID id : checkedIds) { T obj = clazz.newInstance(); PropertyUtils.setProperty(obj, idName, id); srcObjects.add(obj); } } catch (Exception e) { throw ReflectionUtils.convertReflectionExceptionToUnchecked(e); } } /** * PropertyFilterRequest,PropertyFilter. * Filterfilter_. * * @see #buildPropertyFilters(HttpServletRequest, String) */ public static List<PropertyFilter> buildPropertyFilters(final HttpServletRequest request) { return buildPropertyFilters(request, "filter_"); } /** * PropertyFilterRequest,PropertyFilter. * PropertyFilterFilter__. * * eg. filter_EQS_name filter_LIKES_name_OR_email */ @SuppressWarnings("unchecked") public static List<PropertyFilter> buildPropertyFilters(final HttpServletRequest request, final String filterPrefix) { List<PropertyFilter> filterList = new ArrayList<PropertyFilter>(); // request,Map. Map<String, String> filterParamMap = ServletUtils.getParametersStartingWith(request, filterPrefix); // Map,PropertyFilter for (Map.Entry<String, String> entry : filterParamMap.entrySet()) { String filterName = entry.getKey(); String value = entry.getValue(); // value,filter. if (StringUtils.isNotBlank(value)) { PropertyFilter filter = new PropertyFilter(filterName, value); filterList.add(filter); } } return filterList; } /** * ,filterParamMapkey:EQS_name,value:. */ @SuppressWarnings("unchecked") public static List<PropertyFilter> buildPropertyFilters(final Map<String, String> filterParamMap) { List<PropertyFilter> filterList = new ArrayList<PropertyFilter>(); // Map,PropertyFilter for (Map.Entry<String, String> entry : filterParamMap.entrySet()) { String filterName = entry.getKey(); String value = entry.getValue(); // value,filter. if (StringUtils.isNotBlank(value)) { PropertyFilter filter = new PropertyFilter(filterName, value); filterList.add(filter); } } return filterList; } }