|
Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelRelation.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelRelation.java?rev=1477929&r1=1477928&r2=1477929&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelRelation.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelRelation.java Wed May 1 08:16:56 2013 @@ -19,166 +19,168 @@ package org.ofbiz.entity.model; import java.util.ArrayList; -import java.util.HashSet; +import java.util.Collections; import java.util.Iterator; import java.util.List; -import java.util.Set; -import org.ofbiz.base.util.StringUtil; +import org.ofbiz.base.lang.ThreadSafe; import org.ofbiz.base.util.UtilValidate; import org.ofbiz.base.util.UtilXml; import org.w3c.dom.Document; import org.w3c.dom.Element; -import org.w3c.dom.NodeList; /** - * Generic Entity - Relation model class + * An object that models the <code><relation></code> element. * */ +@ThreadSafe @SuppressWarnings("serial") -public class ModelRelation extends ModelChild { +public final class ModelRelation extends ModelChild { + + /** + * Returns a new <code>ModelRelation</code> instance, initialized with the specified values. + * + * @param modelEntity The <code>ModelEntity</code> this relation is a member of. + * @param description The relation description. + * @param type The relation type. + * @param title The relation title. + * @param relEntityName The related entity's name. + * @param fkName The foreign key name. + * @param keyMaps The key maps included in this relation. + * @param isAutoRelation <code>true</code> if this relation was generated automatically by the entity engine. + */ + @SuppressWarnings("unchecked") + public static ModelRelation create(ModelEntity modelEntity, String description, String type, String title, String relEntityName, String fkName, List<ModelKeyMap> keyMaps, boolean isAutoRelation) { + if (description == null) { + description = ""; + } + if (type == null) { + type = ""; + } + if (title == null) { + title = ""; + } + if (relEntityName == null) { + relEntityName = ""; + } + if (fkName == null) { + fkName = ""; + } + if (keyMaps == null) { + keyMaps = Collections.EMPTY_LIST; + } else { + keyMaps = Collections.unmodifiableList(keyMaps); + } + return new ModelRelation(modelEntity, description, type, title, relEntityName, fkName, keyMaps, isAutoRelation); + } + + /** + * Returns a new <code>ModelRelation</code> instance, initialized with the specified values. + * + * @param modelEntity The <code>ModelEntity</code> this relation is a member of. + * @param relationElement The <code><relation></code> element containing the values for this relation. + * @param isAutoRelation <code>true</code> if this relation was generated automatically by the entity engine. + */ + @SuppressWarnings("unchecked") + public static ModelRelation create(ModelEntity modelEntity, Element relationElement, boolean isAutoRelation) { + String type = relationElement.getAttribute("type").intern(); + String title = relationElement.getAttribute("title").intern(); + String relEntityName = relationElement.getAttribute("rel-entity-name").intern(); + String fkName = relationElement.getAttribute("fk-name").intern(); + String description = UtilXml.childElementValue(relationElement, "description"); + List<ModelKeyMap >keyMaps = Collections.EMPTY_LIST; + List<? extends Element> elementList = UtilXml.childElementList(relationElement, "key-map"); + if (!elementList.isEmpty()) { + keyMaps = new ArrayList<ModelKeyMap>(elementList.size()); + for (Element keyMapElement : elementList) { + keyMaps.add(new ModelKeyMap(keyMapElement)); + } + keyMaps = Collections.unmodifiableList(keyMaps); + } + return new ModelRelation(modelEntity, description, type, title, relEntityName, fkName, keyMaps, isAutoRelation); + } + + /* + * 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. + */ /** the title, gives a name/description to the relation */ - protected String title; + private final String title; /** the type: either "one" or "many" or "one-nofk" */ - protected String type; + private final String type; /** the name of the related entity */ - protected String relEntityName; + private final String relEntityName; /** the name to use for a database foreign key, if applies */ - protected String fkName; + private final String fkName; /** keyMaps defining how to lookup the relatedTable using columns from this table */ - protected List<ModelKeyMap> keyMaps = new ArrayList<ModelKeyMap>(); + private final List<ModelKeyMap> keyMaps; - /** the main entity of this relation */ - protected ModelEntity mainEntity = null; + private final boolean isAutoRelation; - protected boolean isAutoRelation = false; + /** A String to uniquely identify this relation. */ + private final String fullName; - /** Default Constructor */ - public ModelRelation() { - title = ""; - type = ""; - relEntityName = ""; - fkName = ""; - } + private final String combinedName; - /** Default Constructor */ - public ModelRelation(String type, String title, String relEntityName, String fkName, List<ModelKeyMap> keyMaps) { - if (title == null) title = ""; + private ModelRelation(ModelEntity modelEntity, String description, String type, String title, String relEntityName, String fkName, List<ModelKeyMap> keyMaps, boolean isAutoRelation) { + super(modelEntity, description); this.title = title; this.type = type; this.relEntityName = relEntityName; this.fkName = fkName; - this.keyMaps.addAll(keyMaps); - } - - /** XML Constructor */ - public ModelRelation(ModelEntity mainEntity, Element relationElement) { - this.mainEntity = mainEntity; - - this.type = UtilXml.checkEmpty(relationElement.getAttribute("type")).intern(); - this.title = UtilXml.checkEmpty(relationElement.getAttribute("title")).intern(); - this.relEntityName = UtilXml.checkEmpty(relationElement.getAttribute("rel-entity-name")).intern(); - this.fkName = UtilXml.checkEmpty(relationElement.getAttribute("fk-name")).intern(); - this.description = StringUtil.internString(UtilXml.childElementValue(relationElement, "description")); - - NodeList keyMapList = relationElement.getElementsByTagName("key-map"); - for (int i = 0; i < keyMapList.getLength(); i++) { - Element keyMapElement = (Element) keyMapList.item(i); - - if (keyMapElement.getParentNode() == relationElement) { - ModelKeyMap keyMap = new ModelKeyMap(keyMapElement); - - if (keyMap != null) { - this.keyMaps.add(keyMap); - } - } - } + this.keyMaps = keyMaps; + this.isAutoRelation = isAutoRelation; + StringBuilder sb = new StringBuilder(); + sb.append(modelEntity).append("->").append(title).append(relEntityName); + this.fullName = sb.toString(); + this.combinedName = title.concat(relEntityName); } + /** Returns the combined name (title + related entity name). */ public String getCombinedName() { - return this.title + this.relEntityName; + return this.combinedName; } - /** the title, gives a name/description to the relation */ + /** Returns the title. */ public String getTitle() { return this.title; } - public void setTitle(String title) { - if (title == null) { - this.title = ""; - } else { - this.title = title; - } - } - - /** the type: either "one" or "many" or "one-nofk" */ + /** Returns the type. */ public String getType() { return this.type; } - public void setType(String type) { - this.type = type; - } - - /** the name of the related entity */ + /** Returns the related entity name. */ public String getRelEntityName() { return this.relEntityName; } - public void setRelEntityName(String relEntityName) { - this.relEntityName = relEntityName; - } - + /** Returns the foreign key name. */ public String getFkName() { return this.fkName; } - public void setFkName(String fkName) { - this.fkName = fkName; - } - - /** @deprecated */ - @Deprecated - public void setMainEntity(ModelEntity mainEntity) { - setModelEntity(mainEntity); - } - - /** keyMaps defining how to lookup the relatedTable using columns from this table */ - public Iterator<ModelKeyMap> getKeyMapsIterator() { - return this.keyMaps.iterator(); - } - - public List<ModelKeyMap> getKeyMapsClone() { - List<ModelKeyMap> kmList = new ArrayList<ModelKeyMap>(this.keyMaps); - return kmList; - } - - public int getKeyMapsSize() { - return this.keyMaps.size(); - } - - public ModelKeyMap getKeyMap(int index) { - return this.keyMaps.get(index); + /** Returns the key maps. */ + public List<ModelKeyMap> getKeyMaps() { + return this.keyMaps; } - public void addKeyMap(ModelKeyMap keyMap) { - this.keyMaps.add(keyMap); - } - - public ModelKeyMap removeKeyMap(int index) { - return this.keyMaps.remove(index); + /** Returns <code>true</code> if this relation was generated automatically by the entity engine. */ + public boolean isAutoRelation() { + return isAutoRelation; } /** Find a KeyMap with the specified fieldName */ public ModelKeyMap findKeyMap(String fieldName) { for (ModelKeyMap keyMap: keyMaps) { - if (keyMap.fieldName.equals(fieldName)) return keyMap; + if (keyMap.getFieldName().equals(fieldName)) return keyMap; } return null; } @@ -186,12 +188,30 @@ public class ModelRelation extends Model /** Find a KeyMap with the specified relFieldName */ public ModelKeyMap findKeyMapByRelated(String relFieldName) { for (ModelKeyMap keyMap: keyMaps) { - if (keyMap.relFieldName.equals(relFieldName)) + if (keyMap.getRelFieldName().equals(relFieldName)) return keyMap; } return null; } + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj instanceof ModelRelation) { + ModelRelation that = (ModelRelation) obj; + return this.fullName.equals(that.fullName); + } + return false; + } + + @Override + public String toString() { + return this.fullName; + } + + // TODO: Externalize this. public String keyMapString(String separator, String afterLast) { StringBuilder stringBuilder = new StringBuilder(""); @@ -202,14 +222,15 @@ public class ModelRelation extends Model int i = 0; for (; i < keyMaps.size() - 1; i++) { - stringBuilder.append(keyMaps.get(i).fieldName); + stringBuilder.append(keyMaps.get(i).getFieldName()); stringBuilder.append(separator); } - stringBuilder.append(keyMaps.get(i).fieldName); + stringBuilder.append(keyMaps.get(i).getFieldName()); stringBuilder.append(afterLast); return stringBuilder.toString(); } + // TODO: Externalize this. public String keyMapUpperString(String separator, String afterLast) { if (keyMaps.size() < 1) return ""; @@ -218,7 +239,7 @@ public class ModelRelation extends Model int i=0; while (true) { ModelKeyMap kmap = keyMaps.get(i); - returnString.append(ModelUtil.upperFirstChar(kmap.fieldName)); + returnString.append(ModelUtil.upperFirstChar(kmap.getFieldName())); i++; if (i >= keyMaps.size()) { @@ -232,6 +253,7 @@ public class ModelRelation extends Model return returnString.toString(); } + // TODO: Externalize this. public String keyMapRelatedUpperString(String separator, String afterLast) { if (keyMaps.size() < 1) return ""; @@ -240,7 +262,7 @@ public class ModelRelation extends Model int i=0; while (true) { ModelKeyMap kmap = keyMaps.get(i); - returnString.append(ModelUtil.upperFirstChar(kmap.relFieldName)); + returnString.append(ModelUtil.upperFirstChar(kmap.getRelFieldName())); i++; if (i >= keyMaps.size()) { @@ -253,36 +275,8 @@ public class ModelRelation extends Model return returnString.toString(); } - /** - * @return Returns the isAutoRelation. - */ - public boolean isAutoRelation() { - return isAutoRelation; - } - /** - * @param isAutoRelation The isAutoRelation to set. - */ - public void setAutoRelation(boolean isAutoRelation) { - this.isAutoRelation = isAutoRelation; - } - - // FIXME: CCE - @Override - public boolean equals(Object other) { - ModelRelation otherRel = (ModelRelation) other; - - if (!otherRel.type.equals(this.type)) return false; - if (!otherRel.title.equals(this.title)) return false; - if (!otherRel.relEntityName.equals(this.relEntityName)) return false; - - Set<ModelKeyMap> thisKeyNames = new HashSet<ModelKeyMap>(this.keyMaps); - Set<ModelKeyMap> otherKeyNames = new HashSet<ModelKeyMap>(otherRel.keyMaps); - if (!thisKeyNames.containsAll(otherKeyNames)) return false; - if (!otherKeyNames.containsAll(thisKeyNames)) return false; - - return true; - } + // TODO: Externalize this. public Element toXmlElement(Document document) { Element root = document.createElement("relation"); root.setAttribute("type", this.getType()); @@ -295,7 +289,7 @@ public class ModelRelation extends Model root.setAttribute("fk-name", this.getFkName()); } - Iterator<ModelKeyMap> kmIter = this.getKeyMapsIterator(); + Iterator<ModelKeyMap> kmIter = this.keyMaps.iterator(); while (kmIter != null && kmIter.hasNext()) { ModelKeyMap km = kmIter.next(); root.appendChild(km.toXmlElement(document)); 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=1477929&r1=1477928&r2=1477929&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 Wed May 1 08:16:56 2013 @@ -342,7 +342,7 @@ public class ModelViewEntity extends Mod ModelField field = fldsIt.next(); sb.append(field.getColValue()); if (alias) { - ModelAlias modelAlias = this.getAlias(field.name); + ModelAlias modelAlias = this.getAlias(field.getName()); if (modelAlias != null) { sb.append(" AS ").append(modelAlias.getColAlias()); } @@ -406,15 +406,9 @@ public class ModelViewEntity extends Mod Iterator<ModelField> aliasedFieldIterator = aliasedEntity.getFieldsIterator(); while (aliasedFieldIterator.hasNext()) { ModelField aliasedModelField = aliasedFieldIterator.next(); - ModelField newModelField = new ModelField(); - for (int i = 0; i < aliasedModelField.getValidatorsSize(); i++) { - newModelField.addValidator(aliasedModelField.getValidator(i)); - } - newModelField.setColName(modelMemberEntity.getEntityAlias() + "." + aliasedModelField.getColName()); - newModelField.setName(modelMemberEntity.getEntityAlias() + "." + aliasedModelField.getName()); - newModelField.setType(aliasedModelField.getType()); - newModelField.setDescription(aliasedModelField.getDescription()); - newModelField.setIsPk(false); + ModelField newModelField = ModelField.create(this, aliasedModelField.getDescription(), modelMemberEntity.getEntityAlias() + "." + aliasedModelField.getName(), + aliasedModelField.getType(), modelMemberEntity.getEntityAlias() + "." + aliasedModelField.getColName(), null, null, false, false, false, false, + false, aliasedModelField.getValidators()); aliasedModelEntity.addField(newModelField); } } @@ -422,31 +416,32 @@ public class ModelViewEntity extends Mod expandAllAliasAlls(modelReader); for (ModelAlias alias: aliases) { - ModelField field = new ModelField(); - field.setModelEntity(this); - field.name = alias.name; - field.description = alias.description; - - // if this is a groupBy field, add it to the groupBys list - if (alias.groupBy || groupByFields.contains(alias.name)) { - this.groupBys.add(field); - } - // show a warning if function is specified and groupBy is true if (UtilValidate.isNotEmpty(alias.function) && alias.groupBy) { Debug.logWarning("[" + this.getEntityName() + "]: The view-entity alias with name=" + alias.name + " has a function value and is specified as a group-by field; this may be an error, but is not necessarily.", module); } - + String description = alias.description; + String name = alias.name; + String type = ""; + String colName = ""; + String colValue = ""; + String fieldSet = ""; + boolean isNotNull = false; + boolean isPk = false; + boolean encrypt = false; + boolean isAutoCreatedInternal = false; + boolean enableAuditLog = false; + List<String> validators = null; if (alias.isComplexAlias()) { // if this is a complex alias, make a complex column name... StringBuilder colNameBuffer = new StringBuilder(); StringBuilder fieldTypeBuffer = new StringBuilder(); alias.makeAliasColName(colNameBuffer, fieldTypeBuffer, this, modelReader); - field.colValue = colNameBuffer.toString(); - field.colName = ModelUtil.javaNameToDbName(alias.name); - field.type = fieldTypeBuffer.toString(); - field.isPk = false; - field.fieldSet = alias.getFieldSet(); + colValue = colNameBuffer.toString(); + colName = ModelUtil.javaNameToDbName(alias.name); + type = fieldTypeBuffer.toString(); + isPk = false; + fieldSet = alias.getFieldSet(); } else { ModelEntity aliasedEntity = getAliasedEntity(alias.entityAlias, modelReader); ModelField aliasedField = getAliasedField(aliasedEntity, alias.field, modelReader); @@ -455,55 +450,51 @@ public class ModelViewEntity extends Mod alias.field + "\" on entity with name: " + aliasedEntity.getEntityName(), module); continue; } - if (alias.isPk != null) { - field.isPk = alias.isPk.booleanValue(); + isPk = alias.isPk.booleanValue(); } else { - field.isPk = aliasedField.isPk; + isPk = aliasedField.getIsPk(); } - - field.encrypt = aliasedField.encrypt; - - field.type = aliasedField.type; - field.validators = aliasedField.validators; - - field.colValue = alias.entityAlias + "." + SqlJdbcUtil.filterColName(aliasedField.colName); - field.colName = SqlJdbcUtil.filterColName(field.colValue); - if (UtilValidate.isEmpty(field.description)) { - field.description = aliasedField.description; + encrypt = aliasedField.getEncrypt(); + type = aliasedField.getType(); + validators = aliasedField.getValidators(); + colValue = alias.entityAlias + "." + SqlJdbcUtil.filterColName(aliasedField.getColName()); + colName = SqlJdbcUtil.filterColName(colValue); + if (description.isEmpty()) { + description = aliasedField.getDescription(); } - if (UtilValidate.isEmpty(alias.getFieldSet())) { + if (alias.getFieldSet().isEmpty()) { String aliasedFieldSet = aliasedField.getFieldSet(); - if (UtilValidate.isNotEmpty(aliasedFieldSet)) { + if (!aliasedFieldSet.isEmpty()) { StringBuilder fieldSetBuffer = new StringBuilder(alias.entityAlias); fieldSetBuffer.append("_"); fieldSetBuffer.append(Character.toUpperCase(aliasedFieldSet.charAt(0))); fieldSetBuffer.append(aliasedFieldSet.substring(1)); - field.fieldSet = fieldSetBuffer.toString().intern(); - Debug.logInfo("[" + this.getEntityName() + "]: copied field set on [" + field.name + "]: " + field.fieldSet, module); - } else { - field.fieldSet = ""; + fieldSet = fieldSetBuffer.toString().intern(); + Debug.logInfo("[" + this.getEntityName() + "]: copied field set on [" + name + "]: " + fieldSet, module); } } else { - field.fieldSet = alias.getFieldSet(); + fieldSet = alias.getFieldSet(); } } - - addField(field); - if ("count".equals(alias.function) || "count-distinct".equals(alias.function)) { // if we have a "count" function we have to change the type - field.type = "numeric"; + type = "numeric"; } - if (UtilValidate.isNotEmpty(alias.function)) { String prefix = functionPrefixMap.get(alias.function); if (prefix == null) { Debug.logWarning("[" + this.getEntityName() + "]: Specified alias function [" + alias.function + "] not valid; must be: min, max, sum, avg, count or count-distinct; using a column name with no function function", module); } else { - field.colValue = prefix + field.getColValue() + ")"; + colValue = prefix + colValue + ")"; } } + ModelField field = ModelField.create(this, description, name, type, colName, colValue, fieldSet, isNotNull, isPk, encrypt, isAutoCreatedInternal, enableAuditLog, validators); + // if this is a groupBy field, add it to the groupBys list + if (alias.groupBy || groupByFields.contains(alias.name)) { + this.groupBys.add(field); + } + addField(field); } } @@ -1083,7 +1074,7 @@ public class ModelViewEntity extends Mod colNameBuffer.append(colName); //set fieldTypeBuffer if not already set if (fieldTypeBuffer.length() == 0) { - fieldTypeBuffer.append(modelField.type); + fieldTypeBuffer.append(modelField.getType()); } } } Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java?rev=1477929&r1=1477928&r2=1477929&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java Wed May 1 08:16:56 2013 @@ -79,7 +79,7 @@ public class EntityTestSuite extends Ent assertNotNull("TestingType entity model not null", modelEntity); ModelField modelField = modelEntity.getField("description"); assertNotNull("TestingType.description field model not null", modelField); - modelField = new ModelField("newDesc", modelField.getType(), "NEW_DESC", false); + modelField = ModelField.create(modelEntity, null, "newDesc", modelField.getType(), "NEW_DESC", null, null, false, false, false, false, false, null); modelEntity.addField(modelField); modelField = modelEntity.getField("newDesc"); assertNotNull("TestingType.newDesc field model not null", modelField); Modified: ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/GenericWebEvent.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/GenericWebEvent.java?rev=1477929&r1=1477928&r2=1477929&view=diff ============================================================================== --- ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/GenericWebEvent.java (original) +++ ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/GenericWebEvent.java Wed May 1 08:16:56 2013 @@ -227,8 +227,7 @@ public class GenericWebEvent { while (fieldIter.hasNext()) { ModelField field = fieldIter.next(); - for (int j = 0; j < field.getValidatorsSize(); j++) { - String curValidate = field.getValidator(j); + for (String curValidate : field.getValidators()) { Class<?>[] paramTypes = new Class[] {String.class}; Object[] params = new Object[] {findByEntity.get(field.getName()).toString()}; Modified: ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/WebToolsServices.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/WebToolsServices.java?rev=1477929&r1=1477928&r2=1477929&view=diff ============================================================================== --- ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/WebToolsServices.java (original) +++ ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/WebToolsServices.java Wed May 1 08:16:56 2013 @@ -721,9 +721,9 @@ public class WebToolsServices { Map<String, Object> relationMap = FastMap.newInstance(); ModelRelation relation = entity.getRelation(r); List<Map<String, Object>> keysList = FastList.newInstance(); - for (int km = 0; km < relation.getKeyMapsSize(); km++) { + int row = 1; + for (ModelKeyMap keyMap : relation.getKeyMaps()) { Map<String, Object> keysMap = FastMap.newInstance(); - ModelKeyMap keyMap = relation.getKeyMap(km); String fieldName = null; String relFieldName = null; if (keyMap.getFieldName().equals(keyMap.getRelFieldName())) { @@ -733,7 +733,7 @@ public class WebToolsServices { fieldName = keyMap.getFieldName(); relFieldName = keyMap.getRelFieldName(); } - keysMap.put("row", km + 1); + keysMap.put("row", row++); keysMap.put("fieldName", fieldName); keysMap.put("relFieldName", relFieldName); keysList.add(keysMap); @@ -754,7 +754,7 @@ public class WebToolsServices { List<String> fieldNameList = FastList.newInstance(); ModelIndex index = entity.getIndex(r); - for (Iterator<ModelIndex.Field> fieldIterator = index.getFieldsIterator(); fieldIterator.hasNext();) { + for (Iterator<ModelIndex.Field> fieldIterator = index.getFields().iterator(); fieldIterator.hasNext();) { fieldNameList.add(fieldIterator.next().getFieldName()); } |
| Free forum by Nabble | Edit this page |
