[ofbiz-framework] branch trunk updated: Improved: Converted OrderDeliveryServices.xml minilang to groovy (OFBIZ-11461)

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

[ofbiz-framework] branch trunk updated: Improved: Converted OrderDeliveryServices.xml minilang to groovy (OFBIZ-11461)

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

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


The following commit(s) were added to refs/heads/trunk by this push:
     new 11634ae  Improved: Converted OrderDeliveryServices.xml minilang to groovy (OFBIZ-11461)
11634ae is described below

commit 11634ae08ce8b3df0ea5ad36edbb0d9fab9fd9cc
Author: Sebastian Berg <[hidden email]>
AuthorDate: Tue Feb 2 08:11:25 2021 +0100

    Improved: Converted OrderDeliveryServices.xml minilang to groovy
    (OFBIZ-11461)
---
 .../order/OrderDeliveryServices.groovy             | 176 ++++++++++++++++++++
 .../order/minilang/order/OrderDeliveryServices.xml | 181 ---------------------
 applications/order/servicedef/services.xml         |  16 +-
 3 files changed, 184 insertions(+), 189 deletions(-)

diff --git a/applications/order/groovyScripts/order/OrderDeliveryServices.groovy b/applications/order/groovyScripts/order/OrderDeliveryServices.groovy
new file mode 100644
index 0000000..d705fef
--- /dev/null
+++ b/applications/order/groovyScripts/order/OrderDeliveryServices.groovy
@@ -0,0 +1,176 @@
+/*
+ * 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.UtilProperties
+import org.apache.ofbiz.entity.GenericValue
+import org.apache.ofbiz.service.ServiceUtil
+
+
+/**
+ * Creates a new Purchase Order Schedule
+ * @return
+ */
+def createOrderDeliverySchedule() {
+    String checkAction = "CREATE"
+    Map serviceResult = checkSupplierRelatedPermission("createOrderDeliverySchedule", checkAction, parameters.orderId)
+    if (!ServiceUtil.isSuccess(serviceResult)) {
+        return serviceResult
+    }
+
+    GenericValue schedule = makeValue("OrderDeliverySchedule")
+    schedule.setPKFields(parameters)
+    if(!schedule.orderItemSeqId) {
+        schedule.orderItemSeqId = "_NA_"
+    }
+    // only set statusId if hasScheduleAdminRelatedPermission
+    schedule.setNonPKFields(parameters)
+    if (!security.hasEntityPermission("ORDERMGR", ("_" + checkAction), parameters.userLogin)) {
+        // no permission, set to initial
+        schedule.statusId = "ODS_SUBMITTED"
+    }
+    schedule.create()
+    return success()
+}
+
+/**
+ * Updates an existing Purchase Order Schedule
+ * @return
+ */
+def updateOrderDeliverySchedule() {
+    // Verify the user is allowed to edit the fields
+    String checkAction = "UPDATE"
+    Map serviceResult = checkSupplierRelatedPermission("updateOrderDeliverySchedule", checkAction, parameters.orderId)
+    if (!ServiceUtil.isSuccess(serviceResult)) {
+        return serviceResult
+    }
+
+    // Lookup the existing schedule to modify
+    GenericValue schedule = from("OrderDeliverySchedule").where(parameters).queryOne()
+
+    // only set statusId if hasScheduleAdminRelatedPermission
+    String saveStatusId = schedule.statusId
+    schedule.setNonPKFields(parameters)
+    if (!security.hasEntityPermission("ORDERMGR", ("_" + checkAction), parameters.userLogin)) {
+        schedule.statusId = saveStatusId
+    }
+    // Update the actual schedule
+    schedule.store()
+    return success()
+}
+
+def sendOrderDeliveryScheduleNotification() {
+    String checkAction = "UPDATE"
+    Map serviceResult = checkSupplierRelatedPermission("sendOrderDeliveryScheduleNotification", checkAction, parameters.orderId)
+    if (!ServiceUtil.isSuccess(serviceResult)) {
+        return serviceResult
+    }
+    if (!parameters.orderItemSeqId) {
+        parameters.orderItemSeqId = "_NA_"
+    }
+    GenericValue orderDeliverySchedule = from("OrderDeliverySchedule").where(parameters).queryOne()
+    // find email address for currently logged in user, set as sendFrom
+    Map curUserPcmFindMap = [partyId: userLogin.partyId, contactMechTypeId: "EMAIL_ADDRESS"]
+    GenericValue curUserPartyAndContactMech = from("PartyAndContactMech").where(curUserPcmFindMap).queryFirst()
+    Map sendEmailMap = [sendFrom: ("," + curUserPartyAndContactMech.infoString)]
+
+    // find email addresses of all parties in SHIPMENT_CLERK roleTypeId, set as sendTo
+    Map shipmentClerkFindMap = [roleTypeId: "SHIPMENT_CLERK"]
+    List shipmentClerkRoles = from("PartyRole").where(shipmentClerkFindMap).queryList()
+    Map sendToPartyIdMap = [:]
+    for (GenericValue shipmentClerkRole : shipmentClerkRoles) {
+        sendToPartyIdMap[shipmentClerkRole.partyId] = shipmentClerkRole.partyId
+    }
+    // go through all send to parties and get email addresses
+    for (Map.Entry entry : sendToPartyIdMap) {
+        Map sendToPartyPcmFindMap = [partyId: entry.getKey(), contactMechTypeId: "EMAIL_ADDRESS"]
+        List sendToPartyPartyAndContactMechs = from("PartyAndContactMech").where(sendToPartyPcmFindMap).queryList()
+        for (GenericValue sendToPartyPartyAndContactMech : sendToPartyPartyAndContactMechs) {
+            StringBuilder newContact = new StringBuilder();
+            if (sendEmailMap.sendTo) {
+                newContact.append(sendEmailMap.sendTo)
+            }
+            newContact.append(",").append(sendToPartyPartyAndContactMech.infoString)
+            sendEmailMap.sendTo = newContact.toString()
+        }
+    }
+    // set subject, contentType, templateName, templateData
+    sendEmailMap.subject = "Delivery Information Updated for Order #" + orderDeliverySchedule.orderId
+    if (orderDeliverySchedule.orderItemSeqId != "_NA_") {
+        StringBuilder newSubject = new StringBuilder()
+        newSubject.append(sendEmailMap.subject)
+        newSubject.append(" Item #" + orderDeliverySchedule.orderItemSeqId)
+        sendEmailMap.subject = newSubject.toString()
+    }
+    sendEmailMap.contentType = "text/html"
+    sendEmailMap.templateName = "component://order/template/email/OrderDeliveryUpdatedNotice.ftl"
+    Map templateData = [orderDeliverySchedule: orderDeliverySchedule]
+    sendEmailMap.templateData = templateData
+
+    // call sendGenericNotificationEmail service, if enough information was found
+    logInfo("Sending generic notification email (if all info is in place): ${sendEmailMap}")
+    if (sendEmailMap.sendTo && sendEmailMap.sendFrom) {
+        run service:"sendGenericNotificationEmail", with: sendEmailMap
+    } else {
+        logError("Insufficient data to send notice email: ${sendEmailMap}")
+    }
+    return success()
+}
+
+/**
+ * Check Supplier Related Permission Service
+ * @return
+ */
+def checkSupplierRelatedOrderPermissionService() {
+    Map result = success()
+    Map serviceResult = checkSupplierRelatedPermission(parameters.callingMethodName, parameters.checkAction, parameters.orderId)
+    result.hasSupplierRelatedPermission = serviceResult.hasSupplierRelatedPermission
+    return result
+}
+
+// Should be called in-line to use its out parameter indicating whether the user has permission or not.
+
+/**
+ * Check Supplier Related Permission
+ * @return
+ */
+def checkSupplierRelatedPermission(String callingMethodName, String checkAction, String orderId) {
+    Map result = success()
+    if (!callingMethodName) {
+        callingMethodName = UtilProperties.getMessage("CommonUiLabels", "CommonPermissionThisOperation", locale)
+    }
+    if (!checkAction) {
+        checkAction = "UPDATE"
+    }
+    result.hasSupplierRelatedPermission = false
+    if (security.hasEntityPermission("ORDERMGR", ("_" + checkAction), userLogin)) {
+        result.hasSupplierRelatedPermission = true
+    } else {
+        Map lookupOrderRoleMap = [orderId: orderId, partyId: userLogin.partyId, roleTypeId: "SUPPLIER_AGENT"]
+        GenericValue permOrderRole = from("OrderRole").where(lookupOrderRoleMap).queryOne()
+        if (!permOrderRole) {
+            result = error("ERROR: You do not have permission to ${checkAction} Delivery Schedule Information; you must be associated with this order as a Supplier Agent or have the ORDERMGR_${checkAction} permission.")
+            result.hasSupplierRelatedPermission = false
+        } else {
+            result.hasSupplierRelatedPermission = true
+        }
+    }
+    logInfo("hasSupplierRelatedPermission is: " + result.hasSupplierRelatedPermission)
+    return result
+}
\ No newline at end of file
diff --git a/applications/order/minilang/order/OrderDeliveryServices.xml b/applications/order/minilang/order/OrderDeliveryServices.xml
deleted file mode 100644
index 90c384d..0000000
--- a/applications/order/minilang/order/OrderDeliveryServices.xml
+++ /dev/null
@@ -1,181 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" ?>
-<!--
-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.
--->
-
-<simple-methods xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-        xmlns="http://ofbiz.apache.org/Simple-Method" xsi:schemaLocation="http://ofbiz.apache.org/Simple-Method http://ofbiz.apache.org/dtds/simple-methods.xsd">
-    <simple-method method-name="createOrderDeliverySchedule" short-description="Creates a new Purchase Order Schedule">
-        <!-- Verify the user is allowed to set the fields -->
-        <set value="createOrderDeliverySchedule" field="callingMethodName"/>
-        <set value="CREATE" field="checkAction"/>
-        <call-simple-method method-name="checkSupplierRelatedPermission"/>
-        <check-errors/>
-
-        <make-value entity-name="OrderDeliverySchedule" value-field="schedule"/>
-        <set-pk-fields map="parameters" value-field="schedule"/>
-        <if-empty field="schedule.orderItemSeqId">
-            <set value="_NA_" field="schedule.orderItemSeqId"/>
-        </if-empty>
-
-        <!-- only set statusId if hasScheduleAdminRelatedPermission -->
-        <set-nonpk-fields map="parameters" value-field="schedule"/>
-        <if-has-permission permission="ORDERMGR" action="_${checkAction}">
-        <else>
-            <!-- no permission, set to initial -->
-            <set value="ODS_SUBMITTED" field="schedule.statusId"/>
-        </else>
-        </if-has-permission>
-
-        <create-value value-field="schedule"/>
-    </simple-method>
-    <simple-method method-name="updateOrderDeliverySchedule" short-description="Updates an existing Purchase Order Schedule">
-        <!-- Verify the user is allowed to edit the fields -->
-        <set value="updateOrderDeliverySchedule" field="callingMethodName"/>
-        <set value="UPDATE" field="checkAction"/>
-        <call-simple-method method-name="checkSupplierRelatedPermission"/>
-        <check-errors/>
-
-        <!-- Lookup the existing schedule to modify -->
-        <make-value entity-name="OrderDeliverySchedule" value-field="lookupPkMap"/>
-        <set-pk-fields map="parameters" value-field="lookupPkMap"/>
-        <find-by-primary-key entity-name="OrderDeliverySchedule" map="lookupPkMap" value-field="schedule"/>
-
-        <!-- only set statusId if hasScheduleAdminRelatedPermission -->
-        <set from-field="schedule.statusId" field="saveStatusId"/>
-        <set-nonpk-fields map="parameters" value-field="schedule"/>
-        <if-has-permission permission="ORDERMGR" action="_${checkAction}">
-        <else>
-            <!-- no permission, restore saved status -->
-            <set from-field="saveStatusId" field="schedule.statusId"/>
-        </else>
-        </if-has-permission>
-
-        <!-- Update the actual schedule -->
-        <store-value value-field="schedule"/>
-    </simple-method>
-    <simple-method method-name="sendOrderDeliveryScheduleNotification" short-description="Send Order Delivery Schedule Notification">
-        <set value="sendOrderDeliveryScheduleNotification" field="callingMethodName"/>
-        <set value="UPDATE" field="checkAction"/>
-        <call-simple-method method-name="checkSupplierRelatedPermission"/>
-        <check-errors/>
-
-        <if-empty field="parameters.orderItemSeqId">
-            <set field="parameters.orderItemSeqId" value="_NA_"/>
-        </if-empty>
-
-        <make-value entity-name="OrderDeliverySchedule" value-field="orderDeliverySchedule"/>
-        <set-pk-fields value-field="orderDeliverySchedule" map="parameters"/>
-        <find-by-primary-key map="orderDeliverySchedule" value-field="orderDeliverySchedule"/>
-
-        <!-- find email address for currently logged in user, set as sendFrom -->
-        <set field="curUserPcmFindMap.partyId" from-field="userLogin.partyId"/>
-        <set field="curUserPcmFindMap.contactMechTypeId" value="EMAIL_ADDRESS"/>
-        <find-by-and entity-name="PartyAndContactMech" map="curUserPcmFindMap" list="curUserPartyAndContactMechs"/>
-        <first-from-list list="curUserPartyAndContactMechs" entry="curUserPartyAndContactMech"/>
-        <set field="sendEmailMap.sendFrom" from-field="curUserPartyAndContactMech.infoString"/>
-
-        <!-- find email addresses of all parties in SHIPMENT_CLERK roleTypeId, set as sendTo -->
-        <set value="SHIPMENT_CLERK" field="shipmentClerkFindMap.roleTypeId"/>
-        <find-by-and entity-name="PartyRole" map="shipmentClerkFindMap" list="shipmentClerkRoles"/>
-        <iterate list="shipmentClerkRoles" entry="shipmentClerkRole">
-            <set field="sendToPartyIdMap[shipmentClerkRole.partyId]" from-field="shipmentClerkRole.partyId"/>
-        </iterate>
-
-        <!-- go through all send to parties and get email addresses -->
-        <iterate-map key="sendToPartyId" value="sendToPartyIdValue" map="sendToPartyIdMap">
-            <set field="sendToPartyPcmFindMap.partyId" from-field="sendToPartyId"/>
-            <set field="sendToPartyPcmFindMap.contactMechTypeId" value="EMAIL_ADDRESS"/>
-            <find-by-and entity-name="PartyAndContactMech" map="sendToPartyPcmFindMap" list="sendToPartyPartyAndContactMechs"/>
-            <iterate list="sendToPartyPartyAndContactMechs" entry="sendToPartyPartyAndContactMech">
-                <string-append field="sendEmailMap.sendTo" string="${sendToPartyPartyAndContactMech.infoString}" prefix=","/>
-            </iterate>
-        </iterate-map>
-
-        <!-- set subject, contentType, templateName, templateData -->
-        <set field="sendEmailMap.subject" value="Delivery Information Updated for Order #${orderDeliverySchedule.orderId}"/>
-        <if-compare field="orderDeliverySchedule.orderItemSeqId" operator="not-equals" value="_NA_">
-            <string-append string=" Item #${orderDeliverySchedule.orderItemSeqId}" field="sendEmailMap.subject"/>
-        </if-compare>
-        <set field="sendEmailMap.contentType" value="text/html"/>
-        <set field="sendEmailMap.templateName" value="component://order/template/email/OrderDeliveryUpdatedNotice.ftl"/>
-        <set field="sendEmailMap.templateData.orderDeliverySchedule" from-field="orderDeliverySchedule"/>
-
-        <!-- call sendGenericNotificationEmail service, if enough information was found -->
-        <log level="info" message="Sending generic notification email (if all info is in place): ${sendEmailMap}"/>
-        <if>
-            <condition>
-                <and>
-                    <not><if-empty field="sendEmailMap.sendTo"/></not>
-                    <not><if-empty field="sendEmailMap.sendFrom"/></not>
-                </and>
-            </condition>
-            <then>
-                <call-service service-name="sendGenericNotificationEmail" in-map-name="sendEmailMap"/>
-            </then>
-            <else>
-                <log level="error" message="Insufficient data to send notice email: ${sendEmailMap}"/>
-            </else>
-        </if>
-    </simple-method>
-
-    <simple-method method-name="checkSupplierRelatedOrderPermissionService" short-description="Check Supplier Related Permission Service">
-        <set field="checkAction" from-field="parameters.checkAction"/>
-        <set field="callingMethodName" from-field="parameters.callingMethodName"/>
-        <call-simple-method method-name="checkSupplierRelatedPermission"/>
-        <field-to-result field="hasSupplierRelatedPermission"/>
-    </simple-method>
-
-    <!-- Should be called in-line to use its out parameter indicating whether the user has permission or not. -->
-    <simple-method method-name="checkSupplierRelatedPermission" short-description="Check Supplier Related Permission">
-        <if-empty field="callingMethodName">
-            <property-to-field resource="CommonUiLabels" property="CommonPermissionThisOperation" field="callingMethodName"/>
-        </if-empty>
-        <if-empty field="checkAction">
-            <set value="UPDATE" field="checkAction"/>
-        </if-empty>
-        <set field="hasSupplierRelatedPermission" value="false"/>
-        <if>
-            <condition>
-                <if-has-permission permission="ORDERMGR" action="_${checkAction}"/>
-            </condition>
-            <then>
-                <set field="hasSupplierRelatedPermission" value="true"/>
-            </then>
-            <else>
-                <set from-field="parameters.orderId" field="lookupOrderRoleMap.orderId"/>
-                <set from-field="userLogin.partyId" field="lookupOrderRoleMap.partyId"/>
-                <set value="SUPPLIER_AGENT" field="lookupOrderRoleMap.roleTypeId"/>
-                <find-by-primary-key entity-name="OrderRole" map="lookupOrderRoleMap" value-field="permOrderRole"/>
-                <if>
-                    <condition>
-                        <if-empty field="permOrderRole"/>
-                    </condition>
-                    <then>
-                        <set field="hasSupplierRelatedPermission" value="false"/>
-                        <string-to-list list="error_list" string="ERROR: You do not have permission to ${checkAction} Delivery Schedule Information; you must be associated with this order as a Supplier Agent or have the ORDERMGR_${checkAction} permission."/>
-                    </then>
-                    <else>
-                        <set field="hasSupplierRelatedPermission" value="true"/>
-                    </else>
-                </if>
-            </else>
-        </if>
-        <log level="info" message="hasSupplierRelatedPermission is: ${hasSupplierRelatedPermission}"/>
-    </simple-method>
-</simple-methods>
diff --git a/applications/order/servicedef/services.xml b/applications/order/servicedef/services.xml
index a974c0b..194fa61 100644
--- a/applications/order/servicedef/services.xml
+++ b/applications/order/servicedef/services.xml
@@ -714,21 +714,21 @@ under the License.
     </service>
 
     <!-- Order Delivery Services -->
-    <service name="createOrderDeliverySchedule" default-entity-name="OrderDeliverySchedule" engine="simple"
-            location="component://order/minilang/order/OrderDeliveryServices.xml" invoke="createOrderDeliverySchedule">
+    <service name="createOrderDeliverySchedule" default-entity-name="OrderDeliverySchedule" engine="groovy"
+            location="component://order/groovyScripts/order/OrderDeliveryServices.groovy" invoke="createOrderDeliverySchedule">
         <description>Creates a delivery schedule for the specified order</description>
         <auto-attributes include="pk" mode="IN" optional="false"/>
         <auto-attributes include="nonpk" mode="IN" optional="true"/>
         <override name="orderItemSeqId" optional="true"/>
     </service>
-    <service name="updateOrderDeliverySchedule" default-entity-name="OrderDeliverySchedule" engine="simple"
-            location="component://order/minilang/order/OrderDeliveryServices.xml" invoke="updateOrderDeliverySchedule">
+    <service name="updateOrderDeliverySchedule" default-entity-name="OrderDeliverySchedule" engine="groovy"
+            location="component://order/groovyScripts/order/OrderDeliveryServices.groovy" invoke="updateOrderDeliverySchedule">
         <description>Update an existing delivery schedule for a specified purchase order</description>
         <auto-attributes include="pk" mode="IN" optional="false"/>
         <auto-attributes include="nonpk" mode="IN" optional="true"/>
     </service>
