⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 genericentity.java

📁 Sequoia ERP是一个真正的企业级开源ERP解决方案。它提供的模块包括:电子商务应用(e-commerce), POS系统(point of sales),知识管理,存货与仓库管理
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* * $Id: GenericEntity.java 7032 2006-03-20 23:28:15Z jonesde $ * *  Copyright (c) 2002-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;import java.io.PrintWriter;import java.io.Serializable;import java.math.BigDecimal;import java.text.NumberFormat;import java.util.Collection;import java.util.Collections;import java.util.Iterator;import java.util.Locale;import java.util.Map;import java.util.MissingResourceException;import java.util.Observable;import java.util.ResourceBundle;import java.util.TreeSet;import javolution.lang.Reusable;import javolution.util.FastList;import javolution.util.FastMap;import org.ofbiz.base.util.Base64;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.ObjectType;import org.ofbiz.base.util.UtilDateTime;import org.ofbiz.base.util.UtilProperties;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.base.util.UtilXml;import org.ofbiz.base.util.collections.LocalizedMap;import org.ofbiz.entity.condition.EntityCondition;import org.ofbiz.entity.jdbc.SqlJdbcUtil;import org.ofbiz.entity.model.ModelEntity;import org.ofbiz.entity.model.ModelField;import org.ofbiz.entity.model.ModelFieldType;import org.ofbiz.entity.util.ByteWrapper;import org.w3c.dom.Document;import org.w3c.dom.Element;/** * Generic Entity Value Object - Handles persisntence for any defined entity. * <p>Note that this class extends <code>Observable</code> to achieve change notification for * <code>Observer</code>s. Whenever a field changes the name of the field will be passed to * the <code>notifyObservers()</code> method, and through that to the <code>update()</code> method of each * <code>Observer</code>. * *@author     <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> *@author     <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> *@version    $Rev: 7032 $ *@since      2.0 */public class GenericEntity extends Observable implements Map, LocalizedMap, Serializable, Comparable, Cloneable, Reusable {    public static final String module = GenericEntity.class.getName();    public static final GenericEntity NULL_ENTITY = new NullGenericEntity();    public static final NullField NULL_FIELD = new NullField();    /** Name of the GenericDelegator, used to reget the GenericDelegator when deserialized */    protected String delegatorName = null;    /** Reference to an instance of GenericDelegator used to do some basic operations on this entity value. If null various methods in this class will fail. This is automatically set by the GenericDelegator for all GenericValue objects instantiated through it. You may set this manually for objects you instantiate manually, but it is optional. */    protected transient GenericDelegator internalDelegator = null;    /** Contains the fields for this entity. Note that this should always be a     *  HashMap to allow for two things: non-synchronized reads (synchronized     *  writes are done through synchronized setters) and being able to store     *  null values. Null values are important because with them we can distinguish     *  between desiring to set a value to null and desiring to not modify the     *  current value on an update.     */    protected Map fields = FastMap.newInstance();    /** Contains the entityName of this entity, necessary for efficiency when creating EJBs */    protected String entityName = null;    /** Contains the ModelEntity instance that represents the definition of this entity, not to be serialized */    protected transient ModelEntity modelEntity = null;    /** Denotes whether or not this entity has been modified, or is known to be out of sync with the persistent record */    protected boolean modified = false;    protected boolean generateHashCode = true;    protected int cachedHashCode = 0;    /** Used to specify whether or not this representation of the entity can be changed; generally cleared when this object comes from a cache */    protected boolean mutable = true;    /** This is an internal field used to specify that a value has come from a sync process and that the auto-stamps should not be over-written */    protected boolean isFromEntitySync = false;        /** Creates new GenericEntity - Should never be used, prefer the other options. */    protected GenericEntity() { }        /** Creates new GenericEntity */    public static GenericEntity createGenericEntity(ModelEntity modelEntity) {        if (modelEntity == null) {            throw new IllegalArgumentException("Cannot create a GenericEntity with a null modelEntity parameter");        }                GenericEntity newEntity = new GenericEntity();        newEntity.init(modelEntity);        return newEntity;    }    /** Creates new GenericEntity from existing Map */    public static GenericEntity createGenericEntity(ModelEntity modelEntity, Map fields) {        if (modelEntity == null) {            throw new IllegalArgumentException("Cannot create a GenericEntity with a null modelEntity parameter");        }                GenericEntity newEntity = new GenericEntity();        newEntity.init(modelEntity, fields);        return newEntity;    }    /** Copy Factory Method: Creates new GenericEntity from existing GenericEntity */    public static GenericEntity createGenericEntity(GenericEntity value) {        if (value == null) {            throw new IllegalArgumentException("Cannot create a GenericEntity with a null value parameter");        }                GenericEntity newEntity = new GenericEntity();        newEntity.init(value);        return newEntity;    }    /** Creates new GenericEntity */    protected void init(ModelEntity modelEntity) {        if (modelEntity == null) {            throw new IllegalArgumentException("Cannont create a GenericEntity with a null modelEntity parameter");        }        this.modelEntity = modelEntity;        this.entityName = modelEntity.getEntityName();                // check some things        if (this.entityName == null) {            throw new IllegalArgumentException("Cannont create a GenericEntity with a null entityName in the modelEntity parameter");        }    }    /** Creates new GenericEntity from existing Map */    protected void init(ModelEntity modelEntity, Map fields) {        if (modelEntity == null) {            throw new IllegalArgumentException("Cannont create a GenericEntity with a null modelEntity parameter");        }        this.modelEntity = modelEntity;        this.entityName = modelEntity.getEntityName();        setFields(fields);                // check some things        if (this.entityName == null) {            throw new IllegalArgumentException("Cannont create a GenericEntity with a null entityName in the modelEntity parameter");        }    }    /** Copy Constructor: Creates new GenericEntity from existing GenericEntity */    protected void init(GenericEntity value) {        if (value.modelEntity == null) {            throw new IllegalArgumentException("Cannont create a GenericEntity from another GenericEntity with a null modelEntity in the value parameter");        }        this.entityName = value.modelEntity.getEntityName();        this.modelEntity = value.modelEntity;        if (value.fields != null) this.fields.putAll(value.fields);        this.delegatorName = value.delegatorName;        this.internalDelegator = value.internalDelegator;                // check some things        if (this.entityName == null) {            throw new IllegalArgumentException("Cannont create a GenericEntity with a null entityName in the modelEntity parameter");        }    }    public void reset() {        // from GenericEntity        this.delegatorName = null;        this.internalDelegator = null;        this.fields = FastMap.newInstance();        this.entityName = null;        this.modelEntity = null;        this.modified = false;        this.generateHashCode = true;        this.cachedHashCode = 0;        this.mutable = true;        this.isFromEntitySync = false;    }        public void refreshFromValue(GenericEntity newValue) throws GenericEntityException {        if (newValue == null) {            throw new GenericEntityException("Could not refresh value, new value not found for: " + this);        }        GenericPK thisPK = this.getPrimaryKey();        GenericPK newPK = newValue.getPrimaryKey();        if (!thisPK.equals(newPK)) {            throw new GenericEntityException("Could not refresh value, new value did not have the same primary key; this PK=" + thisPK + ", new value PK=" + newPK);        }        this.fields = newValue.fields;        this.setDelegator(newValue.getDelegator());        this.generateHashCode = newValue.generateHashCode;        this.cachedHashCode = newValue.cachedHashCode;        this.modified = false;    }    public boolean isModified() {        return this.modified;    }    public void synchronizedWithDatasource() {        this.modified = false;    }    public void removedFromDatasource() {        // seems kind of minimal, but should do for now...        this.modified = true;    }    public boolean isMutable() {        return this.mutable;    }    public void setImmutable() {        this.mutable = false;    }    /**     * @return Returns the isFromEntitySync.     */    public boolean getIsFromEntitySync() {        return this.isFromEntitySync;    }    /**     * @param isFromEntitySync The isFromEntitySync to set.     */    public void setIsFromEntitySync(boolean isFromEntitySync) {        this.isFromEntitySync = isFromEntitySync;    }        public String getEntityName() {        return entityName;    }    public ModelEntity getModelEntity() {        if (modelEntity == null) {            if (entityName != null) modelEntity = this.getDelegator().getModelEntity(entityName);            if (modelEntity == null) {                throw new IllegalStateException("[GenericEntity.getModelEntity] could not find modelEntity for entityName " + entityName);            }        }        return modelEntity;    }    /** Get the GenericDelegator instance that created this value object and that is repsonsible for it.     *@return GenericDelegator object     */    public GenericDelegator getDelegator() {        if (internalDelegator == null) {            if (delegatorName == null) delegatorName = "default";            if (delegatorName != null) internalDelegator = GenericDelegator.getGenericDelegator(delegatorName);            if (internalDelegator == null) {                throw new IllegalStateException("[GenericEntity.getDelegator] could not find delegator with name " + delegatorName);            }        }        return internalDelegator;    }    /** Set the GenericDelegator instance that created this value object and that is repsonsible for it. */    public void setDelegator(GenericDelegator internalDelegator) {        if (internalDelegator == null) return;        this.delegatorName = internalDelegator.getDelegatorName();        this.internalDelegator = internalDelegator;    }    public Object get(String name) {        if (getModelEntity().getField(name) == null) {            throw new IllegalArgumentException("[GenericEntity.get] \"" + name + "\" is not a field of " + entityName);        }        return fields.get(name);    }    /** Returns true if the entity contains all of the primary key fields, but NO others. */    public boolean isPrimaryKey() {        return isPrimaryKey(false);    }    public boolean isPrimaryKey(boolean requireValue) {        TreeSet fieldKeys = new TreeSet(this.fields.keySet());        Iterator pkIter = getModelEntity().getPksIterator();        while (pkIter.hasNext()) {            ModelField curPk = (ModelField) pkIter.next();            String fieldName = curPk.getName();            if (requireValue) {                if (this.fields.get(fieldName) == null) return false;            } else {                if (!this.fields.containsKey(fieldName)) return false;            }            fieldKeys.remove(fieldName);        }        if (!fieldKeys.isEmpty()) return false;        return true;    }    /** Returns true if the entity contains all of the primary key fields. */    public boolean containsPrimaryKey() {        return containsPrimaryKey(false);    }    public boolean containsPrimaryKey(boolean requireValue) {        //TreeSet fieldKeys = new TreeSet(fields.keySet());

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -