svn commit: r492770 - in /ofbiz/trunk/framework/service: dtd/service-eca.xsd dtd/services.xsd src/org/ofbiz/service/eca/ServiceEcaAction.java src/org/ofbiz/service/eca/ServiceEcaRule.java src/org/ofbiz/service/eca/ServiceEcaSetField.java

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

svn commit: r492770 - in /ofbiz/trunk/framework/service: dtd/service-eca.xsd dtd/services.xsd src/org/ofbiz/service/eca/ServiceEcaAction.java src/org/ofbiz/service/eca/ServiceEcaRule.java src/org/ofbiz/service/eca/ServiceEcaSetField.java

jaz-3
Author: jaz
Date: Thu Jan  4 13:56:22 2007
New Revision: 492770

URL: http://svn.apache.org/viewvc?view=rev&rev=492770
Log:
implement field override for eca services; you can now use :
<set field-name="[name of context field]" value="RAW_VALUE"/>
or
<set field-name="[name of context field]" env-name="[another field name]"/>

There are also format options, see the updated XSD for options.

Added:
    ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaSetField.java   (with props)
Modified:
    ofbiz/trunk/framework/service/dtd/service-eca.xsd
    ofbiz/trunk/framework/service/dtd/services.xsd
    ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaAction.java
    ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaRule.java

Modified: ofbiz/trunk/framework/service/dtd/service-eca.xsd
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/dtd/service-eca.xsd?view=diff&rev=492770&r1=492769&r2=492770
==============================================================================
--- ofbiz/trunk/framework/service/dtd/service-eca.xsd (original)
+++ ofbiz/trunk/framework/service/dtd/service-eca.xsd Thu Jan  4 13:56:22 2007
@@ -30,7 +30,10 @@
                     <xs:element ref="condition-field"/>
                     <xs:element ref="condition-service"/>
                 </xs:choice>
-                <xs:element maxOccurs="unbounded" ref="action"/>
+                <xs:choice minOccurs="1" maxOccurs="unbounded">
+                    <xs:element ref="set"/>
+                    <xs:element ref="action"/>
+                </xs:choice>                
             </xs:sequence>
             <xs:attributeGroup ref="attlist.eca"/>
         </xs:complexType>
@@ -111,6 +114,32 @@
             </xs:simpleType>
         </xs:attribute>
         <xs:attribute type="xs:string" name="format"/>
+    </xs:attributeGroup>
+    <xs:element name="set">
+        <xs:complexType>
+            <xs:attributeGroup ref="attlist.set"/>
+        </xs:complexType>
+    </xs:element>
+    <xs:attributeGroup name="attlist.set">
+        <xs:attribute type="xs:string" name="field-name" use="required"/>
+        <xs:attribute type="xs:string" name="env-name"/>
+        <xs:attribute type="xs:string" name="value"/>
+        <xs:attribute type="xs:string" name="format">
+            <xs:simpleType>
+                <xs:restriction base="xs:token">
+                    <xs:enumeration value="append"/>                    
+                    <xs:enumeration value="to-upper"/>
+                    <xs:enumeration value="to-lower"/>
+                    <xs:enumeration value="hash-code"/>
+                    <xs:enumeration value="long"/>
+                    <xs:enumeration value="double"/>
+                    <xs:enumeration value="upper-first-char"/>
+                    <xs:enumeration value="lower-first-char"/>
+                    <xs:enumeration value="db-to-java"/>
+                    <xs:enumeration value="java-to-db"/>
+                </xs:restriction>
+            </xs:simpleType>
+        </xs:attribute>
     </xs:attributeGroup>
     <xs:element name="condition-field">
         <xs:complexType>

Modified: ofbiz/trunk/framework/service/dtd/services.xsd
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/dtd/services.xsd?view=diff&rev=492770&r1=492769&r2=492770
==============================================================================
--- ofbiz/trunk/framework/service/dtd/services.xsd (original)
+++ ofbiz/trunk/framework/service/dtd/services.xsd Thu Jan  4 13:56:22 2007
@@ -48,8 +48,8 @@
     <xs:attributeGroup name="attlist.service">
         <xs:attribute type="xs:string" name="name" use="required"/>
         <xs:attribute type="xs:string" name="engine" use="required"/>
-        <xs:attribute type="xs:string" name="location" use="required"/>
-        <xs:attribute type="xs:string" name="invoke" use="required"/>
+        <xs:attribute type="xs:string" name="location"/>
+        <xs:attribute type="xs:string" name="invoke"/>
         <xs:attribute name="auth" default="false">
             <xs:simpleType>
                 <xs:restriction base="xs:token">

Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaAction.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaAction.java?view=diff&rev=492770&r1=492769&r2=492770
==============================================================================
--- ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaAction.java (original)
+++ ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaAction.java Thu Jan  4 13:56:22 2007
@@ -15,10 +15,7 @@
  */
 package org.ofbiz.service.eca;
 
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import javax.transaction.xa.XAException;
 
 import org.ofbiz.base.util.UtilValidate;
@@ -56,6 +53,7 @@
         this.serviceMode = action.getAttribute("mode");
         this.runAsUser = action.getAttribute("runAsUser");
         this.resultMapName = action.getAttribute("result-map-name");
