📄 modelentity.java
字号:
/* * $Id: ModelEntity.java 6327 2005-12-14 18:56:54Z jaz $ * * 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.model;import java.io.Serializable;import java.util.Collection;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 org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.GeneralException;import org.ofbiz.base.util.ObjectType;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilTimer;import org.ofbiz.base.util.UtilXml;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntity;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.config.DatasourceInfo;import org.ofbiz.entity.config.EntityConfigUtil;import org.ofbiz.entity.jdbc.DatabaseUtil;/** * Generic Entity - Entity model class * * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @version $Rev: 6327 $ * @since 2.0 */public class ModelEntity extends ModelInfo implements Comparable, Serializable { public static final String module = ModelEntity.class.getName(); /** The name of the time stamp field for locking/syncronization */ public static final String STAMP_FIELD = "lastUpdatedStamp"; public static final String STAMP_TX_FIELD = "lastUpdatedTxStamp"; public static final String CREATE_STAMP_FIELD = "createdStamp"; public static final String CREATE_STAMP_TX_FIELD = "createdTxStamp"; /** The ModelReader that created this Entity */ protected ModelReader modelReader = null; /** The entity-name of the Entity */ protected String entityName = ""; /** The table-name of the Entity */ protected String tableName = ""; /** The package-name of the Entity */ protected String packageName = ""; /** The default-resource-name of the Entity, used with the getResource call to check for a value in a resource bundle */ protected String defaultResourceName = ""; /** The entity-name of the Entity that this Entity is dependent on, if empty then no dependency */ protected String dependentOn = ""; /** A List of the Field objects for the Entity */ protected List fields = FastList.newInstance(); protected Map fieldsMap = null; /** A List of the Field objects for the Entity, one for each Primary Key */ protected List pks = FastList.newInstance(); /** A List of the Field objects for the Entity, one for each NON Primary Key */ protected List nopks = FastList.newInstance(); /** relations defining relationships between this entity and other entities */ protected List relations = FastList.newInstance(); /** indexes on fields/columns in this entity */ protected List indexes = FastList.newInstance(); /** map of ModelViewEntities that references this model */ protected Map viewEntities = FastMap.newInstance(); /** An indicator to specify if this entity requires locking for updates */ protected boolean doLock = false; /** Can be used to disable automatically creating update stamp fields and populating them on inserts and updates */ protected boolean noAutoStamp = false; /** An indicator to specify if this entity is never cached. * If true causes the delegator to not clear caches on write and to not get * from cache on read showing a warning messages to that effect */ protected boolean neverCache = false; protected boolean autoClearCache = true; // ===== CONSTRUCTORS ===== /** Default Constructor */ public ModelEntity() {} /** XML Constructor */ protected ModelEntity(ModelReader reader, Element entityElement, ModelInfo def) { super(def); populateFromAttributes(entityElement); this.modelReader = reader; } /** XML Constructor */ public ModelEntity(ModelReader reader, Element entityElement, UtilTimer utilTimer, ModelInfo def) { this(reader, entityElement, def); if (utilTimer != null) utilTimer.timerString(" createModelEntity: before general/basic info"); this.populateBasicInfo(entityElement); if (utilTimer != null) utilTimer.timerString(" createModelEntity: before fields"); NodeList fieldList = entityElement.getElementsByTagName("field"); for (int i = 0; i < fieldList.getLength(); i++) { ModelField field = reader.createModelField((Element) fieldList.item(i)); if (field != null) { field.setModelEntity(this); this.fields.add(field); } } // if applicable automatically add the STAMP_FIELD and STAMP_TX_FIELD fields if ((this.doLock || !this.noAutoStamp) && !this.isField(STAMP_FIELD)) { ModelField newField = reader.createModelField(STAMP_FIELD, "date-time", null, false); newField.setIsAutoCreatedInternal(true); newField.setModelEntity(this); this.fields.add(newField); } if (!this.noAutoStamp && !this.isField(STAMP_TX_FIELD)) { ModelField newField = reader.createModelField(STAMP_TX_FIELD, "date-time", null, false); newField.setIsAutoCreatedInternal(true); newField.setModelEntity(this); this.fields.add(newField); // also add an index for this field String indexName = ModelUtil.shortenDbName(this.tableName + "_TXSTMP", 18); ModelIndex txIndex = new ModelIndex(this, indexName, false); txIndex.addIndexField(ModelEntity.STAMP_TX_FIELD); txIndex.setModelEntity(this); indexes.add(txIndex); } // if applicable automatically add the CREATE_STAMP_FIELD and CREATE_STAMP_TX_FIELD fields if ((this.doLock || !this.noAutoStamp) && !this.isField(CREATE_STAMP_FIELD)) { ModelField newField = reader.createModelField(CREATE_STAMP_FIELD, "date-time", null, false); newField.setIsAutoCreatedInternal(true); newField.setModelEntity(this); this.fields.add(newField); } if (!this.noAutoStamp && !this.isField(CREATE_STAMP_TX_FIELD)) { ModelField newField = reader.createModelField(CREATE_STAMP_TX_FIELD, "date-time", null, false); newField.setIsAutoCreatedInternal(true); newField.setModelEntity(this); this.fields.add(newField); // also add an index for this field String indexName = ModelUtil.shortenDbName(this.tableName + "_TXCRTS", 18); ModelIndex txIndex = new ModelIndex(this, indexName, false); txIndex.addIndexField(ModelEntity.CREATE_STAMP_TX_FIELD); txIndex.setModelEntity(this); indexes.add(txIndex); } if (utilTimer != null) utilTimer.timerString(" createModelEntity: before prim-keys"); NodeList pkList = entityElement.getElementsByTagName("prim-key"); for (int i = 0; i < pkList.getLength(); i++) { ModelField field = reader.findModelField(this, ((Element) pkList.item(i)).getAttribute("field")); if (field != null) { this.pks.add(field); field.isPk = true; } else { Debug.logError("[ModelReader.createModelEntity] ERROR: Could not find field \"" + ((Element) pkList.item(i)).getAttribute("field") + "\" specified in a prim-key", module); } } // now that we have the pks and the fields, make the nopks vector this.nopks = FastList.newInstance(); for (int ind = 0; ind < this.fields.size(); ind++) { ModelField field = (ModelField) this.fields.get(ind); if (!field.isPk) this.nopks.add(field); } if (utilTimer != null) utilTimer.timerString(" createModelEntity: before relations"); this.populateRelated(reader, entityElement); this.populateIndexes(entityElement); } /** DB Names Constructor */ public ModelEntity(String tableName, Map colMap, ModelFieldTypeReader modelFieldTypeReader, boolean isCaseSensitive) { // if there is a dot in the name, remove it and everything before it, should be the schema name this.tableName = tableName; int dotIndex = this.tableName.indexOf("."); if (dotIndex >= 0) { this.tableName = this.tableName.substring(dotIndex + 1); } this.entityName = ModelUtil.dbNameToClassName(this.tableName); Iterator columnEntryIter = colMap.entrySet().iterator(); while (columnEntryIter.hasNext()) { Map.Entry columnEntry = (Map.Entry) columnEntryIter.next(); DatabaseUtil.ColumnCheckInfo ccInfo = (DatabaseUtil.ColumnCheckInfo) columnEntry.getValue(); ModelField newField = new ModelField(ccInfo, modelFieldTypeReader); this.fields.add(newField); } this.updatePkLists(); } protected void populateBasicInfo(Element entityElement) { this.entityName = UtilXml.checkEmpty(entityElement.getAttribute("entity-name")); this.tableName = UtilXml.checkEmpty(entityElement.getAttribute("table-name"), ModelUtil.javaNameToDbName(this.entityName)); this.packageName = UtilXml.checkEmpty(entityElement.getAttribute("package-name")); this.defaultResourceName = UtilXml.checkEmpty(entityElement.getAttribute("default-resource-name")); this.dependentOn = UtilXml.checkEmpty(entityElement.getAttribute("dependent-on")); this.doLock = UtilXml.checkBoolean(entityElement.getAttribute("enable-lock"), false); this.noAutoStamp = UtilXml.checkBoolean(entityElement.getAttribute("no-auto-stamp"), false); this.neverCache = UtilXml.checkBoolean(entityElement.getAttribute("never-cache"), false); this.autoClearCache = UtilXml.checkBoolean(entityElement.getAttribute("auto-clear-cache"), true); } protected void populateRelated(ModelReader reader, Element entityElement) { NodeList relationList = entityElement.getElementsByTagName("relation"); for (int i = 0; i < relationList.getLength(); i++) { Element relationElement = (Element) relationList.item(i); if (relationElement.getParentNode() == entityElement) { ModelRelation relation = reader.createRelation(this, relationElement); if (relation != null) { relation.setModelEntity(this); this.relations.add(relation); } } } } protected void populateIndexes(Element entityElement) { NodeList indexList = entityElement.getElementsByTagName("index"); for (int i = 0; i < indexList.getLength(); i++) { Element indexElement = (Element) indexList.item(i); if (indexElement.getParentNode() == entityElement) { ModelIndex index = new ModelIndex(this, indexElement); index.setModelEntity(this); this.indexes.add(index); } } } public boolean containsAllPkFieldNames(Set fieldNames) { Iterator pksIter = this.getPksIterator(); while (pksIter.hasNext()) { ModelField pkField = (ModelField) pksIter.next(); if (!fieldNames.contains(pkField.getName())) { return false; } } return true; } // ===== GETTERS/SETTERS ===== public ModelReader getModelReader() { return modelReader; } /** The entity-name of the Entity */ public String getEntityName() { return this.entityName; } public void setEntityName(String entityName) { this.entityName = entityName; } /** The plain table-name of the Entity without a schema name prefix */ public String getPlainTableName() { return this.tableName; } /** The table-name of the Entity including a Schema name if specified in the datasource config */ public String getTableName(String helperName) { return getTableName(EntityConfigUtil.getDatasourceInfo(helperName)); } /** The table-name of the Entity including a Schema name if specified in the datasource config */ public String getTableName(DatasourceInfo datasourceInfo) { if (datasourceInfo != null && datasourceInfo.schemaName != null && datasourceInfo.schemaName.length() > 0) { return datasourceInfo.schemaName + "." + this.tableName; } else { return this.tableName; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -