This is an automated email from the ASF dual-hosted git repository.
mridulpathak pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git
The following commit(s) were added to refs/heads/trunk by this push:
new a8e21e6f7e Build ModelEntity and ModelViewEntity through static
factories
a8e21e6f7e is described below
commit a8e21e6f7e8edae80828c09c40ed08279e259812
Author: Mridul Pathak <[email protected]>
AuthorDate: Sun Nov 1 09:03:52 2026 +0530
Build ModelEntity and ModelViewEntity through static factories
The constructors that built an entity model from XML, from database
metadata or from a DynamicViewEntity passed this to field, relation, index and
view-link factories and called overridable methods before a subclass was
initialized. They are replaced by ModelEntity.createFromXml and
createFromDatabaseMetaData and by ModelViewEntity.createFromXml and
createFromDynamicView, which construct an empty model and populate it
afterwards. Both classes stay open, keeping the assign-only constru [...]
---
.../org/apache/ofbiz/entity/jdbc/DatabaseUtil.java | 16 +--
.../ofbiz/entity/model/DynamicViewEntity.java | 3 +-
.../org/apache/ofbiz/entity/model/ModelEntity.java | 115 +++++++++------------
.../org/apache/ofbiz/entity/model/ModelReader.java | 14 ++-
.../apache/ofbiz/entity/model/ModelViewEntity.java | 53 +++++++---
5 files changed, 92 insertions(+), 109 deletions(-)
diff --git
a/framework/entity/src/main/java/org/apache/ofbiz/entity/jdbc/DatabaseUtil.java
b/framework/entity/src/main/java/org/apache/ofbiz/entity/jdbc/DatabaseUtil.java
index fcb6e4e2c4..47eaeb9aee 100644
---
a/framework/entity/src/main/java/org/apache/ofbiz/entity/jdbc/DatabaseUtil.java
+++
b/framework/entity/src/main/java/org/apache/ofbiz/entity/jdbc/DatabaseUtil.java
@@ -867,12 +867,11 @@ public class DatabaseUtil {
// go through each table and make a ModelEntity object, add to list
// for each entity make corresponding ModelField objects
// then print out XML for the entities/fields
- boolean isCaseSensitive = getIsCaseSensitive(messages);
// iterate over the table names is alphabetical order
for (String tableName : new TreeSet<>(colInfo.keySet())) {
Map<String, ColumnCheckInfo> colMap = colInfo.get(tableName);
Map<String, ReferenceCheckInfo> refMap =
refInfo.get(tableName);
- ModelEntity newEntity = new ModelEntity(tableName, colMap,
refMap, modelFieldTypeReader, isCaseSensitive);
+ ModelEntity newEntity =
ModelEntity.createFromDatabaseMetaData(tableName, colMap, refMap,
modelFieldTypeReader);
newEntList.add(newEntity);
}
}
@@ -890,19 +889,6 @@ public class DatabaseUtil {
}
}
- private boolean getIsCaseSensitive(Collection<String> messages) {
- boolean isCaseSensitive = false;
- try (Connection connection = getConnectionLogged(messages)) {
- DatabaseMetaData dbData = this.getDatabaseMetaData(connection,
messages);
- if (dbData != null) {
- isCaseSensitive = dbData.supportsMixedCaseIdentifiers();
- }
- } catch (SQLException e) {
- Debug.logError(e, "Error getting db meta data about case
sensitive", MODULE);
- }
- return isCaseSensitive;
- }
-
/**
* Gets database meta data.
* @param connection the connection
diff --git
a/framework/entity/src/main/java/org/apache/ofbiz/entity/model/DynamicViewEntity.java
b/framework/entity/src/main/java/org/apache/ofbiz/entity/model/DynamicViewEntity.java
index 091387db26..d22d16b8d1 100644
---
a/framework/entity/src/main/java/org/apache/ofbiz/entity/model/DynamicViewEntity.java
+++
b/framework/entity/src/main/java/org/apache/ofbiz/entity/model/DynamicViewEntity.java
@@ -84,8 +84,7 @@ public class DynamicViewEntity {
* @return the model view entity
*/
public ModelViewEntity makeModelViewEntity(Delegator delegator) {
- ModelViewEntity modelViewEntity = new ModelViewEntity(this,
delegator.getModelReader());
- return modelViewEntity;
+ return ModelViewEntity.createFromDynamicView(this,
delegator.getModelReader());
}
/**
diff --git
a/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelEntity.java
b/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelEntity.java
index f524cd0cb1..02d120ab98 100644
---
a/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelEntity.java
+++
b/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelEntity.java
@@ -40,7 +40,6 @@ import org.apache.ofbiz.base.util.GeneralException;
import org.apache.ofbiz.base.util.ObjectType;
import org.apache.ofbiz.base.util.UtilMisc;
import org.apache.ofbiz.base.util.UtilPlist;
-import org.apache.ofbiz.base.util.UtilTimer;
import org.apache.ofbiz.base.util.UtilValidate;
import org.apache.ofbiz.base.util.UtilXml;
import org.apache.ofbiz.entity.Delegator;
@@ -151,37 +150,64 @@ public class ModelEntity implements
Comparable<ModelEntity>, Serializable {
this.modelInfo = modelInfo;
}
- /** XML Constructor */
- protected ModelEntity(ModelReader reader, Element entityElement, ModelInfo
modelInfo) {
- this.modelReader = reader;
- this.modelInfo = ModelInfo.createFromAttributes(modelInfo,
entityElement);
+ /**
+ * Creates the model of an entity defined by an {@code <entity>} element.
+ * @param reader the reader the entity belongs to
+ * @param entityElement the {@code <entity>} element
+ * @param def the default model information of the entity-model file
+ * @return the entity model
+ */
+ static ModelEntity createFromXml(ModelReader reader, Element
entityElement, ModelInfo def) {
+ ModelEntity modelEntity = new ModelEntity(reader,
ModelInfo.createFromAttributes(def, entityElement));
+ modelEntity.populateFromXml(reader, entityElement);
+ return modelEntity;
}
- /** XML Constructor */
- 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");
+ /**
+ * Creates the model of an entity from the metadata of an existing
database table.
+ * @param tableName the table name, optionally prefixed by the schema name
+ * @param colMap the columns of the table
+ * @param refMap the foreign keys of the table, may be {@code null}
+ * @param modelFieldTypeReader the field type reader of the datasource
+ * @return the entity model
+ */
+ public static ModelEntity createFromDatabaseMetaData(String tableName,
Map<String, DatabaseUtil.ColumnCheckInfo> colMap,
+ Map<String, DatabaseUtil.ReferenceCheckInfo> refMap,
ModelFieldTypeReader modelFieldTypeReader) {
+ ModelEntity modelEntity = new ModelEntity();
+ // if there is a dot in the name, remove it and everything before it,
should be the schema name
+ int dotIndex = tableName.indexOf('.');
+ modelEntity.tableName = dotIndex >= 0 ? tableName.substring(dotIndex +
1) : tableName;
+ modelEntity.entityName =
ModelUtil.dbNameToClassName(modelEntity.tableName);
+ for (DatabaseUtil.ColumnCheckInfo ccInfo : colMap.values()) {
+ modelEntity.addField(ModelField.create(modelEntity, ccInfo,
modelFieldTypeReader));
+ }
+ if (UtilValidate.isNotEmpty(refMap)) {
+ for (DatabaseUtil.ReferenceCheckInfo rcInfo : refMap.values()) {
+ modelEntity.addRelation(ModelRelation.create(modelEntity,
rcInfo, false));
+ }
+ }
+ return modelEntity;
+ }
+
+ private void populateFromXml(ModelReader reader, Element entityElement) {
this.populateBasicInfo(entityElement);
- if (utilTimer != null) utilTimer.timerString(" createModelEntity:
before prim-keys");
for (Element pkElement: UtilXml.childElementList(entityElement,
"prim-key")) {
pkFieldNames.add(pkElement.getAttribute("field").intern());
}
- if (utilTimer != null) utilTimer.timerString(" createModelEntity:
before fields");
for (Element fieldElement: UtilXml.childElementList(entityElement,
"field")) {
String fieldName =
UtilXml.checkEmpty(fieldElement.getAttribute("name")).intern();
boolean isPk = pkFieldNames.contains(fieldName);
ModelField field = ModelField.create(this, fieldElement, isPk);
- internalAddField(field, pkFieldNames);
+ internalAddField(field);
}
// if applicable automatically add the STAMP_FIELD and STAMP_TX_FIELD
fields
if ((this.doLock || !this.noAutoStamp) &&
!fieldsMap.containsKey(STAMP_FIELD)) {
ModelField newField = ModelField.create(this, "", STAMP_FIELD,
"date-time", null, null, null, false, false, false, true, false, null);
- internalAddField(newField, pkFieldNames);
+ internalAddField(newField);
}
if (!this.noAutoStamp && !fieldsMap.containsKey(STAMP_TX_FIELD)) {
ModelField newField = ModelField.create(this, "", STAMP_TX_FIELD,
"date-time", null, null, null, false, false, false, true, false, null);
- internalAddField(newField, pkFieldNames);
+ internalAddField(newField);
// also add an index for this field
String indexName = ModelUtil.shortenDbName(this.tableName +
"_TXSTMP", 18);
Field indexField = new Field(STAMP_TX_FIELD, null);
@@ -192,12 +218,12 @@ public class ModelEntity implements
Comparable<ModelEntity>, Serializable {
if ((this.doLock || !this.noAutoStamp) &&
!fieldsMap.containsKey(CREATE_STAMP_FIELD)) {
ModelField newField = ModelField.create(this, "",
CREATE_STAMP_FIELD, "date-time", null, null, null, false, false, false, true,
false, null);
- internalAddField(newField, pkFieldNames);
+ internalAddField(newField);
}
if (!this.noAutoStamp &&
!fieldsMap.containsKey(CREATE_STAMP_TX_FIELD)) {
ModelField newField = ModelField.create(this, "",
CREATE_STAMP_TX_FIELD, "date-time", null, null, null, false, false, false, true,
false, null);
- internalAddField(newField, pkFieldNames);
+ internalAddField(newField);
// also add an index for this field
String indexName = ModelUtil.shortenDbName(this.tableName +
"_TXCRTS", 18);
Field indexField = new Field(CREATE_STAMP_TX_FIELD, null);
@@ -217,62 +243,15 @@ public class ModelEntity implements
Comparable<ModelEntity>, Serializable {
pks.trimToSize();
nopks.trimToSize();
reader.incrementFieldCount(fieldsMap.size());
- if (utilTimer != null) utilTimer.timerString(" createModelEntity:
before relations");
this.populateRelated(reader, entityElement);
this.populateIndexes(entityElement);
}
- /** DB Names Constructor */
- 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) {
- this.tableName = this.tableName.substring(dotIndex + 1);
- }
- this.entityName = ModelUtil.dbNameToClassName(this.tableName);
- for (Map.Entry<String, DatabaseUtil.ColumnCheckInfo> columnEntry :
colMap.entrySet()) {
- DatabaseUtil.ColumnCheckInfo ccInfo = columnEntry.getValue();
- ModelField newField = ModelField.create(this, ccInfo,
modelFieldTypeReader);
- addField(newField);
- }
- }
-
- public ModelEntity(String tableName, Map<String,
DatabaseUtil.ColumnCheckInfo> colMap,
- Map<String, DatabaseUtil.ReferenceCheckInfo> refMap,
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) {
- this.tableName = this.tableName.substring(dotIndex + 1);
- }
- this.entityName = ModelUtil.dbNameToClassName(this.tableName);
- for (Map.Entry<String, DatabaseUtil.ColumnCheckInfo> columnEntry :
colMap.entrySet()) {
- DatabaseUtil.ColumnCheckInfo ccInfo = columnEntry.getValue();
- ModelField newField = ModelField.create(this, ccInfo,
modelFieldTypeReader);
- addField(newField);
- }
- if (UtilValidate.isNotEmpty(refMap)) {
- for (Map.Entry<String, DatabaseUtil.ReferenceCheckInfo>
referenceEntry : refMap.entrySet()) {
- DatabaseUtil.ReferenceCheckInfo rcInfo =
referenceEntry.getValue();
- ModelRelation newRel = ModelRelation.create(this, rcInfo,
false);
- addRelation(newRel);
- }
- }
- }
-
/**
* Populate basic info.
* @param entityElement the entity element
*/
- protected void populateBasicInfo(Element entityElement) {
+ void populateBasicInfo(Element entityElement) {
this.entityName =
UtilXml.checkEmpty(entityElement.getAttribute("entity-name")).intern();
this.tableName =
UtilXml.checkEmpty(entityElement.getAttribute("table-name"),
ModelUtil.javaNameToDbName(this.entityName)).intern();
this.packageName =
UtilXml.checkEmpty(entityElement.getAttribute("package-name")).intern();
@@ -293,7 +272,7 @@ public class ModelEntity implements
Comparable<ModelEntity>, Serializable {
}
}
- private void internalAddField(ModelField newField, List<String>
pkFieldNames) {
+ private void internalAddField(ModelField newField) {
if (!newField.getIsPk()) {
this.nopks.add(newField);
}
@@ -306,7 +285,7 @@ public class ModelEntity implements
Comparable<ModelEntity>, Serializable {
* @param reader the reader
* @param entityElement the entity element
*/
- protected void populateRelated(ModelReader reader, Element entityElement) {
+ void populateRelated(ModelReader reader, Element entityElement) {
List<ModelRelation> tempList = new ArrayList<>(this.relations);
for (Element relationElement: UtilXml.childElementList(entityElement,
"relation")) {
ModelRelation relation = reader.createRelation(this,
relationElement);
@@ -322,7 +301,7 @@ public class ModelEntity implements
Comparable<ModelEntity>, Serializable {
* Populate indexes.
* @param entityElement the entity element
*/
- protected void populateIndexes(Element entityElement) {
+ private void populateIndexes(Element entityElement) {
List<ModelIndex> tempList = new ArrayList<>(this.indexes);
for (Element indexElement: UtilXml.childElementList(entityElement,
"index")) {
ModelIndex index = ModelIndex.create(this, indexElement);
diff --git
a/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelReader.java
b/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelReader.java
index eeaff7cb21..a7bb9e10b4 100644
---
a/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelReader.java
+++
b/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelReader.java
@@ -158,9 +158,9 @@ public final class ModelReader implements Serializable {
ModelEntity modelEntity = null;
if (isEntity) {
- modelEntity = createModelEntity(curEntityElement, null, def);
+ modelEntity = createModelEntity(curEntityElement, def);
} else {
- modelEntity = createModelViewEntity(curEntityElement, null, def);
+ modelEntity = createModelViewEntity(curEntityElement, def);
}
String resourceLocation = entityResourceHandler.getLocation();
@@ -611,22 +611,20 @@ public final class ModelReader implements Serializable {
return (!entityName.isEmpty() ? entityName : null);
}
- ModelEntity createModelEntity(Element entityElement, UtilTimer utilTimer,
ModelInfo def) {
+ ModelEntity createModelEntity(Element entityElement, ModelInfo def) {
if (entityElement == null) {
return null;
}
this.numEntities++;
- ModelEntity entity = new ModelEntity(this, entityElement, utilTimer,
def);
- return entity;
+ return ModelEntity.createFromXml(this, entityElement, def);
}
- ModelEntity createModelViewEntity(Element entityElement, UtilTimer
utilTimer, ModelInfo def) {
+ ModelEntity createModelViewEntity(Element entityElement, ModelInfo def) {
if (entityElement == null) {
return null;
}
this.numViewEntities++;
- ModelViewEntity entity = new ModelViewEntity(this, entityElement,
utilTimer, def);
- return entity;
+ return ModelViewEntity.createFromXml(this, entityElement, def);
}
public ModelRelation createRelation(ModelEntity entity, Element
relationElement) {
diff --git
a/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelViewEntity.java
b/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelViewEntity.java
index 0659fc9335..ca5c289b28 100644
---
a/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelViewEntity.java
+++
b/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelViewEntity.java
@@ -34,7 +34,6 @@ import java.util.Set;
import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.StringUtil;
import org.apache.ofbiz.base.util.UtilFormatOut;
-import org.apache.ofbiz.base.util.UtilTimer;
import org.apache.ofbiz.base.util.UtilValidate;
import org.apache.ofbiz.base.util.UtilXml;
import org.apache.ofbiz.entity.condition.EntityComparisonOperator;
@@ -114,18 +113,49 @@ public class ModelViewEntity extends ModelEntity {
private ViewEntityCondition viewEntityCondition = null;
- public ModelViewEntity(ModelReader reader, Element entityElement,
UtilTimer utilTimer, ModelInfo def) {
- super(reader, entityElement, def);
+ protected ModelViewEntity(ModelReader reader, ModelInfo modelInfo) {
+ super(reader, modelInfo);
+ }
+
+ /**
+ * Creates the model of a view entity defined by a {@code <view-entity>}
element.
+ * @param reader the reader the view entity belongs to
+ * @param entityElement the {@code <view-entity>} element
+ * @param def the default model information of the entity-model file
+ * @return the view entity model, its fields are populated once all
entities are read
+ */
+ static ModelViewEntity createFromXml(ModelReader reader, Element
entityElement, ModelInfo def) {
+ ModelViewEntity modelViewEntity = new ModelViewEntity(reader,
ModelInfo.createFromAttributes(def, entityElement));
+ modelViewEntity.populateFromXml(reader, entityElement);
+ return modelViewEntity;
+ }
- if (utilTimer != null) utilTimer.timerString(" createModelViewEntity:
before general/basic info");
+ /**
+ * Creates the model of a view entity from a dynamic view entity.
+ * @param dynamicViewEntity the dynamic view entity
+ * @param modelReader the reader of the member entities
+ * @return the view entity model
+ */
+ static ModelViewEntity createFromDynamicView(DynamicViewEntity
dynamicViewEntity, ModelReader modelReader) {
+ ModelViewEntity modelViewEntity = new ModelViewEntity(modelReader, new
ModelInfo(
+ dynamicViewEntity.getTitle(),
+ ModelInfo.DEFAULT.getDescription(),
+ ModelInfo.DEFAULT.getCopyright(),
+ ModelInfo.DEFAULT.getAuthor(),
+ ModelInfo.DEFAULT.getVersion(),
+ dynamicViewEntity.getDefaultResourceName()));
+ modelViewEntity.populateFromDynamicView(dynamicViewEntity,
modelReader);
+ return modelViewEntity;
+ }
+
+ private void populateFromXml(ModelReader reader, Element entityElement) {
this.populateBasicInfo(entityElement);
- if (utilTimer != null) utilTimer.timerString(" createModelViewEntity:
before \"member-entity\"s");
for (Element memberEntityElement:
UtilXml.childElementList(entityElement, "member-entity")) {
String alias =
UtilXml.checkEmpty(memberEntityElement.getAttribute("entity-alias")).intern();
String name =
UtilXml.checkEmpty(memberEntityElement.getAttribute("entity-name")).intern();
if (name.length() <= 0 || alias.length() <= 0) {
- Debug.logError("[new ModelViewEntity]: entity-alias or
entity-name missing on member-entity element of the view-entity "
+ Debug.logError("[ModelViewEntity.createFromXml]: entity-alias
or entity-name missing on member-entity element of the view-entity "
+ this.getEntityName(), MODULE);
} else {
ModelMemberEntity modelMemberEntity = new
ModelMemberEntity(alias, name);
@@ -140,7 +170,6 @@ public class ModelViewEntity extends ModelEntity {
this.aliasAlls.add(aliasAll);
}
- if (utilTimer != null) utilTimer.timerString(" createModelViewEntity:
before aliases");
for (Element aliasElement: UtilXml.childElementList(entityElement,
"alias")) {
ModelViewEntity.ModelAlias alias = new ModelAlias(aliasElement);
this.aliases.add(alias);
@@ -151,7 +180,6 @@ public class ModelViewEntity extends ModelEntity {
this.addViewLink(viewLink);
}
- if (utilTimer != null) utilTimer.timerString(" createModelEntity:
before relations");
this.populateRelated(reader, entityElement);
Element entityConditionElement =
UtilXml.firstChildElement(entityElement, "entity-condition");
@@ -163,14 +191,7 @@ public class ModelViewEntity extends ModelEntity {
this.setTableName(null);
}
- public ModelViewEntity(DynamicViewEntity dynamicViewEntity, ModelReader
modelReader) {
- super(modelReader, new ModelInfo(
- dynamicViewEntity.getTitle(),
- ModelInfo.DEFAULT.getDescription(),
- ModelInfo.DEFAULT.getCopyright(),
- ModelInfo.DEFAULT.getAuthor(),
- ModelInfo.DEFAULT.getVersion(),
- dynamicViewEntity.getDefaultResourceName()));
+ private void populateFromDynamicView(DynamicViewEntity dynamicViewEntity,
ModelReader modelReader) {
this.setEntityName(dynamicViewEntity.getEntityName());
this.setPackageName(dynamicViewEntity.getPackageName());