Re: svn commit: r1342854 - in /ofbiz/trunk/framework/minilang: dtd/simple-methods-v2.xsd src/org/ofbiz/minilang/method/entityops/FilterListByDate.java

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

Re: svn commit: r1342854 - in /ofbiz/trunk/framework/minilang: dtd/simple-methods-v2.xsd src/org/ofbiz/minilang/method/entityops/FilterListByDate.java

Jacques Le Roux
Administrator
Right, I forgot to save pending change in Eclipse, thanks Adrian

Fixed at revision: 1342871

Jacques

> Author: adrianc
> Date: Sat May 26 05:01:00 2012
> New Revision: 1342854
>
> URL: http://svn.apache.org/viewvc?rev=1342854&view=rev
> Log:
> Overhauled Mini-language <filter-list-by-date> element.
>
> Modified:
>    ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd
>    ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/entityops/FilterListByDate.java
>
> Modified: ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd
> URL:
> http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd?rev=1342854&r1=1342853&r2=1342854&view=diff
> ==============================================================================
> --- ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd (original)
> +++ ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd Sat May 26 05:01:00 2012
> @@ -2884,6 +2884,7 @@ under the License.
>                 <xs:annotation>
>                     <xs:documentation>
>                         The name of the field that contains the map that will be used for the entity field comparison.
> +                        &lt;br/&gt;&lt;br/&gt;
>                         Required. Attribute type: expression.
>                     </xs:documentation>
>                 </xs:annotation>
> @@ -2893,6 +2894,7 @@ under the License.
>                     <xs:documentation>
>                         The name of the field the filtered list will be put into.
>                         Defaults to the value of the list-name attribute (replaces the old list).
> +                        &lt;br/&gt;&lt;br/&gt;
>                         Optional. Attribute type: expression.
>                     </xs:documentation>
>                 </xs:annotation>
> @@ -2902,68 +2904,64 @@ under the License.
>     <xs:element name="filter-list-by-date" substitutionGroup="EntityFindOperations">
>         <xs:annotation>
>             <xs:documentation>
> -                The filter-list-by-date tag filters the given list by the valid date using the from and thru dates in each value
> object.
> +                Filters a list of entity values by their date-range fields.
> +                Each entity value in the list will be compared to the specified date, and
> +                if the entity value's date-range fields include the date, the entity value
> +                will be copied to the target list.
>             </xs:documentation>
>         </xs:annotation>
>         <xs:complexType>
> -            <xs:attributeGroup ref="attlist.filter-list-by-date"/>
> +            <xs:attribute type="xs:string" name="list" use="required">
> +                <xs:annotation>
> +                    <xs:documentation>
> +                        The name of the field that contains the list of entity values.
> +                        &lt;br/&gt;&lt;br/&gt;
> +                        Required. Attribute type: expression.
> +                    </xs:documentation>
> +                </xs:annotation>
> +            </xs:attribute>
> +            <xs:attribute type="xs:string" name="to-list">
> +                <xs:annotation>
> +                    <xs:documentation>
> +                        The name of the field the filtered list will be put into.
> +                        Defaults to the value of the list-name attribute (replaces the old list).
> +                        &lt;br/&gt;&lt;br/&gt;
> +                        Optional. Attribute type: expression.
> +                    </xs:documentation>
> +                </xs:annotation>
> +            </xs:attribute>
> +            <xs:attribute type="xs:string" name="valid-date">
> +                <xs:annotation>
> +                    <xs:documentation>
> +                        The name of a field in the method environment date to filter by.
> +                        Defaults to now.
> +                        &lt;br/&gt;&lt;br/&gt;
> +                        Optional. Attribute type: expression.
> +                    </xs:documentation>
> +                </xs:annotation>
> +            </xs:attribute>
> +            <xs:attribute type="xs:string" name="from-field-name">
> +                <xs:annotation>
> +                    <xs:documentation>
> +                        The name of the entity field to use as the beginning date.
> +                        Defaults to "fromDate".
> +                        &lt;br/&gt;&lt;br/&gt;
> +                        Optional. Attribute type: constant.
> +                    </xs:documentation>
> +                </xs:annotation>
> +            </xs:attribute>
> +            <xs:attribute type="xs:string" name="thru-field-name">
> +                <xs:annotation>
> +                    <xs:documentation>
> +                        The name of the entity field to use as the ending date.
> +                        Defaults to "thruDate".
> +                        &lt;br/&gt;&lt;br/&gt;
> +                        Optional. Attribute type: constant.
> +                    </xs:documentation>
> +                </xs:annotation>
> +            </xs:attribute>
>         </xs:complexType>
>     </xs:element>
> -    <xs:attributeGroup name="attlist.filter-list-by-date">
> -        <xs:attribute type="xs:string" name="list" use="required">
> -            <xs:annotation>
> -                <xs:documentation>
> -                    The name of the method environment field that contains the list of GenericValue objects.
> -                </xs:documentation>
> -            </xs:annotation>
> -        </xs:attribute>
> -        <xs:attribute type="xs:string" name="to-list">
> -            <xs:annotation>
> -                <xs:documentation>
> -                    The name of the method environment field the filtered list will be put into.
> -                    Defaults to the value of the list-name attribute (ie goes to the same place it came from, replacing the old
> list).
> -                </xs:documentation>
> -            </xs:annotation>
> -        </xs:attribute>
> -        <xs:attribute type="xs:string" name="valid-date">
> -            <xs:annotation>
> -                <xs:documentation>
> -                    The name of a field in the method environment date to filter by.
> -                    Defaults to now.
> -                </xs:documentation>
> -            </xs:annotation>
> -        </xs:attribute>
> -        <xs:attribute type="xs:string" name="from-field-name" default="fromDate">
> -            <xs:annotation>
> -                <xs:documentation>
> -                    The name of the GenericValue field to use as the beginning effective date.
> -                    Defaults to fromDate.
> -                </xs:documentation>
> -            </xs:annotation>
> -        </xs:attribute>
> -        <xs:attribute type="xs:string" name="thru-field-name" default="thruDate">
> -            <xs:annotation>
> -                <xs:documentation>
> -                    The name of the GenericValue field to use as the ending effective date.
> -                    Defaults to thruDate.
> -                </xs:documentation>
> -            </xs:annotation>
> -        </xs:attribute>
> -        <xs:attribute name="all-same" default="true">
> -            <xs:annotation>
> -                <xs:documentation>
> -                    Specifies whether or not all GenericValue objects in the list are of the same entity.
> -                    Defaults to true.
> -                </xs:documentation>
> -            </xs:annotation>
> -            <xs:simpleType>
> -                <xs:restriction base="xs:token">
> -                    <xs:enumeration value="true"/>
> -                    <xs:enumeration value="false"/>
> -                </xs:restriction>
> -            </xs:simpleType>
> -        </xs:attribute>
> -    </xs:attributeGroup>
>     <xs:element name="make-value" substitutionGroup="EntityValueOperations">
>         <xs:annotation>
>             <xs:documentation>
>
> Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/entityops/FilterListByDate.java
> URL:
> http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/entityops/FilterListByDate.java?rev=1342854&r1=1342853&r2=1342854&view=diff
> ==============================================================================
> --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/entityops/FilterListByDate.java (original)
> +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/entityops/FilterListByDate.java Sat May 26 05:01:00 2012
> @@ -22,72 +22,91 @@ import java.sql.Timestamp;
> import java.util.List;
>
> import org.ofbiz.base.util.UtilDateTime;
> -import org.ofbiz.base.util.UtilValidate;
> +import org.ofbiz.base.util.collections.FlexibleMapAccessor;
> +import org.ofbiz.base.util.string.FlexibleStringExpander;
> import org.ofbiz.entity.GenericEntity;
> import org.ofbiz.entity.util.EntityUtil;
> import org.ofbiz.minilang.MiniLangException;
> +import org.ofbiz.minilang.MiniLangValidate;
> import org.ofbiz.minilang.SimpleMethod;
> -import org.ofbiz.minilang.method.ContextAccessor;
> import org.ofbiz.minilang.method.MethodContext;
> import org.ofbiz.minilang.method.MethodOperation;
> import org.w3c.dom.Element;
>
> /**
> - * Uses the delegator to find entity values by anding the map fields
> + * Implements the &lt;filter-list-by-date&gt; element.
>  */
> -public class FilterListByDate extends MethodOperation {
> +public final class FilterListByDate extends MethodOperation {
>
> -    String allSameStr;
> -    String fromFieldName;
> -    ContextAccessor<List<GenericEntity>> listAcsr;
> -    String thruFieldName;
> -    ContextAccessor<List<GenericEntity>> toListAcsr;
> -    ContextAccessor<Timestamp> validDateAcsr;
> +    private final FlexibleMapAccessor<List<GenericEntity>> listFma;
> +    private final FlexibleMapAccessor<List<GenericEntity>> toListFma;
> +    private final FlexibleMapAccessor<Timestamp> validDateFma;
> +    private final String fromFieldName;
> +    private final String thruFieldName;
>
>     public FilterListByDate(Element element, SimpleMethod simpleMethod) throws MiniLangException {
>         super(element, simpleMethod);
> -        listAcsr = new ContextAccessor<List<GenericEntity>>(element.getAttribute("list"), element.getAttribute("list-name"));
> -        toListAcsr = new ContextAccessor<List<GenericEntity>>(element.getAttribute("to-list"),
> element.getAttribute("to-list-name"));
> -        if (toListAcsr.isEmpty()) {
> -            toListAcsr = listAcsr;
> +        if (MiniLangValidate.validationOn()) {
> +            MiniLangValidate.attributeNames(simpleMethod, element, "list", "to-list", "valid-date", "fromDate", "thruDate");
> +            MiniLangValidate.requiredAttributes(simpleMethod, element, "list");
> +            MiniLangValidate.expressionAttributes(simpleMethod, element, "list", "to-list", "valid-date");
> +            MiniLangValidate.constantAttributes(simpleMethod, element, "fromDate", "thruDate");
> +            MiniLangValidate.noChildElements(simpleMethod, element);
>         }
> -        validDateAcsr = new ContextAccessor<Timestamp>(element.getAttribute("valid-date"),
> element.getAttribute("valid-date-name"));
> -        fromFieldName = element.getAttribute("from-field-name");
> -        if (UtilValidate.isEmpty(fromFieldName))
> -            fromFieldName = "fromDate";
> -        thruFieldName = element.getAttribute("thru-field-name");
> -        if (UtilValidate.isEmpty(thruFieldName))
> -            thruFieldName = "thruDate";
> -        allSameStr = element.getAttribute("all-same");
> +        listFma = FlexibleMapAccessor.getInstance(element.getAttribute("list"));
> +        String toListAttribute = element.getAttribute("to-list");
> +        if (toListAttribute.isEmpty()) {
> +            toListFma = listFma;
> +        } else {
> +            toListFma = FlexibleMapAccessor.getInstance(toListAttribute);
> +        }
> +        validDateFma = FlexibleMapAccessor.getInstance(element.getAttribute("valid-date"));
> +        fromFieldName = MiniLangValidate.checkAttribute(element.getAttribute("from-field-name"), "fromDate");
> +        thruFieldName = MiniLangValidate.checkAttribute(element.getAttribute("thru-field-name"), "thruDate");
>     }
>
>     @Override
>     public boolean exec(MethodContext methodContext) throws MiniLangException {
> -        if (!validDateAcsr.isEmpty()) {
> -            toListAcsr.put(methodContext, EntityUtil.filterByDate(listAcsr.get(methodContext), validDateAcsr.get(methodContext),
> fromFieldName, thruFieldName, true));
> +        if (!validDateFma.isEmpty()) {
> +            toListFma.put(methodContext.getEnvMap(), EntityUtil.filterByDate(listFma.get(methodContext.getEnvMap()),
> validDateFma.get(methodContext.getEnvMap()), fromFieldName, thruFieldName, true));
>         } else {
> -            toListAcsr.put(methodContext, EntityUtil.filterByDate(listAcsr.get(methodContext), UtilDateTime.nowTimestamp(),
> fromFieldName, thruFieldName, true));
> +            toListFma.put(methodContext.getEnvMap(), EntityUtil.filterByDate(listFma.get(methodContext.getEnvMap()),
> UtilDateTime.nowTimestamp(), fromFieldName, thruFieldName, true));
>         }
>         return true;
>     }
>
>     @Override
>     public String expandedString(MethodContext methodContext) {
> -        // TODO: something more than a stub/dummy
> -        return this.rawString();
> +        return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap());
>     }
>
>     @Override
>     public String rawString() {
> -        // TODO: something more than the empty tag
> -        return "<filter-list-by-date/>";
> +        return toString();
> +    }
> +
> +    @Override
> +    public String toString() {
> +        StringBuilder sb = new StringBuilder("<filter-list-by-date ");
> +        sb.append("list=\"").append(this.listFma).append("\" ");
> +        sb.append("to-list=\"").append(this.toListFma).append("\" ");
> +        sb.append("valid-date=\"").append(this.validDateFma).append("\" ");
> +        sb.append("from-field-name=\"").append(this.fromFieldName).append("\" ");
> +        sb.append("thru-field-name=\"").append(this.thruFieldName).append("\" ");
> +        sb.append("/>");
> +        return sb.toString();
>     }
>
> +    /**
> +     * A factory for the &lt;filter-list-by-date&gt; element.
> +     */
>     public static final class FilterListByDateFactory implements Factory<FilterListByDate> {
> +        @Override
>         public FilterListByDate createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
>             return new FilterListByDate(element, simpleMethod);
>         }
>
> +        @Override
>         public String getName() {
>             return "filter-list-by-date";
>         }
>
>