[ofbiz-framework] branch trunk updated: Improved: Convert createArticleContent service from mini-lang to groovy (#140)

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 createArticleContent service from mini-lang to groovy (#140)

jleroux@apache.org
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 6495dc7  Improved: Convert createArticleContent service from mini-lang to groovy (#140)
6495dc7 is described below

commit 6495dc7a917612a879fac8e5bb977eae71018c3a
Author: Priya Sharma <[hidden email]>
AuthorDate: Sun May 17 14:24:16 2020 +0530

    Improved: Convert createArticleContent service from mini-lang to groovy (#140)
   
    Deleted the old mini-lang service file
    Updated the service definitions with the new groovy methods
    Added the groovy implementation (with minor enhancement)
   
    (OFBIZ-11383)
   
    Co-authored-by: Priya Sharma <[hidden email]>
---
 .../groovyScripts/content/ContentServices.groovy   | 127 +++++++++++++++
 .../content/minilang/content/ContentServices.xml   | 178 ---------------------
 applications/content/servicedef/services.xml       |   4 +-
 3 files changed, 129 insertions(+), 180 deletions(-)

diff --git a/applications/content/groovyScripts/content/ContentServices.groovy b/applications/content/groovyScripts/content/ContentServices.groovy
index bf01b26..d860b19 100644
--- a/applications/content/groovyScripts/content/ContentServices.groovy
+++ b/applications/content/groovyScripts/content/ContentServices.groovy
@@ -174,3 +174,130 @@ def updateEmailContent() {
         run service: "updateElectronicText", with: [dataResourceId: parameters.htmlBodyDataResourceId, textData: parameters.htmlBody]
     }
 }
+
+def createArticleContent() {
+    // Post a new Content article Entry
+    String origContentAssocTypeId = parameters.contentAssocTypeId
+    String contentAssocTypeId = parameters.contentAssocTypeId
+    String ownerContentId = parameters.threadContentId
+    if ("PUBLISH_LINK".equals(origContentAssocTypeId)) {
+        ownerContentId = parameters.pubPtContentId
+    }
+    String contentIdFrom = parameters.contentIdFrom
+    String pubPtContentId = parameters.pubPtContentId
+    String textData = parameters.textData
+    String subDescript = null
+    String contentId = null
+    if (textData) {
+        int textDataLen = textData.length()
+        Debug.logInfo("textDataLen: " + textDataLen, module)
+        int descriptLen = 0
+        if (parameters.descriptLen) {
+            descriptLen = (int) parameters.descriptLen
+            Debug.logInfo("descriptLen: " + descriptLen, module)
+        }
+        int subStringLen = Math.min(descriptLen, textDataLen)
+        Debug.logInfo("subStringLen: " + subStringLen, module)
+        subDescript = textData.substring(0, subStringLen)
+        Debug.logInfo("subDescript: " + subDescript, module)
+    }
+    if ("PUBLISH_LINK".equals(contentAssocTypeId)) {
+        ownerContentId = pubPtContentId
+    }
+    //determine of we need to create complex template structure or simple content structure
+    if (parameters.uploadedFile && textData) {
+        Map createMain = [:]
+        createMain.dataResourceId = parameters.dataResourceId
+        createMain.contentAssocTypeId = parameters.contentAssocTypeId
+        createMain.contentName = parameters.contentName
+        createMain.description = subDescript
+        createMain.statusId = parameters.statusId
+        createMain.contentIdFrom = parameters.contentIdFrom
+        createMain.partyId = userLogin.partyId
+        createMain.ownerContentId = ownerContentId
+
+        createMain.dataTemplateTypeId = "SCREEN_COMBINED"
+        createMain.mapKey = "MAIN"
+        Map serviceResult = run service: "createContent", with: createMain
+        if (ServiceUtil.isSuccess(serviceResult)) {
+            contentId = serviceResult.contentId
+        }
+        // reset contentIdFrom to new contentId
+        contentAssocTypeId = "SUB_CONTENT"
+        contentIdFrom = contentId
+    }
+    if (parameters.uploadedFile) {
+        // create image data
+        Map createImage = [:]
+        createImage.dataResourceTypeId = "LOCAL_FILE"
+        createImage.dataTemplateTypeId = "NONE"
+        createImage.mapKey = "IMAGE"
+        createImage.ownerContentId = ownerContentId
+        createImage.contentName = parameters.contentName
+        createImage.description = subDescript
+        createImage.statusId = parameters.statusId
+        createImage.contentAssocTypeId = contentAssocTypeId
+        createImage.contentIdFrom = contentIdFrom
+        createImage.partyId = userLogin.partyId
+        createImage.uploadedFile = parameters.uploadedFile
+        createImage._uploadedFile_fileName = parameters._uploadedFile_fileName
+        createImage._uploadedFile_contentType = parameters._uploadedFile_contentType
+        Map serviceResult = run service: "createContentFromUploadedFile", with: createImage
+        String imageContentId = ServiceUtil.isSuccess(serviceResult)? serviceResult.contentId : null
+        if (!contentId) {
+            contentIdFrom = imageContentId
+            contentId = imageContentId
+            contentAssocTypeId = "SUB_CONTENT"
+        }
+    }
+    if (textData) {
+        Map createText = [:]
+        createText.dataResourceTypeId = "ELECTRONIC_TEXT"
+        createText.dataTemplateTypeId = "NONE"
+        createText.mapKey = "MAIN"
+        createText.ownerContentId = ownerContentId
+        createText.contentName = parameters.contentName
+        createText.description = subDescript
+        createText.statusId = parameters.statusId
+        createText.contentAssocTypeId = contentAssocTypeId
+        createText.textData = textData
+        createText.contentIdFrom = contentIdFrom
+        createText.partyId = userLogin.partyId
+        Debug.logInfo("calling createTextContent with map: " + createText, module)
+        Map serviceResult = run service: "createTextContent", with: createText
+        String textContentId = ServiceUtil.isSuccess(serviceResult)? serviceResult.contentId : null
+        if (!contentId) {
+            contentIdFrom = textContentId
+            contentId = textContentId
+            contentAssocTypeId = "SUB_CONTENT"
+        }
+    }
+    // we should have a primary (at least) contentId
+    if (contentId && parameters.summaryData) {
+        Map createSummary = [:]
+        createSummary.dataResourceTypeId = "ELECTRONIC_TEXT"
+        createSummary.dataTemplateTypeId = "NONE"
+        createSummary.mapKey = "SUMMARY"
+        createSummary.ownerContentId = ownerContentId
+        createSummary.contentName = parameters.contentName
+        createSummary.description = parameters.description
+        createSummary.statusId = parameters.statusId
+        createSummary.contentAssocTypeId = contentAssocTypeId
+        createSummary.textData = parameters.summaryData
+        createSummary.contentIdFrom = contentIdFrom
+        createSummary.partyId = userLogin.partyId
+        run service: "createTextContent", with:  createSummary
+    }
+    // If a response, still link it to the publish point
+    if ("RESPONSE".equals(origContentAssocTypeId)) {
+        Map contentAssocMap = [:]
+        contentAssocMap.contentId = pubPtContentId
+        contentAssocMap.contentIdTo = contentId
+        contentAssocMap.contentAssocTypeId = "RESPONSE"
+        Debug.logInfo("contentAssocMap: " + contentAssocMap, module)
+        run service: "createContentAssoc", with: contentAssocMap
+    }
+    Map result = success()
+    result.contentId = contentId
+    return result
+}
diff --git a/applications/content/minilang/content/ContentServices.xml b/applications/content/minilang/content/ContentServices.xml
index 8bbbe00..696d7ee 100644
--- a/applications/content/minilang/content/ContentServices.xml
+++ b/applications/content/minilang/content/ContentServices.xml
@@ -489,184 +489,6 @@
         <call-simple-method method-name="getContentAndDataResource"/>
     </simple-method>
 
-    <simple-method method-name="createArticleContent" short-description="Post a new Content article Entry">
-        <set field="contentAssocTypeId" from-field="parameters.contentAssocTypeId"/>
-        <set field="origContentAssocTypeId" from-field="parameters.contentAssocTypeId"/>
-        <set field="ownerContentId" from-field="parameters.threadContentId"/>
-        <if-compare field="origContentAssocTypeId" operator="equals" value="PUBLISH_LINK">
-            <set field="ownerContentId" from-field="parameters.pubPtContentId"/>
-        </if-compare>
-        <set field="contentIdFrom" from-field="parameters.contentIdFrom"/>
-        <set field="pubPtContentId" from-field="parameters.pubPtContentId"/>
-        <call-object-method method-name="length" obj-field="parameters.textData" ret-field="textDataLen"/>
-        <log level="info" message="textDataLen:${textDataLen}"/>
-        <property-to-field resource="forum" property="descriptLen" field="descriptLen"/>
-        <set field="descriptLen" from-field="descriptLen" type="Integer"/>
-        <log level="info" message="descriptLen:${descriptLen}"/>
-        <call-class-method method-name="min" class-name="java.lang.Math" ret-field="subStringLen">
-            <field field="textDataLen" type="int"/>
-            <field field="descriptLen" type="int"/>
-        </call-class-method>
-        <log level="info" message="subStringLen:${subStringLen}"/>
-        <set field="zeroValue" value="0" type="Integer"/>
-        <call-object-method method-name="substring" obj-field="parameters.textData" ret-field="subDescript">
-            <field field="zeroValue" type="int"/>
-            <field field="subStringLen" type="int"/>
-        </call-object-method>
-        <log level="info" message="subDescript:${subDescript}"/>
-        <if-compare field="contentAssocTypeId" operator="equals" value="PUBLISH_LINK">
-            <set field="ownerContentId" from-field="pubPtContentId"/>
-        </if-compare>
-
-        <!-- determine of we need to create complex template structure or simple content structure -->
-        <if>
-            <condition>
-                <and>
-                    <not>
-                        <if-empty field="parameters.uploadedFile"/>
-                    </not>
-                    <not>
-                        <if-empty field="parameters.textData"/>
-                    </not>
-                </and>
-            </condition>
-            <then>
-                <!-- complex template structure (image & text) -->
-                <set field="createMain.dataResourceId" from-field="parameters.dataResourceId"/>
-                <set field="createMain.contentAssocTypeId"  from-field="contentAssocTypeId"/>
-                <set field="createMain.contentName" from-field="parameters.contentName"/>
-                <set field="createMain.description" from-field="subDescript"/>
-                <set field="createMain.statusId" from-field="parameters.statusId"/>
-                <set field="createMain.contentIdFrom" from-field="contentIdFrom"/>
-                <set field="createMain.partyId" from-field="userLogin.partyId"/>
-                <set field="createMain.ownerContentId" from-field="ownerContentId"/>
-
-                <set field="createMain.dataTemplateTypeId" value="SCREEN_COMBINED"/>
-                <set field="createMain.mapKey" value="MAIN"/>
-
-                <call-service service-name="createContent" in-map-name="createMain">
-                    <result-to-field result-name="contentId" field="contentId"/>
-                </call-service>
-
-                <!-- reset contentIdFrom to new contentId -->
-                <set field="contentAssocTypeId" value="SUB_CONTENT"/>
-                <set field="contentIdFrom" from-field="contentId"/>
-            </then>
-        </if>
-
-        <if>
-            <condition>
-                <not>
-                    <if-empty field="parameters.uploadedFile"/>
-                </not>
-            </condition>
-            <then>
-                <!-- create image data -->
-                <set field="createImage.dataResourceTypeId" value="LOCAL_FILE"/>
-                <set field="createImage.dataTemplateTypeId" value="NONE"/>
-                <set field="createImage.mapKey" value="IMAGE"/>
-
-                <set field="createMain.ownerContentId" from-field="ownerContentId"/>
-                <set field="createImage.contentName" from-field="parameters.contentName"/>
-                <set field="createImage.description" from-field="subDescript"/>
-                <set field="createImage.statusId" from-field="parameters.statusId"/>
-                <set field="createImage.contentAssocTypeId"  from-field="contentAssocTypeId"/>
-                <set field="createImage.contentIdFrom" from-field="contentIdFrom"/>
-                <set field="createImage.partyId" from-field="userLogin.partyId"/>
-                <set field="createImage.uploadedFile" from-field="parameters.uploadedFile"/>
-                <set field="createImage._uploadedFile_fileName" from-field="parameters._uploadedFile_fileName"/>
-                <set field="createImage._uploadedFile_contentType" from-field="parameters._uploadedFile_contentType"/>
-
-                <call-service service-name="createContentFromUploadedFile" in-map-name="createImage">
-                    <result-to-field result-name="contentId" field="imageContentId"/>
-                </call-service>
-
-                <if-empty field="contentId">
-                    <set field="contentIdFrom" from-field="imageContentId"/>
-                    <set field="contentId" from-field="imageContentId"/>
-                    <set field="contentAssocTypeId" value="SUB_CONTENT"/>
-                </if-empty>
-            </then>
-        </if>
-
-        <if>
-            <condition>
-                <not>
-                    <if-empty field="parameters.textData"/>
-                </not>
-            </condition>
-            <then>
-                <!-- create text data -->
-                <set field="createText.dataResourceTypeId" value="ELECTRONIC_TEXT"/>
-                <set field="createText.dataTemplateTypeId" value="NONE"/>
-                <set field="createText.mapKey" value="MAIN"/>
-
-                <set field="createText.ownerContentId" from-field="ownerContentId"/>
-                <set field="createText.contentName" from-field="parameters.contentName"/>
-                <set field="createText.description" from-field="subDescript"/>
-                <set field="createText.statusId" from-field="parameters.statusId"/>
-                <set field="createText.contentAssocTypeId"  from-field="contentAssocTypeId"/>
-                <set field="createText.textData" from-field="parameters.textData"/>
-                <set field="createText.contentIdFrom" from-field="contentIdFrom"/>
-                <set field="createText.partyId" from-field="userLogin.partyId"/>
-
-                <log level="info" message="calling createTextContent with map: ${createText}"/>
-                <call-service service-name="createTextContent" in-map-name="createText">
-                    <result-to-field result-name="contentId" field="textContentId"/>
-                </call-service>
-
-                <if-empty field="contentId">
-                    <set field="contentIdFrom" from-field="textContentId"/>
-                    <set field="contentId" from-field="textContentId"/>
-                    <set field="contentAssocTypeId" value="SUB_CONTENT"/>
-                </if-empty>
-            </then>
-        </if>
-
-        <!-- we should have a primary (at least) contentId -->
-        <if>
-            <condition>
-                <and>
-                    <not>
-                        <if-empty field="contentId"/>
-                    </not>
-                    <not>
-                        <if-empty field="parameters.summaryData"/>
-                    </not>
-                </and>
-            </condition>
-            <then>
-                <!-- create the summary data -->
-                <set field="createSummary.dataResourceTypeId" value="ELECTRONIC_TEXT"/>
-                <set field="createSummary.dataTemplateTypeId" value="NONE"/>
-                <set field="createSummary.mapKey" value="SUMMARY"/>
-
-                <set field="createSummary.ownerContentId" from-field="ownerContentId"/>
-                <set field="createSummary.contentName" from-field="parameters.contentName"/>
-                <set field="createSummary.description" from-field="parameters.description"/>
-                <set field="createSummary.statusId" from-field="parameters.statusId"/>
-                <set field="createSummary.contentAssocTypeId"  from-field="contentAssocTypeId"/>
-                <set field="createSummary.textData" from-field="parameters.summaryData"/>
-                <set field="createSummary.contentIdFrom" from-field="contentIdFrom"/>
-                <set field="createSummary.partyId" from-field="userLogin.partyId"/>
-
-                <call-service service-name="createTextContent" in-map-name="createSummary"/>
-            </then>
-        </if>
-
-        <!-- If a response, still link it to the publish point -->
-        <if-compare field="origContentAssocTypeId" operator="equals" value="RESPONSE">
-            <set field="contentAssocMap.contentId" from-field="pubPtContentId"/>
-            <set field="contentAssocMap.contentIdTo" from-field="contentId"/>
-            <set field="contentAssocMap.contentAssocTypeId" value="RESPONSE"/>
-        <log level="info" message="contentAssocMap:${contentAssocMap.contentId}"/>
-             <call-service service-name="createContentAssoc" in-map-name="contentAssocMap"/>
-        </if-compare>
-
-
-        <field-to-result field="contentId"/>
-    </simple-method>
-
     <simple-method method-name="forceIndexContentKeywords" short-description="induce all the keywords of a content">
         <entity-one entity-name="Content" value-field="content"/>
         <call-class-method class-name="org.apache.ofbiz.content.content.ContentKeywordIndex" method-name="forceIndexKeywords">
diff --git a/applications/content/servicedef/services.xml b/applications/content/servicedef/services.xml
index f9440f0..43fede2 100644
--- a/applications/content/servicedef/services.xml
+++ b/applications/content/servicedef/services.xml
@@ -448,8 +448,8 @@
         <attribute mode="IN" name="fromDate" optional="true" type="Timestamp"/>
     </service>
 
-    <service name="createArticleContent" engine="simple" transaction-timeout="300" auth="true"
-            location="component://content/minilang/content/ContentServices.xml" invoke="createArticleContent">
+    <service name="createArticleContent" engine="groovy" transaction-timeout="300" auth="true"
+            location="component://content/groovyScripts/content/ContentServices.groovy" invoke="createArticleContent">
         <description>Creates content records for a blog entry</description>
         <required-permissions join-type="AND">
             <check-permission permission="CONTENTMGR" action="_CREATE"/>