0001: /*
0002: * $Id: ModelFormField.java,v 1.10 2004/02/28 01:33:56 jonesde Exp $
0003: *
0004: * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
0005: *
0006: * Permission is hereby granted, free of charge, to any person obtaining a
0007: * copy of this software and associated documentation files (the "Software"),
0008: * to deal in the Software without restriction, including without limitation
0009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0010: * and/or sell copies of the Software, and to permit persons to whom the
0011: * Software is furnished to do so, subject to the following conditions:
0012: *
0013: * The above copyright notice and this permission notice shall be included
0014: * in all copies or substantial portions of the Software.
0015: *
0016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
0017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
0019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
0020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
0021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
0022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0023: */
0024: package org.ofbiz.content.widget.form;
0025:
0026: import java.util.HashMap;
0027: import java.util.Iterator;
0028: import java.util.LinkedList;
0029: import java.util.List;
0030: import java.util.Map;
0031:
0032: import org.ofbiz.base.util.BshUtil;
0033: import org.ofbiz.base.util.Debug;
0034: import org.ofbiz.base.util.FlexibleMapAccessor;
0035: import org.ofbiz.base.util.FlexibleStringExpander;
0036: import org.ofbiz.base.util.UtilDateTime;
0037: import org.ofbiz.base.util.UtilMisc;
0038: import org.ofbiz.base.util.UtilValidate;
0039: import org.ofbiz.base.util.UtilXml;
0040: import org.ofbiz.entity.GenericDelegator;
0041: import org.ofbiz.entity.GenericEntityException;
0042: import org.ofbiz.entity.GenericValue;
0043: import org.ofbiz.entity.model.ModelEntity;
0044: import org.ofbiz.entity.model.ModelField;
0045: import org.ofbiz.entity.util.EntityUtil;
0046: import org.ofbiz.service.GenericServiceException;
0047: import org.ofbiz.service.LocalDispatcher;
0048: import org.ofbiz.service.ModelParam;
0049: import org.ofbiz.service.ModelService;
0050: import org.w3c.dom.Element;
0051:
0052: import bsh.EvalError;
0053: import bsh.Interpreter;
0054:
0055: /**
0056: * Widget Library - Form model class
0057: *
0058: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
0059: * @author <a href="mailto:byersa@automationgroups.com">Al Byers</a>
0060: * @version $Revision: 1.10 $
0061: * @since 2.2
0062: */
0063: public class ModelFormField {
0064:
0065: public static final String module = ModelFormField.class.getName();
0066:
0067: protected ModelForm modelForm;
0068:
0069: protected String name;
0070: protected FlexibleMapAccessor mapAcsr;
0071: protected String entityName;
0072: protected String serviceName;
0073: protected FlexibleMapAccessor entryAcsr;
0074: protected String parameterName;
0075: protected String fieldName;
0076: protected String attributeName;
0077: protected FlexibleStringExpander title;
0078: protected FlexibleStringExpander tooltip;
0079: protected String titleStyle;
0080: protected String widgetStyle;
0081: protected String tooltipStyle;
0082: protected Integer position = null;
0083: protected String redWhen;
0084: protected FlexibleStringExpander useWhen;
0085:
0086: protected FieldInfo fieldInfo = null;
0087: protected String idName;
0088:
0089: // ===== CONSTRUCTORS =====
0090: /** Default Constructor */
0091: public ModelFormField(ModelForm modelForm) {
0092: this .modelForm = modelForm;
0093: }
0094:
0095: /** XML Constructor */
0096: public ModelFormField(Element fieldElement, ModelForm modelForm) {
0097: this .modelForm = modelForm;
0098: this .name = fieldElement.getAttribute("name");
0099: this .setMapName(fieldElement.getAttribute("map-name"));
0100: this .entityName = fieldElement.getAttribute("entity-name");
0101: this .serviceName = fieldElement.getAttribute("service-name");
0102: this .setEntryName(UtilXml.checkEmpty(fieldElement
0103: .getAttribute("entry-name"), this .name));
0104: this .parameterName = UtilXml.checkEmpty(fieldElement
0105: .getAttribute("parameter-name"), this .name);
0106: this .fieldName = UtilXml.checkEmpty(fieldElement
0107: .getAttribute("field-name"), this .name);
0108: this .attributeName = UtilXml.checkEmpty(fieldElement
0109: .getAttribute("attribute-name"), this .name);
0110: this .setTitle(fieldElement.getAttribute("title"));
0111: this .setTooltip(fieldElement.getAttribute("tooltip"));
0112: this .titleStyle = fieldElement.getAttribute("title-style");
0113: this .widgetStyle = fieldElement.getAttribute("widget-style");
0114: this .tooltipStyle = fieldElement.getAttribute("tooltip-style");
0115: this .redWhen = fieldElement.getAttribute("red-when");
0116: this .setUseWhen(fieldElement.getAttribute("use-when"));
0117: this .idName = fieldElement.getAttribute("id-name");
0118:
0119: String positionStr = fieldElement.getAttribute("position");
0120: try {
0121: if (positionStr != null && positionStr.length() > 0) {
0122: position = Integer.valueOf(positionStr);
0123: }
0124: } catch (Exception e) {
0125: Debug
0126: .logError(
0127: e,
0128: "Could not convert position attribute of the field element to an integer: ["
0129: + positionStr
0130: + "], using the default of the form renderer",
0131: module);
0132: }
0133:
0134: // get sub-element and set fieldInfo
0135: Element subElement = UtilXml.firstChildElement(fieldElement,
0136: null);
0137: if (subElement != null) {
0138: String subElementName = subElement.getTagName();
0139: if (Debug.verboseOn())
0140: Debug.logVerbose("Processing field " + this .name
0141: + " with type info tag " + subElementName,
0142: module);
0143:
0144: if (UtilValidate.isEmpty(subElementName)) {
0145: this .fieldInfo = null;
0146: this .induceFieldInfo(null); //no defaultFieldType specified here, will default to edit
0147: } else if ("display".equals(subElementName)) {
0148: this .fieldInfo = new DisplayField(subElement, this );
0149: } else if ("hyperlink".equals(subElementName)) {
0150: this .fieldInfo = new HyperlinkField(subElement, this );
0151: } else if ("text".equals(subElementName)) {
0152: this .fieldInfo = new TextField(subElement, this );
0153: } else if ("textarea".equals(subElementName)) {
0154: this .fieldInfo = new TextareaField(subElement, this );
0155: } else if ("date-time".equals(subElementName)) {
0156: this .fieldInfo = new DateTimeField(subElement, this );
0157: } else if ("drop-down".equals(subElementName)) {
0158: this .fieldInfo = new DropDownField(subElement, this );
0159: } else if ("check".equals(subElementName)) {
0160: this .fieldInfo = new CheckField(subElement, this );
0161: } else if ("radio".equals(subElementName)) {
0162: this .fieldInfo = new RadioField(subElement, this );
0163: } else if ("submit".equals(subElementName)) {
0164: this .fieldInfo = new SubmitField(subElement, this );
0165: } else if ("reset".equals(subElementName)) {
0166: this .fieldInfo = new ResetField(subElement, this );
0167: } else if ("hidden".equals(subElementName)) {
0168: this .fieldInfo = new HiddenField(subElement, this );
0169: } else if ("ignored".equals(subElementName)) {
0170: this .fieldInfo = new IgnoredField(subElement, this );
0171: } else if ("text-find".equals(subElementName)) {
0172: this .fieldInfo = new TextFindField(subElement, this );
0173: } else if ("date-find".equals(subElementName)) {
0174: this .fieldInfo = new DateFindField(subElement, this );
0175: } else if ("range-find".equals(subElementName)) {
0176: this .fieldInfo = new RangeFindField(subElement, this );
0177: } else if ("lookup".equals(subElementName)) {
0178: this .fieldInfo = new LookupField(subElement, this );
0179: } else if ("file".equals(subElementName)) {
0180: this .fieldInfo = new FileField(subElement, this );
0181: } else {
0182: throw new IllegalArgumentException(
0183: "The field sub-element with name "
0184: + subElementName + " is not supported");
0185: }
0186: }
0187: }
0188:
0189: public void mergeOverrideModelFormField(
0190: ModelFormField overrideFormField) {
0191: if (overrideFormField == null)
0192: return;
0193: // incorporate updates for values that are not empty in the overrideFormField
0194: if (UtilValidate.isNotEmpty(overrideFormField.name))
0195: this .name = overrideFormField.name;
0196: if (overrideFormField.mapAcsr != null
0197: && !overrideFormField.mapAcsr.isEmpty()) {
0198: //Debug.logInfo("overriding mapAcsr, old=" + (this.mapAcsr==null?"null":this.mapAcsr.getOriginalName()) + ", new=" + overrideFormField.mapAcsr.getOriginalName(), module);
0199: this .mapAcsr = overrideFormField.mapAcsr;
0200: }
0201: if (UtilValidate.isNotEmpty(overrideFormField.entityName))
0202: this .entityName = overrideFormField.entityName;
0203: if (UtilValidate.isNotEmpty(overrideFormField.serviceName))
0204: this .serviceName = overrideFormField.serviceName;
0205: if (overrideFormField.entryAcsr != null
0206: && !overrideFormField.entryAcsr.isEmpty())
0207: this .entryAcsr = overrideFormField.entryAcsr;
0208: if (UtilValidate.isNotEmpty(overrideFormField.parameterName))
0209: this .parameterName = overrideFormField.parameterName;
0210: if (UtilValidate.isNotEmpty(overrideFormField.fieldName))
0211: this .fieldName = overrideFormField.fieldName;
0212: if (UtilValidate.isNotEmpty(overrideFormField.attributeName))
0213: this .attributeName = overrideFormField.attributeName;
0214: if (overrideFormField.title != null
0215: && !overrideFormField.title.isEmpty())
0216: this .title = overrideFormField.title;
0217: if (overrideFormField.tooltip != null
0218: && !overrideFormField.tooltip.isEmpty())
0219: this .tooltip = overrideFormField.tooltip;
0220: if (UtilValidate.isNotEmpty(overrideFormField.titleStyle))
0221: this .titleStyle = overrideFormField.titleStyle;
0222: if (UtilValidate.isNotEmpty(overrideFormField.widgetStyle))
0223: this .widgetStyle = overrideFormField.widgetStyle;
0224: if (overrideFormField.position != null)
0225: this .position = overrideFormField.position;
0226: if (UtilValidate.isNotEmpty(overrideFormField.redWhen))
0227: this .redWhen = overrideFormField.redWhen;
0228: if (overrideFormField.useWhen != null
0229: && !overrideFormField.useWhen.isEmpty())
0230: this .useWhen = overrideFormField.useWhen;
0231: if (overrideFormField.fieldInfo != null) {
0232: this .setFieldInfo(overrideFormField.fieldInfo);
0233: }
0234: if (UtilValidate.isNotEmpty(overrideFormField.idName))
0235: this .idName = overrideFormField.idName;
0236: }
0237:
0238: public boolean induceFieldInfo(String defaultFieldType) {
0239: if (this .induceFieldInfoFromEntityField(defaultFieldType)) {
0240: return true;
0241: }
0242: if (this .induceFieldInfoFromServiceParam(defaultFieldType)) {
0243: return true;
0244: }
0245: return false;
0246: }
0247:
0248: public boolean induceFieldInfoFromServiceParam(
0249: String defaultFieldType) {
0250: if (UtilValidate.isEmpty(this .getServiceName())
0251: || UtilValidate.isEmpty(this .getAttributeName())) {
0252: return false;
0253: }
0254: LocalDispatcher dispatcher = this .getModelForm().getDispacher();
0255: try {
0256: ModelService modelService = dispatcher.getDispatchContext()
0257: .getModelService(this .getServiceName());
0258: if (modelService != null) {
0259: ModelParam modelParam = modelService.getParam(this
0260: .getAttributeName());
0261: if (modelParam != null) {
0262: if (UtilValidate.isNotEmpty(modelParam.entityName)
0263: && UtilValidate
0264: .isNotEmpty(modelParam.fieldName)) {
0265: this .entityName = modelParam.entityName;
0266: this .fieldName = modelParam.fieldName;
0267: if (this
0268: .induceFieldInfoFromEntityField(defaultFieldType)) {
0269: return true;
0270: }
0271: }
0272:
0273: this .induceFieldInfoFromServiceParam(modelService,
0274: modelParam, defaultFieldType);
0275: return true;
0276: }
0277: }
0278: } catch (GenericServiceException e) {
0279: Debug.logError(e,
0280: "error getting service parameter definition for auto-field with serviceName: "
0281: + this .getServiceName()
0282: + ", and attributeName: "
0283: + this .getAttributeName(), module);
0284: }
0285: return false;
0286: }
0287:
0288: public boolean induceFieldInfoFromServiceParam(
0289: ModelService modelService, ModelParam modelParam,
0290: String defaultFieldType) {
0291: if (modelService == null || modelParam == null) {
0292: return false;
0293: }
0294:
0295: this .serviceName = modelService.name;
0296: this .attributeName = modelParam.name;
0297:
0298: if ("find".equals(defaultFieldType)) {
0299: if (modelParam.type.indexOf("Double") != -1
0300: || modelParam.type.indexOf("Float") != -1
0301: || modelParam.type.indexOf("Long") != -1
0302: || modelParam.type.indexOf("Integer") != -1) {
0303: ModelFormField.RangeFindField textField = new ModelFormField.RangeFindField(
0304: ModelFormField.FieldInfo.SOURCE_AUTO_SERVICE,
0305: this );
0306: textField.setSize(6);
0307: this .setFieldInfo(textField);
0308: } else if (modelParam.type.indexOf("Timestamp") != -1) {
0309: ModelFormField.DateFindField dateTimeField = new ModelFormField.DateFindField(
0310: ModelFormField.FieldInfo.SOURCE_AUTO_SERVICE,
0311: this );
0312: dateTimeField.setType("timestamp");
0313: this .setFieldInfo(dateTimeField);
0314: } else if (modelParam.type.indexOf("Date") != -1) {
0315: ModelFormField.DateFindField dateTimeField = new ModelFormField.DateFindField(
0316: ModelFormField.FieldInfo.SOURCE_AUTO_SERVICE,
0317: this );
0318: dateTimeField.setType("date");
0319: this .setFieldInfo(dateTimeField);
0320: } else if (modelParam.type.indexOf("Time") != -1) {
0321: ModelFormField.DateFindField dateTimeField = new ModelFormField.DateFindField(
0322: ModelFormField.FieldInfo.SOURCE_AUTO_SERVICE,
0323: this );
0324: dateTimeField.setType("time");
0325: this .setFieldInfo(dateTimeField);
0326: } else {
0327: ModelFormField.TextFindField textField = new ModelFormField.TextFindField(
0328: ModelFormField.FieldInfo.SOURCE_AUTO_SERVICE,
0329: this );
0330: this .setFieldInfo(textField);
0331: }
0332: } else if ("display".equals(defaultFieldType)) {
0333: ModelFormField.DisplayField displayField = new ModelFormField.DisplayField(
0334: ModelFormField.FieldInfo.SOURCE_AUTO_SERVICE, this );
0335: this .setFieldInfo(displayField);
0336: } else {
0337: // default to "edit"
0338: if (modelParam.type.indexOf("Double") != -1
0339: || modelParam.type.indexOf("Float") != -1
0340: || modelParam.type.indexOf("Long") != -1
0341: || modelParam.type.indexOf("Integer") != -1) {
0342: ModelFormField.TextField textField = new ModelFormField.TextField(
0343: ModelFormField.FieldInfo.SOURCE_AUTO_SERVICE,
0344: this );
0345: textField.setSize(6);
0346: this .setFieldInfo(textField);
0347: } else if (modelParam.type.indexOf("Timestamp") != -1) {
0348: ModelFormField.DateTimeField dateTimeField = new ModelFormField.DateTimeField(
0349: ModelFormField.FieldInfo.SOURCE_AUTO_SERVICE,
0350: this );
0351: dateTimeField.setType("timestamp");
0352: this .setFieldInfo(dateTimeField);
0353: } else if (modelParam.type.indexOf("Date") != -1) {
0354: ModelFormField.DateTimeField dateTimeField = new ModelFormField.DateTimeField(
0355: ModelFormField.FieldInfo.SOURCE_AUTO_SERVICE,
0356: this );
0357: dateTimeField.setType("date");
0358: this .setFieldInfo(dateTimeField);
0359: } else if (modelParam.type.indexOf("Time") != -1) {
0360: ModelFormField.DateTimeField dateTimeField = new ModelFormField.DateTimeField(
0361: ModelFormField.FieldInfo.SOURCE_AUTO_SERVICE,
0362: this );
0363: dateTimeField.setType("time");
0364: this .setFieldInfo(dateTimeField);
0365: } else {
0366: ModelFormField.TextField textField = new ModelFormField.TextField(
0367: ModelFormField.FieldInfo.SOURCE_AUTO_SERVICE,
0368: this );
0369: this .setFieldInfo(textField);
0370: }
0371: }
0372:
0373: return true;
0374: }
0375:
0376: public boolean induceFieldInfoFromEntityField(
0377: String defaultFieldType) {
0378: if (UtilValidate.isEmpty(this .getEntityName())
0379: || UtilValidate.isEmpty(this .getFieldName())) {
0380: return false;
0381: }
0382: GenericDelegator delegator = this .getModelForm().getDelegator();
0383: ModelEntity modelEntity = delegator.getModelEntity(this
0384: .getEntityName());
0385: if (modelEntity != null) {
0386: ModelField modelField = modelEntity.getField(this
0387: .getFieldName());
0388: if (modelField != null) {
0389: // okay, populate using the entity field info...
0390: this .induceFieldInfoFromEntityField(modelEntity,
0391: modelField, defaultFieldType);
0392: return true;
0393: }
0394: }
0395: return false;
0396: }
0397:
0398: public boolean induceFieldInfoFromEntityField(
0399: ModelEntity modelEntity, ModelField modelField,
0400: String defaultFieldType) {
0401: if (modelEntity == null || modelField == null) {
0402: return false;
0403: }
0404:
0405: this .entityName = modelEntity.getEntityName();
0406: this .fieldName = modelField.getName();
0407:
0408: if ("find".equals(defaultFieldType)) {
0409: if ("id".equals(modelField.getType())
0410: || "id-ne".equals(modelField.getType())) {
0411: ModelFormField.TextFindField textField = new ModelFormField.TextFindField(
0412: ModelFormField.FieldInfo.SOURCE_AUTO_ENTITY,
0413: this );
0414: textField.setSize(20);
0415: textField.setMaxlength(new Integer(20));
0416: this .setFieldInfo(textField);
0417: } else if ("id-long".equals(modelField.getType())
0418: || "id-long-ne".equals(modelField.getType())) {
0419: ModelFormField.TextFindField textField = new ModelFormField.TextFindField(
0420: ModelFormField.FieldInfo.SOURCE_AUTO_ENTITY,
0421: this );
0422: textField.setSize(40);
0423: textField.setMaxlength(new Integer(60));
0424: this .setFieldInfo(textField);
0425: } else if ("id-vlong".equals(modelField.getType())
0426: || "id-vlong-ne".equals(modelField.getType())) {
0427: ModelFormField.TextFindField textField = new ModelFormField.TextFindField(
0428: ModelFormField.FieldInfo.SOURCE_AUTO_ENTITY,
0429: this );
0430: textField.setSize(60);
0431: textField.setMaxlength(new Integer(250));
0432: this .setFieldInfo(textField);
0433: } else if ("very-short".equals(modelField.getType())) {
0434: ModelFormField.TextField textField = new ModelFormField.TextField(
0435: ModelFormField.FieldInfo.SOURCE_AUTO_ENTITY,
0436: this );
0437: textField.setSize(6);
0438: textField.setMaxlength(new Integer(10));
0439: this .setFieldInfo(textField);
0440: } else if ("name".equals(modelField.getType())
0441: || "short-varchar".equals(modelField.getType())) {
0442: ModelFormField.TextFindField textField = new ModelFormField.TextFindField(
0443: ModelFormField.FieldInfo.SOURCE_AUTO_ENTITY,
0444: this );
0445: textField.setSize(40);
0446: textField.setMaxlength(new Integer(60));
0447: this .setFieldInfo(textField);
0448: } else if ("value".equals(modelField.getType())
0449: || "comment".equals(modelField.getType())
0450: || "description".equals(modelField.getType())
0451: || "long-varchar".equals(modelField.getType())
0452: || "url".equals(modelField.getType())
0453: || "email".equals(modelField.getType())) {
0454: ModelFormField.TextFindField textField = new ModelFormField.TextFindField(
0455: ModelFormField.FieldInfo.SOURCE_AUTO_ENTITY,
0456: this );
0457: textField.setSize(60);
0458: textField.setMaxlength(new Integer(250));
0459: this .setFieldInfo(textField);
0460: } else if ("floating-point".equals(modelField.getType())
0461: || "currency-amount".equals(modelField.getType())
0462: || "numeric".equals(modelField.getType())) {
0463: ModelFormField.RangeFindField textField = new ModelFormField.RangeFindField(
0464: ModelFormField.FieldInfo.SOURCE_AUTO_ENTITY,
0465: this );
0466: textField.setSize(6);
0467: this .setFieldInfo(textField);
0468: } else if ("date-time".equals(modelField.getType())
0469: || "date".equals(modelField.getType())
0470: || "time".equals(modelField.getType())) {
0471: ModelFormField.DateFindField dateTimeField = new ModelFormField.DateFindField(
0472: ModelFormField.FieldInfo.SOURCE_AUTO_ENTITY,
0473: this );
0474: if ("date-time".equals(modelField.getType())) {
0475: dateTimeField.setType("timestamp");
0476: } else if ("date".equals(modelField.getType())) {
0477: dateTimeField.setType("date");
0478: } else if ("time".equals(modelField.getType())) {
0479: dateTimeField.setType("time");
0480: }
0481: this .setFieldInfo(dateTimeField);
0482: } else {
0483: ModelFormField.TextFindField textField = new ModelFormField.TextFindField(
0484: ModelFormField.FieldInfo.SOURCE_AUTO_ENTITY,
0485: this );
0486: this .setFieldInfo(textField);
0487: }
0488: } else if ("display".equals(defaultFieldType)) {
0489: ModelFormField.DisplayField displayField = new ModelFormField.DisplayField(
0490: ModelFormField.FieldInfo.SOURCE_AUTO_SERVICE, this );
0491: this .setFieldInfo(displayField);
0492: } else if ("hidden".equals(defaultFieldType)) {
0493: ModelFormField.HiddenField hiddenField = new ModelFormField.HiddenField(
0494: ModelFormField.FieldInfo.SOURCE_AUTO_SERVICE, this );
0495: this .setFieldInfo(hiddenField);
0496: } else {
0497: if ("id".equals(modelField.getType())
0498: || "id-ne".equals(modelField.getType())) {
0499: ModelFormField.TextField textField = new ModelFormField.TextField(
0500: ModelFormField.FieldInfo.SOURCE_AUTO_ENTITY,
0501: this );
0502: textField.setSize(20);
0503: textField.setMaxlength(new Integer(20));
0504: this .setFieldInfo(textField);
0505: } else if ("id-long".equals(modelField.getType())
0506: || "id-long-ne".equals(modelField.getType())) {
0507: ModelFormField.TextField textField = new ModelFormField.TextField(
0508: ModelFormField.FieldInfo.SOURCE_AUTO_ENTITY,
0509: this );
0510: textField.setSize(40);
0511: textField.setMaxlength(new Integer(60));
0512: this .setFieldInfo(textField);
0513: } else if ("id-vlong".equals(modelField.getType())
0514: || "id-vlong-ne".equals(modelField.getType())) {
0515: ModelFormField.TextField textField = new ModelFormField.TextField(
0516: ModelFormField.FieldInfo.SOURCE_AUTO_ENTITY,
0517: this );
0518: textField.setSize(60);
0519: textField.setMaxlength(new Integer(250));
0520: this .setFieldInfo(textField);
0521: } else if ("indicator".equals(modelField.getType())) {
0522: ModelFormField.DropDownField dropDownField = new ModelFormField.DropDownField(
0523: ModelFormField.FieldInfo.SOURCE_AUTO_ENTITY,
0524: this );
0525: dropDownField.setAllowEmpty(false);
0526: dropDownField
0527: .addOptionSource(new ModelFormField.SingleOption(
0528: "Y", null, dropDownField));
0529: dropDownField
0530: .addOptionSource(new ModelFormField.SingleOption(
0531: "N", null, dropDownField));
0532: this .setFieldInfo(dropDownField);
0533: //ModelFormField.TextField textField = new ModelFormField.TextField(ModelFormField.FieldInfo.SOURCE_AUTO_ENTITY, this);
0534: //textField.setSize(1);
0535: //textField.setMaxlength(new Integer(1));
0536: //this.setFieldInfo(textField);
0537: } else if ("very-short".equals(modelField.getType())) {
0538: ModelFormField.TextField textField = new ModelFormField.TextField(
0539: ModelFormField.FieldInfo.SOURCE_AUTO_ENTITY,
0540: this );
0541: textField.setSize(6);
0542: textField.setMaxlength(new Integer(10));
0543: this .setFieldInfo(textField);
0544: } else if ("very-long".equals(modelField.getType())) {
0545: ModelFormField.TextareaField textareaField = new ModelFormField.TextareaField(
0546: ModelFormField.FieldInfo.SOURCE_AUTO_ENTITY,
0547: this );
0548: textareaField.setCols(60);
0549: textareaField.setRows(2);
0550: this .setFieldInfo(textareaField);
0551: } else if ("name".equals(modelField.getType())
0552: || "short-varchar".equals(modelField.getType())) {
0553: ModelFormField.TextField textField = new ModelFormField.TextField(
0554: ModelFormField.FieldInfo.SOURCE_AUTO_ENTITY,
0555: this );
0556: textField.setSize(40);
0557: textField.setMaxlength(new Integer(60));
0558: this .setFieldInfo(textField);
0559: } else if ("value".equals(modelField.getType())
0560: || "comment".equals(modelField.getType())
0561: || "description".equals(modelField.getType())
0562: || "long-varchar".equals(modelField.getType())
0563: || "url".equals(modelField.getType())
0564: || "email".equals(modelField.getType())) {
0565: ModelFormField.TextField textField = new ModelFormField.TextField(
0566: ModelFormField.FieldInfo.SOURCE_AUTO_ENTITY,
0567: this );
0568: textField.setSize(60);
0569: textField.setMaxlength(new Integer(250));
0570: this .setFieldInfo(textField);
0571: } else if ("floating-point".equals(modelField.getType())
0572: || "currency-amount".equals(modelField.getType())
0573: || "numeric".equals(modelField.getType())) {
0574: ModelFormField.TextField textField = new ModelFormField.TextField(
0575: ModelFormField.FieldInfo.SOURCE_AUTO_ENTITY,
0576: this );
0577: textField.setSize(6);
0578: this .setFieldInfo(textField);
0579: } else if ("date-time".equals(modelField.getType())
0580: || "date".equals(modelField.getType())
0581: || "time".equals(modelField.getType())) {
0582: ModelFormField.DateTimeField dateTimeField = new ModelFormField.DateTimeField(
0583: ModelFormField.FieldInfo.SOURCE_AUTO_ENTITY,
0584: this );
0585: if ("date-time".equals(modelField.getType())) {
0586: dateTimeField.setType("timestamp");
0587: } else if ("date".equals(modelField.getType())) {
0588: dateTimeField.setType("date");
0589: } else if ("time".equals(modelField.getType())) {
0590: dateTimeField.setType("time");
0591: }
0592: this .setFieldInfo(dateTimeField);
0593: } else {
0594: ModelFormField.TextField textField = new ModelFormField.TextField(
0595: ModelFormField.FieldInfo.SOURCE_AUTO_ENTITY,
0596: this );
0597: this .setFieldInfo(textField);
0598: }
0599: }
0600:
0601: return true;
0602: }
0603:
0604: public void renderFieldString(StringBuffer buffer, Map context,
0605: FormStringRenderer formStringRenderer) {
0606: this .fieldInfo.renderFieldString(buffer, context,
0607: formStringRenderer);
0608: }
0609:
0610: /**
0611: * @return
0612: */
0613: public FieldInfo getFieldInfo() {
0614: return fieldInfo;
0615: }
0616:
0617: /**
0618: * @return
0619: */
0620: public ModelForm getModelForm() {
0621: return modelForm;
0622: }
0623:
0624: /**
0625: * @param fieldInfo
0626: */
0627: public void setFieldInfo(FieldInfo fieldInfo) {
0628: if (fieldInfo == null)
0629: return;
0630:
0631: // field info is a little different, check source for priority
0632: if (this .fieldInfo == null
0633: || (fieldInfo.getFieldSource() <= this .fieldInfo
0634: .getFieldSource())) {
0635: this .fieldInfo = fieldInfo;
0636: this .fieldInfo.modelFormField = this ;
0637: }
0638: }
0639:
0640: /**
0641: * Gets the name of the Service Attribute (aka Parameter) that corresponds
0642: * with this field. This can be used to get additional information about the field.
0643: * Use the getServiceName() method to get the Entity name that the field is in.
0644: *
0645: * @return
0646: */
0647: public String getAttributeName() {
0648: if (UtilValidate.isNotEmpty(this .attributeName)) {
0649: return this .attributeName;
0650: } else {
0651: return this .name;
0652: }
0653: }
0654:
0655: /**
0656: * @return
0657: */
0658: public String getEntityName() {
0659: if (UtilValidate.isNotEmpty(this .entityName)) {
0660: return this .entityName;
0661: } else {
0662: return this .modelForm.getDefaultEntityName();
0663: }
0664: }
0665:
0666: /**
0667: * @return
0668: */
0669: public String getEntryName() {
0670: if (this .entryAcsr != null && !this .entryAcsr.isEmpty()) {
0671: return this .entryAcsr.getOriginalName();
0672: } else {
0673: return this .name;
0674: }
0675: }
0676:
0677: /**
0678: * Gets the entry from the context that corresponds to this field; if this
0679: * form is being rendered in an error condition (ie isError in the context
0680: * is true) then the value will be retreived from the parameters Map in
0681: * the context.
0682: *
0683: * @param context
0684: * @return
0685: */
0686: public String getEntry(Map context) {
0687: return this .getEntry(context, "");
0688: }
0689:
0690: public String getEntry(Map context, String defaultValue) {
0691: Boolean isError = (Boolean) context.get("isError");
0692: Boolean useRequestParameters = (Boolean) context
0693: .get("useRequestParameters");
0694: // if useRequestParameters is TRUE then parameters will always be used, if FALSE then parameters will never be used
0695: // if isError is TRUE and useRequestParameters is not FALSE (ie is null or TRUE) then parameters will be used
0696: if ((Boolean.TRUE.equals(isError) && !Boolean.FALSE
0697: .equals(useRequestParameters))
0698: || (Boolean.TRUE.equals(useRequestParameters))) {
0699: //Debug.logInfo("Getting entry, isError true so getting from parameters for field " + this.getName() + " of form " + this.modelForm.getName(), module);
0700: Map parameters = (Map) context.get("parameters");
0701: if (parameters != null
0702: && parameters.get(this .getParameterName(context)) != null) {
0703: return (String) parameters.get(this
0704: .getParameterName(context));
0705: } else {
0706: return defaultValue;
0707: }
0708: } else {
0709: //Debug.logInfo("Getting entry, isError false so getting from Map in context for field " + this.getName() + " of form " + this.modelForm.getName(), module);
0710: Map dataMap = this .getMap(context);
0711: if (dataMap == null) {
0712: //Debug.logInfo("Getting entry, no Map found with name " + this.getMapName() + ", using context for field " + this.getName() + " of form " + this.modelForm.getName(), module);
0713: dataMap = context;
0714: }
0715: Object retVal = null;
0716: if (this .entryAcsr != null && !this .entryAcsr.isEmpty()) {
0717: //Debug.logInfo("Getting entry, using entryAcsr for field " + this.getName() + " of form " + this.modelForm.getName(), module);
0718: retVal = this .entryAcsr.get(dataMap);
0719: } else {
0720: //Debug.logInfo("Getting entry, no entryAcsr so using field name " + this.name + " for field " + this.getName() + " of form " + this.modelForm.getName(), module);
0721: // if no entry name was specified, use the field's name
0722: retVal = dataMap.get(this .name);
0723: }
0724:
0725: if (retVal != null) {
0726: return retVal.toString();
0727: } else {
0728: return defaultValue;
0729: }
0730: }
0731: }
0732:
0733: public Map getMap(Map context) {
0734: if (this .mapAcsr == null || this .mapAcsr.isEmpty()) {
0735: //Debug.logInfo("Getting Map from default of the form because of no mapAcsr for field " + this.getName(), module);
0736: return this .modelForm.getDefaultMap(context);
0737: } else {
0738: //Debug.logInfo("Getting Map from mapAcsr for field " + this.getName(), module);
0739: return (Map) mapAcsr.get(context);
0740: }
0741: }
0742:
0743: /**
0744: * Gets the name of the Entity Field that corresponds
0745: * with this field. This can be used to get additional information about the field.
0746: * Use the getEntityName() method to get the Entity name that the field is in.
0747: *
0748: * @return
0749: */
0750: public String getFieldName() {
0751: if (UtilValidate.isNotEmpty(this .fieldName)) {
0752: return this .fieldName;
0753: } else {
0754: return this .name;
0755: }
0756: }
0757:
0758: /** Get the name of the Map in the form context that contains the entry,
0759: * available from the getEntryName() method. This entry is used to
0760: * pre-populate the field widget when not in an error condition. In an
0761: * error condition the parameter name is used to get the value from the
0762: * parameters Map.
0763: *
0764: * @return
0765: */
0766: public String getMapName() {
0767: if (this .mapAcsr != null && !this .mapAcsr.isEmpty()) {
0768: return this .mapAcsr.getOriginalName();
0769: } else {
0770: return this .modelForm.getDefaultMapName();
0771: }
0772: }
0773:
0774: /**
0775: * @return
0776: */
0777: public String getName() {
0778: return name;
0779: }
0780:
0781: /**
0782: * Get the name to use for the parameter for this field in the form interpreter.
0783: * For HTML forms this is the request parameter name.
0784: *
0785: * @return
0786: */
0787: public String getParameterName(Map context) {
0788: String baseName;
0789: if (UtilValidate.isNotEmpty(this .parameterName)) {
0790: baseName = this .parameterName;
0791: } else {
0792: baseName = this .name;
0793: }
0794:
0795: Integer itemIndex = (Integer) context.get("itemIndex");
0796: if (itemIndex != null
0797: && "multi".equals(this .modelForm.getType())) {
0798: return baseName + this .modelForm.getItemIndexSeparator()
0799: + itemIndex.intValue();
0800: } else {
0801: return baseName;
0802: }
0803: }
0804:
0805: /**
0806: * @return
0807: */
0808: public int getPosition() {
0809: if (this .position == null) {
0810: return 1;
0811: } else {
0812: return position.intValue();
0813: }
0814: }
0815:
0816: /**
0817: * @return
0818: */
0819: public String getRedWhen() {
0820: return redWhen;
0821: }
0822:
0823: /**
0824: * the widget/interaction part will be red if the date value is
0825: * before-now (for ex. thruDate), after-now (for ex. fromDate), or by-name (if the
0826: * field's name or entry-name or fromDate or thruDate the corresponding
0827: * action will be done); only applicable when the field is a timestamp
0828: *
0829: * @param context
0830: * @return
0831: */
0832: public boolean shouldBeRed(Map context) {
0833: // red-when ( never | before-now | after-now | by-name ) "by-name"
0834:
0835: String redCondition = this .redWhen;
0836:
0837: if ("never".equals(redCondition)) {
0838: return false;
0839: }
0840:
0841: // for performance resaons we check this first, most fields will be eliminated here and the valueOfs will not be necessary
0842: if (UtilValidate.isEmpty(redCondition)
0843: || "by-name".equals(redCondition)) {
0844: if ("fromDate".equals(this .name)
0845: || (this .entryAcsr != null && "fromDate"
0846: .equals(this .entryAcsr.getOriginalName()))) {
0847: redCondition = "after-now";
0848: } else if ("thruDate".equals(this .name)
0849: || (this .entryAcsr != null && "thruDate"
0850: .equals(this .entryAcsr.getOriginalName()))) {
0851: redCondition = "before-now";
0852: } else {
0853: return false;
0854: }
0855: }
0856:
0857: boolean isBeforeNow = false;
0858: if ("before-now".equals(redCondition)) {
0859: isBeforeNow = true;
0860: } else if ("after-now".equals(redCondition)) {
0861: isBeforeNow = false;
0862: } else {
0863: return false;
0864: }
0865:
0866: java.sql.Date dateVal = null;
0867: java.sql.Time timeVal = null;
0868: java.sql.Timestamp timestampVal = null;
0869:
0870: //now before going on, check to see if the current entry is a valid date and/or time and get the value
0871: String value = this .getEntry(context);
0872: try {
0873: timestampVal = java.sql.Timestamp.valueOf(value);
0874: } catch (Exception e) {
0875: // okay, not a timestamp...
0876: }
0877:
0878: if (timestampVal == null) {
0879: try {
0880: dateVal = java.sql.Date.valueOf(value);
0881: } catch (Exception e) {
0882: // okay, not a date...
0883: }
0884: }
0885:
0886: if (timestampVal == null && dateVal == null) {
0887: try {
0888: timeVal = java.sql.Time.valueOf(value);
0889: } catch (Exception e) {
0890: // okay, not a time...
0891: }
0892: }
0893:
0894: if (timestampVal == null && dateVal == null && timeVal == null) {
0895: return false;
0896: }
0897:
0898: long nowMillis = System.currentTimeMillis();
0899: if (timestampVal != null) {
0900: java.sql.Timestamp nowStamp = new java.sql.Timestamp(
0901: nowMillis);
0902: if (!timestampVal.equals(nowStamp)) {
0903: if (isBeforeNow) {
0904: if (timestampVal.before(nowStamp)) {
0905: return true;
0906: }
0907: } else {
0908: if (timestampVal.after(nowStamp)) {
0909: return true;
0910: }
0911: }
0912: }
0913: } else if (dateVal != null) {
0914: java.sql.Date nowDate = new java.sql.Date(nowMillis);
0915: if (!dateVal.equals(nowDate)) {
0916: if (isBeforeNow) {
0917: if (dateVal.before(nowDate)) {
0918: return true;
0919: }
0920: } else {
0921: if (dateVal.after(nowDate)) {
0922: return true;
0923: }
0924: }
0925: }
0926: } else if (timeVal != null) {
0927: java.sql.Time nowTime = new java.sql.Time(nowMillis);
0928: if (!timeVal.equals(nowTime)) {
0929: if (isBeforeNow) {
0930: if (timeVal.before(nowTime)) {
0931: return true;
0932: }
0933: } else {
0934: if (timeVal.after(nowTime)) {
0935: return true;
0936: }
0937: }
0938: }
0939: }
0940:
0941: return false;
0942: }
0943:
0944: /**
0945: * @return
0946: */
0947: public String getServiceName() {
0948: if (UtilValidate.isNotEmpty(this .serviceName)) {
0949: return this .serviceName;
0950: } else {
0951: return this .modelForm.getDefaultServiceName();
0952: }
0953: }
0954:
0955: /**
0956: * @return
0957: */
0958: public String getTitle(Map context) {
0959: if (this .title != null && !this .title.isEmpty()) {
0960: return title.expandString(context);
0961: } else {
0962: // create a title from the name of this field; expecting a Java method/field style name, ie productName or productCategoryId
0963: if (this .name == null || this .name.length() == 0) {
0964: // this should never happen, ie name is required
0965: return "";
0966: }
0967:
0968: StringBuffer autoTitleBuffer = new StringBuffer();
0969:
0970: // always use upper case first letter...
0971: autoTitleBuffer.append(Character.toUpperCase(this .name
0972: .charAt(0)));
0973:
0974: // just put spaces before the upper case letters
0975: for (int i = 1; i < this .name.length(); i++) {
0976: char curChar = this .name.charAt(i);
0977: if (Character.isUpperCase(curChar)) {
0978: autoTitleBuffer.append(' ');
0979: }
0980: autoTitleBuffer.append(curChar);
0981: }
0982:
0983: return autoTitleBuffer.toString();
0984: }
0985: }
0986:
0987: /**
0988: * @return
0989: */
0990: public String getTitleStyle() {
0991: if (UtilValidate.isNotEmpty(this .titleStyle)) {
0992: return this .titleStyle;
0993: } else {
0994: return this .modelForm.getDefaultTitleStyle();
0995: }
0996: }
0997:
0998: /**
0999: * @return
1000: */
1001: public String getTooltip(Map context) {
1002: if (tooltip != null && !tooltip.isEmpty()) {
1003: return tooltip.expandString(context);
1004: } else {
1005: return "";
1006: }
1007: }
1008:
1009: /**
1010: * @return
1011: */
1012: public String getUseWhen(Map context) {
1013: if (useWhen != null && !useWhen.isEmpty()) {
1014: return useWhen.expandString(context);
1015: } else {
1016: return "";
1017: }
1018: }
1019:
1020: /**
1021: * @return
1022: */
1023: public String getIdName() {
1024: return idName;
1025: }
1026:
1027: /**
1028: * @param string
1029: */
1030: public void setIdName(String string) {
1031: idName = string;
1032: }
1033:
1034: public boolean isUseWhenEmpty() {
1035: if (this .useWhen == null) {
1036: return true;
1037: }
1038:
1039: return this .useWhen.isEmpty();
1040: }
1041:
1042: public boolean shouldUse(Map context) {
1043: String useWhenStr = this .getUseWhen(context);
1044: if (UtilValidate.isEmpty(useWhenStr)) {
1045: return true;
1046: } else {
1047: try {
1048: Interpreter bsh = this .modelForm
1049: .getBshInterpreter(context);
1050: Object retVal = bsh.eval(useWhenStr);
1051: boolean condTrue = false;
1052: // retVal should be a Boolean, if not something weird is up...
1053: if (retVal instanceof Boolean) {
1054: Boolean boolVal = (Boolean) retVal;
1055: condTrue = boolVal.booleanValue();
1056: } else {
1057: throw new IllegalArgumentException(
1058: "Return value from use-when condition eval was not a Boolean: "
1059: + retVal.getClass().getName()
1060: + " [" + retVal + "] on the field "
1061: + this .name + " of form "
1062: + this .modelForm.name);
1063: }
1064:
1065: return condTrue;
1066: } catch (EvalError e) {
1067: String errMsg = "Error evaluating BeanShell use-when condition ["
1068: + this .useWhen
1069: + "] on the field "
1070: + this .name
1071: + " of form "
1072: + this .modelForm.name
1073: + ": "
1074: + e.toString();
1075: Debug.logError(e, errMsg, module);
1076: throw new IllegalArgumentException(errMsg);
1077: }
1078: }
1079: }
1080:
1081: /**
1082: * @return
1083: */
1084: public String getWidgetStyle() {
1085: if (UtilValidate.isNotEmpty(this .widgetStyle)) {
1086: return this .widgetStyle;
1087: } else {
1088: return this .modelForm.getDefaultWidgetStyle();
1089: }
1090: }
1091:
1092: /**
1093: * @return
1094: */
1095: public String getTooltipStyle() {
1096: if (UtilValidate.isNotEmpty(this .tooltipStyle)) {
1097: return this .tooltipStyle;
1098: } else {
1099: return this .modelForm.getDefaultTooltipStyle();
1100: }
1101: }
1102:
1103: /**
1104: * @param string
1105: */
1106: public void setAttributeName(String string) {
1107: attributeName = string;
1108: }
1109:
1110: /**
1111: * @param string
1112: */
1113: public void setEntityName(String string) {
1114: entityName = string;
1115: }
1116:
1117: /**
1118: * @param string
1119: */
1120: public void setEntryName(String string) {
1121: entryAcsr = new FlexibleMapAccessor(string);
1122: }
1123:
1124: /**
1125: * @param string
1126: */
1127: public void setFieldName(String string) {
1128: fieldName = string;
1129: }
1130:
1131: /**
1132: * @param string
1133: */
1134: public void setMapName(String string) {
1135: this .mapAcsr = new FlexibleMapAccessor(string);
1136: }
1137:
1138: /**
1139: * @param string
1140: */
1141: public void setName(String string) {
1142: name = string;
1143: }
1144:
1145: /**
1146: * @param string
1147: */
1148: public void setParameterName(String string) {
1149: parameterName = string;
1150: }
1151:
1152: /**
1153: * @param i
1154: */
1155: public void setPosition(int i) {
1156: position = new Integer(i);
1157: }
1158:
1159: /**
1160: * @param string
1161: */
1162: public void setRedWhen(String string) {
1163: redWhen = string;
1164: }
1165:
1166: /**
1167: * @param string
1168: */
1169: public void setServiceName(String string) {
1170: serviceName = string;
1171: }
1172:
1173: /**
1174: * @param string
1175: */
1176: public void setTitle(String string) {
1177: this .title = new FlexibleStringExpander(string);
1178: }
1179:
1180: /**
1181: * @param string
1182: */
1183: public void setTitleStyle(String string) {
1184: this .titleStyle = string;
1185: }
1186:
1187: /**
1188: * @param string
1189: */
1190: public void setTooltip(String string) {
1191: this .tooltip = new FlexibleStringExpander(string);
1192: }
1193:
1194: /**
1195: * @param string
1196: */
1197: public void setUseWhen(String string) {
1198: this .useWhen = new FlexibleStringExpander(string);
1199: }
1200:
1201: /**
1202: * @param string
1203: */
1204: public void setWidgetStyle(String string) {
1205: this .widgetStyle = string;
1206: }
1207:
1208: /**
1209: * @param string
1210: */
1211: public void setTooltipStyle(String string) {
1212: this .tooltipStyle = string;
1213: }
1214:
1215: public static abstract class FieldInfo {
1216:
1217: public static final int DISPLAY = 1;
1218: public static final int HYPERLINK = 2;
1219: public static final int TEXT = 3;
1220: public static final int TEXTAREA = 4;
1221: public static final int DATE_TIME = 5;
1222: public static final int DROP_DOWN = 6;
1223: public static final int CHECK = 7;
1224: public static final int RADIO = 8;
1225: public static final int SUBMIT = 9;
1226: public static final int RESET = 10;
1227: public static final int HIDDEN = 11;
1228: public static final int IGNORED = 12;
1229: public static final int TEXTQBE = 13;
1230: public static final int DATEQBE = 14;
1231: public static final int RANGEQBE = 15;
1232: public static final int LOOKUP = 16;
1233: public static final int FILE = 17;
1234:
1235: // the numbering here represents the priority of the source;
1236: //when setting a new fieldInfo on a modelFormField it will only set
1237: //the new one if the fieldSource is less than or equal to the existing
1238: //fieldSource, which should always be passed as one of the following...
1239: public static final int SOURCE_EXPLICIT = 1;
1240: public static final int SOURCE_AUTO_ENTITY = 2;
1241: public static final int SOURCE_AUTO_SERVICE = 3;
1242:
1243: public static Map fieldTypeByName = new HashMap();
1244:
1245: static {
1246: fieldTypeByName.put("display", new Integer(1));
1247: fieldTypeByName.put("hyperlink", new Integer(2));
1248: fieldTypeByName.put("text", new Integer(3));
1249: fieldTypeByName.put("textarea", new Integer(4));
1250: fieldTypeByName.put("date-time", new Integer(5));
1251: fieldTypeByName.put("drop-down", new Integer(6));
1252: fieldTypeByName.put("check", new Integer(7));
1253: fieldTypeByName.put("radio", new Integer(8));
1254: fieldTypeByName.put("submit", new Integer(9));
1255: fieldTypeByName.put("reset", new Integer(10));
1256: fieldTypeByName.put("hidden", new Integer(11));
1257: fieldTypeByName.put("ignored", new Integer(12));
1258: fieldTypeByName.put("text-find", new Integer(13));
1259: fieldTypeByName.put("date-find", new Integer(14));
1260: fieldTypeByName.put("range-find", new Integer(15));
1261: fieldTypeByName.put("lookup", new Integer(16));
1262: fieldTypeByName.put("file", new Integer(17));
1263: }
1264:
1265: protected int fieldType;
1266: protected int fieldSource;
1267: protected ModelFormField modelFormField;
1268:
1269: /** Don't allow the Default Constructor */
1270: protected FieldInfo() {
1271: }
1272:
1273: /** Value Constructor */
1274: public FieldInfo(int fieldSource, int fieldType,
1275: ModelFormField modelFormField) {
1276: this .fieldType = fieldType;
1277: this .fieldSource = fieldSource;
1278: this .modelFormField = modelFormField;
1279: }
1280:
1281: /** XML Constructor */
1282: public FieldInfo(Element element, ModelFormField modelFormField) {
1283: this .fieldSource = FieldInfo.SOURCE_EXPLICIT;
1284: this .fieldType = findFieldTypeFromName(element.getTagName());
1285: this .modelFormField = modelFormField;
1286: }
1287:
1288: /**
1289: * @return
1290: */
1291: public ModelFormField getModelFormField() {
1292: return modelFormField;
1293: }
1294:
1295: /**
1296: * @return
1297: */
1298: public int getFieldType() {
1299: return fieldType;
1300: }
1301:
1302: /**
1303: * @return
1304: */
1305: public int getFieldSource() {
1306: return this .fieldSource;
1307: }
1308:
1309: public static int findFieldTypeFromName(String name) {
1310: Integer fieldTypeInt = (Integer) FieldInfo.fieldTypeByName
1311: .get(name);
1312: if (fieldTypeInt != null) {
1313: return fieldTypeInt.intValue();
1314: } else {
1315: throw new IllegalArgumentException(
1316: "Could not get fieldType for field type name "
1317: + name);
1318: }
1319: }
1320:
1321: public abstract void renderFieldString(StringBuffer buffer,
1322: Map context, FormStringRenderer formStringRenderer);
1323: }
1324:
1325: public static abstract class FieldInfoWithOptions extends FieldInfo {
1326: protected FieldInfoWithOptions() {
1327: super ();
1328: }
1329:
1330: protected String noCurrentSelectedKey;
1331: protected List optionSources = new LinkedList();
1332:
1333: public FieldInfoWithOptions(int fieldSource, int fieldType,
1334: ModelFormField modelFormField) {
1335: super (fieldSource, fieldType, modelFormField);
1336: }
1337:
1338: public FieldInfoWithOptions(Element element,
1339: ModelFormField modelFormField) {
1340: super (element, modelFormField);
1341:
1342: noCurrentSelectedKey = element
1343: .getAttribute("no-current-selected-key");
1344:
1345: // read all option and entity-options sub-elements, maintaining order
1346: List childElements = UtilXml
1347: .childElementList(element, null);
1348: Iterator childElementIter = childElements.iterator();
1349: while (childElementIter.hasNext()) {
1350: Element childElement = (Element) childElementIter
1351: .next();
1352: if ("option".equals(childElement.getTagName())) {
1353: this .addOptionSource(new SingleOption(childElement,
1354: this ));
1355: } else if ("list-options".equals(childElement
1356: .getTagName())) {
1357: this .addOptionSource(new ListOptions(childElement,
1358: this ));
1359: } else if ("entity-options".equals(childElement
1360: .getTagName())) {
1361: this .addOptionSource(new EntityOptions(
1362: childElement, this ));
1363: }
1364: }
1365: }
1366:
1367: public List getAllOptionValues(Map context,
1368: GenericDelegator delegator) {
1369: List optionValues = new LinkedList();
1370: Iterator optionSourceIter = this .optionSources.iterator();
1371: while (optionSourceIter.hasNext()) {
1372: OptionSource optionSource = (OptionSource) optionSourceIter
1373: .next();
1374: optionSource.addOptionValues(optionValues, context,
1375: delegator);
1376: }
1377: return optionValues;
1378: }
1379:
1380: public static String getDescriptionForOptionKey(String key,
1381: List allOptionValues) {
1382: if (UtilValidate.isEmpty(key)) {
1383: return "";
1384: }
1385:
1386: if (UtilValidate.isEmpty(allOptionValues)) {
1387: return key;
1388: }
1389:
1390: Iterator optionValueIter = allOptionValues.iterator();
1391: while (optionValueIter.hasNext()) {
1392: OptionValue optionValue = (OptionValue) optionValueIter
1393: .next();
1394: if (key.equals(optionValue.getKey())) {
1395: return optionValue.getDescription();
1396: }
1397: }
1398:
1399: // if we get here we didn't find a match, just return the key
1400: return key;
1401: }
1402:
1403: public String getNoCurrentSelectedKey() {
1404: return this .noCurrentSelectedKey;
1405: }
1406:
1407: public void setNoCurrentSelectedKey(String string) {
1408: this .noCurrentSelectedKey = string;
1409: }
1410:
1411: public void addOptionSource(OptionSource optionSource) {
1412: this .optionSources.add(optionSource);
1413: }
1414: }
1415:
1416: public static class OptionValue {
1417: protected String key;
1418: protected String description;
1419:
1420: public OptionValue(String key, String description) {
1421: this .key = key;
1422: this .description = description;
1423: }
1424:
1425: public String getKey() {
1426: return key;
1427: }
1428:
1429: public String getDescription() {
1430: return description;
1431: }
1432: }
1433:
1434: public static abstract class OptionSource {
1435: protected FieldInfo fieldInfo;
1436:
1437: public abstract void addOptionValues(List optionValues,
1438: Map context, GenericDelegator delegator);
1439: }
1440:
1441: public static class SingleOption extends OptionSource {
1442: protected FlexibleStringExpander key;
1443: protected FlexibleStringExpander description;
1444:
1445: public SingleOption(String key, String description,
1446: FieldInfo fieldInfo) {
1447: this .key = new FlexibleStringExpander(key);
1448: this .description = new FlexibleStringExpander(UtilXml
1449: .checkEmpty(description, key));
1450: this .fieldInfo = fieldInfo;
1451: }
1452:
1453: public SingleOption(Element optionElement, FieldInfo fieldInfo) {
1454: this .key = new FlexibleStringExpander(optionElement
1455: .getAttribute("key"));
1456: this .description = new FlexibleStringExpander(UtilXml
1457: .checkEmpty(optionElement
1458: .getAttribute("description"), optionElement
1459: .getAttribute("key")));
1460: this .fieldInfo = fieldInfo;
1461: }
1462:
1463: public void addOptionValues(List optionValues, Map context,
1464: GenericDelegator delegator) {
1465: optionValues.add(new OptionValue(key.expandString(context),
1466: description.expandString(context)));
1467: }
1468: }
1469:
1470: public static class ListOptions extends OptionSource {
1471: protected FlexibleMapAccessor listAcsr;
1472: protected String listEntryName;
1473: protected FlexibleMapAccessor keyAcsr;
1474: protected FlexibleStringExpander description;
1475:
1476: public ListOptions(String listName, String listEntryName,
1477: String keyName, String description, FieldInfo fieldInfo) {
1478: this .listAcsr = new FlexibleMapAccessor(listName);
1479: this .listEntryName = listEntryName;
1480: this .keyAcsr = new FlexibleMapAccessor(keyName);
1481: this .description = new FlexibleStringExpander(description);
1482: this .fieldInfo = fieldInfo;
1483: }
1484:
1485: public ListOptions(Element optionElement, FieldInfo fieldInfo) {
1486: this .listEntryName = optionElement
1487: .getAttribute("list-entry-name");
1488: this .listAcsr = new FlexibleMapAccessor(optionElement
1489: .getAttribute("list-name"));
1490: this .keyAcsr = new FlexibleMapAccessor(optionElement
1491: .getAttribute("key-name"));
1492: this .listAcsr = new FlexibleMapAccessor(optionElement
1493: .getAttribute("list-name"));
1494: this .listEntryName = optionElement
1495: .getAttribute("list-entry-name");
1496: this .description = new FlexibleStringExpander(optionElement
1497: .getAttribute("description"));
1498: this .fieldInfo = fieldInfo;
1499: }
1500:
1501: public void addOptionValues(List optionValues, Map context,
1502: GenericDelegator delegator) {
1503: List dataList = (List) this .listAcsr.get(context);
1504: if (dataList != null && dataList.size() != 0) {
1505: Iterator dataIter = dataList.iterator();
1506: while (dataIter.hasNext()) {
1507: Object data = dataIter.next();
1508: Map localContext = new HashMap(context);
1509: if (UtilValidate.isNotEmpty(this .listEntryName)) {
1510: localContext.put(this .listEntryName, data);
1511: } else {
1512: localContext.putAll((Map) data);
1513: }
1514: optionValues.add(new OptionValue((String) keyAcsr
1515: .get(localContext), description
1516: .expandString(localContext)));
1517: }
1518: }
1519: }
1520: }
1521:
1522: public static class EntityOptions extends OptionSource {
1523: protected String entityName;
1524: protected String keyFieldName;
1525: protected FlexibleStringExpander description;
1526: protected boolean cache = true;
1527: protected String filterByDate;
1528:
1529: protected Map constraintMap = null;
1530: protected List orderByList = null;
1531:
1532: public EntityOptions(FieldInfo fieldInfo) {
1533: this .fieldInfo = fieldInfo;
1534: }
1535:
1536: public EntityOptions(Element entityOptionsElement,
1537: FieldInfo fieldInfo) {
1538: this .entityName = entityOptionsElement
1539: .getAttribute("entity-name");
1540: this .keyFieldName = entityOptionsElement
1541: .getAttribute("key-field-name");
1542: this .description = new FlexibleStringExpander(
1543: entityOptionsElement.getAttribute("description"));
1544: this .cache = !"false".equals(entityOptionsElement
1545: .getAttribute("cache"));
1546: this .filterByDate = entityOptionsElement
1547: .getAttribute("filter-by-date");
1548:
1549: List constraintElements = UtilXml.childElementList(
1550: entityOptionsElement, "entity-constraint");
1551: if (constraintElements != null
1552: && constraintElements.size() > 0) {
1553: this .constraintMap = new HashMap();
1554: Iterator constraintElementIter = constraintElements
1555: .iterator();
1556: while (constraintElementIter.hasNext()) {
1557: Element constraintElement = (Element) constraintElementIter
1558: .next();
1559: constraintMap.put(constraintElement
1560: .getAttribute("name"),
1561: new FlexibleStringExpander(
1562: constraintElement
1563: .getAttribute("value")));
1564: }
1565: }
1566:
1567: List orderByElements = UtilXml.childElementList(
1568: entityOptionsElement, "entity-order-by");
1569: if (orderByElements != null && orderByElements.size() > 0) {
1570: this .orderByList = new LinkedList();
1571: Iterator orderByElementIter = orderByElements
1572: .iterator();
1573: while (orderByElementIter.hasNext()) {
1574: Element orderByElement = (Element) orderByElementIter
1575: .next();
1576: orderByList.add(orderByElement
1577: .getAttribute("field-name"));
1578: }
1579: }
1580:
1581: this .fieldInfo = fieldInfo;
1582: }
1583:
1584: public String getKeyFieldName() {
1585: if (UtilValidate.isNotEmpty(this .keyFieldName)) {
1586: return this .keyFieldName;
1587: } else {
1588: // get the modelFormField fieldName
1589: return this .fieldInfo.getModelFormField()
1590: .getFieldName();
1591: }
1592: }
1593:
1594: public void addOptionValues(List optionValues, Map context,
1595: GenericDelegator delegator) {
1596: // first expand any conditions that need expanding based on the current context
1597: Map expandedConstraintMap = null;
1598: if (this .constraintMap != null) {
1599: expandedConstraintMap = new HashMap();
1600: Iterator constraintMapIter = this .constraintMap
1601: .entrySet().iterator();
1602: while (constraintMapIter.hasNext()) {
1603: Map.Entry entry = (Map.Entry) constraintMapIter
1604: .next();
1605: expandedConstraintMap.put(entry.getKey(),
1606: ((FlexibleStringExpander) entry.getValue())
1607: .expandString(context));
1608: }
1609: }
1610:
1611: try {
1612: List values = null;
1613: if (this .cache) {
1614: values = delegator.findByAndCache(this .entityName,
1615: expandedConstraintMap, this .orderByList);
1616: } else {
1617: values = delegator.findByAnd(this .entityName,
1618: expandedConstraintMap, this .orderByList);
1619: }
1620:
1621: // filter-by-date if requested
1622: if ("true".equals(this .filterByDate)) {
1623: values = EntityUtil.filterByDate(values, true);
1624: } else if (!"false".equals(this .filterByDate)) {
1625: // not explicitly true or false, check to see if has fromDate and thruDate, if so do the filter
1626: ModelEntity modelEntity = delegator
1627: .getModelEntity(this .entityName);
1628: if (modelEntity != null
1629: && modelEntity.isField("fromDate")
1630: && modelEntity.isField("thruDate")) {
1631: values = EntityUtil.filterByDate(values, true);
1632: }
1633: }
1634:
1635: Iterator valueIter = values.iterator();
1636: while (valueIter.hasNext()) {
1637: GenericValue value = (GenericValue) valueIter
1638: .next();
1639: // add key and description with string expansion, ie expanding ${} stuff, passing locale explicitly to expand value stirng because it won't be found in the Entity
1640: String optionDesc = this .description.expandString(
1641: value, UtilMisc.ensureLocale(context
1642: .get("locale")));
1643: Object keyFieldObject = value.get(this
1644: .getKeyFieldName());
1645: if (keyFieldObject == null) {
1646: throw new IllegalArgumentException(
1647: "The value found for key-name ["
1648: + this .getKeyFieldName()
1649: + "], may not be a valid key field name.");
1650: }
1651: String keyFieldValue = keyFieldObject.toString();
1652: optionValues.add(new OptionValue(keyFieldValue,
1653: optionDesc));
1654: }
1655: } catch (GenericEntityException e) {
1656: Debug.logError(e,
1657: "Error getting entity options in form", module);
1658: }
1659: }
1660: }
1661:
1662: public static class DisplayField extends FieldInfo {
1663: protected boolean alsoHidden = true;
1664: protected FlexibleStringExpander description;
1665:
1666: protected DisplayField() {
1667: super ();
1668: }
1669:
1670: public DisplayField(ModelFormField modelFormField) {
1671: super (FieldInfo.SOURCE_EXPLICIT, FieldInfo.DISPLAY,
1672: modelFormField);
1673: }
1674:
1675: public DisplayField(int fieldSource,
1676: ModelFormField modelFormField) {
1677: super (fieldSource, FieldInfo.DISPLAY, modelFormField);
1678: }
1679:
1680: public DisplayField(Element element,
1681: ModelFormField modelFormField) {
1682: super (element, modelFormField);
1683: this .setDescription(element.getAttribute("description"));
1684: this .alsoHidden = !"false".equals(element
1685: .getAttribute("also-hidden"));
1686: }
1687:
1688: public void renderFieldString(StringBuffer buffer, Map context,
1689: FormStringRenderer formStringRenderer) {
1690: formStringRenderer
1691: .renderDisplayField(buffer, context, this );
1692: }
1693:
1694: /**
1695: * @return
1696: */
1697: public boolean getAlsoHidden() {
1698: return alsoHidden;
1699: }
1700:
1701: /**
1702: * @return
1703: */
1704: public String getDescription(Map context) {
1705: String retVal = null;
1706: if (this .description != null && !this .description.isEmpty()) {
1707: retVal = this .description.expandString(context);
1708: } else {
1709: retVal = modelFormField.getEntry(context);
1710: }
1711: if (retVal == null || retVal.length() == 0)
1712: retVal = " ";
1713: return retVal;
1714: }
1715:
1716: /**
1717: * @param b
1718: */
1719: public void setAlsoHidden(boolean b) {
1720: alsoHidden = b;
1721: }
1722:
1723: /**
1724: * @param string
1725: */
1726: public void setDescription(String string) {
1727: description = new FlexibleStringExpander(string);
1728: }
1729: }
1730:
1731: public static class HyperlinkField extends FieldInfo {
1732: public static String DEFAULT_TARGET_TYPE = "intra-app";
1733:
1734: protected boolean alsoHidden = true;
1735: protected String targetType;
1736: protected FlexibleStringExpander target;
1737: protected FlexibleStringExpander description;
1738:
1739: protected HyperlinkField() {
1740: super ();
1741: }
1742:
1743: public HyperlinkField(ModelFormField modelFormField) {
1744: super (FieldInfo.SOURCE_EXPLICIT, FieldInfo.HYPERLINK,
1745: modelFormField);
1746: }
1747:
1748: public HyperlinkField(int fieldSource,
1749: ModelFormField modelFormField) {
1750: super (fieldSource, FieldInfo.HYPERLINK, modelFormField);
1751: }
1752:
1753: public HyperlinkField(Element element,
1754: ModelFormField modelFormField) {
1755: super (element, modelFormField);
1756:
1757: this .setDescription(element.getAttribute("description"));
1758: this .setTarget(element.getAttribute("target"));
1759: this .alsoHidden = !"false".equals(element
1760: .getAttribute("also-hidden"));
1761: this .targetType = element.getAttribute("target-type");
1762: }
1763:
1764: public void renderFieldString(StringBuffer buffer, Map context,
1765: FormStringRenderer formStringRenderer) {
1766: formStringRenderer.renderHyperlinkField(buffer, context,
1767: this );
1768: }
1769:
1770: /**
1771: * @return
1772: */
1773: public boolean getAlsoHidden() {
1774: return this .alsoHidden;
1775: }
1776:
1777: /**
1778: * @return
1779: */
1780: public String getTargetType() {
1781: if (UtilValidate.isNotEmpty(this .targetType)) {
1782: return this .targetType;
1783: } else {
1784: return HyperlinkField.DEFAULT_TARGET_TYPE;
1785: }
1786: }
1787:
1788: /**
1789: * @return
1790: */
1791: public String getDescription(Map context) {
1792: return this .description.expandString(context);
1793: }
1794:
1795: /**
1796: * @return
1797: */
1798: public String getTarget(Map context) {
1799: return this .target.expandString(context);
1800: }
1801:
1802: /**
1803: * @param b
1804: */
1805: public void setAlsoHidden(boolean b) {
1806: this .alsoHidden = b;
1807: }
1808:
1809: /**
1810: * @param string
1811: */
1812: public void setTargetType(String string) {
1813: this .targetType = string;
1814: }
1815:
1816: /**
1817: * @param string
1818: */
1819: public void setDescription(String string) {
1820: this .description = new FlexibleStringExpander(string);
1821: }
1822:
1823: /**
1824: * @param string
1825: */
1826: public void setTarget(String string) {
1827: this .target = new FlexibleStringExpander(string);
1828: }
1829: }
1830:
1831: public static class SubHyperlink {
1832: protected FlexibleStringExpander useWhen;
1833: protected String linkStyle;
1834: protected String targetType;
1835: protected FlexibleStringExpander target;
1836: protected FlexibleStringExpander description;
1837:
1838: public SubHyperlink(Element element) {
1839: this .setDescription(element.getAttribute("description"));
1840: this .setTarget(element.getAttribute("target"));
1841: this .setUseWhen(element.getAttribute("use-when"));
1842: this .linkStyle = element.getAttribute("link-style");
1843: this .targetType = element.getAttribute("target-type");
1844: }
1845:
1846: /**
1847: * @return
1848: */
1849: public String getLinkStyle() {
1850: return this .linkStyle;
1851: }
1852:
1853: /**
1854: * @return
1855: */
1856: public String getTargetType() {
1857: if (UtilValidate.isNotEmpty(this .targetType)) {
1858: return this .targetType;
1859: } else {
1860: return HyperlinkField.DEFAULT_TARGET_TYPE;
1861: }
1862: }
1863:
1864: /**
1865: * @return
1866: */
1867: public String getDescription(Map context) {
1868: if (this .description != null) {
1869: return this .description.expandString(context);
1870: } else {
1871: return "";
1872: }
1873: }
1874:
1875: /**
1876: * @return
1877: */
1878: public String getTarget(Map context) {
1879: if (this .target != null) {
1880: return this .target.expandString(context);
1881: } else {
1882: return "";
1883: }
1884: }
1885:
1886: /**
1887: * @return
1888: */
1889: public String getUseWhen(Map context) {
1890: if (this .useWhen != null) {
1891: return this .useWhen.expandString(context);
1892: } else {
1893: return "";
1894: }
1895: }
1896:
1897: public boolean shouldUse(Map context) {
1898: boolean shouldUse = true;
1899: String useWhen = this .getUseWhen(context);
1900: if (UtilValidate.isNotEmpty(useWhen)) {
1901: try {
1902: Interpreter bsh = (Interpreter) context
1903: .get("bshInterpreter");
1904: if (bsh == null) {
1905: bsh = BshUtil.makeInterpreter(context);
1906: context.put("bshInterpreter", bsh);
1907: }
1908:
1909: Object retVal = bsh.eval(useWhen);
1910:
1911: // retVal should be a Boolean, if not something weird is up...
1912: if (retVal instanceof Boolean) {
1913: Boolean boolVal = (Boolean) retVal;
1914: shouldUse = boolVal.booleanValue();
1915: } else {
1916: throw new IllegalArgumentException(
1917: "Return value from target condition eval was not a Boolean: "
1918: + retVal.getClass().getName()
1919: + " [" + retVal + "]");
1920: }
1921: } catch (EvalError e) {
1922: String errmsg = "Error evaluating BeanShell target conditions";
1923: Debug.logError(e, errmsg, module);
1924: throw new IllegalArgumentException(errmsg);
1925: }
1926: }
1927: return shouldUse;
1928: }
1929:
1930: /**
1931: * @param string
1932: */
1933: public void setLinkStyle(String string) {
1934: this .linkStyle = string;
1935: }
1936:
1937: /**
1938: * @param string
1939: */
1940: public void setTargetType(String string) {
1941: this .targetType = string;
1942: }
1943:
1944: /**
1945: * @param string
1946: */
1947: public void setDescription(String string) {
1948: this .description = new FlexibleStringExpander(string);
1949: }
1950:
1951: /**
1952: * @param string
1953: */
1954: public void setTarget(String string) {
1955: this .target = new FlexibleStringExpander(string);
1956: }
1957:
1958: /**
1959: * @param string
1960: */
1961: public void setUseWhen(String string) {
1962: this .useWhen = new FlexibleStringExpander(string);
1963: }
1964: }
1965:
1966: public static class TextField extends FieldInfo {
1967: protected int size = 25;
1968: protected Integer maxlength;
1969: protected FlexibleStringExpander defaultValue;
1970: protected SubHyperlink subHyperlink;
1971:
1972: protected TextField() {
1973: super ();
1974: }
1975:
1976: public TextField(ModelFormField modelFormField) {
1977: super (FieldInfo.SOURCE_EXPLICIT, FieldInfo.TEXT,
1978: modelFormField);
1979: }
1980:
1981: public TextField(int fieldSource, ModelFormField modelFormField) {
1982: super (fieldSource, FieldInfo.TEXT, modelFormField);
1983: }
1984:
1985: public TextField(Element element, ModelFormField modelFormField) {
1986: super (element, modelFormField);
1987: this .setDefaultValue(element.getAttribute("default-value"));
1988:
1989: String sizeStr = element.getAttribute("size");
1990: try {
1991: size = Integer.parseInt(sizeStr);
1992: } catch (Exception e) {
1993: if (sizeStr != null && sizeStr.length() > 0) {
1994: Debug.logError(
1995: "Could not parse the size value of the text element: ["
1996: + sizeStr
1997: + "], setting to the default of "
1998: + size, module);
1999: }
2000: }
2001:
2002: String maxlengthStr = element.getAttribute("maxlength");
2003: try {
2004: maxlength = Integer.valueOf(maxlengthStr);
2005: } catch (Exception e) {
2006: maxlength = null;
2007: if (maxlengthStr != null && maxlengthStr.length() > 0) {
2008: Debug
2009: .logError(
2010: "Could not parse the size value of the text element: ["
2011: + sizeStr
2012: + "], setting to null; default of no maxlength will be used",
2013: module);
2014: }
2015: }
2016:
2017: Element subHyperlinkElement = UtilXml.firstChildElement(
2018: element, "sub-hyperlink");
2019: if (subHyperlinkElement != null) {
2020: this .subHyperlink = new SubHyperlink(
2021: subHyperlinkElement);
2022: }
2023: }
2024:
2025: public void renderFieldString(StringBuffer buffer, Map context,
2026: FormStringRenderer formStringRenderer) {
2027: formStringRenderer.renderTextField(buffer, context, this );
2028: }
2029:
2030: /**
2031: * @return
2032: */
2033: public Integer getMaxlength() {
2034: return maxlength;
2035: }
2036:
2037: /**
2038: * @return
2039: */
2040: public int getSize() {
2041: return size;
2042: }
2043:
2044: /**
2045: * @return
2046: */
2047: public String getDefaultValue(Map context) {
2048: if (this .defaultValue != null) {
2049: return this .defaultValue.expandString(context);
2050: } else {
2051: return "";
2052: }
2053: }
2054:
2055: /**
2056: * @param integer
2057: */
2058: public void setMaxlength(Integer integer) {
2059: maxlength = integer;
2060: }
2061:
2062: /**
2063: * @param i
2064: */
2065: public void setSize(int i) {
2066: size = i;
2067: }
2068:
2069: /**
2070: * @param str
2071: */
2072: public void setDefaultValue(String str) {
2073: this .defaultValue = new FlexibleStringExpander(str);
2074: }
2075:
2076: public SubHyperlink getSubHyperlink() {
2077: return this .subHyperlink;
2078: }
2079:
2080: public void setSubHyperlink(SubHyperlink newSubHyperlink) {
2081: this .subHyperlink = newSubHyperlink;
2082: }
2083: }
2084:
2085: public static class TextareaField extends FieldInfo {
2086: protected int cols = 60;
2087: protected int rows = 2;
2088: protected FlexibleStringExpander defaultValue;
2089:
2090: protected TextareaField() {
2091: super ();
2092: }
2093:
2094: public TextareaField(ModelFormField modelFormField) {
2095: super (FieldInfo.SOURCE_EXPLICIT, FieldInfo.TEXTAREA,
2096: modelFormField);
2097: }
2098:
2099: public TextareaField(int fieldSource,
2100: ModelFormField modelFormField) {
2101: super (fieldSource, FieldInfo.TEXTAREA, modelFormField);
2102: }
2103:
2104: public TextareaField(Element element,
2105: ModelFormField modelFormField) {
2106: super (element, modelFormField);
2107: this .setDefaultValue(element.getAttribute("default-value"));
2108:
2109: String colsStr = element.getAttribute("cols");
2110: try {
2111: cols = Integer.parseInt(colsStr);
2112: } catch (Exception e) {
2113: if (colsStr != null && colsStr.length() > 0) {
2114: Debug.logError(
2115: "Could not parse the size value of the text element: ["
2116: + colsStr
2117: + "], setting to default of "
2118: + cols, module);
2119: }
2120: }
2121:
2122: String rowsStr = element.getAttribute("rows");
2123: try {
2124: rows = Integer.parseInt(rowsStr);
2125: } catch (Exception e) {
2126: if (rowsStr != null && rowsStr.length() > 0) {
2127: Debug.logError(
2128: "Could not parse the size value of the text element: ["
2129: + rowsStr
2130: + "], setting to default of "
2131: + rows, module);
2132: }
2133: }
2134: }
2135:
2136: public void renderFieldString(StringBuffer buffer, Map context,
2137: FormStringRenderer formStringRenderer) {
2138: formStringRenderer.renderTextareaField(buffer, context,
2139: this );
2140: }
2141:
2142: /**
2143: * @return
2144: */
2145: public int getCols() {
2146: return cols;
2147: }
2148:
2149: /**
2150: * @return
2151: */
2152: public int getRows() {
2153: return rows;
2154: }
2155:
2156: /**
2157: * @return
2158: */
2159: public String getDefaultValue(Map context) {
2160: if (this .defaultValue != null) {
2161: return this .defaultValue.expandString(context);
2162: } else {
2163: return "";
2164: }
2165: }
2166:
2167: /**
2168: * @param i
2169: */
2170: public void setCols(int i) {
2171: cols = i;
2172: }
2173:
2174: /**
2175: * @param i
2176: */
2177: public void setRows(int i) {
2178: rows = i;
2179: }
2180:
2181: /**
2182: * @param str
2183: */
2184: public void setDefaultValue(String str) {
2185: this .defaultValue = new FlexibleStringExpander(str);
2186: }
2187: }
2188:
2189: public static class DateTimeField extends FieldInfo {
2190: protected String type;
2191: protected FlexibleStringExpander defaultValue;
2192:
2193: protected DateTimeField() {
2194: super ();
2195: }
2196:
2197: public DateTimeField(ModelFormField modelFormField) {
2198: super (FieldInfo.SOURCE_EXPLICIT, FieldInfo.DATE_TIME,
2199: modelFormField);
2200: }
2201:
2202: public DateTimeField(int fieldSource,
2203: ModelFormField modelFormField) {
2204: super (fieldSource, FieldInfo.DATE_TIME, modelFormField);
2205: }
2206:
2207: public DateTimeField(Element element,
2208: ModelFormField modelFormField) {
2209: super (element, modelFormField);
2210: this .setDefaultValue(element.getAttribute("default-value"));
2211: type = element.getAttribute("type");
2212: }
2213:
2214: public void renderFieldString(StringBuffer buffer, Map context,
2215: FormStringRenderer formStringRenderer) {
2216: formStringRenderer.renderDateTimeField(buffer, context,
2217: this );
2218: }
2219:
2220: /**
2221: * @return
2222: */
2223: public String getType() {
2224: return type;
2225: }
2226:
2227: /**
2228: * @return
2229: */
2230: public String getDefaultValue(Map context) {
2231: if (this .defaultValue != null) {
2232: return this .defaultValue.expandString(context);
2233: } else {
2234: return "";
2235: }
2236: }
2237:
2238: /**
2239: * @param string
2240: */
2241: public void setType(String string) {
2242: type = string;
2243: }
2244:
2245: /**
2246: * @param str
2247: */
2248: public void setDefaultValue(String str) {
2249: this .defaultValue = new FlexibleStringExpander(str);
2250: }
2251:
2252: /**
2253: * Returns the default-value if specified, otherwise the current date, time or timestamp
2254: *
2255: * @param context Context Map
2256: * @return Default value string for date-time
2257: */
2258: public String getDefaultDateTimeString(Map context) {
2259: if (this .defaultValue != null
2260: && !this .defaultValue.isEmpty()) {
2261: return this .getDefaultValue(context);
2262: }
2263:
2264: if ("date".equals(this .type)) {
2265: return (new java.sql.Date(System.currentTimeMillis()))
2266: .toString();
2267: } else if ("time".equals(this .type)) {
2268: return (new java.sql.Time(System.currentTimeMillis()))
2269: .toString();
2270: } else {
2271: return UtilDateTime.nowTimestamp().toString();
2272: }
2273: }
2274: }
2275:
2276: public static class DropDownField extends FieldInfoWithOptions {
2277: protected boolean allowEmpty = false;
2278: protected String current;
2279: protected FlexibleStringExpander currentDescription;
2280: protected SubHyperlink subHyperlink;
2281:
2282: protected DropDownField() {
2283: super ();
2284: }
2285:
2286: public DropDownField(ModelFormField modelFormField) {
2287: super (FieldInfo.SOURCE_EXPLICIT, FieldInfo.DROP_DOWN,
2288: modelFormField);
2289: }
2290:
2291: public DropDownField(int fieldSource,
2292: ModelFormField modelFormField) {
2293: super (fieldSource, FieldInfo.DROP_DOWN, modelFormField);
2294: }
2295:
2296: public DropDownField(Element element,
2297: ModelFormField modelFormField) {
2298: super (element, modelFormField);
2299:
2300: this .current = element.getAttribute("current");
2301: this .allowEmpty = "true".equals(element
2302: .getAttribute("allow-empty"));
2303: this .currentDescription = new FlexibleStringExpander(
2304: element.getAttribute("current-description"));
2305:
2306: Element subHyperlinkElement = UtilXml.firstChildElement(
2307: element, "sub-hyperlink");
2308: if (subHyperlinkElement != null) {
2309: this .subHyperlink = new SubHyperlink(
2310: subHyperlinkElement);
2311: }
2312: }
2313:
2314: public void renderFieldString(StringBuffer buffer, Map context,
2315: FormStringRenderer formStringRenderer) {
2316: formStringRenderer.renderDropDownField(buffer, context,
2317: this );
2318: }
2319:
2320: public boolean isAllowEmpty() {
2321: return this .allowEmpty;
2322: }
2323:
2324: public String getCurrent() {
2325: if (UtilValidate.isEmpty(this .current)) {
2326: return "first-in-list";
2327: } else {
2328: return this .current;
2329: }
2330: }
2331:
2332: public String getCurrentDescription(Map context) {
2333: if (this .currentDescription == null)
2334: return null;
2335: else
2336: return this .currentDescription.expandString(context);
2337: }
2338:
2339: public void setAllowEmpty(boolean b) {
2340: this .allowEmpty = b;
2341: }
2342:
2343: public void setCurrent(String string) {
2344: this .current = string;
2345: }
2346:
2347: public void setCurrentDescription(String string) {
2348: this .currentDescription = new FlexibleStringExpander(string);
2349: }
2350:
2351: public SubHyperlink getSubHyperlink() {
2352: return this .subHyperlink;
2353: }
2354:
2355: public void setSubHyperlink(SubHyperlink newSubHyperlink) {
2356: this .subHyperlink = newSubHyperlink;
2357: }
2358: }
2359:
2360: public static class RadioField extends FieldInfoWithOptions {
2361: protected RadioField() {
2362: super ();
2363: }
2364:
2365: public RadioField(ModelFormField modelFormField) {
2366: super (FieldInfo.SOURCE_EXPLICIT, FieldInfo.RADIO,
2367: modelFormField);
2368: }
2369:
2370: public RadioField(int fieldSource, ModelFormField modelFormField) {
2371: super (fieldSource, FieldInfo.RADIO, modelFormField);
2372: }
2373:
2374: public RadioField(Element element, ModelFormField modelFormField) {
2375: super (element, modelFormField);
2376: }
2377:
2378: public void renderFieldString(StringBuffer buffer, Map context,
2379: FormStringRenderer formStringRenderer) {
2380: formStringRenderer.renderRadioField(buffer, context, this );
2381: }
2382: }
2383:
2384: public static class CheckField extends FieldInfo {
2385: protected CheckField() {
2386: super ();
2387: }
2388:
2389: public CheckField(ModelFormField modelFormField) {
2390: super (FieldInfo.SOURCE_EXPLICIT, FieldInfo.CHECK,
2391: modelFormField);
2392: }
2393:
2394: public CheckField(int fieldSource, ModelFormField modelFormField) {
2395: super (fieldSource, FieldInfo.CHECK, modelFormField);
2396: }
2397:
2398: public CheckField(Element element, ModelFormField modelFormField) {
2399: super (element, modelFormField);
2400: }
2401:
2402: public void renderFieldString(StringBuffer buffer, Map context,
2403: FormStringRenderer formStringRenderer) {
2404: formStringRenderer.renderCheckField(buffer, context, this );
2405: }
2406: }
2407:
2408: public static class SubmitField extends FieldInfo {
2409: protected String buttonType;
2410: protected String imageLocation;
2411:
2412: protected SubmitField() {
2413: super ();
2414: }
2415:
2416: public SubmitField(ModelFormField modelFormField) {
2417: super (FieldInfo.SOURCE_EXPLICIT, FieldInfo.SUBMIT,
2418: modelFormField);
2419: }
2420:
2421: public SubmitField(int fieldInfo, ModelFormField modelFormField) {
2422: super (fieldInfo, FieldInfo.SUBMIT, modelFormField);
2423: }
2424:
2425: public SubmitField(Element element,
2426: ModelFormField modelFormField) {
2427: super (element, modelFormField);
2428: this .buttonType = element.getAttribute("button-type");
2429: this .imageLocation = element.getAttribute("image-location");
2430: }
2431:
2432: public void renderFieldString(StringBuffer buffer, Map context,
2433: FormStringRenderer formStringRenderer) {
2434: formStringRenderer.renderSubmitField(buffer, context, this );
2435: }
2436:
2437: /**
2438: * @return
2439: */
2440: public String getButtonType() {
2441: return buttonType;
2442: }
2443:
2444: /**
2445: * @return
2446: */
2447: public String getImageLocation() {
2448: return imageLocation;
2449: }
2450:
2451: /**
2452: * @param string
2453: */
2454: public void setButtonType(String string) {
2455: buttonType = string;
2456: }
2457:
2458: /**
2459: * @param string
2460: */
2461: public void setImageLocation(String string) {
2462: imageLocation = string;
2463: }
2464: }
2465:
2466: public static class ResetField extends FieldInfo {
2467: protected ResetField() {
2468: super ();
2469: }
2470:
2471: public ResetField(ModelFormField modelFormField) {
2472: super (FieldInfo.SOURCE_EXPLICIT, FieldInfo.RESET,
2473: modelFormField);
2474: }
2475:
2476: public ResetField(int fieldSource, ModelFormField modelFormField) {
2477: super (fieldSource, FieldInfo.RESET, modelFormField);
2478: }
2479:
2480: public ResetField(Element element, ModelFormField modelFormField) {
2481: super (element, modelFormField);
2482: }
2483:
2484: public void renderFieldString(StringBuffer buffer, Map context,
2485: FormStringRenderer formStringRenderer) {
2486: formStringRenderer.renderResetField(buffer, context, this );
2487: }
2488: }
2489:
2490: public static class HiddenField extends FieldInfo {
2491: protected FlexibleStringExpander value;
2492:
2493: protected HiddenField() {
2494: super ();
2495: }
2496:
2497: public HiddenField(ModelFormField modelFormField) {
2498: super (FieldInfo.SOURCE_EXPLICIT, FieldInfo.HIDDEN,
2499: modelFormField);
2500: }
2501:
2502: public HiddenField(int fieldSource,
2503: ModelFormField modelFormField) {
2504: super (fieldSource, FieldInfo.HIDDEN, modelFormField);
2505: }
2506:
2507: public HiddenField(Element element,
2508: ModelFormField modelFormField) {
2509: super (element, modelFormField);
2510: this .setValue(element.getAttribute("value"));
2511: }
2512:
2513: public void renderFieldString(StringBuffer buffer, Map context,
2514: FormStringRenderer formStringRenderer) {
2515: formStringRenderer.renderHiddenField(buffer, context, this );
2516: }
2517:
2518: public String getValue(Map context) {
2519: if (this .value != null && !this .value.isEmpty()) {
2520: return this .value.expandString(context);
2521: } else {
2522: return modelFormField.getEntry(context);
2523: }
2524: }
2525:
2526: public void setValue(String string) {
2527: this .value = new FlexibleStringExpander(string);
2528: }
2529: }
2530:
2531: public static class IgnoredField extends FieldInfo {
2532: protected IgnoredField() {
2533: super ();
2534: }
2535:
2536: public IgnoredField(ModelFormField modelFormField) {
2537: super (FieldInfo.SOURCE_EXPLICIT, FieldInfo.IGNORED,
2538: modelFormField);
2539: }
2540:
2541: public IgnoredField(int fieldSource,
2542: ModelFormField modelFormField) {
2543: super (fieldSource, FieldInfo.IGNORED, modelFormField);
2544: }
2545:
2546: public IgnoredField(Element element,
2547: ModelFormField modelFormField) {
2548: super (element, modelFormField);
2549: }
2550:
2551: public void renderFieldString(StringBuffer buffer, Map context,
2552: FormStringRenderer formStringRenderer) {
2553: formStringRenderer
2554: .renderIgnoredField(buffer, context, this );
2555: }
2556: }
2557:
2558: public static class TextFindField extends TextField {
2559: public TextFindField(Element element,
2560: ModelFormField modelFormField) {
2561: super (element, modelFormField);
2562: }
2563:
2564: public TextFindField(int fieldSource,
2565: ModelFormField modelFormField) {
2566: super (fieldSource, modelFormField);
2567: }
2568:
2569: public void renderFieldString(StringBuffer buffer, Map context,
2570: FormStringRenderer formStringRenderer) {
2571: formStringRenderer.renderTextFindField(buffer, context,
2572: this );
2573: }
2574: }
2575:
2576: public static class DateFindField extends DateTimeField {
2577: public DateFindField(Element element,
2578: ModelFormField modelFormField) {
2579: super (element, modelFormField);
2580: }
2581:
2582: public DateFindField(int fieldSource,
2583: ModelFormField modelFormField) {
2584: super (fieldSource, modelFormField);
2585: }
2586:
2587: public void renderFieldString(StringBuffer buffer, Map context,
2588: FormStringRenderer formStringRenderer) {
2589: formStringRenderer.renderDateFindField(buffer, context,
2590: this );
2591: }
2592: }
2593:
2594: public static class RangeFindField extends TextField {
2595: public RangeFindField(Element element,
2596: ModelFormField modelFormField) {
2597: super (element, modelFormField);
2598: }
2599:
2600: public RangeFindField(int fieldSource,
2601: ModelFormField modelFormField) {
2602: super (fieldSource, modelFormField);
2603: }
2604:
2605: public void renderFieldString(StringBuffer buffer, Map context,
2606: FormStringRenderer formStringRenderer) {
2607: formStringRenderer.renderRangeFindField(buffer, context,
2608: this );
2609: }
2610: }
2611:
2612: public static class LookupField extends TextField {
2613: protected String formName;
2614:
2615: public LookupField(Element element,
2616: ModelFormField modelFormField) {
2617: super (element, modelFormField);
2618: this .formName = element.getAttribute("target-form-name");
2619: }
2620:
2621: public LookupField(int fieldSource,
2622: ModelFormField modelFormField) {
2623: super (fieldSource, modelFormField);
2624: }
2625:
2626: public void renderFieldString(StringBuffer buffer, Map context,
2627: FormStringRenderer formStringRenderer) {
2628: formStringRenderer.renderLookupField(buffer, context, this );
2629: }
2630:
2631: public String getFormName() {
2632: return this .formName;
2633: }
2634:
2635: public void setFormName(String str) {
2636: this .formName = str;
2637: }
2638: }
2639:
2640: public static class FileField extends TextField {
2641:
2642: public FileField(Element element, ModelFormField modelFormField) {
2643: super (element, modelFormField);
2644: }
2645:
2646: public FileField(int fieldSource, ModelFormField modelFormField) {
2647: super (fieldSource, modelFormField);
2648: }
2649:
2650: public void renderFieldString(StringBuffer buffer, Map context,
2651: FormStringRenderer formStringRenderer) {
2652: formStringRenderer.renderFileField(buffer, context, this);
2653: }
2654: }
2655: }
|