-    <service name="sendOrderDeliveryScheduleNotification" engine="simple"
-            location="component://order/minilang/order/OrderDeliveryServices.xml" invoke="sendOrderDeliveryScheduleNotification">
+    <service name="sendOrderDeliveryScheduleNotification" engine="groovy"
+            location="component://order/groovyScripts/order/OrderDeliveryServices.groovy" invoke="sendOrderDeliveryScheduleNotification">
         <description>Send Order Delivery Schedule Notification</description>
         <attribute name="orderId" type="String" mode="IN" optional="false">
             <type-validate>
@@ -738,8 +738,8 @@ under the License.
         <attribute name="orderItemSeqId" type="String" mode="IN" optional="true"/>
     </service>
 
-    <service name="checkSupplierRelatedOrderPermission" engine="simple"
-            location="component://order/minilang/order/OrderDeliveryServices.xml" invoke="checkSupplierRelatedOrderPermissionService">
+    <service name="checkSupplierRelatedOrderPermission" engine="groovy"
+            location="component://order/groovyScripts/order/OrderDeliveryServices.groovy" invoke="checkSupplierRelatedOrderPermissionService">
         <description>Check Supplier Related Order Permission</description>
         <attribute name="orderId" type="String" mode="IN" optional="false">
             <type-validate>