📄 entityutil.java
字号:
/* * $Id: EntityUtil.java 5462 2005-08-05 18:35:48Z jonesde $ * * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package org.ofbiz.entity.util;import java.sql.Timestamp;import java.util.Collection;import java.util.Collections;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import javolution.util.FastList;import javolution.util.FastMap;import javolution.util.FastSet;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilDateTime;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.condition.EntityCondition;import org.ofbiz.entity.condition.EntityConditionList;import org.ofbiz.entity.condition.EntityDateFilterCondition;import org.ofbiz.entity.condition.EntityFieldMap;import org.ofbiz.entity.condition.EntityOperator;import org.ofbiz.entity.condition.OrderByList;import org.ofbiz.entity.model.ModelField;/** * Helper methods when dealing with Entities, especially ones that follow certain conventions * *@author Eric Pabst *@version $ Revision: $ *@since 1.0 */public class EntityUtil { public static final String module = EntityUtil.class.getName(); public static GenericValue getFirst(List values) { if ((values != null) && (values.size() > 0)) { return (GenericValue) values.iterator().next(); } else { return null; } } public static GenericValue getOnly(List values) { if (values != null) { if (values.size() <= 0) { return null; } if (values.size() == 1) { return (GenericValue) values.iterator().next(); } else { throw new IllegalArgumentException("Passed List had more than one value."); } } else { return null; } } public static EntityCondition getFilterByDateExpr() { return new EntityDateFilterCondition("fromDate", "thruDate"); } public static EntityCondition getFilterByDateExpr(String fromDateName, String thruDateName) { return new EntityDateFilterCondition(fromDateName, thruDateName); } public static EntityCondition getFilterByDateExpr(java.util.Date moment) { return EntityDateFilterCondition.makeCondition(new java.sql.Timestamp(moment.getTime()), "fromDate", "thruDate"); } public static EntityCondition getFilterByDateExpr(java.sql.Timestamp moment) { return EntityDateFilterCondition.makeCondition(moment, "fromDate", "thruDate"); } public static EntityCondition getFilterByDateExpr(java.sql.Timestamp moment, String fromDateName, String thruDateName) { return EntityDateFilterCondition.makeCondition(moment, fromDateName, thruDateName); } /** *returns the values that are currently active. * *@param datedValues GenericValue's that have "fromDate" and "thruDate" fields *@return List of GenericValue's that are currently active */ public static List filterByDate(List datedValues) { return filterByDate(datedValues, UtilDateTime.nowTimestamp(), null, null, true); } /** *returns the values that are currently active. * *@param datedValues GenericValue's that have "fromDate" and "thruDate" fields *@param allAreSame Specifies whether all values in the List are of the same entity; this can help speed things up a fair amount since we only have to see if the from and thru date fields are valid once *@return List of GenericValue's that are currently active */ public static List filterByDate(List datedValues, boolean allAreSame) { return filterByDate(datedValues, UtilDateTime.nowTimestamp(), null, null, allAreSame); } /** *returns the values that are active at the moment. * *@param datedValues GenericValue's that have "fromDate" and "thruDate" fields *@param moment the moment in question *@return List of GenericValue's that are active at the moment */ public static List filterByDate(List datedValues, java.util.Date moment) { return filterByDate(datedValues, new java.sql.Timestamp(moment.getTime()), null, null, true); } /** *returns the values that are active at the moment. * *@param datedValues GenericValue's that have "fromDate" and "thruDate" fields *@param moment the moment in question *@return List of GenericValue's that are active at the moment */ public static List filterByDate(List datedValues, java.sql.Timestamp moment) { return filterByDate(datedValues, moment, null, null, true); } /** *returns the values that are active at the moment. * *@param datedValues GenericValue's that have "fromDate" and "thruDate" fields *@param moment the moment in question *@param allAreSame Specifies whether all values in the List are of the same entity; this can help speed things up a fair amount since we only have to see if the from and thru date fields are valid once *@return List of GenericValue's that are active at the moment */ public static List filterByDate(List datedValues, java.sql.Timestamp moment, String fromDateName, String thruDateName, boolean allAreSame) { if (datedValues == null) return null; if (moment == null) return datedValues; if (fromDateName == null) fromDateName = "fromDate"; if (thruDateName == null) thruDateName = "thruDate"; List result = FastList.newInstance(); Iterator iter = datedValues.iterator(); if (allAreSame) { ModelField fromDateField = null; ModelField thruDateField = null; if (iter.hasNext()) { GenericValue datedValue = (GenericValue) iter.next(); fromDateField = datedValue.getModelEntity().getField(fromDateName); if (fromDateField == null) throw new IllegalArgumentException("\"" + fromDateName + "\" is not a field of " + datedValue.getEntityName()); thruDateField = datedValue.getModelEntity().getField(thruDateName); if (thruDateField == null) throw new IllegalArgumentException("\"" + thruDateName + "\" is not a field of " + datedValue.getEntityName()); java.sql.Timestamp fromDate = (java.sql.Timestamp) datedValue.dangerousGetNoCheckButFast(fromDateField); java.sql.Timestamp thruDate = (java.sql.Timestamp) datedValue.dangerousGetNoCheckButFast(thruDateField); if ((thruDate == null || thruDate.after(moment)) && (fromDate == null || fromDate.before(moment) || fromDate.equals(moment))) { result.add(datedValue); }// else not active at moment } while (iter.hasNext()) { GenericValue datedValue = (GenericValue) iter.next(); java.sql.Timestamp fromDate = (java.sql.Timestamp) datedValue.dangerousGetNoCheckButFast(fromDateField); java.sql.Timestamp thruDate = (java.sql.Timestamp) datedValue.dangerousGetNoCheckButFast(thruDateField); if ((thruDate == null || thruDate.after(moment)) && (fromDate == null || fromDate.before(moment) || fromDate.equals(moment))) { result.add(datedValue); }// else not active at moment } } else { // if not all values are known to be of the same entity, must check each one... while (iter.hasNext()) { GenericValue datedValue = (GenericValue) iter.next(); java.sql.Timestamp fromDate = datedValue.getTimestamp(fromDateName); java.sql.Timestamp thruDate = datedValue.getTimestamp(thruDateName); if ((thruDate == null || thruDate.after(moment)) && (fromDate == null || fromDate.before(moment) || fromDate.equals(moment))) { result.add(datedValue); }// else not active at moment } } return result; } public static boolean isValueActive(GenericValue datedValue, java.sql.Timestamp moment) { return isValueActive(datedValue, moment, "fromDate", "thruDate"); } public static boolean isValueActive(GenericValue datedValue, java.sql.Timestamp moment, String fromDateName, String thruDateName) { java.sql.Timestamp fromDate = datedValue.getTimestamp(fromDateName); java.sql.Timestamp thruDate = datedValue.getTimestamp(thruDateName); if ((thruDate == null || thruDate.after(moment)) && (fromDate == null || fromDate.before(moment) || fromDate.equals(moment))) { return true; } else { // else not active at moment return false; } } /** *returns the values that match the values in fields * *@param values List of GenericValues *@param fields the field-name/value pairs that must match *@return List of GenericValue's that match the values in fields */ public static List filterByAnd(List values, Map fields) { if (values == null) return null; List result = null; if (fields == null || fields.size() == 0) { result = FastList.newInstance(); result.addAll(values); } else { result = FastList.newInstance(); Iterator iter = values.iterator(); while (iter.hasNext()) { GenericValue value = (GenericValue) iter.next(); if (value.matchesFields(fields)) { result.add(value); }// else did not match } } return result; } /** *returns the values that match all of the exprs in list * *@param values List of GenericValues *@param exprs the expressions that must validate to true *@return List of GenericValue's that match the values in fields */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -