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

nmalin 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 a19807e136 Improved: Convert RequirementServices.xml mini lang to 
groovy (OFBIZ-12745)
a19807e136 is described below

commit a19807e13609cc6f2e0724dbe4ce8c30bad325a1
Author: Nicolas Malin <nicolas.ma...@nereide.fr>
AuthorDate: Mon Jan 23 18:39:24 2023 +0100

    Improved: Convert RequirementServices.xml mini lang to groovy (OFBIZ-12745)
    
    This conversion convert three services :
     * deleteRequirementAndRelated
     * autoAssignRequirementToSupplier
     * createTransferFromRequirement
---
 .../requirement/RequirementServices.groovy         | 102 +++++++++++++++++++++
 .../minilang/requirement/RequirementServices.xml   |  88 ------------------
 .../order/servicedef/services_requirement.xml      |  15 +--
 3 files changed, 111 insertions(+), 94 deletions(-)

diff --git 
a/applications/order/groovyScripts/requirement/RequirementServices.groovy 
b/applications/order/groovyScripts/requirement/RequirementServices.groovy
new file mode 100644
index 0000000000..9d7b273536
--- /dev/null
+++ b/applications/order/groovyScripts/requirement/RequirementServices.groovy
@@ -0,0 +1,102 @@
+/*
+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.requirement
+
+import org.apache.ofbiz.base.util.UtilDateTime
+import org.apache.ofbiz.entity.GenericValue
+import org.apache.ofbiz.entity.condition.EntityCondition
+import org.apache.ofbiz.entity.condition.EntityConditionBuilder
+import org.apache.ofbiz.entity.util.EntityQuery
+
+import java.sql.Timestamp
+
+/**
+ * Delete a requirement after deleting related entity records
+ */
+def deleteRequirementAndRelated() {
+    GenericValue requirement = from("Requirement").where(parameters).queryOne()
+    if (requirement) {
+        requirement.removeRelated("RequirementAttribute")
+        requirement.removeRelated("RequirementRole")
+        requirement.removeRelated("RequirementStatus")
+        requirement.removeRelated("RequirementCustRequest")
+        requirement.remove()
+        return success()
+    }
+    return error("Entity value not found with name: requirement Method = 
deleteRequirementAndRelated")
+}
+
+/**
+ * If the requirement is a product requirement (purchasing) try to assign it 
to the primary supplier
+ */
+def autoAssignRequirementToSupplier() {
+    GenericValue requirement = from("Requirement").where(parameters).queryOne()
+    if (requirement) {
+        if (requirement.requirementTypeId == "PRODUCT_REQUIREMENT"
+                && requirement.productId
+                && requirement.quantity) {
+            EntityCondition condition = new EntityConditionBuilder().AND() {
+                EQUALS(productId: requirement.productId)
+                LESS_THAN_EQUAL_TO(minimumOrderQuantity: requirement.quantity)
+            }
+            EntityQuery supplierProductsQuery = 
from("SupplierProduct").where(condition).orderBy("lastPrice", 
"supplierPrefOrderId")
+            if (requirement.requiredByDate) {
+                supplierProductsQuery.filterByDate((Timestamp) 
requirement.requiredByDate, "availableFromDate", "availableThruDate")
+            }
+            GenericValue supplierProduct = supplierProductsQuery.queryFirst()
+            if (supplierProduct?.partyId) {
+                delegator.createOrStore("RequirementRole", [requirementId: 
requirement.requirementId,
+                                                            partyId      : 
supplierProduct.partyId,
+                                                            roleTypeId   : 
"SUPPLIER",
+                                                            fromDate     : 
UtilDateTime.nowTimestamp()])
+            }
+        }
+        return success()
+    }
+    return error("Entity value not found with name: requirement Method = 
autoAssignRequirementToSupplier")
+}
+
+/**
+ * Create the inventory transfers required to fulfill the requirement
+ */
+def createTransferFromRequirement() {
+    GenericValue requirement = from("Requirement").where(parameters).queryOne()
+    if (!requirement) {
+        return error("Entity value not found with name: requirement Method = 
createTransferFromRequirement")
+    }
+    try {
+        Map serviceResult = run service: "createInventoryTransfersForProduct",
+                with: [productId   : requirement.productId,
+                       facilityId  : parameters.fromFacilityId,
+                       facilityIdTo: requirement.facilityId,
+                       quantity    : requirement.quantity,
+                       sendDate    : requirement.requiredByDate]
+        BigDecimal quantityNotTransferred = 
serviceResult.quantityNotTransferred
+        if (quantityNotTransferred > 0) {
+            // we create a new requirement for the quantity not transferred 
(because not available)
+            run service: "createRequirement", with: [*       : 
requirement.getAllFields(),
+                                                     quantity: 
quantityNotTransferred]
+        }
+        run service: "updateRequirement", with: [requirementId: 
requirement.requirementId,
+                                                 statusId     : "REQ_ORDERED"]
+    } catch (Exception e) {
+        return error("Failed to create the requirement with " + e.toString())
+    }
+}
+
diff --git a/applications/order/minilang/requirement/RequirementServices.xml 
b/applications/order/minilang/requirement/RequirementServices.xml
deleted file mode 100644
index ec650b5a9e..0000000000
--- a/applications/order/minilang/requirement/RequirementServices.xml
+++ /dev/null
@@ -1,88 +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="deleteRequirementAndRelated" 
short-description="Delete a requirement after deleting related entity records">
-        <entity-one entity-name="Requirement" value-field="requirement" 
auto-field-map="true"/>
-        <check-errors/>
-        <remove-related value-field="requirement" 
relation-name="RequirementRole"/>
-        <remove-related value-field="requirement" 
relation-name="RequirementCustRequest"/>
-        <remove-value value-field="requirement"/>
-    </simple-method>
-    <simple-method method-name="autoAssignRequirementToSupplier" 
short-description="If the requirement is a product requirement (purchasing) try 
to assign it to the primary supplier">
-        <entity-one entity-name="Requirement" value-field="requirement" 
auto-field-map="true"/>
-        <check-errors/>
-        <if-compare field="requirement.requirementTypeId" operator="equals" 
value="PRODUCT_REQUIREMENT">
-            <if-not-empty field="requirement.productId">
-                <if-not-empty field="requirement.quantity">
-                <entity-condition entity-name="SupplierProduct" 
list="supplierProducts">
-                    <condition-list>
-                        <condition-expr field-name="productId" 
from-field="requirement.productId"/>
-                        <condition-expr field-name="minimumOrderQuantity" 
operator="less-equals" from-field="requirement.quantity"/>
-                    </condition-list>
-                    <order-by field-name="lastPrice"/>
-                    <order-by field-name="supplierPrefOrderId"/>
-                </entity-condition>
-                <filter-list-by-date list="supplierProducts" 
valid-date="requirement.requiredByDate" from-field-name="availableFromDate" 
thru-field-name="availableThruDate"/>
-                <first-from-list list="supplierProducts" 
entry="supplierProduct"/>
-                <if-not-empty field="supplierProduct.partyId">
-                    <make-value entity-name="RequirementRole" 
value-field="requirementSupplier"/>
-                    <set field="requirementSupplier.requirementId" 
from-field="requirement.requirementId"/>
-                    <set field="requirementSupplier.partyId" 
from-field="supplierProduct.partyId"/>
-                    <set field="requirementSupplier.roleTypeId" 
value="SUPPLIER"/>
-                    <now-timestamp field="requirementSupplier.fromDate"/>
-                    <create-value value-field="requirementSupplier"/>
-                </if-not-empty>
-                </if-not-empty>
-            </if-not-empty>
-        </if-compare>
-    </simple-method>
-    <simple-method method-name="createTransferFromRequirement" 
short-description="Create the inventory transfers required to fulfill the 
requirement.">
-        <check-permission permission="ORDERMGR" action="_CREATE">
-            <fail-property resource="OrderErrorUiLabels" 
property="OrderErrorCreatePermissionError"/>
-        </check-permission>
-        <check-errors/>
-        <entity-one entity-name="Requirement" value-field="requirement" 
auto-field-map="true"/>
-        <check-errors/>
-        <set field="inputMap.productId" from-field="requirement.productId"/>
-        <set field="inputMap.facilityId" 
from-field="parameters.fromFacilityId"/>
-        <set field="inputMap.facilityIdTo" 
from-field="requirement.facilityId"/>
-        <set field="inputMap.quantity" from-field="requirement.quantity"/>
-        <set field="inputMap.sendDate" 
from-field="requirement.requiredByDate"/>
-        <call-service service-name="createInventoryTransfersForProduct" 
in-map-name="inputMap">
-            <result-to-field result-name="quantityNotTransferred" 
field="quantityNotTransferred"/>
-        </call-service>
-        <check-errors/>
-        <if-compare field="quantityNotTransferred" value="0.0" 
operator="greater" type="BigDecimal">
-            <!-- we create a new requirement for the quantity not trasferred 
(because not available) -->
-            <clear-field field="inputMap"/>
-            <set-service-fields service-name="createRequirement" 
map="requirement" to-map="inputMap"/>
-            <set field="inputMap.quantity" 
from-field="quantityNotTransferred"/>
-            <call-service service-name="createRequirement" 
in-map-name="inputMap"/>
-            <check-errors/>
-        </if-compare>
-        <check-errors/>
-        <clear-field field="inputMap"/>
-        <set field="inputMap.requirementId" 
from-field="requirement.requirementId"/>
-        <set field="inputMap.statusId" value="REQ_ORDERED"/>
-        <call-service service-name="updateRequirement" in-map-name="inputMap"/>
-    </simple-method>
-</simple-methods>
diff --git a/applications/order/servicedef/services_requirement.xml 
b/applications/order/servicedef/services_requirement.xml
index af64bd4f8d..ceb47a1f24 100644
--- a/applications/order/servicedef/services_requirement.xml
+++ b/applications/order/servicedef/services_requirement.xml
@@ -42,8 +42,8 @@ under the License.
         <description>Delete a requirement</description>
         <auto-attributes mode="IN" include="pk" optional="false"/>
     </service>
-    <service name="deleteRequirementAndRelated" engine="simple"
-            
location="component://order/minilang/requirement/RequirementServices.xml" 
invoke="deleteRequirementAndRelated">
+    <service name="deleteRequirementAndRelated" engine="groovy"
+            
location="component://order/groovyScripts/requirement/RequirementServices.groovy"
 invoke="deleteRequirementAndRelated">
         <description>Delete a requirement after deleting related entity 
records.</description>
         <attribute name="requirementId" type="String" mode="IN" 
optional="false"/>
     </service>
@@ -157,15 +157,18 @@ under the License.
         <attribute mode="IN" name="quantity" optional="true" 
type="BigDecimal"/>
     </service>
 
-    <service name="autoAssignRequirementToSupplier" engine="simple" auth="true"
-            
location="component://order/minilang/requirement/RequirementServices.xml" 
invoke="autoAssignRequirementToSupplier">
+    <service name="autoAssignRequirementToSupplier" engine="groovy" auth="true"
+             
location="component://order/groovyScripts/requirement/RequirementServices.groovy"
 invoke="autoAssignRequirementToSupplier">
         <description>If the requirement is a product requirement (purchasing) 
try to assign it to the primary supplier</description>
         <attribute name="requirementId" type="String" mode="IN" 
optional="false"/>
     </service>
 
-    <service name="createTransferFromRequirement" engine="simple" auth="true"
-            
location="component://order/minilang/requirement/RequirementServices.xml" 
invoke="createTransferFromRequirement">
+    <service name="createTransferFromRequirement" engine="groovy" auth="true"
+             
location="component://order/groovyScripts/requirement/RequirementServices.groovy"
 invoke="createTransferFromRequirement">
         <description>Create the inventory transfers required to fulfill the 
requirement.</description>
+        <required-permissions join-type="AND">
+            <check-permission permission="ORDERMGR" action="_CREATE"/>
+        </required-permissions>
         <attribute mode="IN" name="requirementId" optional="false" 
type="String"/>
         <attribute mode="IN" name="fromFacilityId" optional="false" 
type="String"/>
         <attribute mode="IN" name="quantity" optional="true" 
type="BigDecimal"/>

Reply via email to