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

mleila 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 7b24e2baa5 Improved: display Run Service forms with dynamic fields 
(OFBIZ-12869) (#675)
7b24e2baa5 is described below

commit 7b24e2baa5010e588d40c5f537788441451df31b
Author: MLeila <mle...@apache.org>
AuthorDate: Fri Mar 15 08:28:24 2024 +0100

    Improved: display Run Service forms with dynamic fields (OFBIZ-12869) (#675)
    
    With this PR the run service forms will display service parameters
    fields dynamically (for example a field for a Timestamp parameter will
    be displayed as a datetime picker)
---
 .../webtools/service/SetServiceParameters.groovy   | 137 +++++++++++++++++++++
 .../template/service/SetServiceParameter.ftl       |   2 +-
 .../template/service/SetServiceParameterSync.ftl   |   2 +-
 framework/webtools/widget/ServiceScreens.xml       |   4 +-
 4 files changed, 141 insertions(+), 4 deletions(-)

diff --git 
a/framework/webtools/src/main/groovy/org/apache/ofbiz/webtools/service/SetServiceParameters.groovy
 
b/framework/webtools/src/main/groovy/org/apache/ofbiz/webtools/service/SetServiceParameters.groovy
new file mode 100644
index 0000000000..7f5e44634b
--- /dev/null
+++ 
b/framework/webtools/src/main/groovy/org/apache/ofbiz/webtools/service/SetServiceParameters.groovy
@@ -0,0 +1,137 @@
+/*
+ * 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.webtools.service
+
+import org.apache.ofbiz.base.util.Debug
+import org.apache.ofbiz.base.util.UtilProperties
+import org.apache.ofbiz.base.util.UtilXml
+import org.apache.ofbiz.entity.Delegator
+import org.apache.ofbiz.entity.GenericEntityException
+import org.apache.ofbiz.entity.model.ModelEntity
+import org.apache.ofbiz.entity.model.ModelField
+import org.apache.ofbiz.service.ModelParam
+import org.apache.ofbiz.service.ModelService
+import org.apache.ofbiz.service.config.ServiceConfigUtil
+import org.apache.ofbiz.widget.model.FormFactory
+import org.apache.ofbiz.widget.model.ModelForm
+import org.apache.ofbiz.widget.model.ModelFormFieldBuilder
+import org.apache.ofbiz.widget.renderer.macro.MacroFormRenderer
+import org.w3c.dom.Document
+
+
+savedSyncResult = null
+if (session.getAttribute('_SAVED_SYNC_RESULT_') != null) {
+    savedSyncResult = session.getAttribute('_SAVED_SYNC_RESULT_')
+}
+
+serviceName = parameters.SERVICE_NAME
+context.POOL_NAME = 
ServiceConfigUtil.getServiceEngine().getThreadPool().getSendToPool()
+
+scheduleOptions = []
+serviceParameters = []
+e = request.getParameterNames()
+while (e.hasMoreElements()) {
+    paramName = e.nextElement()
+    paramValue = parameters[paramName]
+    scheduleOptions.add([name: paramName, value: paramValue])
+}
+
+context.scheduleOptions = scheduleOptions
+
+if (serviceName) {
+    dctx = dispatcher.getDispatchContext()
+    ModelService modelService = null
+    try {
+        modelService = dctx.getModelService(serviceName)
+    } catch (Exception exc) {
+        context.errorMessageList = [exc.getMessage()]
+    }
+
+    context.uiLabelMap = UtilProperties.getResourceBundleMap('CommonUiLabels', 
locale)
+    String formRendererLocationTheme = 
context.visualTheme.getModelTheme().getFormRendererLocation('screen')
+    MacroFormRenderer renderer = new 
MacroFormRenderer(formRendererLocationTheme, request, response)
+    String dynamicServiceForm = """<?xml version="1.0" 
encoding="UTF-8"?><forms><form name="scheduleForm" type="single"/></forms>"""
+    Document dynamicServiceFormXml = 
UtilXml.readXmlDocument(dynamicServiceForm, true, true)
+    Map<String, ModelForm> modelFormMap = 
FormFactory.readFormDocument(dynamicServiceFormXml, null, context.visualTheme, 
dispatcher.getDispatchContext(), null)
+    ModelForm form
+    if (modelFormMap) {
+        Map.Entry<String, ModelForm> entry = 
modelFormMap.entrySet().iterator().next()
+        form = entry.getValue()
+    }
+
+    if (modelService != null) {
+        modelService.getInParamNames().each { paramName ->
+            ModelParam modelParam = modelService.getParam(paramName)
+            if (modelParam.internal) {
+                return
+            }
+            serviceParam = null
+            if (savedSyncResult?.get(modelParam.name)) {
+                serviceParam = [name: modelParam.name, type: modelParam.type, 
optional: modelParam.optional ? 'Y' : 'N',
+                                defaultValue: modelParam.defaultValue, value: 
savedSyncResult.get(modelParam.name)]
+            } else {
+                serviceParam = [name: modelParam.name, type: modelParam.type, 
optional: modelParam.optional ? 'Y' : 'N', defaultValue: 
modelParam.defaultValue]
+            }
+            serviceParam.field = prepareServiceParamFieldHtml(delegator, 
modelParam, form, context, renderer, modelService)
+
+            serviceParameters.add(serviceParam)
+        }
+    }
+}
+context.serviceParameters = serviceParameters
+
+
+private String prepareServiceParamFieldHtml(Delegator delegator, ModelParam 
modelParam, ModelForm form, Map context, MacroFormRenderer renderer, 
ModelService modelService) {
+    Writer writer = new StringWriter()
+    ModelFormFieldBuilder builder = new ModelFormFieldBuilder()
+    boolean isEntityField = false
+    if (modelParam.getEntityName() && modelParam.getFieldName()) {
+        try {
+            ModelEntity modelEntity = 
delegator.getModelEntity(modelParam.getEntityName())
+            ModelField modelField = 
modelEntity.getField(modelParam.getFieldName())
+            if (modelField != null) {
+                isEntityField = true
+                prepareEntityFieldBuilder(builder, modelField, modelEntity)
+            }
+        } catch (GenericEntityException e) {
+            Debug.logError(e, "SetServiceParameters.groovy")
+        }
+    }
+    if (!isEntityField) prepareServiceFieldBuilder(builder, modelParam, 
modelService)
+    builder.setModelForm(form)
+    builder.setAttributeName(modelParam.getName())
+    builder.setTitle(modelParam.getFormLabel())
+    builder.setRequiredField(!modelParam.isOptional())
+    builder.build().renderFieldString(writer, context, renderer)
+    return writer.toString()
+}
+
+private prepareEntityFieldBuilder(ModelFormFieldBuilder builder, ModelField 
modelField, ModelEntity modelEntity) {
+    builder.setName(modelField.getName())
+    builder.setFieldName(modelField.getName())
+    builder.setEntityName(modelEntity.getEntityName())
+    builder.induceFieldInfoFromEntityField(modelEntity, modelField, 'edit')
+}
+
+private prepareServiceFieldBuilder(ModelFormFieldBuilder builder, ModelParam 
modelParam, ModelService modelService) {
+    builder.setName(modelParam.getName())
+    builder.setFieldName(modelParam.getName())
+    builder.setServiceName(modelService.getName())
+    builder.induceFieldInfoFromServiceParam(modelService, modelParam, 'edit')
+}
diff --git a/framework/webtools/template/service/SetServiceParameter.ftl 
b/framework/webtools/template/service/SetServiceParameter.ftl
index 7c9ee40eb6..3ac351dceb 100644
--- a/framework/webtools/template/service/SetServiceParameter.ftl
+++ b/framework/webtools/template/service/SetServiceParameter.ftl
@@ -27,7 +27,7 @@ under the License.
       <tr>
         <td class="label">${serviceParameter.name} 
(${serviceParameter.type})</td>
         <td>
-          <input type="text" size="20" name="${serviceParameter.name}" 
value="${serviceParameter.value!}" <#if "N" == 
serviceParameter.optional>class="required"</#if>/>
+          ${StringUtil.wrapString(serviceParameter.field!)}
           <#if "N" == serviceParameter.optional><span 
class="tooltip">${uiLabelMap.CommonRequired}</span></#if>
           <#if 
serviceParameter.defaultValue?has_content>${uiLabelMap.WebtoolsServiceDefault} 
${serviceParameter.defaultValue}</#if>
         </td>
diff --git a/framework/webtools/template/service/SetServiceParameterSync.ftl 
b/framework/webtools/template/service/SetServiceParameterSync.ftl
index d219459925..d120823392 100644
--- a/framework/webtools/template/service/SetServiceParameterSync.ftl
+++ b/framework/webtools/template/service/SetServiceParameterSync.ftl
@@ -27,7 +27,7 @@ under the License.
       <tr>
         <td class="label">${serviceParameter.name} 
(${serviceParameter.type})</td>
         <td>
-          <input type="text" size="20" name="${serviceParameter.name}" 
value="<#if 
serviceParameter.value??>${serviceParameter.value?string}</#if>"<#if "N" == 
serviceParameter.optional> class="required"</#if>/>
+          ${StringUtil.wrapString(serviceParameter.field!)}
           <#if "N" == serviceParameter.optional><span 
class="tooltip">${uiLabelMap.CommonRequired}</span></#if>
           <#if 
serviceParameter.defaultValue?has_content>${uiLabelMap.WebtoolsServiceDefault} 
${serviceParameter.defaultValue?string}</#if>
           <#if serviceParameter_index == 0>
diff --git a/framework/webtools/widget/ServiceScreens.xml 
b/framework/webtools/widget/ServiceScreens.xml
index fb47431c5e..373f6d2fa8 100644
--- a/framework/webtools/widget/ServiceScreens.xml
+++ b/framework/webtools/widget/ServiceScreens.xml
@@ -166,7 +166,7 @@ under the License.
             <actions>
                 <set field="titleProperty" value="PageTitleScheduleJob"/>
                 <set field="tabButtonItem" value="scheduleJob"/>
-                <script 
location="component://webtools/src/main/groovy/org/apache/ofbiz/webtools/service/ScheduleJob.groovy"/>
+                <script 
location="component://webtools/src/main/groovy/org/apache/ofbiz/webtools/service/SetServiceParameters.groovy"/>
             </actions>
             <widgets>
                 <decorator-screen name="CommonServiceDecorator" 
location="${parameters.mainDecoratorLocation}">
@@ -188,7 +188,7 @@ under the License.
                 <set field="titleProperty" value="PageTitleScheduleJob"/>
                 <set field="headerItem" value="services"/>
                 <set field="tabButtonItem" value="runService"/>
-                <script 
location="component://webtools/src/main/groovy/org/apache/ofbiz/webtools/service/ScheduleJob.groovy"/>
+                <script 
location="component://webtools/src/main/groovy/org/apache/ofbiz/webtools/service/SetServiceParameters.groovy"/>
             </actions>
             <widgets>
                 <decorator-screen name="CommonServiceDecorator" 
location="${parameters.mainDecoratorLocation}">

Reply via email to