[ofbiz-framework] branch trunk updated: Improved: Convert ProductFeatureServices.xml mini lang to groovy (OFBIZ-11439)

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: Convert ProductFeatureServices.xml mini lang to groovy (OFBIZ-11439)

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

commit 92963609dac2539837d38dd7b93f684b968106cd
Author: Nicolas Malin <[hidden email]>
AuthorDate: Fri Mar 6 11:36:17 2020 +0100

    Improved: Convert ProductFeatureServices.xml mini lang to groovy
    (OFBIZ-11439)
   
    Thanks to Sebastian Berg for providing the patch
---
 .../product/feature/ProductFeatureServices.groovy  |  88 +++++++++++++++++
 .../product/feature/ProductFeatureServices.xml     | 104 ---------------------
 .../product/servicedef/services_feature.xml        |  12 +--
 3 files changed, 94 insertions(+), 110 deletions(-)

diff --git a/applications/product/groovyScripts/product/feature/ProductFeatureServices.groovy b/applications/product/groovyScripts/product/feature/ProductFeatureServices.groovy
new file mode 100644
index 0000000..dc3b43c
--- /dev/null
+++ b/applications/product/groovyScripts/product/feature/ProductFeatureServices.groovy
@@ -0,0 +1,88 @@
+/*
+ * 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 java.util.regex.Pattern
+import org.apache.ofbiz.base.util.UtilProperties
+import org.apache.ofbiz.entity.GenericValue
+
+
+/**
+ * Apply Feature to Product using Feature Type and ID Code
+ * @return
+ */
+def applyFeatureToProductFromTypeAndCode() {
+    // find the ProductFeatures by type and id code
+    List productFeatures = from("ProductFeature")
+            .where(productFeatureTypeId: parameters.productFeatureTypeId,
+                    idCode: parameters.idCode)
+            .queryList()
+    for (GenericValue productFeature : productFeatures) {
+        Map applyFeatureContext = parameters
+        applyFeatureContext.productFeatureId = productFeature.productFeatureId
+        if (! applyFeatureContext.sequenceNum) {
+            applyFeatureContext.sequenceNum = productFeature.defaultSequenceNum
+        }
+        run service: "applyFeatureToProduct", with: applyFeatureContext
+    }
+    return success()
+}
+
+/**
+ * Create a Product Feature Type
+ * @return
+ */
+def createProductFeatureType() {
+    Map result = success()
+    if (!security.hasEntityPermission("CATALOG", "_CREATE", parameters.userLogin)) {
+        return error(UtilProperties.getMessage("ProductUiLabels",
+                "ProductCatalogCreatePermissionError", parameters.locale))
+    }
+    if (! parameters.productFeatureTypeId) {
+        parameters.productFeatureTypeId = delegator.getNextSeqId("ProductFeatureType")
+    } else if (!Pattern.matches('^[a-zA-Z_0-9]+$', parameters.productFeatureTypeId)) {
+        return error(UtilProperties.getMessage("ProductErrorUiLabels",
+                "ProductFeatureTypeIdMustContainsLettersAndDigits", parameters.locale))
+    }
+    GenericValue newEntity = makeValue("ProductFeatureType", parameters)
+    newEntity.create()
+    result.productFeatureTypeId = newEntity.productFeatureTypeId
+    return result
+}
+
+/**
+ * Create a ProductFeatureApplAttr
+ * @return
+ */
+def createProductFeatureApplAttr() {
+    if (!security.hasEntityPermission("CATALOG", "_CREATE", parameters.userLogin)) {
+        return error(UtilProperties.getMessage("ProductUiLabels",
+                "ProductCatalogCreatePermissionError", parameters.locale))
+    }
+    GenericValue newEntity = makeValue("ProductFeatureApplAttr", parameters)
+    if (! newEntity.fromDate) {
+        GenericValue productFeatureAppl = from("ProductFeatureAppl")
+                .where(productId: newEntity.productId, productFeatureId: newEntity.productFeatureId)
+                .filterByDate().orderBy("-fromDate").queryFirst()
+        if (productFeatureAppl) {
+            newEntity.fromDate = productFeatureAppl.fromDate
+        }
+    }
+    newEntity.create()
+    return success()
+}
\ No newline at end of file
diff --git a/applications/product/minilang/product/feature/ProductFeatureServices.xml b/applications/product/minilang/product/feature/ProductFeatureServices.xml
deleted file mode 100644
index 6810a6a..0000000
--- a/applications/product/minilang/product/feature/ProductFeatureServices.xml
+++ /dev/null
@@ -1,104 +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="applyFeatureToProductFromTypeAndCode" short-description="Apply Feature to Product using Feature Type and ID Code">
-        <!-- find the ProductFeatures by type and id code -->
-        <entity-and entity-name="ProductFeature" list="productFeatures">
-            <field-map field-name="productFeatureTypeId" from-field="parameters.productFeatureTypeId"/>
-            <field-map field-name="idCode" from-field="parameters.idCode"/>
-        </entity-and>
-
-        <iterate list="productFeatures" entry="productFeature">
-            <set-service-fields service-name="applyFeatureToProduct" map="parameters" to-map="applyFeatureContext"/>
-            <set field="applyFeatureContext.productFeatureId" from-field="productFeature.productFeatureId"/>
-
-            <if-empty field="applyFeatureContext.sequenceNum">
-                <set field="applyFeatureContext.sequenceNum" from-field="productFeature.defaultSequenceNum"/>
-            </if-empty>
-
-            <call-service service-name="applyFeatureToProduct" in-map-name="applyFeatureContext"/>
-        </iterate>
-    </simple-method>
-    <!-- Methods for ProductFeatureType -->
-    <simple-method method-name="createProductFeatureType" short-description="Create a Product Feature Type">
-        <check-permission permission="CATALOG" action="_CREATE">
-            <fail-property resource="ProductUiLabels" property="ProductCatalogCreatePermissionError"/>
-        </check-permission>
-        <check-errors/>
-
-        <if>
-            <condition>
-                <if-empty field="parameters.productFeatureTypeId" />
-            </condition>
-            <then>
-                <sequenced-id sequence-name="ProductFeatureType" field="parameters.productFeatureTypeId"/>
-            </then>
-        </if>
-        <check-errors/>
-
-        <if>
-            <condition>
-                <not>
-                    <if-regexp field="parameters.productFeatureTypeId" expr="^[a-zA-Z_0-9]+$"></if-regexp>
-                </not>
-            </condition>
-            <then>
-                <add-error>
-                    <fail-property resource="ProductErrorUiLabels" property="ProductFeatureTypeIdMustContainsLettersAndDigits"/>
-                </add-error>
-            </then>
-        </if>
-        <check-errors/>
-
-        <make-value entity-name="ProductFeatureType" value-field="newEntity"/>
-        <set-nonpk-fields map="parameters" value-field="newEntity"/>
-        <set-pk-fields map="parameters" value-field="newEntity"/>
-
-        <create-value value-field="newEntity"/>
-        <field-to-result field="newEntity.productFeatureTypeId" result-name="productFeatureTypeId"/>
-    </simple-method>
-    <!-- create a new ProductFeatureApplAttr -->
-    <simple-method method-name="createProductFeatureApplAttr" short-description="Create a ProductFeatureApplAttr">
-        <check-permission permission="CATALOG" action="_CREATE">
-            <fail-property resource="ProductUiLabels" property="ProductCatalogCreatePermissionError"/>
-        </check-permission>
-        <check-errors/>
-        <make-value entity-name="ProductFeatureApplAttr" value-field="newEntity"/>
-        <set-pk-fields map="parameters" value-field="newEntity"/>
-        <set-nonpk-fields map="parameters" value-field="newEntity"/>
-        <if-empty field="newEntity.fromDate">
-            <entity-condition entity-name="ProductFeatureAppl" list="productFeatureAppls" filter-by-date="true">
-                <condition-list combine="and">
-                    <condition-expr field-name="productId" from-field="newEntity.productId"/>
-                    <condition-expr field-name="productFeatureId" from-field="newEntity.productFeatureId"/>
-                </condition-list>
-                <order-by field-name="-fromDate"/>
-            </entity-condition>
-            <first-from-list list="productFeatureAppls" entry="productFeatureAppl"/>
-            <set field="newEntity.fromDate" from-field="productFeatureAppl.fromDate"/>
-        </if-empty>
-        <create-value value-field="newEntity"/>
-        <check-errors/>
-    </simple-method>
-
-</simple-methods>
diff --git a/applications/product/servicedef/services_feature.xml b/applications/product/servicedef/services_feature.xml
index f7e2320..722729f 100644
--- a/applications/product/servicedef/services_feature.xml
+++ b/applications/product/servicedef/services_feature.xml
@@ -77,8 +77,8 @@ under the License.
         <permission-service service-name="productGenericPermission" main-action="DELETE"/>
         <auto-attributes include="pk" mode="IN" optional="false"/>
     </service>
-    <service name="applyFeatureToProductFromTypeAndCode" engine="simple"
-                location="component://product/minilang/product/feature/ProductFeatureServices.xml" invoke="applyFeatureToProductFromTypeAndCode" auth="true">
+    <service name="applyFeatureToProductFromTypeAndCode" engine="groovy"
+                location="component://product/groovyScripts/product/feature/ProductFeatureServices.groovy" invoke="applyFeatureToProductFromTypeAndCode" auth="true">
         <description>Apply a ProductFeature to a Product</description>
         <permission-service service-name="checkProductRelatedPermission" main-action="CREATE"/>
         <attribute name="productId" type="String" mode="IN" optional="false"/>
@@ -177,8 +177,8 @@ under the License.
         <auto-attributes include="pk" mode="IN" optional="false"/>
     </service>
     <!-- ProductFeatureType Services -->
-    <service name="createProductFeatureType" default-entity-name="ProductFeatureType" engine="simple"
-                location="component://product/minilang/product/feature/ProductFeatureServices.xml" invoke="createProductFeatureType" auth="true">
+    <service name="createProductFeatureType" default-entity-name="ProductFeatureType" engine="groovy"
+                location="component://product/groovyScripts/product/feature/ProductFeatureServices.groovy" invoke="createProductFeatureType" auth="true">
         <description>Create a ProductFeatureType</description>
         <auto-attributes include="pk" mode="IN" optional="false"/>
         <auto-attributes include="nonpk" mode="IN" optional="true"/>
@@ -196,8 +196,8 @@ under the License.
         <auto-attributes include="pk" mode="IN" optional="false"/>
     </service>
     <!-- ProductFeatureApplAttr  -->
-    <service name="createProductFeatureApplAttr" default-entity-name="ProductFeatureApplAttr" engine="simple"
-                location="component://product/minilang/product/feature/ProductFeatureServices.xml" invoke="createProductFeatureApplAttr" auth="true">
+    <service name="createProductFeatureApplAttr" default-entity-name="ProductFeatureApplAttr" engine="groovy"
+                location="component://product/groovyScripts/product/feature/ProductFeatureServices.groovy" invoke="createProductFeatureApplAttr" auth="true">
         <description>Create a ProductFeatureApplAttr</description>
         <permission-service service-name="productGenericPermission" main-action="CREATE"/>
         <auto-attributes include="pk" mode="IN" optional="false"/>