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 4d0827503d Improved: Convert SubscriptionServices.xml service from 
mini-lang to groovy DSL (OFBIZ-12583)
4d0827503d is described below

commit 4d0827503d2693a65dcea20beef85a129f4e1517
Author: Nicolas Malin <nicolas.ma...@nereide.fr>
AuthorDate: Fri Apr 22 13:41:00 2022 +0200

    Improved: Convert SubscriptionServices.xml service from mini-lang to groovy 
DSL (OFBIZ-12583)
    
    Thanks to Leila Mekika for solve this issue
---
 .../subscription/SubscriptionServices.groovy       | 119 +++++++++++++++++++++
 .../product/subscription/SubscriptionServices.xml  | 117 --------------------
 .../product/servicedef/services_subscription.xml   |  20 ++--
 3 files changed, 129 insertions(+), 127 deletions(-)

diff --git 
a/applications/product/groovyScripts/product/subscription/SubscriptionServices.groovy
 
b/applications/product/groovyScripts/product/subscription/SubscriptionServices.groovy
new file mode 100644
index 0000000000..06e320ef7e
--- /dev/null
+++ 
b/applications/product/groovyScripts/product/subscription/SubscriptionServices.groovy
@@ -0,0 +1,119 @@
+/*
+* 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.entity.GenericValue
+
+
+/**
+ * Create a Subscription
+ * @return
+ */
+def createSubscription() {
+    GenericValue newEntity = makeValue('Subscription')
+    String subscriptionId = parameters.subscriptionId ?: 
delegator.getNextSeqId('Subscription')
+    newEntity.subscriptionId = subscriptionId
+
+    // lookup the product subscription resource (if exists)
+    if (parameters.subscriptionResourceId && parameters.productId) {
+        GenericValue resource = from('ProductSubscriptionResource')
+            .where(subscriptionResourceId: parameters.subscriptionResourceId,
+                   productId: parameters.productId )
+            .filterByDate()
+            .orderBy('-fromDate')
+            .queryFirst()
+        if (resource) {
+            newEntity.setNonPKFields(resource.getAllFields())
+        }
+    }
+
+    newEntity.setNonPKFields(parameters)
+    newEntity.create()
+
+    return success(subscriptionId: subscriptionId)
+}
+
+/**
+ * Check if a party has a subscription
+ * @return
+ */
+def isSubscribed() {
+    Map result = success()
+
+    Map serviceContext = [entityName: 'Subscription',
+        inputFields: parameters,
+        filterByDate: parameters.filterByDate ?: 'Y']
+    Map serviceResult = run service: 'performFindList', with: serviceContext
+
+    Boolean found = serviceResult.list
+    if (serviceResult.list) {
+        result.subscriptionId = serviceResult.list[0].subscriptionId
+    }
+    result.isSubscribed = found
+    return result
+}
+
+/**
+ * Get Subscription data
+ * @return
+ */
+def getSubscription() {
+    Map result = success()
+
+    GenericValue subscription = from('Subscription')
+        .where(parameters)
+        .queryOne()
+    result.subscriptionId = parameters.subscriptionId
+    if (subscription) result.subscription = subscription
+    return result
+}
+
+/**
+ * Create (when not exist) or update (when exist) a Subscription attribute
+ * @return
+ */
+def updateSubscriptionAttribute() {
+    GenericValue lookedUpValue = from('SubscriptionAttribute')
+        .where(parameters)
+        .queryOne()
+    if (lookedUpValue) {
+        lookedUpValue.setNonPKFields(parameters)
+        lookedUpValue.store()
+    } else {
+        GenericValue newEntity = makeValue('SubscriptionAttribute')
+        newEntity.setPKFields(parameters)
+        newEntity.setNonPKFields(parameters)
+        newEntity.create()
+    }
+    return success(subscriptionId: parameters.subscriptionId)
+}
+
+/**
+ * Subscription permission checking logic
+ * @return
+ */
+def subscriptionPermissionCheck() {
+    parameters.primaryPermission = "CATALOG"
+    Map result = run service: "genericBasePermissionCheck", with: parameters
+    // Backwards compatibility - check for non-existent CATALOG_READ permission
+    result.hasPermission = result.hasPermission ||
+            (parameters.mainAction == 'VIEW' &&
+                    security.hasPermission('CATALOG_READ', userLogin))
+    return result
+}
diff --git 
a/applications/product/minilang/product/subscription/SubscriptionServices.xml 
b/applications/product/minilang/product/subscription/SubscriptionServices.xml
deleted file mode 100644
index d640a78a46..0000000000
--- 
a/applications/product/minilang/product/subscription/SubscriptionServices.xml
+++ /dev/null
@@ -1,117 +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="createSubscription" short-description="Create 
a Subscription">
-        <make-value entity-name="Subscription" value-field="newEntity"/>
-        <if-empty field="parameters.subscriptionId">
-            <sequenced-id sequence-name="Subscription" 
field="newEntity.subscriptionId"/> <!-- get the next sequenced ID -->
-            <else>
-                <set field="newEntity.subscriptionId" 
from-field="parameters.subscriptionId"/>
-            </else>
-        </if-empty>
-        <field-to-result field="newEntity.subscriptionId" 
result-name="subscriptionId"/>
-
-        <!-- lookup the product subscription resource (if exists) -->
-        <if-not-empty field="parameters.subscriptionResourceId">
-            <if-not-empty field="parameters.productId">
-                <entity-and entity-name="ProductSubscriptionResource" 
list="resourceList" filter-by-date="true">
-                    <field-map field-name="subscriptionResourceId" 
from-field="parameters.subscriptionResourceId"/>
-                    <field-map field-name="productId" 
from-field="parameters.productId"/>
-                    <order-by field-name="-fromDate"/>
-                </entity-and>
-                <first-from-list list="resourceList" entry="resource"/>
-                <if-not-empty field="resource">
-                    <set-nonpk-fields map="resource" value-field="newEntity"/>
-                </if-not-empty>
-            </if-not-empty>
-        </if-not-empty>
-
-        <set-nonpk-fields map="parameters" value-field="newEntity"/>
-        <create-value value-field="newEntity"/>
-    </simple-method>
-
-    <simple-method method-name="isSubscribed" short-description="check if a 
party has a subscription">
-        <set field="pfInput.inputFields" from-field="parameters"/>
-        <set field="pfInput.entityName" value="Subscription"/>
-        <set field="pfInput.filterByDate" from-field="parameters.filterByDate" 
default-value="Y"/>
-
-        <call-service service-name="performFindList" in-map-name="pfInput">
-            <result-to-field result-name="list" field="pfResultList"/>
-        </call-service>
-
-        <if-empty field="pfResultList">
-            <set field="found" value="false" type="Boolean"/>
-            <else>
-                <set field="found" value="true" type="Boolean"/>
-                <first-from-list list="pfResultList" entry="subscription"/>
-                <field-to-result field="subscription.subscriptionId" 
result-name="subscriptionId"/>
-            </else>
-        </if-empty>
-
-        <field-to-result field="found" result-name="isSubscribed"/>
-    </simple-method>
-
-    <simple-method method-name="getSubscription" short-description="Get 
Subscription data">
-        <entity-one entity-name="Subscription" value-field="subscription"/>
-        <field-to-result field="parameters.subscriptionId" 
result-name="subscriptionId"/>
-        <if-not-empty field="subscription">
-            <field-to-result field="subscription" result-name="subscription"/>
-        </if-not-empty>
-    </simple-method>
-
-    <simple-method method-name="updateSubscriptionAttribute" 
short-description="Create (when not exist) or update (when exist) a 
Subscription attribute">
-        <field-to-result field="parameters.subscriptionId" 
result-name="subscriptionId"/>
-        <entity-one entity-name="SubscriptionAttribute" 
value-field="lookedUpValue"/>
-        <if-empty field="lookedUpValue">
-            <make-value entity-name="SubscriptionAttribute" 
value-field="newEntity"/>
-            <set-pk-fields map="parameters" value-field="newEntity"/>
-            <set-nonpk-fields map="parameters" value-field="newEntity"/>
-            <create-value value-field="newEntity"/>
-            <else>
-                <set-nonpk-fields map="parameters" 
value-field="lookedUpValue"/>
-                <store-value value-field="lookedUpValue"/>
-            </else>
-        </if-empty>
-    </simple-method>
-
-    <!-- ============== Permission Checking Service ============= -->
-    <simple-method method-name="subscriptionPermissionCheck" 
short-description="Subscription permission checking logic">
-        <set field="primaryPermission" value="CATALOG"/>
-        <call-simple-method method-name="genericBasePermissionCheck" 
xml-resource="component://common/minilang/permission/CommonPermissionServices.xml"/>
-        <!-- Backwards compatibility - check for non-existent CATALOG_READ 
permission -->
-        <if>
-            <condition>
-                <and>
-                    <if-compare field="hasPermission" value="false" 
operator="equals" type="Boolean"/>
-                    <if-compare field="mainAction" value="VIEW" 
operator="equals"/>
-                </and>
-            </condition>
-            <then>
-                <if-has-permission permission="CATALOG_READ">
-                    <set field="hasPermission" type="Boolean" value="true"/>
-                    <field-to-result field="hasPermission"/>
-                </if-has-permission>
-            </then>
-        </if>
-    </simple-method>
-
-</simple-methods>
diff --git a/applications/product/servicedef/services_subscription.xml 
b/applications/product/servicedef/services_subscription.xml
index 7dbadaeaa6..deddc18615 100644
--- a/applications/product/servicedef/services_subscription.xml
+++ b/applications/product/servicedef/services_subscription.xml
@@ -23,8 +23,8 @@ under the License.
     <description>Product Component Services for subscriptions entity 
(subscription entity not ContentRoles)</description>
     <vendor>OFBiz</vendor>
 
-    <service name="createSubscription" default-entity-name="Subscription" 
auth="true" engine="simple"
-        
location="component://product/minilang/product/subscription/SubscriptionServices.xml"
 invoke="createSubscription">
+    <service name="createSubscription" default-entity-name="Subscription" 
auth="true" engine="groovy"
+        
location="component://product/groovyScripts/product/subscription/SubscriptionServices.groovy"
 invoke="createSubscription">
         <description>Create a Subscription Record</description>
         <permission-service service-name="subscriptionPermissionCheck" 
main-action="CREATE"/>
         <auto-attributes include="pk" mode="INOUT" optional="true"/>
@@ -37,8 +37,8 @@ under the License.
         <auto-attributes include="nonpk" mode="IN" optional="true"/>
     </service>
 
-    <service name="isSubscribed" default-entity-name="Subscription" 
auth="true" engine="simple"
-        
location="component://product/minilang/product/subscription/SubscriptionServices.xml"
 invoke="isSubscribed">
+    <service name="isSubscribed" default-entity-name="Subscription" 
auth="true" engine="groovy"
+        
location="component://product/groovyScripts/product/subscription/SubscriptionServices.groovy"
 invoke="isSubscribed">
         <description>Check if a particular party has at this moment a 
subscription</description>
         <permission-service service-name="subscriptionPermissionCheck" 
main-action="VIEW"/>
         <auto-attributes include="all" mode="IN" optional="true"/>
@@ -48,8 +48,8 @@ under the License.
         <override name="partyId" optional="false" mode="IN"/>
     </service>
 
-    <service name="getSubscriptionEnt" default-entity-name="Subscription" 
auth="true" engine="simple"
-        
location="component://product/minilang/product/subscription/SubscriptionServices.xml"
 invoke="getSubscription">
+    <service name="getSubscriptionEnt" default-entity-name="Subscription" 
auth="true" engine="groovy"
+        
location="component://product/groovyScripts/product/subscription/SubscriptionServices.groovy"
 invoke="getSubscription">
         <description>Retrieve a single Subscription Entity Record</description>
         <permission-service service-name="subscriptionPermissionCheck" 
main-action="VIEW"/>
         <auto-attributes include="pk" mode="INOUT" optional="false"/>
@@ -130,8 +130,8 @@ under the License.
         <auto-attributes include="nonpk" mode="IN" optional="true"/>
         <auto-attributes include="pk" mode="INOUT" optional="false"/>
     </service>
-    <service name="updateSubscriptionAttribute" 
default-entity-name="SubscriptionAttribute" auth="true" engine="simple"
-             
location="component://product/minilang/product/subscription/SubscriptionServices.xml"
 invoke="updateSubscriptionAttribute">
+    <service name="updateSubscriptionAttribute" 
default-entity-name="SubscriptionAttribute" auth="true" engine="groovy"
+             
location="component://product/groovyScripts/product/subscription/SubscriptionServices.groovy"
 invoke="updateSubscriptionAttribute">
         <description>Create (when not exist) or update (when exist) a 
Subscription attribute</description>
         <permission-service service-name="subscriptionPermissionCheck" 
main-action="UPDATE"/>
         <attribute name="subscriptionId" type="String" mode="INOUT" 
optional="false">
@@ -162,8 +162,8 @@ under the License.
         <auto-attributes include="pk" mode="IN" optional="false"/>
     </service>
 
-    <service name="subscriptionPermissionCheck" engine="simple"
-        
location="component://product/minilang/product/subscription/SubscriptionServices.xml"
 invoke="subscriptionPermissionCheck" auth="true">
+    <service name="subscriptionPermissionCheck" engine="groovy"
+        
location="component://product/groovyScripts/product/subscription/SubscriptionServices.groovy"
 invoke="subscriptionPermissionCheck" auth="true">
         <description>Subscription Permission Checking Logic</description>
         <implements service="permissionInterface"/>
     </service>

Reply via email to