Author: mbrohl
Date: Fri Dec 8 20:18:41 2017 New Revision: 1817559 URL: http://svn.apache.org/viewvc?rev=1817559&view=rev Log: Improved: Fixing defects reported by FindBugs, package org.apache.ofbiz.entity.model. (OFBIZ-9719) Thanks Julian Leichert for reporting and providing the patch. Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelEntity.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelEntityChecker.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelField.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelFieldType.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelGroupReader.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelIndex.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelReader.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelUtil.java ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelViewEntity.java Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelEntity.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelEntity.java?rev=1817559&r1=1817558&r2=1817559&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelEntity.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelEntity.java Fri Dec 8 20:18:41 2017 @@ -94,32 +94,32 @@ public class ModelEntity implements Comp /** Model fields in the order they were defined. This list duplicates the values in fieldsMap, but * we must keep the list in its original sequence for SQL DISTINCT operations to work properly. */ - private final List<ModelField> fieldsList = new ArrayList<ModelField>(); + private final List<ModelField> fieldsList = new ArrayList<>(); - private final Map<String, ModelField> fieldsMap = new HashMap<String, ModelField>(); + private final Map<String, ModelField> fieldsMap = new HashMap<>(); - private final ArrayList<String> pkFieldNames = new ArrayList<String>(); + private final ArrayList<String> pkFieldNames = new ArrayList<>(); /** A List of the Field objects for the Entity, one for each Primary Key */ - private final ArrayList<ModelField> pks = new ArrayList<ModelField>(); + private final ArrayList<ModelField> pks = new ArrayList<>(); /** A List of the Field objects for the Entity, one for each NON Primary Key */ - private final ArrayList<ModelField> nopks = new ArrayList<ModelField>(); + private final ArrayList<ModelField> nopks = new ArrayList<>(); /** relations defining relationships between this entity and other entities */ - protected CopyOnWriteArrayList<ModelRelation> relations = new CopyOnWriteArrayList<ModelRelation>(); + protected CopyOnWriteArrayList<ModelRelation> relations = new CopyOnWriteArrayList<>(); /** indexes on fields/columns in this entity */ - private CopyOnWriteArrayList<ModelIndex> indexes = new CopyOnWriteArrayList<ModelIndex>(); + private CopyOnWriteArrayList<ModelIndex> indexes = new CopyOnWriteArrayList<>(); /** The reference of the dependentOn entity model */ protected ModelEntity specializationOfModelEntity = null; /** The list of entities that are specialization of on this entity */ - protected Map<String, ModelEntity> specializedEntities = new HashMap<String, ModelEntity>(); + protected Map<String, ModelEntity> specializedEntities = new HashMap<>(); /** map of ModelViewEntities that references this model */ - private final Set<String> viewEntities = new HashSet<String>(); + private final Set<String> viewEntities = new HashSet<>(); /** An indicator to specify if this entity requires locking for updates */ protected boolean doLock = false; @@ -232,7 +232,7 @@ public class ModelEntity implements Comp this.modelReader = null; this.modelInfo = ModelInfo.DEFAULT; this.tableName = tableName; - int dotIndex = this.tableName.indexOf("."); + int dotIndex = this.tableName.indexOf('.'); if (dotIndex >= 0) { this.tableName = this.tableName.substring(dotIndex + 1); } @@ -274,24 +274,24 @@ public class ModelEntity implements Comp } protected void populateRelated(ModelReader reader, Element entityElement) { - List<ModelRelation> tempList = new ArrayList<ModelRelation>(this.relations); + List<ModelRelation> tempList = new ArrayList<>(this.relations); for (Element relationElement: UtilXml.childElementList(entityElement, "relation")) { ModelRelation relation = reader.createRelation(this, relationElement); if (relation != null) { tempList.add(relation); } } - this.relations = new CopyOnWriteArrayList<ModelRelation>(tempList); + this.relations = new CopyOnWriteArrayList<>(tempList); } protected void populateIndexes(Element entityElement) { - List<ModelIndex> tempList = new ArrayList<ModelIndex>(this.indexes); + List<ModelIndex> tempList = new ArrayList<>(this.indexes); for (Element indexElement: UtilXml.childElementList(entityElement, "index")) { ModelIndex index = ModelIndex.create(this, indexElement); tempList.add(index); } - this.indexes = new CopyOnWriteArrayList<ModelIndex>(tempList); + this.indexes = new CopyOnWriteArrayList<>(tempList); } public boolean containsAllPkFieldNames(Set<String> fieldNames) { @@ -310,19 +310,19 @@ public class ModelEntity implements Comp if (extendEntityElement.hasAttribute("enable-lock")) { this.doLock = UtilXml.checkBoolean(extendEntityElement.getAttribute("enable-lock"), false); } - + if (extendEntityElement.hasAttribute("no-auto-stamp")) { this.noAutoStamp = UtilXml.checkBoolean(extendEntityElement.getAttribute("no-auto-stamp"), false); } - + if (extendEntityElement.hasAttribute("auto-clear-cache")) { this.autoClearCache = UtilXml.checkBoolean(extendEntityElement.getAttribute("auto-clear-cache"), false); } - + if (extendEntityElement.hasAttribute("never-cache")) { this.neverCache = UtilXml.checkBoolean(extendEntityElement.getAttribute("never-cache"), false); } - + if (extendEntityElement.hasAttribute("sequence-bank-size")) { String sequenceBankSizeStr = UtilXml.checkEmpty(extendEntityElement.getAttribute("sequence-bank-size")); if (UtilValidate.isNotEmpty(sequenceBankSizeStr)) { @@ -333,7 +333,7 @@ public class ModelEntity implements Comp } } } - + for (Element fieldElement : UtilXml.childElementList(extendEntityElement, "field")) { ModelField newField = ModelField.create(this, fieldElement, false); ModelField existingField = this.getField(newField.getName()); @@ -457,7 +457,7 @@ public class ModelEntity implements Comp public void setNeverCache(boolean neverCache) { this.neverCache = neverCache; } - + /** * An indicator to specific if this entity should ignore automatic DB checks. * This should be set when the entity is mapped to a database view to prevent @@ -466,11 +466,11 @@ public class ModelEntity implements Comp public boolean getNeverCheck() { return neverCheck; } - + public void setNeverCheck(boolean neverCheck) { this.neverCheck = neverCheck; } - + public boolean getAutoClearCache() { return this.autoClearCache; } @@ -557,7 +557,7 @@ public class ModelEntity implements Comp public List<ModelField> getPkFields() { synchronized (fieldsLock) { - return new ArrayList<ModelField>(this.pks); + return new ArrayList<>(this.pks); } } @@ -586,7 +586,7 @@ public class ModelEntity implements Comp public List<ModelField> getNopksCopy() { synchronized (fieldsLock) { - return new ArrayList<ModelField>(this.nopks); + return new ArrayList<>(this.nopks); } } @@ -598,14 +598,14 @@ public class ModelEntity implements Comp public Iterator<ModelField> getFieldsIterator() { synchronized (fieldsLock) { - List<ModelField> newList = new ArrayList<ModelField>(this.fieldsList); + List<ModelField> newList = new ArrayList<>(this.fieldsList); return newList.iterator(); } } public List<ModelField> getFieldsUnmodifiable() { synchronized (fieldsLock) { - List<ModelField> newList = new ArrayList<ModelField>(this.fieldsList); + List<ModelField> newList = new ArrayList<>(this.fieldsList); return Collections.unmodifiableList(newList); } } @@ -661,13 +661,13 @@ public class ModelEntity implements Comp public List<String> getAllFieldNames() { synchronized (fieldsLock) { - return new ArrayList<String>(this.fieldsMap.keySet()); + return new ArrayList<>(this.fieldsMap.keySet()); } } public List<String> getPkFieldNames() { synchronized (fieldsLock) { - return new ArrayList<String>(pkFieldNames); + return new ArrayList<>(pkFieldNames); } } @@ -676,7 +676,7 @@ public class ModelEntity implements Comp } private List<String> getFieldNamesFromFieldVector(List<ModelField> modelFields) { - List<String> nameList = new ArrayList<String>(modelFields.size()); + List<String> nameList = new ArrayList<>(modelFields.size()); for (ModelField field: modelFields) { nameList.add(field.getName()); } @@ -687,7 +687,7 @@ public class ModelEntity implements Comp * @return field names list, managed by entity-engine */ public List<String> getAutomaticFieldNames() { - List<String> nameList = new LinkedList<String>(); + List<String> nameList = new LinkedList<>(); if (! this.noAutoStamp) { nameList.add(STAMP_FIELD); nameList.add(STAMP_TX_FIELD); @@ -722,7 +722,7 @@ public class ModelEntity implements Comp } public List<ModelRelation> getRelationsList(boolean includeOne, boolean includeOneNoFk, boolean includeMany) { - List<ModelRelation> relationsList = new LinkedList<ModelRelation>(); + List<ModelRelation> relationsList = new LinkedList<>(); Iterator<ModelRelation> allIter = this.getRelationsIterator(); while (allIter.hasNext()) { ModelRelation modelRelation = allIter.next(); @@ -797,7 +797,7 @@ public class ModelEntity implements Comp public Iterator<String> getViewConvertorsIterator() { synchronized (viewEntities) { - return new HashSet<String>(this.viewEntities).iterator(); + return new HashSet<>(this.viewEntities).iterator(); } } @@ -1463,7 +1463,7 @@ public class ModelEntity implements Comp if (this.getNeverCache()) { root.setAttribute("never-cache", "true"); } - + if (this.getNeverCheck()) { root.setAttribute("never-check", "true"); } @@ -1499,7 +1499,7 @@ public class ModelEntity implements Comp // append field elements Iterator<ModelField> fieldIter = this.getFieldsIterator(); - while (fieldIter != null && fieldIter.hasNext()) { + while (fieldIter.hasNext()) { ModelField field = fieldIter.next(); if (!field.getIsAutoCreatedInternal()) { root.appendChild(field.toXmlElement(document)); @@ -1517,14 +1517,14 @@ public class ModelEntity implements Comp // append relation elements Iterator<ModelRelation> relIter = this.getRelationsIterator(); - while (relIter != null && relIter.hasNext()) { + while (relIter.hasNext()) { ModelRelation rel = relIter.next(); root.appendChild(rel.toXmlElement(document)); } // append index elements Iterator<ModelIndex> idxIter = this.getIndexesIterator(); - while (idxIter != null && idxIter.hasNext()) { + while (idxIter.hasNext()) { ModelIndex idx = idxIter.next(); root.appendChild(idx.toXmlElement(document)); @@ -1562,14 +1562,14 @@ public class ModelEntity implements Comp final boolean useRelationshipNames = false; ModelFieldTypeReader modelFieldTypeReader = ModelFieldTypeReader.getModelFieldTypeReader(helperName); - Map<String, Object> topLevelMap = new HashMap<String, Object>(); + Map<String, Object> topLevelMap = new HashMap<>(); topLevelMap.put("name", this.getEntityName()); topLevelMap.put("externalName", this.getTableName(helperName)); topLevelMap.put("className", "EOGenericRecord"); // for classProperties add field names AND relationship names to get a nice, complete chart - List<String> classPropertiesList = new LinkedList<String>(); + List<String> classPropertiesList = new LinkedList<>(); topLevelMap.put("classProperties", classPropertiesList); for (ModelField field: this.fieldsList) { if (field.getIsAutoCreatedInternal()) continue; @@ -1587,14 +1587,14 @@ public class ModelEntity implements Comp } // attributes - List<Map<String, Object>> attributesList = new LinkedList<Map<String, Object>>(); + List<Map<String, Object>> attributesList = new LinkedList<>(); topLevelMap.put("attributes", attributesList); for (ModelField field: this.fieldsList) { if (field.getIsAutoCreatedInternal()) continue; ModelFieldType fieldType = modelFieldTypeReader.getModelFieldType(field.getType()); - Map<String, Object> attributeMap = new HashMap<String, Object>(); + Map<String, Object> attributeMap = new HashMap<>(); attributesList.add(attributeMap); if (field.getIsPk()) { @@ -1606,15 +1606,15 @@ public class ModelEntity implements Comp attributeMap.put("valueClassName", fieldType.getJavaType()); String sqlType = fieldType.getSqlType(); - if (sqlType.indexOf("(") >= 0) { - attributeMap.put("externalType", sqlType.substring(0, sqlType.indexOf("("))); + if (sqlType.indexOf('(') >= 0) { + attributeMap.put("externalType", sqlType.substring(0, sqlType.indexOf('('))); // since there is a field length set that - String widthStr = sqlType.substring(sqlType.indexOf("(") + 1, sqlType.indexOf(")")); + String widthStr = sqlType.substring(sqlType.indexOf('(') + 1, sqlType.indexOf(')')); // if there is a comma split by it for width,precision - if (widthStr.indexOf(",") >= 0) { - attributeMap.put("width", widthStr.substring(0, widthStr.indexOf(","))); + if (widthStr.indexOf(',') >= 0) { + attributeMap.put("width", widthStr.substring(0, widthStr.indexOf(','))); // since there is a field precision set that - attributeMap.put("precision", widthStr.substring(widthStr.indexOf(",") + 1)); + attributeMap.put("precision", widthStr.substring(widthStr.indexOf(',') + 1)); } else { attributeMap.put("width", widthStr); } @@ -1624,19 +1624,19 @@ public class ModelEntity implements Comp } // primaryKeyAttributes - List<String> primaryKeyAttributesList = new LinkedList<String>(); + List<String> primaryKeyAttributesList = new LinkedList<>(); topLevelMap.put("primaryKeyAttributes", primaryKeyAttributesList); for (ModelField pkField : getPkFields()) { primaryKeyAttributesList.add(pkField.getName()); } // relationships - List<Map<String, Object>> relationshipsMapList = new LinkedList<Map<String, Object>>(); + List<Map<String, Object>> relationshipsMapList = new LinkedList<>(); for (ModelRelation relationship: this.relations) { if (entityNameIncludeSet.contains(relationship.getRelEntityName())) { ModelEntity relEntity = entityModelReader.getModelEntity(relationship.getRelEntityName()); - Map<String, Object> relationshipMap = new HashMap<String, Object>(); + Map<String, Object> relationshipMap = new HashMap<>(); relationshipsMapList.add(relationshipMap); if (useRelationshipNames || relationship.isAutoRelation()) { @@ -1655,10 +1655,10 @@ public class ModelEntity implements Comp relationshipMap.put("joinSemantic", "EOInnerJoin"); - List<Map<String, Object>> joinsMapList = new LinkedList<Map<String, Object>>(); + List<Map<String, Object>> joinsMapList = new LinkedList<>(); relationshipMap.put("joins", joinsMapList); for (ModelKeyMap keyMap: relationship.getKeyMaps()) { - Map<String, Object> joinsMap = new HashMap<String, Object>(); + Map<String, Object> joinsMap = new HashMap<>(); joinsMapList.add(joinsMap); ModelField thisField = this.getField(keyMap.getFieldName()); Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelEntityChecker.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelEntityChecker.java?rev=1817559&r1=1817558&r2=1817559&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelEntityChecker.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelEntityChecker.java Fri Dec 8 20:18:41 2017 @@ -23,6 +23,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.TreeSet; @@ -43,7 +44,7 @@ public class ModelEntityChecker { public static void checkEntities(Delegator delegator, List<String> warningList) throws GenericEntityException { ModelReader reader = delegator.getModelReader(); - Set<String> reservedWords = new HashSet<String>(); + Set<String> reservedWords = new HashSet<>(); if (Debug.infoOn()) { Debug.logInfo("[initReservedWords] array length = " + rwArray.length, module); } @@ -51,13 +52,13 @@ public class ModelEntityChecker { reservedWords.add(rwArray[i]); } - Map<String, Set<String>> packages = new HashMap<String, Set<String>>(); - Set<String> packageNames = new TreeSet<String>(); - Set<String> tableNames = new HashSet<String>(); + Map<String, Set<String>> packages = new HashMap<>(); + Set<String> packageNames = new TreeSet<>(); + Set<String> tableNames = new HashSet<>(); //put the entityNames TreeSets in a HashMap by packageName Collection<String> ec = reader.getEntityNames(); - Set<String> entityNames = new HashSet<String>(ec); + Set<String> entityNames = new HashSet<>(ec); for (String eName: ec) { ModelEntity ent = reader.getModelEntity(eName); @@ -68,15 +69,15 @@ public class ModelEntityChecker { Set<String> entities = packages.get(ent.getPackageName()); if (entities == null) { - entities = new TreeSet<String>(); + entities = new TreeSet<>(); packages.put(ent.getPackageName(), entities); packageNames.add(ent.getPackageName()); } entities.add(eName); } - Set<String> fkNames = new HashSet<String>(); - Set<String> indexNames = new HashSet<String>(); + Set<String> fkNames = new HashSet<>(); + Set<String> indexNames = new HashSet<>(); for (String pName: packageNames) { Set<String> entities = packages.get(pName); @@ -95,16 +96,16 @@ public class ModelEntityChecker { if (entity.getPlainTableName() != null && entity.getPlainTableName().length() > 30) { warningList.add("[TableNameGT30] Table name [" + entity.getPlainTableName() + "] of entity " + entity.getEntityName() + " is longer than 30 characters."); } - if (entity.getPlainTableName() != null && reservedWords.contains(entity.getPlainTableName().toUpperCase())) { + if (entity.getPlainTableName() != null && reservedWords.contains(entity.getPlainTableName().toUpperCase(Locale.getDefault()))) { warningList.add("[TableNameRW] Table name [" + entity.getPlainTableName() + "] of entity " + entity.getEntityName() + " is a reserved word."); } - + // don't check columns/relations/keys when never-check is set to "true" if (entity.getNeverCheck()) { continue; } - - Set<String> ufields = new HashSet<String>(); + + Set<String> ufields = new HashSet<>(); Iterator<ModelField> fieldIter = entity.getFieldsIterator(); while (fieldIter.hasNext()) { ModelField field = fieldIter.next(); @@ -121,7 +122,7 @@ public class ModelEntityChecker { if (field.getColName().length() == 0) { warningList.add("[FieldNameEQ0] Column name for field name \"" + field.getName() + "\" of entity " + entity.getEntityName() + " is empty (zero length)."); } - if (reservedWords.contains(field.getColName().toUpperCase())) + if (reservedWords.contains(field.getColName().toUpperCase(Locale.getDefault()))) warningList.add("[FieldNameRW] Column name " + field.getColName() + " of entity " + entity.getEntityName() + " is a reserved word."); if (type == null) { StringBuilder warningMsg = new StringBuilder(); @@ -167,7 +168,7 @@ public class ModelEntityChecker { } } - Set<String> relations = new HashSet<String>(); + Set<String> relations = new HashSet<>(); for (int r = 0; r < entity.getRelationsSize(); r++) { ModelRelation relation = entity.getRelation(r); @@ -285,7 +286,7 @@ public class ModelEntityChecker { } } - public static final String[] rwArray = { "ABORT", "ABS", "ABSOLUTE", + protected static final String[] rwArray = { "ABORT", "ABS", "ABSOLUTE", "ACCEPT", "ACCES", "ACCESS", "ACS", "ACTION", "ACTIVATE", "ADD", "ADDFORM", "ADMIN", "AFTER", "AGGREGATE", "ALIAS", "ALL", "ALLOCATE", "ALTER", "ANALYZE", "AND", "ANDFILENAME", "ANY", "ANYFINISH", "APPEND", Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelField.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelField.java?rev=1817559&r1=1817558&r2=1817559&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelField.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelField.java Fri Dec 8 20:18:41 2017 @@ -22,9 +22,9 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.Locale; import org.apache.ofbiz.base.lang.ThreadSafe; -import org.apache.ofbiz.base.util.Debug; import org.apache.ofbiz.base.util.UtilXml; import org.apache.ofbiz.entity.jdbc.DatabaseUtil; import org.w3c.dom.Document; @@ -147,11 +147,7 @@ public final class ModelField extends Mo if (isPk) { isNotNull = true; } - EncryptMethod encrypt = EncryptMethod.valueOf(fieldElement.getAttribute("encrypt").toUpperCase()); - if (encrypt == null) { - Debug.logWarning("invalid encrypt value: %s", module, fieldElement.getAttribute("encrypt")); - encrypt = EncryptMethod.FALSE; - } + EncryptMethod encrypt = EncryptMethod.valueOf(fieldElement.getAttribute("encrypt").toUpperCase(Locale.getDefault())); boolean enableAuditLog = "true".equals(fieldElement.getAttribute("enable-audit-log")); List<String>validators = Collections.emptyList(); List<? extends Element> elementList = UtilXml.childElementList(fieldElement, "validate"); @@ -177,7 +173,7 @@ public final class ModelField extends Mo String name = ModelUtil.dbNameToVarName(colName); String type = ModelUtil.induceFieldType(ccInfo.typeName, ccInfo.columnSize, ccInfo.decimalDigits, modelFieldTypeReader); boolean isPk = ccInfo.isPk; - boolean isNotNull = "NO".equals(ccInfo.isNullable.toUpperCase()); + boolean isNotNull = "NO".equals(ccInfo.isNullable.toUpperCase(Locale.getDefault())); String description = ""; String colValue = ""; String fieldSet = ""; @@ -303,7 +299,7 @@ public final class ModelField extends Mo } root.setAttribute("type", this.getType()); if (this.getEncryptMethod().isEncrypted()) { - root.setAttribute("encrypt", this.getEncryptMethod().toString().toLowerCase()); + root.setAttribute("encrypt", this.getEncryptMethod().toString().toLowerCase(Locale.getDefault())); } if (this.getIsNotNull()) { root.setAttribute("not-null", "true"); Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelFieldType.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelFieldType.java?rev=1817559&r1=1817558&r2=1817559&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelFieldType.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelFieldType.java Fri Dec 8 20:18:41 2017 @@ -19,6 +19,7 @@ package org.apache.ofbiz.entity.model; import java.io.Serializable; +import java.util.Locale; import org.apache.ofbiz.base.util.UtilXml; import org.apache.ofbiz.entity.jdbc.JdbcValueHandler; @@ -90,18 +91,18 @@ public class ModelFieldType implements S * @return max length of a String representing the Field value */ public int stringLength() { - String sqlTypeUpperCase = sqlType.toUpperCase(); + String sqlTypeUpperCase = sqlType.toUpperCase(Locale.getDefault()); if (sqlTypeUpperCase.indexOf("VARCHAR") >= 0) { - if (sqlTypeUpperCase.indexOf("(") > 0 && sqlTypeUpperCase.indexOf(")") > 0) { - String length = sqlTypeUpperCase.substring(sqlTypeUpperCase.indexOf("(") + 1, sqlTypeUpperCase.indexOf(")")); + if (sqlTypeUpperCase.indexOf('(') > 0 && sqlTypeUpperCase.indexOf(')') > 0) { + String length = sqlTypeUpperCase.substring(sqlTypeUpperCase.indexOf('(') + 1, sqlTypeUpperCase.indexOf(')')); return Integer.parseInt(length); } else { return 255; } } else if (sqlTypeUpperCase.indexOf("CHAR") >= 0) { - if (sqlTypeUpperCase.indexOf("(") > 0 && sqlTypeUpperCase.indexOf(")") > 0) { - String length = sqlTypeUpperCase.substring(sqlTypeUpperCase.indexOf("(") + 1, sqlTypeUpperCase.indexOf(")")); + if (sqlTypeUpperCase.indexOf('(') > 0 && sqlTypeUpperCase.indexOf(')') > 0) { + String length = sqlTypeUpperCase.substring(sqlTypeUpperCase.indexOf('(') + 1, sqlTypeUpperCase.indexOf(')')); return Integer.parseInt(length); } else { Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelGroupReader.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelGroupReader.java?rev=1817559&r1=1817558&r2=1817559&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelGroupReader.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelGroupReader.java Fri Dec 8 20:18:41 2017 @@ -55,11 +55,11 @@ public class ModelGroupReader implements public static final String module = ModelGroupReader.class.getName(); private static final UtilCache<String, ModelGroupReader> readers = UtilCache.createUtilCache("entity.ModelGroupReader", 0, 0); - private Map<String, String> groupCache = null; + private volatile Map<String, String> groupCache = null; private Set<String> groupNames = null; public String modelName; - public List<ResourceHandler> entityGroupResourceHandlers = new LinkedList<ResourceHandler>(); + public List<ResourceHandler> entityGroupResourceHandlers = new LinkedList<>(); public static ModelGroupReader getModelGroupReader(String delegatorName) throws GenericEntityConfException { DelegatorElement delegatorInfo = EntityConfig.getInstance().getDelegator(delegatorName); @@ -105,8 +105,8 @@ public class ModelGroupReader implements // must check if null again as one of the blocked threads can still enter if (this.groupCache == null) { // now it's safe - this.groupCache = new HashMap<String, String>(); - this.groupNames = new TreeSet<String>(); + this.groupCache = new HashMap<>(); + this.groupNames = new TreeSet<>(); UtilTimer utilTimer = new UtilTimer(); // utilTimer.timerString("[ModelGroupReader.getGroupCache] Before getDocument"); @@ -141,7 +141,6 @@ public class ModelGroupReader implements String entityName = UtilXml.checkEmpty(curEntity.getAttribute("entity")).intern(); String groupName = UtilXml.checkEmpty(curEntity.getAttribute("group")).intern(); - if (groupName == null || entityName == null) continue; try { if (null == EntityConfig.getInstance().getDelegator(delegatorName).getGroupDataSource(groupName)) { Debug.logError("The declared group name " + groupName + " has no corresponding group-map in entityengine.xml: ", module); @@ -202,7 +201,7 @@ public class ModelGroupReader implements } getGroupCache(delegatorBaseName); if (this.groupNames == null) return null; - Set<String> newSet = new HashSet<String>(); + Set<String> newSet = new HashSet<>(); try { newSet.add(EntityConfig.getInstance().getDelegator(delegatorBaseName).getDefaultGroupName()); } catch (GenericEntityConfException e) { @@ -218,7 +217,7 @@ public class ModelGroupReader implements */ public Set<String> getEntityNamesByGroup(String delegatorBaseName, String groupName) { Map<String, String> gc = getGroupCache(delegatorBaseName); - Set<String> enames = new HashSet<String>(); + Set<String> enames = new HashSet<>(); if (UtilValidate.isEmpty(groupName)) return enames; if (UtilValidate.isEmpty(gc)) return enames; Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelIndex.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelIndex.java?rev=1817559&r1=1817558&r2=1817559&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelIndex.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelIndex.java Fri Dec 8 20:18:41 2017 @@ -21,6 +21,7 @@ package org.apache.ofbiz.entity.model; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Locale; import org.apache.ofbiz.base.lang.ThreadSafe; import org.apache.ofbiz.base.util.UtilValidate; @@ -73,11 +74,12 @@ public final class ModelIndex extends Mo List<Field>fields = Collections.emptyList(); List<? extends Element> elementList = UtilXml.childElementList(indexElement, "index-field"); if (!elementList.isEmpty()) { - fields = new ArrayList<Field>(elementList.size()); + fields = new ArrayList<>(elementList.size()); for (Element indexFieldElement : elementList) { String fieldName = indexFieldElement.getAttribute("name").intern(); String function = indexFieldElement.getAttribute("function").intern(); - fields.add(new Field(fieldName, UtilValidate.isNotEmpty(function) ? Function.valueOf(function.toUpperCase()) : null)); + fields.add(new Field(fieldName, UtilValidate.isNotEmpty(function) ? Function.valueOf(function + .toUpperCase(Locale.getDefault())) : null)); } fields = Collections.unmodifiableList(fields); } Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelReader.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelReader.java?rev=1817559&r1=1817558&r2=1817559&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelReader.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelReader.java Fri Dec 8 20:18:41 2017 @@ -60,7 +60,7 @@ public class ModelReader implements Seri public static final String module = ModelReader.class.getName(); private static final UtilCache<String, ModelReader> readers = UtilCache.createUtilCache("entity.ModelReader", 0, 0); - protected Map<String, ModelEntity> entityCache = null; + protected volatile Map<String, ModelEntity> entityCache = null; protected int numEntities = 0; protected int numViewEntities = 0; @@ -73,10 +73,16 @@ public class ModelReader implements Seri /** collection of filenames for entity definitions */ protected Collection<ResourceHandler> entityResourceHandlers; - /** contains a collection of entity names for each ResourceHandler, populated as they are loaded */ + /** + * contains a collection of entity names for each ResourceHandler, populated as + * they are loaded + */ protected Map<ResourceHandler, Collection<String>> resourceHandlerEntities; - /** for each entity contains a map to the ResourceHandler that the entity came from */ + /** + * for each entity contains a map to the ResourceHandler that the entity came + * from + */ protected Map<String, ResourceHandler> entityResourceHandlerMap; public static ModelReader getModelReader(String delegatorName) throws GenericEntityException { @@ -100,9 +106,9 @@ public class ModelReader implements Seri private ModelReader(String modelName) throws GenericEntityException { this.modelName = modelName; - entityResourceHandlers = new LinkedList<ResourceHandler>(); - resourceHandlerEntities = new HashMap<ResourceHandler, Collection<String>>(); - entityResourceHandlerMap = new HashMap<String, ResourceHandler>(); + entityResourceHandlers = new LinkedList<>(); + resourceHandlerEntities = new HashMap<>(); + entityResourceHandlerMap = new HashMap<>(); EntityModelReader entityModelReaderInfo = EntityConfig.getInstance().getEntityModelReader(modelName); @@ -110,21 +116,26 @@ public class ModelReader implements Seri throw new GenericEntityConfException("Cound not find an entity-model-reader with the name " + modelName); } - // get all of the main resource model stuff, ie specified in the entityengine.xml file + // get all of the main resource model stuff, ie specified in the + // entityengine.xml file for (Resource resourceElement : entityModelReaderInfo.getResourceList()) { - ResourceHandler handler = new MainResourceHandler(EntityConfig.ENTITY_ENGINE_XML_FILENAME, resourceElement.getLoader(), resourceElement.getLocation()); + ResourceHandler handler = new MainResourceHandler(EntityConfig.ENTITY_ENGINE_XML_FILENAME, resourceElement + .getLoader(), resourceElement.getLocation()); entityResourceHandlers.add(handler); } - // get all of the component resource model stuff, ie specified in each ofbiz-component.xml file - for (ComponentConfig.EntityResourceInfo componentResourceInfo: ComponentConfig.getAllEntityResourceInfos("model")) { + // get all of the component resource model stuff, ie specified in each + // ofbiz-component.xml file + for (ComponentConfig.EntityResourceInfo componentResourceInfo : ComponentConfig.getAllEntityResourceInfos( + "model")) { if (modelName.equals(componentResourceInfo.readerName)) { entityResourceHandlers.add(componentResourceInfo.createResourceHandler()); } } } - private ModelEntity buildEntity(ResourceHandler entityResourceHandler, Element curEntityElement, int i, ModelInfo def) throws GenericEntityException { + private ModelEntity buildEntity(ResourceHandler entityResourceHandler, Element curEntityElement, int i, + ModelInfo def) throws GenericEntityException { boolean isEntity = "entity".equals(curEntityElement.getNodeName()); String entityName = UtilXml.checkEmpty(curEntityElement.getAttribute("entity-name")).intern(); boolean redefinedEntity = "true".equals(curEntityElement.getAttribute("redefinition")); @@ -133,7 +144,7 @@ public class ModelReader implements Seri Collection<String> resourceHandlerEntityNames = resourceHandlerEntities.get(entityResourceHandler); if (resourceHandlerEntityNames == null) { - resourceHandlerEntityNames = new LinkedList<String>(); + resourceHandlerEntityNames = new LinkedList<>(); resourceHandlerEntities.put(entityResourceHandler, resourceHandlerEntityNames); } resourceHandlerEntityNames.add(entityName); @@ -141,17 +152,17 @@ public class ModelReader implements Seri // check to see if entity with same name has already been read if (entityCache.containsKey(entityName) && !redefinedEntity) { Debug.logWarning("Entity " + entityName + - " is defined more than once, most recent will over-write " + - "previous definition(s)", module); + " is defined more than once, most recent will over-write " + + "previous definition(s)", module); Debug.logWarning("Entity " + entityName + " was found in " + - entityResourceHandler + ", but was already defined in " + - entityResourceHandlerMap.get(entityName).toString(), module); + entityResourceHandler + ", but was already defined in " + + entityResourceHandlerMap.get(entityName).toString(), module); } // add entityName, entityFileName pair to entityResourceHandlerMap map entityResourceHandlerMap.put(entityName, entityResourceHandler); - // utilTimer.timerString(" After entityEntityName -- " + i + " --"); + // utilTimer.timerString(" After entityEntityName -- " + i + " --"); // ModelEntity entity = createModelEntity(curEntity, utilTimer); ModelEntity modelEntity = null; @@ -168,18 +179,20 @@ public class ModelReader implements Seri Debug.logError(e, "Could not get resource URL", module); } - // utilTimer.timerString(" After createModelEntity -- " + i + " --"); + // utilTimer.timerString(" After createModelEntity -- " + i + " --"); if (modelEntity != null) { modelEntity.setLocation(resourceLocation); - // utilTimer.timerString(" After entityCache.put -- " + i + " --"); + // utilTimer.timerString(" After entityCache.put -- " + i + " --"); if (isEntity) { - if (Debug.verboseOn()) Debug.logVerbose("-- [Entity]: #" + i + ": " + entityName, module); + if (Debug.verboseOn()) + Debug.logVerbose("-- [Entity]: #" + i + ": " + entityName, module); } else { - if (Debug.verboseOn()) Debug.logVerbose("-- [ViewEntity]: #" + i + ": " + entityName, module); + if (Debug.verboseOn()) + Debug.logVerbose("-- [ViewEntity]: #" + i + ": " + entityName, module); } } else { Debug.logWarning("-- -- ENTITYGEN ERROR:getModelEntity: Could not create " + - "entity for entityName: " + entityName, module); + "entity for entityName: " + entityName, module); } return modelEntity; } @@ -195,13 +208,13 @@ public class ModelReader implements Seri numRelations = 0; numAutoRelations = 0; - entityCache = new HashMap<String, ModelEntity>(); - List<ModelViewEntity> tempViewEntityList = new LinkedList<ModelViewEntity>(); - List<Element> tempExtendEntityElementList = new LinkedList<Element>(); + entityCache = new HashMap<>(); + List<ModelViewEntity> tempViewEntityList = new LinkedList<>(); + List<Element> tempExtendEntityElementList = new LinkedList<>(); UtilTimer utilTimer = new UtilTimer(); - for (ResourceHandler entityResourceHandler: entityResourceHandlers) { + for (ResourceHandler entityResourceHandler : entityResourceHandlers) { // utilTimer.timerString("Before getDocument in file " + entityFileName); Document document = null; @@ -212,10 +225,12 @@ public class ModelReader implements Seri throw new GenericEntityConfException("Error getting document from resource handler", e); } if (document == null) { - throw new GenericEntityConfException("Could not get document for " + entityResourceHandler.toString()); + throw new GenericEntityConfException("Could not get document for " + entityResourceHandler + .toString()); } - // utilTimer.timerString("Before getDocumentElement in " + entityResourceHandler.toString()); + // utilTimer.timerString("Before getDocumentElement in " + + // entityResourceHandler.toString()); Element docElement = document.getDocumentElement(); if (docElement == null) { @@ -236,8 +251,10 @@ public class ModelReader implements Seri if ((isEntity || isViewEntity) && curChild.getNodeType() == Node.ELEMENT_NODE) { i++; - ModelEntity modelEntity = buildEntity(entityResourceHandler, (Element) curChild, i, def); - // put the view entity in a list to get ready for the second pass to populate fields... + ModelEntity modelEntity = buildEntity(entityResourceHandler, (Element) curChild, i, + def); + // put the view entity in a list to get ready for the second pass to populate + // fields... if (isViewEntity) { tempViewEntityList.add((ModelViewEntity) modelEntity); } else { @@ -250,14 +267,17 @@ public class ModelReader implements Seri } else { Debug.logWarning("No child nodes found.", module); } - utilTimer.timerString("Finished " + entityResourceHandler.toString() + " - Total Entities: " + i + " FINISHED"); + utilTimer.timerString("Finished " + entityResourceHandler.toString() + " - Total Entities: " + i + + " FINISHED"); } - // all entity elements in, now go through extend-entity elements and add their stuff - for (Element extendEntityElement: tempExtendEntityElementList) { + // all entity elements in, now go through extend-entity elements and add their + // stuff + for (Element extendEntityElement : tempExtendEntityElementList) { String entityName = UtilXml.checkEmpty(extendEntityElement.getAttribute("entity-name")); ModelEntity modelEntity = entityCache.get(entityName); - if (modelEntity == null) throw new GenericEntityConfException("Entity to extend does not exist: " + entityName); + if (modelEntity == null) + throw new GenericEntityConfException("Entity to extend does not exist: " + entityName); modelEntity.addExtendEntity(this, extendEntityElement); } @@ -266,17 +286,16 @@ public class ModelReader implements Seri while (!tempViewEntityList.isEmpty()) { int startSize = tempViewEntityList.size(); Iterator<ModelViewEntity> mveIt = tempViewEntityList.iterator(); -TEMP_VIEW_LOOP: - while (mveIt.hasNext()) { + TEMP_VIEW_LOOP: while (mveIt.hasNext()) { ModelViewEntity curViewEntity = mveIt.next(); - for (ModelViewEntity.ModelMemberEntity mve: curViewEntity.getAllModelMemberEntities()) { + for (ModelViewEntity.ModelMemberEntity mve : curViewEntity.getAllModelMemberEntities()) { if (!entityCache.containsKey(mve.getEntityName())) { continue TEMP_VIEW_LOOP; } } mveIt.remove(); curViewEntity.populateFields(this); - for (ModelViewEntity.ModelMemberEntity mve: curViewEntity.getAllModelMemberEntities()) { + for (ModelViewEntity.ModelMemberEntity mve : curViewEntity.getAllModelMemberEntities()) { ModelEntity me = entityCache.get(mve.getEntityName()); me.addViewEntity(curViewEntity); } @@ -291,13 +310,14 @@ TEMP_VIEW_LOOP: } if (!tempViewEntityList.isEmpty()) { StringBuilder sb = new StringBuilder("View entities reference non-existant members:\n"); - Set<String> allViews = new HashSet<String>(); - for (ModelViewEntity curViewEntity: tempViewEntityList) { + Set<String> allViews = new HashSet<>(); + for (ModelViewEntity curViewEntity : tempViewEntityList) { allViews.add(curViewEntity.getEntityName()); } - for (ModelViewEntity curViewEntity: tempViewEntityList) { - Set<String> perViewMissingEntities = new HashSet<String>(); - Iterator<ModelViewEntity.ModelMemberEntity> mmeIt = curViewEntity.getAllModelMemberEntities().iterator(); + for (ModelViewEntity curViewEntity : tempViewEntityList) { + Set<String> perViewMissingEntities = new HashSet<>(); + Iterator<ModelViewEntity.ModelMemberEntity> mmeIt = curViewEntity + .getAllModelMemberEntities().iterator(); while (mmeIt.hasNext()) { ModelViewEntity.ModelMemberEntity mme = mmeIt.next(); String memberEntityName = mme.getEntityName(); @@ -310,8 +330,9 @@ TEMP_VIEW_LOOP: } } } - for (String perViewMissingEntity: perViewMissingEntities) { - sb.append("\t[").append(curViewEntity.getEntityName()).append("] missing member entity [").append(perViewMissingEntity).append("]\n"); + for (String perViewMissingEntity : perViewMissingEntities) { + sb.append("\t[").append(curViewEntity.getEntityName()).append( + "] missing member entity [").append(perViewMissingEntity).append("]\n"); } } @@ -319,101 +340,116 @@ TEMP_VIEW_LOOP: } // auto-create relationships - Set<String> orderedMessages = new TreeSet<String>(); - for (String curEntityName: new TreeSet<String>(this.getEntityNames())) { + Set<String> orderedMessages = new TreeSet<>(); + for (String curEntityName : new TreeSet<>(this.getEntityNames())) { ModelEntity curModelEntity = this.getModelEntity(curEntityName); if (curModelEntity instanceof ModelViewEntity) { - // for view-entities auto-create relationships for all member-entity relationships that have all corresponding fields in the view-entity + // for view-entities auto-create relationships for all member-entity + // relationships that have all corresponding fields in the view-entity } else { // for entities auto-create many relationships for all type one relationships - // just in case we add a new relation to the same entity, keep in a separate list and add them at the end - List<ModelRelation> newSameEntityRelations = new LinkedList<ModelRelation>(); + // just in case we add a new relation to the same entity, keep in a separate + // list and add them at the end + List<ModelRelation> newSameEntityRelations = new LinkedList<>(); Iterator<ModelRelation> relationsIter = curModelEntity.getRelationsIterator(); while (relationsIter.hasNext()) { ModelRelation modelRelation = relationsIter.next(); - if (("one".equals(modelRelation.getType()) || "one-nofk".equals(modelRelation.getType())) && !modelRelation.isAutoRelation()) { + if (("one".equals(modelRelation.getType()) || "one-nofk".equals(modelRelation + .getType())) && !modelRelation.isAutoRelation()) { ModelEntity relatedEnt = null; try { relatedEnt = this.getModelEntity(modelRelation.getRelEntityName()); } catch (GenericModelException e) { - throw new GenericModelException("Error getting related entity [" + modelRelation.getRelEntityName() + "] definition from entity [" + curEntityName + "]", e); + throw new GenericModelException("Error getting related entity [" + modelRelation + .getRelEntityName() + "] definition from entity [" + curEntityName + + "]", e); } - if (relatedEnt != null) { - // create the new relationship even if one exists so we can show what we are looking for in the info message - // don't do relationship to the same entity, unless title is "Parent", then do a "Child" automatically - String title = modelRelation.getTitle(); - if (curModelEntity.getEntityName().equals(relatedEnt.getEntityName()) && "Parent".equals(title)) { - title = "Child"; - } - String description = ""; - String type = ""; - String relEntityName = curModelEntity.getEntityName(); - String fkName = ""; - ArrayList<ModelKeyMap> keyMaps = new ArrayList<ModelKeyMap>(); - boolean isAutoRelation = true; - Set<String> curEntityKeyFields = new HashSet<String>(); - for (ModelKeyMap curkm : modelRelation.getKeyMaps()) { - keyMaps.add(new ModelKeyMap(curkm.getRelFieldName(), curkm.getFieldName())); - curEntityKeyFields.add(curkm.getFieldName()); - } - keyMaps.trimToSize(); - // decide whether it should be one or many by seeing if the key map represents the complete pk of the relEntity - if (curModelEntity.containsAllPkFieldNames(curEntityKeyFields)) { - // always use one-nofk, we don't want auto-fks getting in for these automatic ones - type = "one-nofk"; - // to keep it clean, remove any additional keys that aren't part of the PK - List<String> curPkFieldNames = curModelEntity.getPkFieldNames(); - Iterator<ModelKeyMap> nrkmIter = keyMaps.iterator(); - while (nrkmIter.hasNext()) { - ModelKeyMap nrkm =nrkmIter.next(); - String checkField = nrkm.getRelFieldName(); - if (!curPkFieldNames.contains(checkField)) { - nrkmIter.remove(); - } + // create the new relationship even if one exists so we can show what we are + // looking for in the info message + // don't do relationship to the same entity, unless title is "Parent", then do a + // "Child" automatically + String title = modelRelation.getTitle(); + if (curModelEntity.getEntityName().equals(relatedEnt.getEntityName()) && "Parent" + .equals(title)) { + title = "Child"; + } + String description = ""; + String type = ""; + String relEntityName = curModelEntity.getEntityName(); + String fkName = ""; + ArrayList<ModelKeyMap> keyMaps = new ArrayList<>(); + boolean isAutoRelation = true; + Set<String> curEntityKeyFields = new HashSet<>(); + for (ModelKeyMap curkm : modelRelation.getKeyMaps()) { + keyMaps.add(new ModelKeyMap(curkm.getRelFieldName(), curkm.getFieldName())); + curEntityKeyFields.add(curkm.getFieldName()); + } + keyMaps.trimToSize(); + // decide whether it should be one or many by seeing if the key map represents + // the complete pk of the relEntity + if (curModelEntity.containsAllPkFieldNames(curEntityKeyFields)) { + // always use one-nofk, we don't want auto-fks getting in for these automatic + // ones + type = "one-nofk"; + // to keep it clean, remove any additional keys that aren't part of the PK + List<String> curPkFieldNames = curModelEntity.getPkFieldNames(); + Iterator<ModelKeyMap> nrkmIter = keyMaps.iterator(); + while (nrkmIter.hasNext()) { + ModelKeyMap nrkm = nrkmIter.next(); + String checkField = nrkm.getRelFieldName(); + if (!curPkFieldNames.contains(checkField)) { + nrkmIter.remove(); } - } else { - type= "many"; } - ModelRelation newRel = ModelRelation.create(relatedEnt, description, type, title, relEntityName, fkName, keyMaps, isAutoRelation); + } else { + type = "many"; + } + ModelRelation newRel = ModelRelation.create(relatedEnt, description, type, title, + relEntityName, fkName, keyMaps, isAutoRelation); - ModelRelation existingRelation = relatedEnt.getRelation(title + curModelEntity.getEntityName()); - if (existingRelation == null) { - numAutoRelations++; - if (curModelEntity.getEntityName().equals(relatedEnt.getEntityName())) { - newSameEntityRelations.add(newRel); - } else { - relatedEnt.addRelation(newRel); - } + ModelRelation existingRelation = relatedEnt.getRelation(title + curModelEntity + .getEntityName()); + if (existingRelation == null) { + numAutoRelations++; + if (curModelEntity.getEntityName().equals(relatedEnt.getEntityName())) { + newSameEntityRelations.add(newRel); } else { - if (newRel.equals(existingRelation)) { - // don't warn if the target title+entity = current title+entity - if (Debug.infoOn() && !(title + curModelEntity.getEntityName()).equals(modelRelation.getTitle() + modelRelation.getRelEntityName())) { - //String errorMsg = "Relation already exists to entity [] with title [" + targetTitle + "],from entity []"; - String message = "Entity [" + relatedEnt.getPackageName() + ":" + relatedEnt.getEntityName() + "] already has identical relationship to entity [" + - curModelEntity.getEntityName() + "] title [" + title + "]; would auto-create: type [" + - newRel.getType() + "] and fields [" + newRel.keyMapString(",", "") + "]"; - orderedMessages.add(message); - } - } else { - String message = "Existing relationship with the same name, but different specs found from what would be auto-created for Entity [" + relatedEnt.getEntityName() + "] and relationship to entity [" + - curModelEntity.getEntityName() + "] title [" + title + "]; would auto-create: type [" + - newRel.getType() + "] and fields [" + newRel.keyMapString(",", "") + "]"; - Debug.logVerbose(message, module); - } + relatedEnt.addRelation(newRel); } } else { - String errorMsg = "Could not find related entity [" - + modelRelation.getRelEntityName() + "], no reverse relation added."; - Debug.logWarning(errorMsg, module); + if (newRel.equals(existingRelation)) { + // don't warn if the target title+entity = current title+entity + if (Debug.infoOn() && !(title + curModelEntity.getEntityName()).equals( + modelRelation.getTitle() + modelRelation.getRelEntityName())) { + // String errorMsg = "Relation already exists to entity [] with title [" + // + targetTitle + "],from entity []"; + String message = "Entity [" + relatedEnt.getPackageName() + ":" + + relatedEnt.getEntityName() + + "] already has identical relationship to entity [" + + curModelEntity.getEntityName() + "] title [" + title + + "]; would auto-create: type [" + + newRel.getType() + "] and fields [" + newRel.keyMapString(",", + "") + "]"; + orderedMessages.add(message); + } + } else { + String message = "Existing relationship with the same name, but different specs found from what would be auto-created for Entity [" + + relatedEnt.getEntityName() + "] and relationship to entity [" + + curModelEntity.getEntityName() + "] title [" + title + + "]; would auto-create: type [" + + newRel.getType() + "] and fields [" + newRel.keyMapString(",", "") + + "]"; + Debug.logVerbose(message, module); + } } } } if (newSameEntityRelations.size() > 0) { - for (ModelRelation newRel: newSameEntityRelations) { + for (ModelRelation newRel : newSameEntityRelations) { curModelEntity.addRelation(newRel); } } @@ -423,7 +459,9 @@ TEMP_VIEW_LOOP: for (String message : orderedMessages) { Debug.logInfo(message, module); } - Debug.logInfo("Finished loading entities; #Entities=" + numEntities + " #ViewEntities=" + numViewEntities + " #Fields=" + numFields + " #Relationships=" + numRelations + " #AutoRelationships=" + numAutoRelations, module); + Debug.logInfo("Finished loading entities; #Entities=" + numEntities + " #ViewEntities=" + + numViewEntities + " #Fields=" + numFields + " #Relationships=" + numRelations + + " #AutoRelationships=" + numAutoRelations, module); } } } @@ -431,13 +469,16 @@ TEMP_VIEW_LOOP: return entityCache; } - /** rebuilds the resourceHandlerEntities Map of Collections based on the current - * entityResourceHandlerMap Map, must be done whenever a manual change is made to the - * entityResourceHandlerMap Map after the initial load to make them consistent again. + /** + * rebuilds the resourceHandlerEntities Map of Collections based on the current + * entityResourceHandlerMap Map, must be done whenever a manual change is made + * to the entityResourceHandlerMap Map after the initial load to make them + * consistent again. */ public void rebuildResourceHandlerEntities() { - resourceHandlerEntities = new HashMap<ResourceHandler, Collection<String>>(); - Iterator<Map.Entry<String, ResourceHandler>> entityResourceIter = entityResourceHandlerMap.entrySet().iterator(); + resourceHandlerEntities = new HashMap<>(); + Iterator<Map.Entry<String, ResourceHandler>> entityResourceIter = entityResourceHandlerMap.entrySet() + .iterator(); while (entityResourceIter.hasNext()) { Map.Entry<String, ResourceHandler> entry = entityResourceIter.next(); @@ -445,7 +486,7 @@ TEMP_VIEW_LOOP: Collection<String> resourceHandlerEntityNames = resourceHandlerEntities.get(entry.getValue()); if (resourceHandlerEntityNames == null) { - resourceHandlerEntityNames = new LinkedList<String>(); + resourceHandlerEntityNames = new LinkedList<>(); resourceHandlerEntities.put(entry.getValue(), resourceHandlerEntityNames); } resourceHandlerEntityNames.add(entry.getKey()); @@ -453,26 +494,34 @@ TEMP_VIEW_LOOP: } public Iterator<ResourceHandler> getResourceHandlerEntitiesKeyIterator() { - if (resourceHandlerEntities == null) return null; + if (resourceHandlerEntities == null) + return null; return resourceHandlerEntities.keySet().iterator(); } public Collection<String> getResourceHandlerEntities(ResourceHandler resourceHandler) { - if (resourceHandlerEntities == null) return null; + if (resourceHandlerEntities == null) + return null; return resourceHandlerEntities.get(resourceHandler); } public void addEntityToResourceHandler(String entityName, String loaderName, String location) { - entityResourceHandlerMap.put(entityName, new MainResourceHandler(EntityConfig.ENTITY_ENGINE_XML_FILENAME, loaderName, location)); + entityResourceHandlerMap.put(entityName, new MainResourceHandler(EntityConfig.ENTITY_ENGINE_XML_FILENAME, + loaderName, location)); } public ResourceHandler getEntityResourceHandler(String entityName) { return entityResourceHandlerMap.get(entityName); } - /** Gets an Entity object based on a definition from the specified XML Entity descriptor file. - * @param entityName The entityName of the Entity definition to use. - * @return An Entity object describing the specified entity of the specified descriptor file. + /** + * Gets an Entity object based on a definition from the specified XML Entity + * descriptor file. + * + * @param entityName + * The entityName of the Entity definition to use. + * @return An Entity object describing the specified entity of the specified + * descriptor file. */ public ModelEntity getModelEntity(String entityName) throws GenericEntityException { if (entityName == null) { @@ -505,7 +554,10 @@ TEMP_VIEW_LOOP: return modelEntity; } - /** Creates a Iterator with the entityName of each Entity defined in the specified XML Entity Descriptor file. + /** + * Creates a Iterator with the entityName of each Entity defined in the + * specified XML Entity Descriptor file. + * * @return A Iterator of entityName Strings */ public Iterator<String> getEntityNamesIterator() throws GenericEntityException { @@ -517,7 +569,10 @@ TEMP_VIEW_LOOP: } } - /** Creates a Set with the entityName of each Entity defined in the specified XML Entity Descriptor file. + /** + * Creates a Set with the entityName of each Entity defined in the specified XML + * Entity Descriptor file. + * * @return A Set of entityName Strings */ public Set<String> getEntityNames() throws GenericEntityException { @@ -529,10 +584,11 @@ TEMP_VIEW_LOOP: } /** Get all entities, organized by package */ - public Map<String, TreeSet<String>> getEntitiesByPackage(Set<String> packageFilterSet, Set<String> entityFilterSet) throws GenericEntityException { - Map<String, TreeSet<String>> entitiesByPackage = new HashMap<String, TreeSet<String>>(); + public Map<String, TreeSet<String>> getEntitiesByPackage(Set<String> packageFilterSet, Set<String> entityFilterSet) + throws GenericEntityException { + Map<String, TreeSet<String>> entitiesByPackage = new HashMap<>(); - //put the entityNames TreeSets in a HashMap by packageName + // put the entityNames TreeSets in a HashMap by packageName Iterator<String> ecIter = this.getEntityNames().iterator(); while (ecIter.hasNext()) { String entityName = ecIter.next(); @@ -542,24 +598,26 @@ TEMP_VIEW_LOOP: if (UtilValidate.isNotEmpty(packageFilterSet)) { // does it match any of these? boolean foundMatch = false; - for (String packageFilter: packageFilterSet) { + for (String packageFilter : packageFilterSet) { if (packageName.contains(packageFilter)) { foundMatch = true; } } if (!foundMatch) { - //Debug.logInfo("Not including entity " + entityName + " becuase it is not in the package set: " + packageFilterSet, module); + // Debug.logInfo("Not including entity " + entityName + " becuase it is not in + // the package set: " + packageFilterSet, module); continue; } } if (UtilValidate.isNotEmpty(entityFilterSet) && !entityFilterSet.contains(entityName)) { - //Debug.logInfo("Not including entity " + entityName + " because it is not in the entity set: " + entityFilterSet, module); + // Debug.logInfo("Not including entity " + entityName + " because it is not in + // the entity set: " + entityFilterSet, module); continue; } TreeSet<String> entities = entitiesByPackage.get(entity.getPackageName()); if (entities == null) { - entities = new TreeSet<String>(); + entities = new TreeSet<>(); entitiesByPackage.put(entity.getPackageName(), entities); } entities.add(entityName); @@ -568,9 +626,12 @@ TEMP_VIEW_LOOP: return entitiesByPackage; } - /** Util method to validate an entity name; if no entity is found with the name, - * characters are stripped from the beginning of the name until a valid entity name is found. - * It is intended to be used to determine the entity name from a relation name. + /** + * Util method to validate an entity name; if no entity is found with the name, + * characters are stripped from the beginning of the name until a valid entity + * name is found. It is intended to be used to determine the entity name from a + * relation name. + * * @return A valid entityName or null */ public String validateEntityName(String entityName) throws GenericEntityException { @@ -581,18 +642,20 @@ TEMP_VIEW_LOOP: while (!allEntities.contains(entityName) && entityName.length() > 0) { entityName = entityName.substring(1); } - return (entityName.length() > 0? entityName: null); + return (entityName.length() > 0 ? entityName : null); } ModelEntity createModelEntity(Element entityElement, UtilTimer utilTimer, ModelInfo def) { - if (entityElement == null) return null; + if (entityElement == null) + return null; this.numEntities++; ModelEntity entity = new ModelEntity(this, entityElement, utilTimer, def); return entity; } ModelEntity createModelViewEntity(Element entityElement, UtilTimer utilTimer, ModelInfo def) { - if (entityElement == null) return null; + if (entityElement == null) + return null; this.numViewEntities++; ModelViewEntity entity = new ModelViewEntity(this, entityElement, utilTimer, def); return entity; Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelUtil.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelUtil.java?rev=1817559&r1=1817558&r2=1817559&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelUtil.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/model/ModelUtil.java Fri Dec 8 20:18:41 2017 @@ -21,6 +21,7 @@ package org.apache.ofbiz.entity.model; import java.io.File; import java.util.Iterator; import java.util.List; +import java.util.Locale; import org.apache.ofbiz.base.util.StringUtil; import org.apache.ofbiz.base.util.UtilMisc; @@ -45,7 +46,7 @@ public final class ModelUtil { */ public static String upperFirstChar(String string) { if (string == null) return null; - if (string.length() <= 1) return string.toLowerCase(); + if (string.length() <= 1) return string.toLowerCase(Locale.getDefault()); StringBuilder sb = new StringBuilder(string); sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); @@ -60,7 +61,7 @@ public final class ModelUtil { */ public static String lowerFirstChar(String string) { if (string == null) return null; - if (string.length() <= 1) return string.toLowerCase(); + if (string.length() <= 1) return string.toLowerCase(Locale.getDefault()); StringBuilder sb = new StringBuilder(string); sb.setCharAt(0, Character.toLowerCase(sb.charAt(0))); |
Free forum by Nabble | Edit this page |