|
Author: adrianc
Date: Thu May 2 08:51:09 2013 New Revision: 1478302 URL: http://svn.apache.org/r1478302 Log: Made ModelInfo immutable, modified ModelEntity so it contains ModelInfo instead of extending it. Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelEntity.java ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelInfo.java ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelReader.java ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelViewEntity.java Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelEntity.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelEntity.java?rev=1478302&r1=1478301&r2=1478302&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelEntity.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelEntity.java Thu May 2 08:51:09 2013 @@ -38,13 +38,11 @@ import java.util.concurrent.CopyOnWriteA import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.GeneralException; import org.ofbiz.base.util.ObjectType; -import org.ofbiz.base.util.StringUtil; import org.ofbiz.base.util.UtilMisc; import org.ofbiz.base.util.UtilPlist; import org.ofbiz.base.util.UtilTimer; import org.ofbiz.base.util.UtilValidate; import org.ofbiz.base.util.UtilXml; -import org.ofbiz.entity.model.ModelIndex.Field; import org.ofbiz.entity.Delegator; import org.ofbiz.entity.GenericEntity; import org.ofbiz.entity.GenericEntityException; @@ -52,6 +50,7 @@ import org.ofbiz.entity.GenericValue; import org.ofbiz.entity.config.DatasourceInfo; import org.ofbiz.entity.config.EntityConfigUtil; import org.ofbiz.entity.jdbc.DatabaseUtil; +import org.ofbiz.entity.model.ModelIndex.Field; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -60,7 +59,7 @@ import org.w3c.dom.Element; * */ @SuppressWarnings("serial") -public class ModelEntity extends ModelInfo implements Comparable<ModelEntity>, Serializable { +public class ModelEntity implements Comparable<ModelEntity>, Serializable { public static final String module = ModelEntity.class.getName(); @@ -70,6 +69,8 @@ public class ModelEntity extends ModelIn public static final String CREATE_STAMP_FIELD = "createdStamp"; public static final String CREATE_STAMP_TX_FIELD = "createdTxStamp"; + private ModelInfo modelInfo; + /** The ModelReader that created this Entity */ private final ModelReader modelReader; @@ -138,26 +139,29 @@ public class ModelEntity extends ModelIn /** Default Constructor */ public ModelEntity() { this.modelReader = null; + this.modelInfo = ModelInfo.DEFAULT; } protected ModelEntity(ModelReader reader) { this.modelReader = reader; + this.modelInfo = ModelInfo.DEFAULT; } - protected ModelEntity(ModelReader reader, ModelInfo def) { - super(def); + protected ModelEntity(ModelReader reader, ModelInfo modelInfo) { this.modelReader = reader; + this.modelInfo = modelInfo; } /** XML Constructor */ - protected ModelEntity(ModelReader reader, Element entityElement, ModelInfo def) { - this(reader, def); - populateFromAttributes(entityElement); + protected ModelEntity(ModelReader reader, Element entityElement, ModelInfo modelInfo) { + this.modelReader = reader; + this.modelInfo = ModelInfo.createFromAttributes(modelInfo, entityElement); } /** XML Constructor */ - public ModelEntity(ModelReader reader, Element entityElement, UtilTimer utilTimer, ModelInfo def) { - this(reader, entityElement, def); + public ModelEntity(ModelReader reader, Element entityElement, UtilTimer utilTimer, ModelInfo modelInfo) { + this.modelReader = reader; + this.modelInfo = ModelInfo.createFromAttributes(modelInfo, entityElement); if (utilTimer != null) utilTimer.timerString(" createModelEntity: before general/basic info"); this.populateBasicInfo(entityElement); if (utilTimer != null) utilTimer.timerString(" createModelEntity: before prim-keys"); @@ -221,6 +225,7 @@ public class ModelEntity extends ModelIn public ModelEntity(String tableName, Map<String, DatabaseUtil.ColumnCheckInfo> 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.modelReader = null; + this.modelInfo = ModelInfo.DEFAULT; this.tableName = tableName; int dotIndex = this.tableName.indexOf("."); if (dotIndex >= 0) { @@ -357,14 +362,7 @@ public class ModelEntity extends ModelIn } } } - - // override the default resource file - String defResourceName = StringUtil.internString(extendEntityElement.getAttribute("default-resource-name")); - //Debug.logInfo("Extended entity - " + extendEntityElement.getAttribute("entity-name") + " new resource name : " + defResourceName, module); - if (UtilValidate.isNotEmpty(defResourceName)) { - this.setDefaultResourceName(defResourceName); - } - + this.modelInfo = ModelInfo.createFromAttributes(this.modelInfo, extendEntityElement); this.populateRelated(reader, extendEntityElement); this.populateIndexes(extendEntityElement); this.dependentOn = UtilXml.checkEmpty(extendEntityElement.getAttribute("dependent-on")).intern(); @@ -1662,4 +1660,29 @@ public class ModelEntity extends ModelIn return topLevelMap; } + + public String getAuthor() { + return modelInfo.getAuthor(); + } + + public String getCopyright() { + return modelInfo.getCopyright(); + } + + public String getDefaultResourceName() { + return modelInfo.getDefaultResourceName(); + } + + public String getDescription() { + return modelInfo.getDescription(); + } + + public String getTitle() { + return modelInfo.getTitle(); + } + + public String getVersion() { + return modelInfo.getVersion(); + } + } Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelInfo.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelInfo.java?rev=1478302&r1=1478301&r2=1478302&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelInfo.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelInfo.java Thu May 2 08:51:09 2013 @@ -21,134 +21,154 @@ package org.ofbiz.entity.model; import java.util.Locale; import java.util.TimeZone; +import org.ofbiz.base.lang.ThreadSafe; import org.ofbiz.base.util.StringUtil; import org.ofbiz.base.util.UtilDateTime; import org.ofbiz.base.util.UtilXml; -import org.ofbiz.base.util.UtilValidate; import org.w3c.dom.Element; /** - * Generic Entity - Entity model class + * An object that models the <code><entitymodel></code> child elements that provide default values. * */ -public class ModelInfo { +@ThreadSafe +public final class ModelInfo { - public static final String module = ModelInfo.class.getName(); + public static final ModelInfo DEFAULT = new ModelInfo("None", "None", getCopyrightString(), "None", "1.0", ""); + + /** + * Returns a new <code>ModelInfo</code> instance initialized to the values found in <code>element</code> attributes. + * + * @param defaultInfo A <code>ModelInfo</code> instance that will provide default values for missing attributes. + * @param element + */ + public static ModelInfo createFromAttributes(ModelInfo defaultInfo, Element element) { + String title = element.getAttribute("title").intern(); + if (title.isEmpty()) { + title = defaultInfo.getTitle(); + } + String description = StringUtil.internString(UtilXml.childElementValue(element, "description")); + if (description == null || description.isEmpty()) { + description = defaultInfo.getDescription(); + } + String copyright = element.getAttribute("copyright").intern(); + if (copyright.isEmpty()) { + copyright = defaultInfo.getCopyright(); + } + String author = element.getAttribute("author").intern(); + if (author.isEmpty()) { + author = defaultInfo.getAuthor(); + } + String version = element.getAttribute("version").intern(); + if (version.isEmpty()) { + version = defaultInfo.getVersion(); + } + String defaultResourceName = StringUtil.internString(element.getAttribute("default-resource-name")); + if (defaultResourceName.isEmpty()) { + defaultResourceName = defaultInfo.getDefaultResourceName(); + } + return new ModelInfo(title, description, copyright, author, version, defaultResourceName); + } + + /** + * Returns a new <code>ModelInfo</code> instance initialized to the values found in <code>element</code> child elements. + * + * @param defaultInfo A <code>ModelInfo</code> instance that will provide default values for missing child elements. + * @param element + */ + public static ModelInfo createFromElements(ModelInfo defaultInfo, Element element) { + String title = StringUtil.internString(UtilXml.childElementValue(element, "title")); + if (title == null || title.isEmpty()) { + title = defaultInfo.getTitle(); + } + String description = StringUtil.internString(UtilXml.childElementValue(element, "description")); + if (description == null || description.isEmpty()) { + description = defaultInfo.getDescription(); + } + String copyright = StringUtil.internString(UtilXml.childElementValue(element, "copyright")); + if (copyright == null || copyright.isEmpty()) { + copyright = defaultInfo.getCopyright(); + } + String author = StringUtil.internString(UtilXml.childElementValue(element, "author")); + if (author == null ||author.isEmpty()) { + author = defaultInfo.getAuthor(); + } + String version = StringUtil.internString(UtilXml.childElementValue(element, "version")); + if (version == null || version.isEmpty()) { + version = defaultInfo.getVersion(); + } + String defaultResourceName = StringUtil.internString(UtilXml.childElementValue(element, "default-resource-name")); + if (defaultResourceName == null || defaultResourceName.isEmpty()) { + defaultResourceName = defaultInfo.getDefaultResourceName(); + } + return new ModelInfo(title, description, copyright, author, version, defaultResourceName); + } + + private static String getCopyrightString() { + int year = UtilDateTime.getYear(UtilDateTime.nowTimestamp(), TimeZone.getDefault(), Locale.getDefault()); + return "Copyright 2001-" + year + " The Apache Software Foundation"; + } + + /* + * Developers - this is an immutable class. Once constructed, the object should not change state. + * Therefore, 'setter' methods are not allowed. If client code needs to modify the object's + * state, then it can create a new copy with the changed values. + */ - protected ModelInfo def; /** The title for documentation purposes */ - protected String title = ""; + private final String title; /** The description for documentation purposes */ - protected String description = ""; + private final String description; /** The copyright for documentation purposes */ - protected String copyright = ""; + private final String copyright; /** The author for documentation purposes */ - protected String author = ""; + private final String author; /** The version for documentation purposes */ - protected String version = ""; + private final String version; /** The default-resource-name of the Entity, used with the getResource call to check for a value in a resource bundle */ - protected String defaultResourceName = ""; - - // ===== CONSTRUCTORS ===== - - public ModelInfo() { - this(DEFAULT); - } - - public ModelInfo(ModelInfo def) { - this.def = def; - } - - public static final ModelInfo DEFAULT = new ModelInfo() { - @Override - public String getTitle() { return "None"; } - @Override - public String getAuthor() { return "None"; } - @Override - public String getCopyright() { int year = UtilDateTime.getYear(UtilDateTime.nowTimestamp(), TimeZone.getDefault(), Locale.getDefault()); return "Copyright 2001-" + year + " The Apache Software Foundation"; } - @Override - public String getVersion() { return "1.0"; } - @Override - public String getDescription() { return "None"; } - @Override - public String getDefaultResourceName() { return ""; } - }; - - public void populateFromAttributes(Element element) { - author = element.getAttribute("author").intern(); - copyright = element.getAttribute("copyright").intern(); - description = StringUtil.internString(UtilXml.childElementValue(element, "description")); - title = element.getAttribute("title").intern(); - version = element.getAttribute("version").intern(); - defaultResourceName = StringUtil.internString(element.getAttribute("default-resource-name")); - } - - public void populateFromElements(Element element) { - author = StringUtil.internString(UtilXml.childElementValue(element, "author")); - copyright = StringUtil.internString(UtilXml.childElementValue(element, "copyright")); - description = StringUtil.internString(UtilXml.childElementValue(element, "description")); - title = StringUtil.internString(UtilXml.childElementValue(element, "title")); - version = StringUtil.internString(UtilXml.childElementValue(element, "version")); - defaultResourceName = StringUtil.internString(UtilXml.childElementValue(element, "default-resource-name")); - } - - // Strings to go in the comment header. - /** The title for documentation purposes */ - public String getTitle() { - return UtilValidate.isNotEmpty(this.title) ? this.title : def.getTitle(); - } + private final String defaultResourceName; - public void setTitle(String title) { + ModelInfo(String title, String description, String copyright, String author, String version, String defaultResourceName) { this.title = title; - } - - /** The description for documentation purposes */ - public String getDescription() { - return UtilValidate.isNotEmpty(this.description) ? this.description : def.getDescription(); - } - - public void setDescription(String description) { this.description = description; - } - - /** The copyright for documentation purposes */ - public String getCopyright() { - return UtilValidate.isNotEmpty(this.copyright) ? this.copyright : def.getCopyright(); - } - - public void setCopyright(String copyright) { this.copyright = copyright; + this.author = author; + this.version = version; + this.defaultResourceName = defaultResourceName; } - /** The author for documentation purposes */ + /** Returns the author. */ public String getAuthor() { - return UtilValidate.isNotEmpty(this.author) ? this.author : def.getAuthor(); + return this.author; } - public void setAuthor(String author) { - this.author = author; + /** Returns the copyright. */ + public String getCopyright() { + return this.copyright; } - /** The version for documentation purposes */ - public String getVersion() { - return UtilValidate.isNotEmpty(this.version) ? this.version : def.getVersion(); + /** Returns the default resource name. */ + public String getDefaultResourceName() { + return this.defaultResourceName; } - public void setVersion(String version) { - this.version = version; + /** Returns the description. */ + public String getDescription() { + return this.description; } - /** The default-resource-name of the Entity */ - public String getDefaultResourceName() { - return UtilValidate.isNotEmpty(this.defaultResourceName) ? this.defaultResourceName : def.getDefaultResourceName(); + /** Returns the title. */ + public String getTitle() { + return this.title; } - public void setDefaultResourceName(String defaultResourceName) { - this.defaultResourceName = defaultResourceName; + /** Returns the version. */ + public String getVersion() { + return this.version; } } Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelReader.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelReader.java?rev=1478302&r1=1478301&r2=1478302&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelReader.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelReader.java Thu May 2 08:51:09 2013 @@ -223,8 +223,7 @@ public class ModelReader implements Seri docElement.normalize(); Node curChild = docElement.getFirstChild(); - ModelInfo def = new ModelInfo(); - def.populateFromElements(docElement); + ModelInfo def = ModelInfo.createFromElements(ModelInfo.DEFAULT, docElement); int i = 0; if (curChild != null) { Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelViewEntity.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelViewEntity.java?rev=1478302&r1=1478301&r2=1478302&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelViewEntity.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelViewEntity.java Thu May 2 08:51:09 2013 @@ -145,11 +145,15 @@ public class ModelViewEntity extends Mod } public ModelViewEntity(DynamicViewEntity dynamicViewEntity, ModelReader modelReader) { - super(modelReader); + super(modelReader, new ModelInfo( + dynamicViewEntity.getTitle(), + ModelInfo.DEFAULT.getDescription(), + ModelInfo.DEFAULT.getCopyright(), + ModelInfo.DEFAULT.getAuthor(), + ModelInfo.DEFAULT.getVersion(), + dynamicViewEntity.getDefaultResourceName())); this.entityName = dynamicViewEntity.getEntityName(); this.packageName = dynamicViewEntity.getPackageName(); - this.title = dynamicViewEntity.getTitle(); - this.defaultResourceName = dynamicViewEntity.getDefaultResourceName(); // member-entities Iterator<Map.Entry<String, ModelMemberEntity>> modelMemberEntitiesEntryIter = dynamicViewEntity.getModelMemberEntitiesEntryIter(); |
| Free forum by Nabble | Edit this page |