+        
         // default is true, so anything but false is true
         this.resultToContext = !"false".equals(action.getAttribute("result-to-context"));
         this.ignoreFailure = !"false".equals(action.getAttribute("ignore-failure"));

Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaRule.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaRule.java?view=diff&rev=492770&r1=492769&r2=492770
==============================================================================
--- ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaRule.java (original)
+++ ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaRule.java Thu Jan  4 13:56:22 2007
@@ -26,6 +26,7 @@
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.UtilXml;
 import org.w3c.dom.Element;
+import javolution.util.FastMap;
 
 /**
  * ServiceEcaRule
@@ -40,6 +41,7 @@
     protected boolean runOnError = false;
     protected List conditions = new LinkedList();
     protected List actions = new LinkedList();
+    protected List sets = new LinkedList();
     protected boolean enabled = true;
 
     protected ServiceEcaRule() {}
@@ -70,6 +72,13 @@
 
         if (Debug.verboseOn()) Debug.logVerbose("Conditions: " + conditions, module);
 
+        List setList = UtilXml.childElementList(eca, "set");
+        Iterator si = setList.iterator();
+        while (si.hasNext()) {
+            Element setElement = (Element) si.next();
+            sets.add(new ServiceEcaSetField(setElement));
+        }
+
         List actList = UtilXml.childElementList(eca, "action");
         Iterator ai = actList.iterator();
         while (ai.hasNext()) {
@@ -106,6 +115,14 @@
             }
         }
 
+        // prepare the internal field setters
+        Iterator i = sets.iterator();
+        while (i.hasNext()) {
+            ServiceEcaSetField sf = (ServiceEcaSetField) i.next();
+            sf.eval(context);
+        }        
+
+        // if all conditions are true, eval the actions
         if (allCondTrue) {
             Iterator a = actions.iterator();
             boolean allOkay = true;

Added: ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaSetField.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaSetField.java?view=auto&rev=492770
==============================================================================
--- ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaSetField.java (added)
+++ ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaSetField.java Thu Jan  4 13:56:22 2007
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2001-2007 The Apache Software Foundation
+ *
+ * Licensed 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.service.eca;
+
+import org.w3c.dom.Element;
+import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.base.util.Debug;
+import org.ofbiz.entity.model.ModelUtil;
+
+import java.util.Map;
+
+import javolution.util.FastMap;
+
+/**
+ * ServiceEcaSetField
+ */
+public class ServiceEcaSetField {
+
+    public static final String module = ServiceEcaSetField.class.getName();
+
+    protected String fieldName = null;
+    protected String envName = null;
+    protected String value = null;
+    protected String format = null;
+
+    public ServiceEcaSetField(Element set) {
+        this.fieldName = set.getAttribute("field-name");
+        this.envName = set.getAttribute("env-name");
+        this.value = set.getAttribute("value");
+        this.format = set.getAttribute("format");
+    }
+
+    public void eval(Map context) {
+        if (fieldName != null) {
+            // process the context changes
+            if (UtilValidate.isNotEmpty(value)) {
+                context.put(fieldName, this.format(value, context));
+            } else if (UtilValidate.isNotEmpty(envName) && context.get(envName) != null) {
+                context.put(fieldName, this.format((String) context.get(envName), context));
+            }
+        }
+    }
+
+    protected Object format(String s, Map c) {
+        if (UtilValidate.isEmpty(s)) {            
+            return s;
+        }
+
+        // string formats
+        if ("append".equalsIgnoreCase(format) && envName != null) {
+            String newStr = "";
+            if (c.get(envName) != null) {
+                newStr = newStr + c.get(envName);
+            }
+            newStr = newStr + s;
+            return newStr;
+        }
+        if ("to-upper".equalsIgnoreCase(format)) {
+            return s.toUpperCase();
+        }
+        if ("to-lower".equalsIgnoreCase(format)) {
+            return s.toLowerCase();
+        }
+        if ("hash-code".equalsIgnoreCase(format)) {
+            return new Integer(s.hashCode());
+        }
+        if ("long".equalsIgnoreCase(format)) {
+            return new Long(s);
+        }
+        if ("double".equalsIgnoreCase(format)) {
+            return new Double(s);
+        }
+
+        // entity formats
+        if ("upper-first-char".equalsIgnoreCase(format)) {
+            return ModelUtil.upperFirstChar(s);
+        }
+        if ("lower-first-char".equalsIgnoreCase(format)) {
+            return ModelUtil.lowerFirstChar(s);
+        }
+        if ("db-to-java".equalsIgnoreCase(format)) {
+            return ModelUtil.dbNameToVarName(s);
+        }
+        if ("java-to-db".equalsIgnoreCase(format)) {
+            return ModelUtil.javaNameToDbName(s);
+        }
+
+        Debug.logWarning("Format function not found [" + format + "] return string unchanged - " + s, module);
+        return s;
+    }
+}

Propchange: ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaSetField.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaSetField.java
------------------------------------------------------------------------------
    svn:keywords = "Date Rev Author URL Id"

Propchange: ofbiz/trunk/framework/service/src/org/ofbiz/service/eca/ServiceEcaSetField.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain