Skip to content

Commit

Permalink
Improved: Improve webtools/control/FindGeneric
Browse files Browse the repository at this point in the history
(OFBIZ-11253)
Update the script to generate xml form to introduce new following features :

   * set equals as operator by default on pk instead of contains, when you use a pk on FindGeneric it's in general to resolve it on completed key.
   * add on tooltip the raw field name
   * add possibility to filter on internal field
   * add sort on column

During this work I also view that the ModelFormField contains an 'IF forest' to analyze each field. I rewrote it to consolidate the analyze by type.
  • Loading branch information
nmalin committed Nov 10, 2019
1 parent 61ade2a commit 2c58133
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 99 deletions.
76 changes: 44 additions & 32 deletions framework/webtools/groovyScripts/entity/FindGeneric.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
* under the License.
*/

import org.apache.ofbiz.base.util.Debug
import org.apache.ofbiz.base.util.UtilValidate
import org.apache.ofbiz.base.util.UtilProperties
import org.apache.ofbiz.base.util.UtilXml
import org.apache.ofbiz.entity.GenericEntityException
import org.apache.ofbiz.entity.model.ModelEntity
Expand All @@ -28,44 +25,50 @@ import org.apache.ofbiz.entity.model.ModelReader
import org.apache.ofbiz.widget.model.FormFactory
import org.apache.ofbiz.widget.model.ModelForm
import org.apache.ofbiz.widget.renderer.FormRenderer
import org.apache.ofbiz.widget.renderer.VisualTheme
import org.apache.ofbiz.widget.renderer.macro.MacroFormRenderer
import org.w3c.dom.Document

ModelEntity modelEntity = null
try {
modelEntity = delegator.getModelEntity(parameters.entityName)
} catch(GenericEntityException e) {
Debug.logError("The entityName " + parameters.entityName + " is not found", "FindGeneric.groovy")
logError("The entityName ${parameters.entityName} isn't found", "FindGeneric.groovy")
}

if (modelEntity) {
entityName = modelEntity.entityName
context.entityName = entityName
ModelReader entityModelReader = delegator.getModelReader()
//create the search form with auto-fields-entity
String dynamicAutoEntityFieldSearchForm = '<?xml version="1.0" encoding="UTF-8"?><forms xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://ofbiz.apache.org/Widget-Form" xsi:schemaLocation="http://ofbiz.apache.org/Widget-Form http://ofbiz.apache.org/dtds/widget-form.xsd">' +
'<form name="FindGeneric" type="single" target="FindGeneric?entityName='+ entityName +'">' +
'<auto-fields-entity entity-name="' + entityName + '" default-field-type="find"/>' +
'<field name="noConditionFind"><hidden value="Y"/></field>' +
'<field name="searchOptions_collapsed" ><hidden value="true"/></field>' +
'<field name="searchButton" title="' + UtilProperties.getMessage("CommonUiLabels", "FormFieldTitle_searchButton", locale) + '"><submit/></field>'
String dynamicAutoEntityFieldSearchForm = """<?xml version="1.0" encoding="UTF-8"?><forms xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://ofbiz.apache.org/Widget-Form" xsi:schemaLocation="http://ofbiz.apache.org/Widget-Form http://ofbiz.apache.org/dtds/widget-form.xsd">
<form name="FindGeneric" type="single" target="FindGeneric?entityName=${entityName}">
<auto-fields-entity entity-name="${entityName}" default-field-type="find" include-internal="true"/>
<field name="noConditionFind"><hidden value="Y"/></field>
<field name="searchOptions_collapsed" ><hidden value="true"/></field>
<field name="searchButton"><submit/></field>"""

//call modelEntity to complete information on the field type
modelEntity.getFieldsUnmodifiable().each {
modelField ->
if (! modelEntity.getAutomaticFieldNames().contains(modelField.name)) {
ModelFieldType type = delegator.getEntityFieldType(modelEntity, modelField.getType())
dynamicAutoEntityFieldSearchForm = dynamicAutoEntityFieldSearchForm +
'<field name="' + modelField.name + '" tooltip="' +
(modelField.getIsPk() ? "* " : " ") +
modelField.getType() + " (${type.getJavaType()} - ${type.getSqlType()})" +
'"/>'
dynamicAutoEntityFieldSearchForm +=
"<field name=\"${modelField.name}\" tooltip=\"${modelField.getName()}" +
(modelField.getIsPk() ? '* ': ' ') +
" / ${modelField.getType()} (${type.getJavaType()} - ${type.getSqlType()})\">"

//In general when your research some entity on the pk field, you check on element, so help by set as default equals comparison
if (modelField.getIsPk() && type.getJavaType() == 'String') {
dynamicAutoEntityFieldSearchForm += '<text-find default-option="equals"/>'
}
dynamicAutoEntityFieldSearchForm += '</field>'
}
}
dynamicAutoEntityFieldSearchForm = dynamicAutoEntityFieldSearchForm + '</form></forms>'
logVerbose(dynamicAutoEntityFieldSearchForm)
Document dynamicAutoEntityFieldSearchFormXml = UtilXml.readXmlDocument(dynamicAutoEntityFieldSearchForm, true, true)
Map<String, ModelForm> modelFormMap = FormFactory.readFormDocument(dynamicAutoEntityFieldSearchFormXml, entityModelReader, dispatcher.getDispatchContext(), entityName)
ModelForm modelForm
if (modelFormMap) {
Map.Entry<String, ModelForm> entry = modelFormMap.entrySet().iterator().next()
modelForm = entry.getValue()
Expand All @@ -79,22 +82,31 @@ if (modelEntity) {
context.dynamicAutoEntitySearchForm = writer

//prepare the result list from performFind
String dynamicAutoEntityFieldListForm = '<?xml version="1.0" encoding="UTF-8"?><forms xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://ofbiz.apache.org/Widget-Form" xsi:schemaLocation="http://ofbiz.apache.org/Widget-Form http://ofbiz.apache.org/dtds/widget-form.xsd">' +
'<form name="ListGeneric" type="list" target="FindGeneric" list-name="listIt" ' +
' odd-row-style="alternate-row" default-table-style="basic-table light-grid hover-bar">' +
'<actions><service service-name="performFind">' +
'<field-map field-name="inputFields" from-field="parameters"/>' +
'<field-map field-name="entityName" value="' + entityName + '"/>' +
'</service></actions>' +
'<auto-fields-entity entity-name="' + entityName + '" default-field-type="display" include-internal="true"/>' +
'<field name="entityName"><hidden value="' + entityName + '"/></field>' +
'<field name="viewGeneric" title=" "><hyperlink target="ViewGeneric" description="view">' +
' <auto-parameters-entity entity-name="' + entityName + '"/>' +
' <parameter param-name="entityName" value="' + entityName + '"/>' +
'</hyperlink></field>' +
'<sort-order><sort-field name="viewGeneric"/></sort-order>' +
'</form></forms>'
//Debug.logInfo(dynamicAutoEntityFieldForm, "")
String dynamicAutoEntityFieldListForm = """<?xml version="1.0" encoding="UTF-8"?><forms xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://ofbiz.apache.org/Widget-Form" xsi:schemaLocation="http://ofbiz.apache.org/Widget-Form http://ofbiz.apache.org/dtds/widget-form.xsd">
<form name="ListGeneric" type="list" target="FindGeneric" list-name="listIt"
odd-row-style="alternate-row" default-table-style="basic-table light-grid hover-bar" header-row-style="header-row-2">
<actions>
<service service-name="performFind">
<field-map field-name="inputFields" from-field="parameters"/>
<field-map field-name="entityName" value="${entityName}"/>
<field-map field-name="orderBy" from-field="parameters.sortField"/>
</service>
</actions>
<auto-fields-entity entity-name="${entityName}" default-field-type="display" include-internal="true"/>
<field name="entityName"><hidden value="${entityName}"/></field>"""
modelEntity.getFieldsUnmodifiable().each {
modelField ->
dynamicAutoEntityFieldListForm +=
"<field name=\"${modelField.name}\" sort-field=\"true\"/>"
}
dynamicAutoEntityFieldListForm += """
<field name="viewGeneric" title=" "><hyperlink target="ViewGeneric" description="view">
<auto-parameters-entity entity-name="${entityName}"/>
<parameter param-name="entityName" value="${entityName}"/>
</hyperlink></field>
<sort-order><sort-field name="viewGeneric"/></sort-order>
</form></forms>"""

Document dynamicAutoEntityFieldListFormXml = UtilXml.readXmlDocument(dynamicAutoEntityFieldListForm, true, true)
modelFormMap = FormFactory.readFormDocument(dynamicAutoEntityFieldListFormXml, entityModelReader, dispatcher.getDispatchContext(), entityName)
if (modelFormMap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
package org.apache.ofbiz.widget.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.UtilMisc;
import org.apache.ofbiz.base.util.UtilValidate;
import org.apache.ofbiz.base.util.UtilXml;
import org.apache.ofbiz.base.util.collections.FlexibleMapAccessor;
Expand Down Expand Up @@ -113,6 +115,43 @@ public class ModelFormFieldBuilder {
private String tabindex = "";
private String conditionGroup = "";

protected static final List<String> numericFieldTypes = Collections.unmodifiableList(UtilMisc.toList(
"floating-point", "numeric", "fixed-point",
"currency-amount", "currency-precise"));
protected static final List<String> textFieldTypes = Collections.unmodifiableList(UtilMisc.toList(
"id", "id-long", "id-vlong",
"very-short", "name", "short-varchar",
"value", "comment", "description",
"long-varchar", "url", "email"));
protected static final Map<String, Integer> textSizeByFieldTypes = Collections.unmodifiableMap(UtilMisc.toMap(
"id", 20,
"id-long", 40,
"id-vlong", 60,
"very-short", 6,
"name", 40,
"short-varchar", 40,
"value", 60,
"comment", 60,
"description", 60,
"long-varchar", 60,
"url", 60,
"email", 60));
protected static final Map<String, Integer> textMaxSizeByFieldTypes = Collections.unmodifiableMap(UtilMisc.toMap(
"id", 20,
"id-long", 60,
"id-vlong", 250,
"very-short", 10,
"name", 60,
"short-varchar", 40,
"value", 250,
"comment", 250,
"description", 250,
"long-varchar", 250,
"url", 250,
"email", 250));
protected static final List<String> dateFieldTypes = Collections.unmodifiableList(UtilMisc.toList(
"date-time", "date", "time"));

public ModelFormFieldBuilder() {
}

Expand Down Expand Up @@ -518,41 +557,26 @@ public boolean induceFieldInfoFromEntityField(ModelEntity modelEntity, ModelFiel
}
this.entityName = modelEntity.getEntityName();
this.fieldName = modelField.getName();
String fieldType = modelField.getType();
if ("find".equals(defaultFieldType)) {
if ("id".equals(modelField.getType())) {
ModelFormField.TextFindField textField = new ModelFormField.TextFindField(FieldInfo.SOURCE_AUTO_ENTITY, 20,
20, null);
this.setFieldInfo(textField);
} else if ("id-long".equals(modelField.getType())) {
ModelFormField.TextFindField textField = new ModelFormField.TextFindField(FieldInfo.SOURCE_AUTO_ENTITY, 40,
60, null);
this.setFieldInfo(textField);
} else if ("id-vlong".equals(modelField.getType())) {
ModelFormField.TextFindField textField = new ModelFormField.TextFindField(FieldInfo.SOURCE_AUTO_ENTITY, 60,
250, null);
this.setFieldInfo(textField);
} else if ("very-short".equals(modelField.getType())) {
ModelFormField.TextField textField = new ModelFormField.TextField(FieldInfo.SOURCE_AUTO_ENTITY, 6,
10, null);
this.setFieldInfo(textField);
} else if ("name".equals(modelField.getType()) || "short-varchar".equals(modelField.getType())) {
ModelFormField.TextFindField textField = new ModelFormField.TextFindField(FieldInfo.SOURCE_AUTO_ENTITY, 40,
60, null);
this.setFieldInfo(textField);
} else if ("value".equals(modelField.getType()) || "comment".equals(modelField.getType())
|| "description".equals(modelField.getType()) || "long-varchar".equals(modelField.getType())
|| "url".equals(modelField.getType()) || "email".equals(modelField.getType())) {
ModelFormField.TextFindField textField = new ModelFormField.TextFindField(FieldInfo.SOURCE_AUTO_ENTITY, 60,
250, null);
if ("indicator".equals(fieldType)) {
List<OptionSource> optionSources = UtilMisc.toList(
new ModelFormField.SingleOption("", null, null),
new ModelFormField.SingleOption("Y", null, null),
new ModelFormField.SingleOption("N", null, null));
ModelFormField.DropDownField dropDownField = new ModelFormField.DropDownField(FieldInfo.SOURCE_AUTO_ENTITY,
optionSources);
this.setFieldInfo(dropDownField);
} else if (textFieldTypes.contains(fieldType)) {
ModelFormField.TextFindField textField = new ModelFormField.TextFindField(FieldInfo.SOURCE_AUTO_ENTITY,
textSizeByFieldTypes.get(fieldType), textMaxSizeByFieldTypes.get(fieldType), null);
this.setFieldInfo(textField);
} else if ("floating-point".equals(modelField.getType()) || "currency-amount".equals(modelField.getType())
|| "numeric".equals(modelField.getType()) || "fixed-point".equals(modelField.getType()) || "currency-precise".equals(modelField.getType())) {
} else if (numericFieldTypes.contains(fieldType)) {
ModelFormField.RangeFindField textField = new ModelFormField.RangeFindField(FieldInfo.SOURCE_AUTO_ENTITY, 6, null);
this.setFieldInfo(textField);
} else if ("date-time".equals(modelField.getType()) || "date".equals(modelField.getType())
|| "time".equals(modelField.getType())) {
String type = modelField.getType();
if ("date-time".equals(modelField.getType())) {
} else if (dateFieldTypes.contains(fieldType)) {
String type = fieldType;
if ("date-time".equals(fieldType)) {
type = "timestamp";
}
ModelFormField.DateFindField dateTimeField = new ModelFormField.DateFindField(FieldInfo.SOURCE_AUTO_ENTITY, type);
Expand All @@ -568,50 +592,26 @@ public boolean induceFieldInfoFromEntityField(ModelEntity modelEntity, ModelFiel
ModelFormField.HiddenField hiddenField = new ModelFormField.HiddenField(FieldInfo.SOURCE_AUTO_SERVICE, null);
this.setFieldInfo(hiddenField);
} else {
if ("id".equals(modelField.getType())) {
ModelFormField.TextField textField = new ModelFormField.TextField(FieldInfo.SOURCE_AUTO_ENTITY, 20,
20, null);
this.setFieldInfo(textField);
} else if ("id-long".equals(modelField.getType())) {
ModelFormField.TextField textField = new ModelFormField.TextField(FieldInfo.SOURCE_AUTO_ENTITY, 40,
60, null);
this.setFieldInfo(textField);
} else if ("id-vlong".equals(modelField.getType())) {
ModelFormField.TextField textField = new ModelFormField.TextField(FieldInfo.SOURCE_AUTO_ENTITY, 60,
250, null);
this.setFieldInfo(textField);
} else if ("indicator".equals(modelField.getType())) {
List<OptionSource> optionSources = new ArrayList<>();
optionSources.add(new ModelFormField.SingleOption("Y", null, null));
optionSources.add(new ModelFormField.SingleOption("N", null, null));
if ("indicator".equals(fieldType)) {
List<OptionSource> optionSources = UtilMisc.toList(
new ModelFormField.SingleOption("Y", null, null),
new ModelFormField.SingleOption("N", null, null));
ModelFormField.DropDownField dropDownField = new ModelFormField.DropDownField(FieldInfo.SOURCE_AUTO_ENTITY,
optionSources);
this.setFieldInfo(dropDownField);
} else if ("very-short".equals(modelField.getType())) {
ModelFormField.TextField textField = new ModelFormField.TextField(FieldInfo.SOURCE_AUTO_ENTITY, 6,
10, null);
this.setFieldInfo(textField);
} else if ("very-long".equals(modelField.getType())) {
} else if ("very-long".equals(fieldType)) {
ModelFormField.TextareaField textareaField = new ModelFormField.TextareaField(FieldInfo.SOURCE_AUTO_ENTITY, null);
this.setFieldInfo(textareaField);
} else if ("name".equals(modelField.getType()) || "short-varchar".equals(modelField.getType())) {
ModelFormField.TextField textField = new ModelFormField.TextField(FieldInfo.SOURCE_AUTO_ENTITY, 40,
60, null);
this.setFieldInfo(textField);
} else if ("value".equals(modelField.getType()) || "comment".equals(modelField.getType())
|| "description".equals(modelField.getType()) || "long-varchar".equals(modelField.getType())
|| "url".equals(modelField.getType()) || "email".equals(modelField.getType())) {
ModelFormField.TextField textField = new ModelFormField.TextField(FieldInfo.SOURCE_AUTO_ENTITY, 60,
250, null);
} if (textFieldTypes.contains(fieldType)) {
ModelFormField.TextField textField = new ModelFormField.TextField(FieldInfo.SOURCE_AUTO_ENTITY,
textSizeByFieldTypes.get(fieldType), textMaxSizeByFieldTypes.get(fieldType), null);
this.setFieldInfo(textField);
} else if ("floating-point".equals(modelField.getType()) || "currency-amount".equals(modelField.getType())
|| "numeric".equals(modelField.getType())) {
} else if (numericFieldTypes.contains(fieldType)) {
ModelFormField.TextField textField = new ModelFormField.TextField(FieldInfo.SOURCE_AUTO_ENTITY, 6, null, null);
this.setFieldInfo(textField);
} else if ("date-time".equals(modelField.getType()) || "date".equals(modelField.getType())
|| "time".equals(modelField.getType())) {
String type = modelField.getType();
if ("date-time".equals(modelField.getType())) {
} else if (dateFieldTypes.contains(fieldType)) {
String type = fieldType;
if ("date-time".equals(fieldType)) {
type = "timestamp";
}
ModelFormField.DateTimeField dateTimeField = new ModelFormField.DateTimeField(FieldInfo.SOURCE_AUTO_ENTITY, type);
Expand Down

0 comments on commit 2c58133

Please sign in to comment.