This is an automated email from the ASF dual-hosted git repository.

pgil pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git

commit eba4157c59bcc0c34c4414e3667333177225c239
Author: Gil Portenseigne <[email protected]>
AuthorDate: Fri Feb 21 16:26:02 2020 +0100

    Improved: Convert OrderServices.xml mini-lang to groovyDSL : getNextOrderId
    
    (OFBIZ-9984)
    Thanks Julien for your old patch, it has been improved.
    Add a new integration test for the service 'getNextOrderId'
---
 .../order/groovyScripts/order/OrderServices.groovy | 77 +++++++++++++++++++++
 .../order/minilang/order/OrderServices.xml         | 51 --------------
 applications/order/servicedef/services.xml         |  4 +-
 .../org/apache/ofbiz/order/test/OrderTest.java     | 78 ++++++++++++++++++++++
 applications/order/testdef/OrderTest.xml           |  3 +
 5 files changed, 160 insertions(+), 53 deletions(-)

diff --git a/applications/order/groovyScripts/order/OrderServices.groovy 
b/applications/order/groovyScripts/order/OrderServices.groovy
new file mode 100644
index 0000000..323b8a8
--- /dev/null
+++ b/applications/order/groovyScripts/order/OrderServices.groovy
@@ -0,0 +1,77 @@
+/*
+ * 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.
+ */
+
+
+import org.apache.ofbiz.base.util.UtilDateTime
+import org.apache.ofbiz.entity.GenericValue
+import org.apache.ofbiz.entity.condition.EntityConditionBuilder
+
+import java.sql.Timestamp
+
+/**
+ * Service to get the next OrderId
+ */
+def getNextOrderId() {
+    GenericValue partyAcctgPreference
+    GenericValue customMethod
+    String customMethodName
+
+    partyAcctgPreference = 
from('PartyAcctgPreference').where(context).queryOne()
+    logInfo "In getNextOrderId partyId is [$parameters.partyId], 
partyAcctgPreference: $partyAcctgPreference"
+
+    if (partyAcctgPreference) {
+        customMethod = partyAcctgPreference.getRelatedOne('OrderCustomMethod', 
true)
+    } else {
+        logWarning "Acctg preference not defined for partyId 
[$parameters.partyId]"
+    }
+
+    if (customMethod) {
+        customMethodName = customMethod.customMethodName
+    } else if (partyAcctgPreference.oldOrderSequenceEnumId == 'ODRSQ_ENF_SEQ') 
{
+        customMethodName = 'orderSequence_enforced'
+    }
+
+    if (customMethodName) {
+        Map customMethodMap = [*: parameters]
+        Map result = run service: customMethodName, with: customMethodMap
+        orderIdTemp = result.orderId
+    } else {
+        logInfo 'In getNextOrderId sequence by Standard'
+        // default to the default sequencing: ODRSQ_STANDARD
+        orderIdTemp = parameters.orderId ?: 
delegator.getNextSeqId('OrderHeader')
+    }
+
+    GenericValue productStore = null
+    if (parameters.productStoreId) {
+        productStore = from('ProductStore').where(context).queryOne()
+    }
+
+    // use orderIdTemp along with the orderIdPrefix to create the real ID
+    String orderId = ''
+    if (productStore) orderId += productStore.orderNumberPrefix
+    if (partyAcctgPreference) orderId += partyAcctgPreference.orderIdPrefix
+    orderId += orderIdTemp.toString()
+
+    Map result = success()
+    result.orderId = orderId
+
+    return result
+}
+
+}
diff --git a/applications/order/minilang/order/OrderServices.xml 
b/applications/order/minilang/order/OrderServices.xml
index 39bccb5..d357c0a 100644
--- a/applications/order/minilang/order/OrderServices.xml
+++ b/applications/order/minilang/order/OrderServices.xml
@@ -427,57 +427,6 @@ under the License.
         </iterate>
     </simple-method>
 
