📄 modelformfield.java
字号:
/*
* $Id: ModelFormField.java,v 1.10 2004/02/28 01:33:56 jonesde Exp $
*
* Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.ofbiz.content.widget.form;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.ofbiz.base.util.BshUtil;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.FlexibleMapAccessor;
import org.ofbiz.base.util.FlexibleStringExpander;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.base.util.UtilXml;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.model.ModelEntity;
import org.ofbiz.entity.model.ModelField;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ModelParam;
import org.ofbiz.service.ModelService;
import org.w3c.dom.Element;
import bsh.EvalError;
import bsh.Interpreter;
/**
* Widget Library - Form model class
*
* @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
* @author <a href="mailto:byersa@automationgroups.com">Al Byers</a>
* @version $Revision: 1.10 $
* @since 2.2
*/
public class ModelFormField {
public static final String module = ModelFormField.class.getName();
protected ModelForm modelForm;
protected String name;
protected FlexibleMapAccessor mapAcsr;
protected String entityName;
protected String serviceName;
protected FlexibleMapAccessor entryAcsr;
protected String parameterName;
protected String fieldName;
protected String attributeName;
protected FlexibleStringExpander title;
protected FlexibleStringExpander tooltip;
protected String titleStyle;
protected String widgetStyle;
protected String tooltipStyle;
protected Integer position = null;
protected String redWhen;
protected FlexibleStringExpander useWhen;
protected FieldInfo fieldInfo = null;
protected String idName;
// ===== CONSTRUCTORS =====
/** Default Constructor */
public ModelFormField(ModelForm modelForm) {
this.modelForm = modelForm;
}
/** XML Constructor */
public ModelFormField(Element fieldElement, ModelForm modelForm) {
this.modelForm = modelForm;
this.name = fieldElement.getAttribute("name");
this.setMapName(fieldElement.getAttribute("map-name"));
this.entityName = fieldElement.getAttribute("entity-name");
this.serviceName = fieldElement.getAttribute("service-name");
this.setEntryName(UtilXml.checkEmpty(fieldElement.getAttribute("entry-name"), this.name));
this.parameterName = UtilXml.checkEmpty(fieldElement.getAttribute("parameter-name"), this.name);
this.fieldName = UtilXml.checkEmpty(fieldElement.getAttribute("field-name"), this.name);
this.attributeName = UtilXml.checkEmpty(fieldElement.getAttribute("attribute-name"), this.name);
this.setTitle(fieldElement.getAttribute("title"));
this.setTooltip(fieldElement.getAttribute("tooltip"));
this.titleStyle = fieldElement.getAttribute("title-style");
this.widgetStyle = fieldElement.getAttribute("widget-style");
this.tooltipStyle = fieldElement.getAttribute("tooltip-style");
this.redWhen = fieldElement.getAttribute("red-when");
this.setUseWhen(fieldElement.getAttribute("use-when"));
this.idName = fieldElement.getAttribute("id-name");
String positionStr = fieldElement.getAttribute("position");
try {
if (positionStr != null && positionStr.length() > 0) {
position = Integer.valueOf(positionStr);
}
} catch (Exception e) {
Debug.logError(
e,
"Could not convert position attribute of the field element to an integer: [" + positionStr + "], using the default of the form renderer",
module);
}
// get sub-element and set fieldInfo
Element subElement = UtilXml.firstChildElement(fieldElement, null);
if (subElement != null) {
String subElementName = subElement.getTagName();
if (Debug.verboseOn())
Debug.logVerbose("Processing field " + this.name + " with type info tag " + subElementName, module);
if (UtilValidate.isEmpty(subElementName)) {
this.fieldInfo = null;
this.induceFieldInfo(null); //no defaultFieldType specified here, will default to edit
} else if ("display".equals(subElementName)) {
this.fieldInfo = new DisplayField(subElement, this);
} else if ("hyperlink".equals(subElementName)) {
this.fieldInfo = new HyperlinkField(subElement, this);
} else if ("text".equals(subElementName)) {
this.fieldInfo = new TextField(subElement, this);
} else if ("textarea".equals(subElementName)) {
this.fieldInfo = new TextareaField(subElement, this);
} else if ("date-time".equals(subElementName)) {
this.fieldInfo = new DateTimeField(subElement, this);
} else if ("drop-down".equals(subElementName)) {
this.fieldInfo = new DropDownField(subElement, this);
} else if ("check".equals(subElementName)) {
this.fieldInfo = new CheckField(subElement, this);
} else if ("radio".equals(subElementName)) {
this.fieldInfo = new RadioField(subElement, this);
} else if ("submit".equals(subElementName)) {
this.fieldInfo = new SubmitField(subElement, this);
} else if ("reset".equals(subElementName)) {
this.fieldInfo = new ResetField(subElement, this);
} else if ("hidden".equals(subElementName)) {
this.fieldInfo = new HiddenField(subElement, this);
} else if ("ignored".equals(subElementName)) {
this.fieldInfo = new IgnoredField(subElement, this);
} else if ("text-find".equals(subElementName)) {
this.fieldInfo = new TextFindField(subElement, this);
} else if ("date-find".equals(subElementName)) {
this.fieldInfo = new DateFindField(subElement, this);
} else if ("range-find".equals(subElementName)) {
this.fieldInfo = new RangeFindField(subElement, this);
} else if ("lookup".equals(subElementName)) {
this.fieldInfo = new LookupField(subElement, this);
} else if ("file".equals(subElementName)) {
this.fieldInfo = new FileField(subElement, this);
} else {
throw new IllegalArgumentException("The field sub-element with name " + subElementName + " is not supported");
}
}
}
public void mergeOverrideModelFormField(ModelFormField overrideFormField) {
if (overrideFormField == null)
return;
// incorporate updates for values that are not empty in the overrideFormField
if (UtilValidate.isNotEmpty(overrideFormField.name))
this.name = overrideFormField.name;
if (overrideFormField.mapAcsr != null && !overrideFormField.mapAcsr.isEmpty()) {
//Debug.logInfo("overriding mapAcsr, old=" + (this.mapAcsr==null?"null":this.mapAcsr.getOriginalName()) + ", new=" + overrideFormField.mapAcsr.getOriginalName(), module);
this.mapAcsr = overrideFormField.mapAcsr;
}
if (UtilValidate.isNotEmpty(overrideFormField.entityName))
this.entityName = overrideFormField.entityName;
if (UtilValidate.isNotEmpty(overrideFormField.serviceName))
this.serviceName = overrideFormField.serviceName;
if (overrideFormField.entryAcsr != null && !overrideFormField.entryAcsr.isEmpty())
this.entryAcsr = overrideFormField.entryAcsr;
if (UtilValidate.isNotEmpty(overrideFormField.parameterName))
this.parameterName = overrideFormField.parameterName;
if (UtilValidate.isNotEmpty(overrideFormField.fieldName))
this.fieldName = overrideFormField.fieldName;
if (UtilValidate.isNotEmpty(overrideFormField.attributeName))
this.attributeName = overrideFormField.attributeName;
if (overrideFormField.title != null && !overrideFormField.title.isEmpty())
this.title = overrideFormField.title;
if (overrideFormField.tooltip != null && !overrideFormField.tooltip.isEmpty())
this.tooltip = overrideFormField.tooltip;
if (UtilValidate.isNotEmpty(overrideFormField.titleStyle))
this.titleStyle = overrideFormField.titleStyle;
if (UtilValidate.isNotEmpty(overrideFormField.widgetStyle))
this.widgetStyle = overrideFormField.widgetStyle;
if (overrideFormField.position != null)
this.position = overrideFormField.position;
if (UtilValidate.isNotEmpty(overrideFormField.redWhen))
this.redWhen = overrideFormField.redWhen;
if (overrideFormField.useWhen != null && !overrideFormField.useWhen.isEmpty())
this.useWhen = overrideFormField.useWhen;
if (overrideFormField.fieldInfo != null) {
this.setFieldInfo(overrideFormField.fieldInfo);
}
if (UtilValidate.isNotEmpty(overrideFormField.idName))
this.idName = overrideFormField.idName;
}
public boolean induceFieldInfo(String defaultFieldType) {
if (this.induceFieldInfoFromEntityField(defaultFieldType)) {
return true;
}
if (this.induceFieldInfoFromServiceParam(defaultFieldType)) {
return true;
}
return false;
}
public boolean induceFieldInfoFromServiceParam(String defaultFieldType) {
if (UtilValidate.isEmpty(this.getServiceName()) || UtilValidate.isEmpty(this.getAttributeName())) {
return false;
}
LocalDispatcher dispatcher = this.getModelForm().getDispacher();
try {
ModelService modelService = dispatcher.getDispatchContext().getModelService(this.getServiceName());
if (modelService != null) {
ModelParam modelParam = modelService.getParam(this.getAttributeName());
if (modelParam != null) {
if (UtilValidate.isNotEmpty(modelParam.entityName) && UtilValidate.isNotEmpty(modelParam.fieldName)) {
this.entityName = modelParam.entityName;
this.fieldName = modelParam.fieldName;
if (this.induceFieldInfoFromEntityField(defaultFieldType)) {
return true;
}
}
this.induceFieldInfoFromServiceParam(modelService, modelParam, defaultFieldType);
return true;
}
}
} catch (GenericServiceException e) {
Debug.logError(e, "error getting service parameter definition for auto-field with serviceName: " + this.getServiceName() + ", and attributeName: " + this.getAttributeName(), module);
}
return false;
}
public boolean induceFieldInfoFromServiceParam(ModelService modelService, ModelParam modelParam, String defaultFieldType) {
if (modelService == null || modelParam == null) {
return false;
}
this.serviceName = modelService.name;
this.attributeName = modelParam.name;
if ("find".equals(defaultFieldType)) {
if (modelParam.type.indexOf("Double") != -1
|| modelParam.type.indexOf("Float") != -1
|| modelParam.type.indexOf("Long") != -1
|| modelParam.type.indexOf("Integer") != -1) {
ModelFormField.RangeFindField textField = new ModelFormField.RangeFindField(ModelFormField.FieldInfo.SOURCE_AUTO_SERVICE, this);
textField.setSize(6);
this.setFieldInfo(textField);
} else if (modelParam.type.indexOf("Timestamp") != -1) {
ModelFormField.DateFindField dateTimeField = new ModelFormField.DateFindField(ModelFormField.FieldInfo.SOURCE_AUTO_SERVICE, this);
dateTimeField.setType("timestamp");
this.setFieldInfo(dateTimeField);
} else if (modelParam.type.indexOf("Date") != -1) {
ModelFormField.DateFindField dateTimeField = new ModelFormField.DateFindField(ModelFormField.FieldInfo.SOURCE_AUTO_SERVICE, this);
dateTimeField.setType("date");
this.setFieldInfo(dateTimeField);
} else if (modelParam.type.indexOf("Time") != -1) {
ModelFormField.DateFindField dateTimeField = new ModelFormField.DateFindField(ModelFormField.FieldInfo.SOURCE_AUTO_SERVICE, this);
dateTimeField.setType("time");
this.setFieldInfo(dateTimeField);
} else {
ModelFormField.TextFindField textField = new ModelFormField.TextFindField(ModelFormField.FieldInfo.SOURCE_AUTO_SERVICE, this);
this.setFieldInfo(textField);
}
} else if ("display".equals(defaultFieldType)) {
ModelFormField.DisplayField displayField = new ModelFormField.DisplayField(ModelFormField.FieldInfo.SOURCE_AUTO_SERVICE, this);
this.setFieldInfo(displayField);
} else {
// default to "edit"
if (modelParam.type.indexOf("Double") != -1
|| modelParam.type.indexOf("Float") != -1
|| modelParam.type.indexOf("Long") != -1
|| modelParam.type.indexOf("Integer") != -1) {
ModelFormField.TextField textField = new ModelFormField.TextField(ModelFormField.FieldInfo.SOURCE_AUTO_SERVICE, this);
textField.setSize(6);
this.setFieldInfo(textField);
} else if (modelParam.type.indexOf("Timestamp") != -1) {
ModelFormField.DateTimeField dateTimeField = new ModelFormField.DateTimeField(ModelFormField.FieldInfo.SOURCE_AUTO_SERVICE, this);
dateTimeField.setType("timestamp");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -