You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ad...@apache.org on 2012/04/26 17:25:04 UTC

svn commit: r1330902 - in /ofbiz/trunk/framework/minilang: dtd/ src/META-INF/services/ src/org/ofbiz/minilang/method/entityops/ src/org/ofbiz/minilang/method/envops/

Author: adrianc
Date: Thu Apr 26 15:25:03 2012
New Revision: 1330902

URL: http://svn.apache.org/viewvc?rev=1330902&view=rev
Log:
Added a new Mini-language element - <now> - that replaces the <now-timestamp> and <now-date-to-env> elements. It is a drop-in replacement for <now-timestamp>.

Added:
    ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Now.java   (with props)
Modified:
    ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd
    ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory
    ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/entityops/NowDateToEnv.java
    ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/entityops/NowTimestampToEnv.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=1330902&r1=1330901&r2=1330902&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd (original)
+++ ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd Thu Apr 26 15:25:03 2012
@@ -57,6 +57,22 @@ under the License.
         </xs:choice>
     </xs:group>
 
+    <xs:attribute name="field">
+        <xs:annotation>
+            <xs:documentation>
+                The name of the field to set. The target of the assignment.
+                &lt;br/&gt;&lt;br/&gt;
+                Required. Attribute type: expression.
+            </xs:documentation>
+        </xs:annotation>
+        <xs:simpleType>
+            <xs:restriction base="xs:string">
+                <xs:whiteSpace value="collapse" />
+                <xs:minLength value="1" />
+            </xs:restriction>
+        </xs:simpleType>
+    </xs:attribute>
+
     <xs:attributeGroup name="attlist.propertyInfoOptional">
         <xs:attribute type="xs:string" name="resource"/>
         <xs:attribute type="xs:string" name="property"/>
@@ -1341,15 +1357,7 @@ under the License.
         </xs:complexType>
     </xs:element>
     <xs:attributeGroup name="attlist.set">
-        <xs:attribute type="xs:string" name="field" use="required">
-            <xs:annotation>
-                <xs:documentation>
-                    The name of the field to set. The target of the assignment.
-                    &lt;br/&gt;&lt;br/&gt;
-                    Required. Attribute type: expression.
-                </xs:documentation>
-            </xs:annotation>
-        </xs:attribute>
+        <xs:attribute ref="field" use="required" />
         <xs:attribute type="xs:string" name="from-field">
             <xs:annotation>
                 <xs:documentation>
@@ -4416,43 +4424,52 @@ under the License.
             </xs:annotation>
         </xs:attribute>
     </xs:attributeGroup>
+    <xs:element name="now" substitutionGroup="OtherOperations">
+        <xs:annotation>
+            <xs:documentation>
+                Sets a field to the current system time.
+            </xs:documentation>
+        </xs:annotation>
+        <xs:complexType>
+            <xs:attribute ref="field" use="required" />
+            <xs:attribute name="type">
+                <xs:annotation>
+                    <xs:documentation>
+                        The field data type. Defaults to java.sql.Timestamp.
+                    </xs:documentation>
+                </xs:annotation>
+                <xs:simpleType>
+                    <xs:restriction base="xs:token">
+                        <xs:enumeration value="java.sql.Timestamp"/>
+                        <xs:enumeration value="java.sql.Date" />
+                        <xs:enumeration value="java.sql.Time" />
+                        <xs:enumeration value="java.util.Date" />
+                        <xs:enumeration value="java.lang.Long" />
+                    </xs:restriction>
+                </xs:simpleType>
+            </xs:attribute>
+        </xs:complexType>
+    </xs:element>
     <xs:element name="now-timestamp" substitutionGroup="OtherOperations">
         <xs:annotation>
             <xs:documentation>
-                The now-timestamp tag creates a java.sql.Timestamp object with the current date and time in it and puts it in a field in the method environment.
+                Deprecated - use &lt;now&gt;.
             </xs:documentation>
         </xs:annotation>
         <xs:complexType>