-    <simple-method method-name="getNextOrderId" short-description="Get Next 
orderId">
-        <!-- try to find PartyAcctgPreference for parameters.partyId, see if 
we need any special order number sequencing -->
-        <entity-one entity-name="PartyAcctgPreference" 
value-field="partyAcctgPreference"/>
-        <log level="info" message="In getNextOrderId partyId is 
[${parameters.partyId}], partyAcctgPreference: ${partyAcctgPreference}"/>
- 
-        <if-not-empty field="partyAcctgPreference">
-           <get-related-one relation-name="OrderCustomMethod" 
value-field="partyAcctgPreference" to-value-field="customMethod"/>
-           <else>
-               <log level="warning" message="Acctg preference not defined for 
partyId [${parameters.partyId}]"/>
-           </else>
-        </if-not-empty>
-
-        <if-not-empty field="customMethod">
-            <set field="customMethodName" 
from-field="customMethod.customMethodName"/>
-            <else><!-- retreive service from deprecated enumeration -->
-                <if-compare operator="equals" value="ODRSQ_ENF_SEQ" 
field="partyAcctgPreference.oldOrderSequenceEnumId">
-                    <set field="customMethodName" 
value="orderSequence_enforced"/>
-                </if-compare>
-            </else>
-        </if-not-empty>
-
-        <if-not-empty field="customMethodName">
-            <set-service-fields service-name="${customMethodName}" 
map="parameters" to-map="customMethodMap"/>
-            <set field="customMethodMap.partyAcctgPreference" 
from-field="partyAcctgPreference"/>
-            <call-service service-name="${customMethodName}" 
in-map-name="customMethodMap">
-               <result-to-field result-name="orderId" field="orderIdTemp"/>
-            </call-service>
-            <else>
-                <!-- <log level="info" message="In getNextOrderId sequence by 
Standard"/> -->
-                <!-- default to the default sequencing: ODRSQ_STANDARD -->
-                <set from-field="parameters.orderId" field="orderIdTemp"/>
-                <if-empty field="orderIdTemp">
-                    <sequenced-id sequence-name="OrderHeader" 
field="orderIdTemp"/>
-                    <else>
-                        <!-- check the provided ID -->
-                        <check-id field="orderIdTemp"/>
-                        <check-errors/>
-                    </else>
-                </if-empty>
-            </else>
-        </if-not-empty>
-        
-        <if-not-empty field="parameters.productStoreId">
-            <entity-one entity-name="ProductStore" value-field="productStore"/>
-        </if-not-empty>
-
-        <!-- use orderIdTemp along with the orderIdPrefix to create the real 
ID -->
-        <set field="orderId" 
value="${productStore.orderNumberPrefix}${partyAcctgPreference.orderIdPrefix}${str:toString(orderIdTemp)}"/>
-        <field-to-result field="orderId" result-name="orderId"/>
-    </simple-method>
-
     <simple-method method-name="orderSequence_enforced" 
short-description="Enforced Sequence (no gaps, per organization)">
         <log level="info" message="In getNextOrderId sequence enum Enforced"/> 
         <set field="partyAcctgPreference" 
from-field="parameters.partyAcctgPreference"/>
diff --git a/applications/order/servicedef/services.xml 
b/applications/order/servicedef/services.xml
index 0771de3..25d68c0 100644
--- a/applications/order/servicedef/services.xml
+++ b/applications/order/servicedef/services.xml
@@ -623,8 +623,8 @@ under the License.
         <attribute name="screenLocation" type="String" mode="IN" 
optional="false"/>
     </service>
 
-    <service name="getNextOrderId" engine="simple"
-        location="component://order/minilang/order/OrderServices.xml" 
invoke="getNextOrderId">
+    <service name="getNextOrderId" engine="groovy"
+        location="component://order/groovyScripts/order/OrderServices.groovy" 
invoke="getNextOrderId">
         <description>Get the Next Order ID According to Settings on the 
PartyAcctgPreference Entity for the given Party</description>
         <implements service="storeOrder" optional="true"/>
         <attribute name="partyId" type="String" mode="IN" optional="false"/>
diff --git 
a/applications/order/src/main/java/org/apache/ofbiz/order/test/OrderTest.java 
b/applications/order/src/main/java/org/apache/ofbiz/order/test/OrderTest.java
new file mode 100644
index 0000000..5295d75
--- /dev/null
+++ 
b/applications/order/src/main/java/org/apache/ofbiz/order/test/OrderTest.java
@@ -0,0 +1,78 @@
+/*******************************************************************************
+ * 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.apache.ofbiz.order.test;
+
+import org.apache.ofbiz.base.util.Debug;
+import org.apache.ofbiz.entity.GenericValue;
+import org.apache.ofbiz.entity.util.EntityQuery;
+import org.apache.ofbiz.service.ServiceUtil;
+import org.apache.ofbiz.service.testtools.OFBizTestCase;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class OrderTest extends OFBizTestCase {
+    public static final String module = OFBizTestCase.class.getName();
+
+    protected GenericValue userLogin = null;
+
+    public OrderTest(String name) {
+        super(name);
+    }
+
+    public void testAdminGetNextOrderSeqId() throws Exception {
+        Map <String, Object> ctx = new HashMap<>();
+        ctx.put("partyId", "admin"); //party with no AcctgPref prefix
+        Map <String, Object> resp = dispatcher.runSync("getNextOrderId", ctx);
+        if (ServiceUtil.isError(resp)) {
+            Debug.logError(ServiceUtil.getErrorMessage(resp), module);
+            return;
+        }
+        String orderId = (String) resp.get("orderId");
+        assertNotNull(orderId);
+        assertTrue(orderId.matches("\\d{5,}"));
+    }
+
+    public void testCompanyGetNextOrderSeqId() throws Exception {
+        Map <String, Object> ctx = new HashMap<>();
+        ctx.put("partyId", "Company"); //party with AcctgPref prefix : CO
+        Map <String, Object> resp = dispatcher.runSync("getNextOrderId", ctx);
+        if (ServiceUtil.isError(resp)) {
+            Debug.logError(ServiceUtil.getErrorMessage(resp), module);
+            return;
+        }
+        String orderId = (String) resp.get("orderId");
+        assertNotNull(orderId);
+        assertTrue(orderId.startsWith("CO"));
+    }
+
+    public void testCompleteGetNextOrderSeqId() throws Exception {
+        Map <String, Object> ctx = new HashMap<>();
+        ctx.put("partyId", "Company"); //party with AcctgPref prefix : CO
+        ctx.put("productStoreId", "9000"); // prefix WS
+        Map <String, Object> resp = dispatcher.runSync("getNextOrderId", ctx);
+        if (ServiceUtil.isError(resp)) {
+            Debug.logError(ServiceUtil.getErrorMessage(resp), module);
+            return;
+        }
+        String orderId = (String) resp.get("orderId");
+        assertNotNull(orderId);
+        assertTrue(orderId.startsWith("WSCO"));
+    }
+}
diff --git a/applications/order/testdef/OrderTest.xml 
b/applications/order/testdef/OrderTest.xml
index e21a330..e55150a 100644
--- a/applications/order/testdef/OrderTest.xml
+++ b/applications/order/testdef/OrderTest.xml
@@ -24,6 +24,9 @@ under the License.
     <test-case case-name="order-tests-data-load">
         <entity-xml action="load" 
entity-xml-url="component://order/testdef/data/OrderTestData.xml"/>
     </test-case>
+    <test-case case-name="order-test">
+        <junit-test-suite class-name="org.apache.ofbiz.order.test.OrderTest"/>
+    </test-case>
     <test-case case-name="purchaseOrder-test">
         <junit-test-suite 
class-name="org.apache.ofbiz.order.test.PurchaseOrderTest"/>
     </test-case>

Reply via email to