Re: svn commit: r1139922 - in /ofbiz/trunk/framework/entity: dtd/ src/org/ofbiz/entity/datasource/ src/org/ofbiz/entity/model/

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: svn commit: r1139922 - in /ofbiz/trunk/framework/entity: dtd/ src/org/ofbiz/entity/datasource/ src/org/ofbiz/entity/model/

Adam Heath-2
On 06/26/2011 02:19 PM, [hidden email] wrote:

> Author: doogie
> Date: Sun Jun 26 19:19:06 2011
> New Revision: 1139922
>
> URL: http://svn.apache.org/viewvc?rev=1139922&view=rev
> Log:
> FEATURE: Add field-set feature to entity engine.  You can now
> put different fields into different field sets, and when you select any
> field in a particular set, all fields in that set will be fetched.  This
> also cascades upwards thru views, including alias-all and alias.

Yet another major feature.

I use this feature in views, when I am combining date-filtered
entities.  I put all the PK fields of the date-filtered entity into
the same group.  Then, any time there is an <alias-all>, <alias>, etc
that pull them in, and I then eventually do a query with just the
fooId field, the fromDate/thruDate fields automatically get added to
the select.

Because, when you think about it, if the entity is date-filtered, you
*must* also select the fromDate/thruDate, and it *must* be filtered in
memory(or at least, not cached).

> Modified:
>      ofbiz/trunk/framework/entity/dtd/entitymodel.xsd
>      ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java
>      ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/DynamicViewEntity.java
>      ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelField.java
>      ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelViewEntity.java
>
> Modified: ofbiz/trunk/framework/entity/dtd/entitymodel.xsd
> URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/dtd/entitymodel.xsd?rev=1139922&r1=1139921&r2=1139922&view=diff
> ==============================================================================
> --- ofbiz/trunk/framework/entity/dtd/entitymodel.xsd (original)
> +++ ofbiz/trunk/framework/entity/dtd/entitymodel.xsd Sun Jun 26 19:19:06 2011
> @@ -139,6 +139,13 @@ under the License.
>                   </xs:documentation>
>               </xs:annotation>
>           </xs:attribute>
> +<xs:attribute name="field-set" default="" type="xs:string">
> +<xs:annotation>
> +<xs:documentation>
> +                    Any fields that have the same field-set will be selected together in generated queries.
> +</xs:documentation>
> +</xs:annotation>
> +</xs:attribute>
>       </xs:attributeGroup>
>       <xs:element name="validate">
>           <xs:complexType>
> @@ -267,6 +274,13 @@ under the License.
>           <xs:attribute name="prefix" type="xs:string"/>
>           <xs:attribute name="group-by" default="false" type="boolean"/>
>           <xs:attribute name="function" type="aggregate-function"/>
> +<xs:attribute name="field-set" default="" type="xs:string">
> +<xs:annotation>
> +<xs:documentation>
> +                    Any fields that have the same field-set will be selected together in generated queries.
> +</xs:documentation>
> +</xs:annotation>
> +</xs:attribute>
>       </xs:attributeGroup>
>       <xs:element name="exclude">
>           <xs:complexType>
> @@ -293,6 +307,13 @@ under the License.
>           <xs:attribute name="prim-key" type="xs:string"/>
>           <xs:attribute name="group-by" default="false" type="boolean"/>
>           <xs:attribute name="function" type="aggregate-function"/>
> +<xs:attribute name="field-set" default="" type="xs:string">
> +<xs:annotation>
> +<xs:documentation>
> +                    Any fields that have the same field-set will be selected together in generated queries.
> +</xs:documentation>
> +</xs:annotation>
> +</xs:attribute>
>       </xs:attributeGroup>
>       <xs:element name="complex-alias">
>           <xs:annotation>
>
> Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java
> URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java?rev=1139922&r1=1139921&r2=1139922&view=diff
> ==============================================================================
> --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java (original)
> +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java Sun Jun 26 19:19:06 2011
> @@ -660,12 +660,16 @@ public class GenericDAO {
>           if (UtilValidate.isNotEmpty(fieldsToSelect)) {
>               Set<String>  tempKeys = FastSet.newInstance();
>               tempKeys.addAll(fieldsToSelect);
> +            Set<String>  fieldSetsToInclude = FastSet.newInstance();
> +            Set<String>  addedFields = FastSet.newInstance();
>               for (String fieldToSelect : fieldsToSelect) {
>                   if (tempKeys.contains(fieldToSelect)) {
>                       ModelField curField = modelEntity.getField(fieldToSelect);
>                       if (curField != null) {
> +                        fieldSetsToInclude.add(curField.getFieldSet());
>                           selectFields.add(curField);
>                           tempKeys.remove(fieldToSelect);
> +                        addedFields.add(fieldToSelect);
>                       }
>                   }
>               }
> @@ -673,6 +677,37 @@ public class GenericDAO {
>               if (tempKeys.size()>  0) {
>                   throw new GenericModelException("In selectListIteratorByCondition invalid field names specified: " + tempKeys.toString());
>               }
> +            fieldSetsToInclude.remove("");
> +            if (verboseOn) {
> +                Debug.logInfo("[" + modelEntity.getEntityName() + "]: field-sets to include: " + fieldSetsToInclude, module);
> +            }
> +            if (UtilValidate.isNotEmpty(fieldSetsToInclude)) {
> +                Iterator<ModelField>  fieldIter = modelEntity.getFieldsIterator();
> +                Set<String>  extraFields = FastSet.newInstance();
> +                Set<String>  reasonSets = FastSet.newInstance();
> +                while (fieldIter.hasNext()) {
> +                    ModelField curField = fieldIter.next();
> +                    String fieldSet = curField.getFieldSet();
> +                    if (UtilValidate.isEmpty(fieldSet)) {
> +                        continue;
> +                    }
> +                    if (!fieldSetsToInclude.contains(fieldSet)) {
> +                        continue;
> +                    }
> +                    String fieldName = curField.getName();
> +                    if (addedFields.contains(fieldName)) {
> +                        continue;
> +                    }
> +                    reasonSets.add(fieldSet);
> +                    extraFields.add(fieldName);
> +                    addedFields.add(fieldName);
> +                    selectFields.add(curField);
> +                }
> +                if (verboseOn) {
> +                    Debug.logInfo("[" + modelEntity.getEntityName() + "]: auto-added select fields: " + extraFields, module);
> +                    Debug.logInfo("[" + modelEntity.getEntityName() + "]: auto-added field-sets: " + reasonSets, module);
> +                }
> +            }
>           } else {
>               selectFields = modelEntity.getFieldsUnmodifiable();
>           }
>
> Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/DynamicViewEntity.java
> URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/DynamicViewEntity.java?rev=1139922&r1=1139921&r2=1139922&view=diff
> ==============================================================================
> --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/DynamicViewEntity.java (original)
> +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/DynamicViewEntity.java Sun Jun 26 19:19:06 2011
> @@ -238,7 +238,7 @@ public class DynamicViewEntity {
>       }
>
>       public void addAliasAll(String entityAlias, String prefix, Collection<String>  excludes) {
> -        ModelAliasAll aliasAll = new ModelAliasAll(entityAlias, prefix, false, null, excludes);
> +        ModelAliasAll aliasAll = new ModelAliasAll(entityAlias, prefix, false, null, null, excludes);
>           this.aliasAlls.add(aliasAll);
>       }
>
> @@ -252,10 +252,14 @@ public class DynamicViewEntity {
>
>       /** Add an alias, full detail. All parameters can be null except entityAlias and name. */
>       public void addAlias(String entityAlias, String name, String field, String colAlias, Boolean primKey, Boolean groupBy, String function) {
> -        addAlias(entityAlias, name, field, colAlias, primKey, groupBy, function, null);
> +        addAlias(entityAlias, name, field, colAlias, primKey, groupBy, function, null, null);
>       }
>
>       public void addAlias(String entityAlias, String name, String field, String colAlias, Boolean primKey, Boolean groupBy, String function, ComplexAliasMember complexAliasMember) {
> +        addAlias(entityAlias, name, field, colAlias, primKey, groupBy, function, null, complexAliasMember);
> +    }
> +
> +    public void addAlias(String entityAlias, String name, String field, String colAlias, Boolean primKey, Boolean groupBy, String function, String fieldSet, ComplexAliasMember complexAliasMember) {
>           if (entityAlias == null&&  complexAliasMember == null) {
>               throw new IllegalArgumentException("entityAlias cannot be null if this is not a complex alias in call to DynamicViewEntity.addAlias");
>           }
> @@ -263,7 +267,7 @@ public class DynamicViewEntity {
>               throw new IllegalArgumentException("name cannot be null in call to DynamicViewEntity.addAlias");
>           }
>
> -        ModelAlias alias = new ModelAlias(entityAlias, name, field, colAlias, primKey, groupBy, function);
> +        ModelAlias alias = new ModelAlias(entityAlias, name, field, colAlias, primKey, groupBy, function, fieldSet);
>           if (complexAliasMember != null) {
>               alias.setComplexAliasMember(complexAliasMember);
>           }
>
> Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelField.java
> URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelField.java?rev=1139922&r1=1139921&r2=1139922&view=diff
> ==============================================================================
> --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelField.java (original)
> +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelField.java Sun Jun 26 19:19:06 2011
> @@ -55,6 +55,9 @@ public class ModelField extends ModelChi
>       protected boolean isAutoCreatedInternal = false;
>       protected boolean enableAuditLog = false;
>
> +    /** when any field in the same set is selected in a query, all fields in that set will be selected */
> +    protected String fieldSet = "";
> +
>       /** validators to be called when an update is done */
>       protected List<String>  validators = new ArrayList<String>();
>
> @@ -85,6 +88,7 @@ public class ModelField extends ModelChi
>           this.description = UtilXml.childElementValue(fieldElement, "description");
>           this.enableAuditLog = UtilXml.checkBoolean(fieldElement.getAttribute("enable-audit-log"), false);
>           this.isNotNull = UtilXml.checkBoolean(fieldElement.getAttribute("not-null"), false);
> +        this.fieldSet = UtilXml.checkEmpty(fieldElement.getAttribute("field-set")).intern();
>
>           NodeList validateList = fieldElement.getElementsByTagName("validate");
>
> @@ -184,6 +188,14 @@ public class ModelField extends ModelChi
>           this.isAutoCreatedInternal = isAutoCreatedInternal;
>       }
>
> +    public String getFieldSet() {
> +        return fieldSet;
> +    }
> +
> +    public void setFieldSet(String fieldSet) {
> +        this.fieldSet = fieldSet;
> +    }
> +
>       /** validators to be called when an update is done */
>       public String getValidator(int index) {
>           return this.validators.get(index);
>
> 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=1139922&r1=1139921&r2=1139922&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 Sun Jun 26 19:19:06 2011
> @@ -459,6 +459,7 @@ public class ModelViewEntity extends Mod
>                   field.colName = ModelUtil.javaNameToDbName(alias.name);
>                   field.type = fieldTypeBuffer.toString();
>                   field.isPk = false;
> +                field.fieldSet = alias.getFieldSet();
>               } else {
>                   ModelEntity aliasedEntity = getAliasedEntity(alias.entityAlias, modelReader);
>                   ModelField aliasedField = getAliasedField(aliasedEntity, alias.field, modelReader);
> @@ -484,6 +485,21 @@ public class ModelViewEntity extends Mod
>                   if (UtilValidate.isEmpty(field.description)) {
>                       field.description = aliasedField.description;
>                   }
> +                if (UtilValidate.isEmpty(alias.getFieldSet())) {
> +                    String aliasedFieldSet = aliasedField.getFieldSet();
> +                    if (UtilValidate.isNotEmpty(aliasedFieldSet)) {
> +                        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 = "";
> +                    }
> +                } else {
> +                    field.fieldSet = alias.getFieldSet();
> +                }
>               }
>
>               this.fields.add(field);
> @@ -627,6 +643,7 @@ public class ModelViewEntity extends Mod
>               String prefix = aliasAll.getPrefix();
>               String function = aliasAll.getFunction();
>               boolean groupBy = aliasAll.getGroupBy();
> +            String aliasAllFieldSet = aliasAll.getFieldSet();
>
>               ModelMemberEntity modelMemberEntity = memberModelMemberEntities.get(entityAlias);
>               if (modelMemberEntity == null) {
> @@ -667,6 +684,27 @@ public class ModelViewEntity extends Mod
>                       newAliasBuffer.append(aliasName.substring(1));
>                       aliasName = newAliasBuffer.toString();
>                   }
> +                String fieldSet;
> +                if (UtilValidate.isEmpty(aliasAllFieldSet)) {
> +                    String aliasedFieldSet = modelField.getFieldSet();
> +                    if (UtilValidate.isNotEmpty(aliasedFieldSet)) {
> +                        StringBuilder fieldSetBuffer = new StringBuilder(entityAlias);
> +                        if (UtilValidate.isNotEmpty(prefix)) {
> +                            fieldSetBuffer.append(Character.toUpperCase(prefix.charAt(0)));
> +                            fieldSetBuffer.append(prefix.substring(1));
> +                        }
> +                        fieldSetBuffer.append(Character.toUpperCase(aliasedFieldSet.charAt(0)));
> +                        fieldSetBuffer.append(aliasedFieldSet.substring(1));
> +                        fieldSet = fieldSetBuffer.toString();
> +                    } else {
> +                        fieldSet = "";
> +                    }
> +                } else {
> +                    fieldSet = aliasAllFieldSet;
> +                }
> +                if (UtilValidate.isNotEmpty(fieldSet)) {
> +                    Debug.logInfo("[" + this.getEntityName() + "]: set field-set on [" + aliasName + "]: " + fieldSet, module);
> +                }
>
>                   ModelAlias existingAlias = this.getAlias(aliasName);
>                   if (existingAlias != null) {
> @@ -703,7 +741,7 @@ public class ModelViewEntity extends Mod
>                       continue;
>                   }
>
> -                ModelAlias expandedAlias = new ModelAlias(aliasAll.getEntityAlias(), aliasName, fieldName, ModelUtil.javaNameToDbName(UtilXml.checkEmpty(aliasName)), null, groupBy, function, true);
> +                ModelAlias expandedAlias = new ModelAlias(aliasAll.getEntityAlias(), aliasName, fieldName, ModelUtil.javaNameToDbName(UtilXml.checkEmpty(aliasName)), null, groupBy, function, fieldSet, true);
>                   expandedAlias.setDescription(modelField.getDescription());
>
>                   aliases.add(expandedAlias);
> @@ -741,17 +779,24 @@ public class ModelViewEntity extends Mod
>           protected final boolean groupBy;
>           // is specified this alias is a calculated value; can be: min, max, sum, avg, count, count-distinct
>           protected final String function;
> +        protected final String fieldSet;
>
>           @Deprecated
>           public ModelAliasAll(String entityAlias, String prefix) {
> -            this(entityAlias, prefix, false, null, null);
> +            this(entityAlias, prefix, false, null, null, null);
>           }
>
> +        @Deprecated
>           public ModelAliasAll(String entityAlias, String prefix, boolean groupBy, String function, Collection<String>  excludes) {
> +            this(entityAlias, prefix, groupBy, function, null, excludes);
> +        }
> +
> +        public ModelAliasAll(String entityAlias, String prefix, boolean groupBy, String function, String fieldSet, Collection<String>  excludes) {
>               this.entityAlias = entityAlias;
>               this.prefix = prefix;
>               this.groupBy = groupBy;
>               this.function = function;
> +            this.fieldSet = fieldSet;
>               if (UtilValidate.isNotEmpty(excludes)) {
>                   this.fieldsToExclude = new HashSet<String>(excludes.size());
>                   this.fieldsToExclude.addAll(excludes);
> @@ -765,6 +810,7 @@ public class ModelViewEntity extends Mod
>               this.prefix = UtilXml.checkEmpty(aliasAllElement.getAttribute("prefix")).intern();
>               this.groupBy = "true".equals(UtilXml.checkEmpty(aliasAllElement.getAttribute("group-by")));
>               this.function = UtilXml.checkEmpty(aliasAllElement.getAttribute("function"));
> +            this.fieldSet = UtilXml.checkEmpty(aliasAllElement.getAttribute("field-set")).intern();
>
>               List<? extends Element>  excludes = UtilXml.childElementList(aliasAllElement, "exclude");
>               if (UtilValidate.isNotEmpty(excludes)) {
> @@ -794,6 +840,10 @@ public class ModelViewEntity extends Mod
>               return this.function;
>           }
>
> +        public String getFieldSet() {
> +            return this.fieldSet;
> +        }
> +
>           public boolean shouldExclude(String fieldName) {
>               if (this.fieldsToExclude == null) {
>                   return false;
> @@ -821,6 +871,7 @@ public class ModelViewEntity extends Mod
>           protected final boolean groupBy;
>           // is specified this alias is a calculated value; can be: min, max, sum, avg, count, count-distinct
>           protected final String function;
> +        protected final String fieldSet;
>           protected final boolean isFromAliasAll;
>           protected ComplexAliasMember complexAliasMember;
>           // The description for documentation purposes
> @@ -840,6 +891,7 @@ public class ModelViewEntity extends Mod
>               }
>               this.groupBy = "true".equals(UtilXml.checkEmpty(aliasElement.getAttribute("group-by")));
>               this.function = UtilXml.checkEmpty(aliasElement.getAttribute("function")).intern();
> +            this.fieldSet = UtilXml.checkEmpty(aliasElement.getAttribute("field-set")).intern();
>               this.isFromAliasAll = false;
>               this.description = UtilXml.checkEmpty(UtilXml.childElementValue(aliasElement, "description")).intern();
>
> @@ -849,11 +901,16 @@ public class ModelViewEntity extends Mod
>               }
>           }
>
> +        @Deprecated
>           public ModelAlias(String entityAlias, String name, String field, String colAlias, Boolean isPk, Boolean groupBy, String function) {
> -            this(entityAlias, name, field, colAlias, isPk, groupBy, function, false);
> +            this(entityAlias, name, field, colAlias, isPk, groupBy, function, null, false);
> +        }
> +
> +        public ModelAlias(String entityAlias, String name, String field, String colAlias, Boolean isPk, Boolean groupBy, String function, String fieldSet) {
> +            this(entityAlias, name, field, colAlias, isPk, groupBy, function, fieldSet, false);
>           }
>
> -        protected ModelAlias(String entityAlias, String name, String field, String colAlias, Boolean isPk, Boolean groupBy, String function, boolean isFromAliasAll) {
> +        protected ModelAlias(String entityAlias, String name, String field, String colAlias, Boolean isPk, Boolean groupBy, String function, String fieldSet, boolean isFromAliasAll) {
>               this.entityAlias = entityAlias;
>               this.name = name;
>               this.field = UtilXml.checkEmpty(field, this.name);
> @@ -865,6 +922,7 @@ public class ModelViewEntity extends Mod
>                   this.groupBy = false;
>               }
>               this.function = function;
> +            this.fieldSet = UtilXml.checkEmpty(fieldSet).intern();
>               this.isFromAliasAll = isFromAliasAll;
>           }
>
> @@ -910,6 +968,10 @@ public class ModelViewEntity extends Mod
>               return this.function;
>           }
>
> +        public String getFieldSet() {
> +            return fieldSet;
> +        }
> +
>           public String getDescription() {
>               return this.description;
>           }
>
>