You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by jl...@apache.org on 2020/04/09 09:13:05 UTC

[ofbiz-framework] branch trunk updated: Implemented: Convert createPayment service from mini-lang to groovy DSL

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

jleroux 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 b884835  Implemented: Convert createPayment service from mini-lang to groovy DSL
b884835 is described below

commit b8848355d188ff91ce84e71b16433c9cf278b8d0
Author: Jacques Le Roux <ja...@les7arts.com>
AuthorDate: Thu Apr 9 10:23:56 2020 +0200

    Implemented: Convert createPayment service from mini-lang to groovy DSL
    
    (OFBIZ-11479)
    
    jleroux: I have only changed the MODULE name (and uppercased it)
    
    Thanks: Priya Sharma
---
 .../groovyScripts/payment/PaymentServices.groovy   | 55 +++++++++++++++++++
 .../minilang/payment/PaymentServices.xml           | 63 ----------------------
 .../accounting/servicedef/services_payment.xml     |  4 +-
 3 files changed, 57 insertions(+), 65 deletions(-)

diff --git a/applications/accounting/groovyScripts/payment/PaymentServices.groovy b/applications/accounting/groovyScripts/payment/PaymentServices.groovy
new file mode 100644
index 0000000..2a3493f
--- /dev/null
+++ b/applications/accounting/groovyScripts/payment/PaymentServices.groovy
@@ -0,0 +1,55 @@
+/*
+ * 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.Debug
+import org.apache.ofbiz.base.util.UtilDateTime
+import org.apache.ofbiz.base.util.UtilProperties
+import org.apache.ofbiz.base.util.UtilValidate
+import org.apache.ofbiz.entity.GenericValue
+
+MODULE = "PaymentServices.groovy"
+def createPayment() {
+    if (!security.hasEntityPermission("ACCOUNTING", "_CREATE", parameters.userLogin) && (!security.hasEntityPermission("PAY_INFO", "_CREATE", parameters.userLogin) && userLogin.partyId != parameters.partyIdFrom && userLogin.partyId != parameters.partyIdTo)) {
+        return error(UtilProperties.getResourceBundleMap("AccountingUiLabels", locale)?.AccountingCreatePaymentPermissionError)
+    }
+    GenericValue payment = delegator.makeValue("Payment")
+    payment.paymentId = parameters.paymentId ?: delegator.getNextSeqId("Payment")
+    paymentId = payment.paymentId
+    parameters.statusId = parameters.statusId ?: "PMNT_NOT_PAID"
+    if (UtilValidate.isNotEmpty(parameters.paymentMethodId)) {
+        GenericValue paymentMethod = from("PaymentMethod").where("paymentMethodId", parameters.paymentMethodId).queryOne()
+        if (parameters.paymentMethodTypeId != paymentMethod.paymentMethodTypeId) {
+            Debug.logInfo("Replacing passed payment method type [" + parameters.paymentMethodTypeId + "] with payment method type [" + paymentMethod.paymentMethodTypeId + "] for payment method [" + parameters.paymentMethodId +"]", MODULE)
+            parameters.paymentMethodTypeId = paymentMethod.paymentMethodTypeId
+        }
+    }
+    if (UtilValidate.isNotEmpty(parameters.paymentPreferenceId)) {
+        GenericValue orderPaymentPreference = from("OrderPaymentPreference").where("orderPaymentPreferenceId", parameters.paymentPreferenceId).queryOne()
+        parameters.paymentId = parameters.paymentId ?: orderPaymentPreference.paymentMethodId
+        parameters.paymentMethodTypeId = parameters.paymentMethodTypeId ?: orderPaymentPreference.paymentMethodTypeId
+    }
+    if (UtilValidate.isEmpty(parameters.paymentMethodTypeId)) {
+        return error(UtilProperties.getResourceBundleMap("AccountingUiLabels", locale)?.AccountingPaymentMethodIdPaymentMethodTypeIdNullError)
+    }
+    payment.setNonPKFields(parameters)
+    payment.effectiveDate = payment.effectiveDate ?: UtilDateTime.nowTimestamp()
+    delegator.create(payment)
+    Map result = success()
+    result.paymentId = paymentId
+    return result
+}
diff --git a/applications/accounting/minilang/payment/PaymentServices.xml b/applications/accounting/minilang/payment/PaymentServices.xml
index 19c2368..3d6f961 100644
--- a/applications/accounting/minilang/payment/PaymentServices.xml
+++ b/applications/accounting/minilang/payment/PaymentServices.xml
@@ -20,69 +20,6 @@ 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="createPayment" short-description="Create a Payment">
-        <if>
-            <condition>
-                <and>
-                    <not><if-has-permission permission="ACCOUNTING" action="_CREATE"/></not>
-                    <not><if-has-permission permission="PAY_INFO" action="_CREATE"/></not>
-                    <not><if-compare-field field="userLogin.partyId" to-field="parameters.partyIdFrom" operator="equals"/></not>
-                    <not><if-compare-field field="userLogin.partyId" to-field="parameters.partyIdTo" operator="equals"/></not>
-                </and>
-            </condition>
-            <then>
-                <add-error>
-                    <fail-property resource="AccountingUiLabels" property="AccountingCreatePaymentPermissionError"/>
-                </add-error>
-            </then>
-        </if>
-        <check-errors/>
-
-        <make-value entity-name="Payment" value-field="payment"/>
-        <if-empty field="parameters.paymentId">
-            <sequenced-id sequence-name="Payment" field="payment.paymentId"/>
-            <else>
-                <set field="payment.paymentId" from-field="parameters.paymentId"/>
-            </else>
-        </if-empty>
-        <field-to-result field="payment.paymentId" result-name="paymentId"/>
-
-        <if-empty field="parameters.statusId">
-            <set field="parameters.statusId" value="PMNT_NOT_PAID"/>
-        </if-empty>
-
-        <if-not-empty field="parameters.paymentMethodId">
-            <entity-one entity-name="PaymentMethod" value-field="paymentMethod">
-                <field-map field-name="paymentMethodId" from-field="parameters.paymentMethodId"/>
-            </entity-one>
-            <if-compare-field field="parameters.paymentMethodTypeId" operator="not-equals" to-field="paymentMethod.paymentMethodTypeId">
-                <log level="info" message="Replacing passed payment method type [${parameters.paymentMethodTypeId}] with payment method type [${paymentMethod.paymentMethodTypeId}] for payment method [${parameters.paymentMethodId}]"/>
-                <set field="parameters.paymentMethodTypeId" from-field="paymentMethod.paymentMethodTypeId"/>
-            </if-compare-field>
-        </if-not-empty>
-        <if-not-empty field="parameters.paymentPreferenceId">
-            <entity-one entity-name="OrderPaymentPreference" value-field="orderPaymentPreference">
-                <field-map field-name="orderPaymentPreferenceId" from-field="parameters.paymentPreferenceId"/>
-            </entity-one>
-            <if-empty field="parameters.paymentMethodId">
-                <set field="parameters.paymentMethodId" from-field="orderPaymentPreference.paymentMethodId"/>
-            </if-empty>
-            <if-empty field="parameters.paymentMethodTypeId">
-                <set field="parameters.paymentMethodTypeId" from-field="orderPaymentPreference.paymentMethodTypeId"/>
-            </if-empty>
-        </if-not-empty>
-        <if-empty field="parameters.paymentMethodTypeId">
-            <add-error>
-                <fail-property resource="AccountingUiLabels" property="AccountingPaymentMethodIdPaymentMethodTypeIdNullError"/>
-            </add-error>
-        </if-empty>
-
-        <set-nonpk-fields map="parameters" value-field="payment"/>
-        <if-empty field="payment.effectiveDate">
-            <now-timestamp field="payment.effectiveDate"/>
-        </if-empty>
-        <create-value value-field="payment"/>
-    </simple-method>
     <simple-method method-name="updatePayment" short-description="Update a Payment">
         <make-value entity-name="Payment" value-field="lookupPayment"/>
         <set-pk-fields value-field="lookupPayment" map="parameters"/>
diff --git a/applications/accounting/servicedef/services_payment.xml b/applications/accounting/servicedef/services_payment.xml
index f96eb37..ba0cacd 100644
--- a/applications/accounting/servicedef/services_payment.xml
+++ b/applications/accounting/servicedef/services_payment.xml
@@ -25,8 +25,8 @@ under the License.
     <version>1.0</version>
 
     <!-- Payment services -->
-    <service name="createPayment" engine="simple" default-entity-name="Payment"
-        location="component://accounting/minilang/payment/PaymentServices.xml" invoke="createPayment" auth="true">
+    <service name="createPayment" engine="groovy" default-entity-name="Payment"
+        location="component://accounting/groovyScripts/payment/PaymentServices.groovy" invoke="createPayment" auth="true">
         <description>Create a Payment.  If a paymentMethodId is supplied, paymentMethodTypeId is gotten from paymentMethod.  Otherwise, it must be supplied.  If no
         paymentMethodTypeId and no paymentMethodId is supplied, then an error will be returned. </description>
         <auto-attributes include="nonpk" mode="IN" optional="true"/>