-            <xs:attributeGroup ref="attlist.now-timestamp"/>
+            <xs:attribute ref="field" use="required" />
         </xs:complexType>
     </xs:element>
-    <xs:attributeGroup name="attlist.now-timestamp">
-        <xs:attribute type="xs:string" name="field" use="required">
-            <xs:annotation>
-                <xs:documentation>
-                    The name of the method environment field the timestamp will be put in.
-                </xs:documentation>
-            </xs:annotation>
-        </xs:attribute>
-    </xs:attributeGroup>
     <xs:element name="now-date-to-env" substitutionGroup="OtherOperations">
         <xs:annotation>
             <xs:documentation>
-                The now-date-to-env tag creates a java.sql.Date object with the current date in it and puts it in a field in the method environment.
+                Deprecated - use &lt;now&gt;.
             </xs:documentation>
-        </xs:annotation><xs:complexType>
-            <xs:attributeGroup ref="attlist.now-date-to-env"/>
+        </xs:annotation>
+        <xs:complexType>
+            <xs:attribute ref="field" use="required" />
         </xs:complexType>
     </xs:element>
-    <xs:attributeGroup name="attlist.now-date-to-env">
-        <xs:attribute type="xs:string" name="field" use="required">
-            <xs:annotation>
-                <xs:documentation>
-                    The name of the method environment field the date will be put in.
-                </xs:documentation>
-            </xs:annotation>
-        </xs:attribute>
-    </xs:attributeGroup>
     <xs:element name="property-to-field" substitutionGroup="OtherOperations">
         <xs:annotation>
             <xs:documentation>

Modified: ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation%24Factory?rev=1330902&r1=1330901&r2=1330902&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory (original)
+++ ofbiz/trunk/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory Thu Apr 26 15:25:03 2012
@@ -49,7 +49,6 @@ org.ofbiz.minilang.method.entityops.GetR
 org.ofbiz.minilang.method.entityops.MakeNextSeqId$MakeNextSeqIdFactory
 org.ofbiz.minilang.method.entityops.MakeValue$MakeValueFactory
 org.ofbiz.minilang.method.entityops.NowDateToEnv$NowDateToEnvFactory
-org.ofbiz.minilang.method.entityops.NowTimestampToEnv$NowTimestampToEnvFactory
 org.ofbiz.minilang.method.entityops.NowTimestampToEnv$NowTimestampFactory
 org.ofbiz.minilang.method.entityops.OrderValueList$OrderValueListFactory
 org.ofbiz.minilang.method.entityops.RefreshValue$RefreshValueFactory
@@ -77,6 +76,7 @@ org.ofbiz.minilang.method.envops.Iterate
 org.ofbiz.minilang.method.envops.ListToList$ListToListFactory
 org.ofbiz.minilang.method.envops.Loop$LoopFactory
 org.ofbiz.minilang.method.envops.MapToMap$MapToMapFactory
+org.ofbiz.minilang.method.envops.Now$NowFactory
 org.ofbiz.minilang.method.envops.OrderMapList$OrderMapListFactory
 org.ofbiz.minilang.method.envops.SetCalendar$SetCalendarFactory
 org.ofbiz.minilang.method.envops.SetOperation$SetOperationFactory

Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/entityops/NowDateToEnv.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/entityops/NowDateToEnv.java?rev=1330902&r1=1330901&r2=1330902&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/entityops/NowDateToEnv.java (original)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/entityops/NowDateToEnv.java Thu Apr 26 15:25:03 2012
@@ -18,51 +18,57 @@
  *******************************************************************************/
 package org.ofbiz.minilang.method.entityops;
 
+import org.ofbiz.base.util.collections.FlexibleMapAccessor;
+import org.ofbiz.base.util.string.FlexibleStringExpander;
 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;
 
 /**
- * Creates a java.sql.Date with the current date and puts it in the env
+ * Creates a <code>java.sql.Timestamp</code> object that is set to the current system time.
  */
 public class NowDateToEnv extends MethodOperation {
 
-    ContextAccessor<java.sql.Date> envAcsr;
+    private final FlexibleMapAccessor<Object> fieldFma;
 
     public NowDateToEnv(Element element, SimpleMethod simpleMethod) throws MiniLangException {
         super(element, simpleMethod);
-        envAcsr = new ContextAccessor<java.sql.Date>(element.getAttribute("field"), element.getAttribute("env-name"));
+        if (MiniLangValidate.validationOn()) {
+            MiniLangValidate.handleError("Deprecated - use <now>", simpleMethod, element);
+            MiniLangValidate.attributeNames(simpleMethod, element, "field");
+            MiniLangValidate.requiredAttributes(simpleMethod, element, "field");
+            MiniLangValidate.noChildElements(simpleMethod, element);
+        }
+        this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
     }
 
     @Override
     public boolean exec(MethodContext methodContext) throws MiniLangException {
-        envAcsr.put(methodContext, new java.sql.Date(System.currentTimeMillis()));
+        this.fieldFma.put(methodContext.getEnvMap(), new java.sql.Date(System.currentTimeMillis()));
         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 "<now-date-to-env/>";
+        return toString();
     }
 
-    public static final class NowDateFactory implements Factory<NowDateToEnv> {
-        public NowDateToEnv createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
-            return new NowDateToEnv(element, simpleMethod);
-        }
-
-        public String getName() {
-            return "now-date";
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder("<now-date-to-env ");
+        if (!this.fieldFma.isEmpty()) {
+            sb.append("field=\"").append(this.fieldFma).append("\" ");
         }
+        sb.append("/>");
+        return sb.toString();
     }
 
     public static final class NowDateToEnvFactory implements Factory<NowDateToEnv> {

Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/entityops/NowTimestampToEnv.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/entityops/NowTimestampToEnv.java?rev=1330902&r1=1330901&r2=1330902&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/entityops/NowTimestampToEnv.java (original)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/entityops/NowTimestampToEnv.java Thu Apr 26 15:25:03 2012
@@ -18,60 +18,66 @@
  *******************************************************************************/
 package org.ofbiz.minilang.method.entityops;
 
+import org.ofbiz.base.util.collections.FlexibleMapAccessor;
+import org.ofbiz.base.util.string.FlexibleStringExpander;
 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;
 
 /**
- * Creates a java.sql.Timestamp with the current date/time in it and puts it in the env
+ * Creates a <code>java.sql.Timestamp</code> object that is set to the current system time.
  */
-public class NowTimestampToEnv extends MethodOperation {
+public final class NowTimestampToEnv extends MethodOperation {
 
-    ContextAccessor<java.sql.Timestamp> envAcsr;
+    private final FlexibleMapAccessor<Object> fieldFma;
 
     public NowTimestampToEnv(Element element, SimpleMethod simpleMethod) throws MiniLangException {
         super(element, simpleMethod);
-        envAcsr = new ContextAccessor<java.sql.Timestamp>(element.getAttribute("field"), element.getAttribute("env-name"));
+        if (MiniLangValidate.validationOn()) {
+            // MiniLangValidate.handleError("Deprecated - use <now>", simpleMethod, element);
+            MiniLangValidate.attributeNames(simpleMethod, element, "field");
+            MiniLangValidate.requiredAttributes(simpleMethod, element, "field");
+            MiniLangValidate.noChildElements(simpleMethod, element);
+        }
+        this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
     }
 
     @Override
     public boolean exec(MethodContext methodContext) throws MiniLangException {
-        envAcsr.put(methodContext, new java.sql.Timestamp(System.currentTimeMillis()));
+        this.fieldFma.put(methodContext.getEnvMap(), new java.sql.Timestamp(System.currentTimeMillis()));
         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 "<now-timestamp-to-env/>";
+        return toString();
     }
 
-    public static final class NowTimestampFactory implements Factory<NowTimestampToEnv> {
-        public NowTimestampToEnv createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
-            return new NowTimestampToEnv(element, simpleMethod);
-        }
-
-        public String getName() {
-            return "now-timestamp";
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder("<now-timestamp ");
+        if (!this.fieldFma.isEmpty()) {
+            sb.append("field=\"").append(this.fieldFma).append("\" ");
         }
+        sb.append("/>");
+        return sb.toString();
     }
 
-    public static final class NowTimestampToEnvFactory implements Factory<NowTimestampToEnv> {
+    public static final class NowTimestampFactory implements Factory<NowTimestampToEnv> {
         public NowTimestampToEnv createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
             return new NowTimestampToEnv(element, simpleMethod);
         }
 
         public String getName() {
-            return "now-timestamp-to-env";
+            return "now-timestamp";
         }
     }
 }

Added: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Now.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Now.java?rev=1330902&view=auto
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Now.java (added)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Now.java Thu Apr 26 15:25:03 2012
@@ -0,0 +1,112 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *******************************************************************************/
+package org.ofbiz.minilang.method.envops;
+
+import org.ofbiz.base.conversion.ConversionException;
+import org.ofbiz.base.conversion.Converter;
+import org.ofbiz.base.conversion.Converters;
+import org.ofbiz.base.util.ObjectType;
+import org.ofbiz.base.util.collections.FlexibleMapAccessor;
+import org.ofbiz.base.util.string.FlexibleStringExpander;
+import org.ofbiz.minilang.MiniLangException;
+import org.ofbiz.minilang.MiniLangRuntimeException;
+import org.ofbiz.minilang.MiniLangValidate;
+import org.ofbiz.minilang.SimpleMethod;
+import org.ofbiz.minilang.ValidationException;
+import org.ofbiz.minilang.method.MethodContext;
+import org.ofbiz.minilang.method.MethodOperation;
+import org.w3c.dom.Element;
+
+/**
+ * Sets a field to the current system time.
+ */
+public final class Now extends MethodOperation {
+
+    private final FlexibleMapAccessor<Object> fieldFma;
+    private final String type;
+    private final Converter<Long, Object> converter;
+
+    @SuppressWarnings("unchecked")
+    public Now(Element element, SimpleMethod simpleMethod) throws MiniLangException {
+        super(element, simpleMethod);
+        if (MiniLangValidate.validationOn()) {
+            MiniLangValidate.attributeNames(simpleMethod, element, "field", "type");
+            MiniLangValidate.requiredAttributes(simpleMethod, element, "field");
+            MiniLangValidate.noChildElements(simpleMethod, element);
+        }
+        this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
+        this.type = element.getAttribute("type");
+        Class<?> targetClass = null;
+        try {
+            if (this.type.length() > 0) {
+                targetClass = ObjectType.loadClass(this.type);
+            }
+            if (targetClass == null) {
+                targetClass = java.sql.Timestamp.class;
+            }
+            this.converter = (Converter<Long, Object>) Converters.getConverter(Long.class, targetClass);
+        } catch (ClassNotFoundException e) {
+            throw new ValidationException(e.getMessage(), simpleMethod, element);
+        }
+    }
+
+    @Override
+    public boolean exec(MethodContext methodContext) throws MiniLangException {
+        try {
+            this.fieldFma.put(methodContext.getEnvMap(), this.converter.convert(System.currentTimeMillis()));
+        } catch (ConversionException e) {
+            throw new MiniLangRuntimeException(e.getMessage(), this);
+        }
+        return true;
+    }
+
+    @Override
+    public String expandedString(MethodContext methodContext) {
+        return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap());
+    }
+
+    @Override
+    public String rawString() {
+        return toString();
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder("<now ");
+        if (!this.fieldFma.isEmpty()) {
+            sb.append("field=\"").append(this.fieldFma).append("\" ");
+        }
+        if (this.type.length() > 0) {
+            sb.append("type=\"").append(this.type).append("\" ");
+        }
+        sb.append("/>");
+        return sb.toString();
+    }
+
+
+    public static final class NowFactory implements Factory<Now> {
+        public Now createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
+            return new Now(element, simpleMethod);
+        }
+
+        public String getName() {
+            return "now";
+        }
+    }
+}

Propchange: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Now.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/Now.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Rev URL