Added: ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelFormFieldBuilder.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelFormFieldBuilder.java?rev=1652852&view=auto ============================================================================== --- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelFormFieldBuilder.java (added) +++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelFormFieldBuilder.java Sun Jan 18 21:03:40 2015 @@ -0,0 +1,921 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.widget.model; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.UtilXml; +import org.ofbiz.base.util.collections.FlexibleMapAccessor; +import org.ofbiz.base.util.string.FlexibleStringExpander; +import org.ofbiz.entity.GenericEntityException; +import org.ofbiz.entity.model.ModelEntity; +import org.ofbiz.entity.model.ModelField; +import org.ofbiz.entity.model.ModelReader; +import org.ofbiz.service.DispatchContext; +import org.ofbiz.service.GenericServiceException; +import org.ofbiz.service.ModelParam; +import org.ofbiz.service.ModelService; +import org.ofbiz.widget.model.ModelForm.UpdateArea; +import org.ofbiz.widget.model.ModelFormField.CheckField; +import org.ofbiz.widget.model.ModelFormField.ContainerField; +import org.ofbiz.widget.model.ModelFormField.DateFindField; +import org.ofbiz.widget.model.ModelFormField.DateTimeField; +import org.ofbiz.widget.model.ModelFormField.DisplayEntityField; +import org.ofbiz.widget.model.ModelFormField.DisplayField; +import org.ofbiz.widget.model.ModelFormField.DropDownField; +import org.ofbiz.widget.model.ModelFormField.FileField; +import org.ofbiz.widget.model.ModelFormField.HiddenField; +import org.ofbiz.widget.model.ModelFormField.HyperlinkField; +import org.ofbiz.widget.model.ModelFormField.IgnoredField; +import org.ofbiz.widget.model.ModelFormField.ImageField; +import org.ofbiz.widget.model.ModelFormField.LookupField; +import org.ofbiz.widget.model.ModelFormField.OptionSource; +import org.ofbiz.widget.model.ModelFormField.PasswordField; +import org.ofbiz.widget.model.ModelFormField.RadioField; +import org.ofbiz.widget.model.ModelFormField.RangeFindField; +import org.ofbiz.widget.model.ModelFormField.ResetField; +import org.ofbiz.widget.model.ModelFormField.SubmitField; +import org.ofbiz.widget.model.ModelFormField.TextField; +import org.ofbiz.widget.model.ModelFormField.TextFindField; +import org.ofbiz.widget.model.ModelFormField.TextareaField; +import org.w3c.dom.Element; + +/** + * A <code>ModelFormField</code> builder. + */ +public class ModelFormFieldBuilder { + + public static final String module = ModelFormFieldBuilder.class.getName(); + + private FlexibleStringExpander action = FlexibleStringExpander.getInstance("");; + private String attributeName = ""; + private boolean encodeOutput = true; + private String entityName = ""; + private FlexibleMapAccessor<Object> entryAcsr = null; + private String event = ""; + private FieldInfo fieldInfo = null; + private String fieldName = ""; + private String fieldType = null; + private String headerLink = ""; + private String headerLinkStyle = ""; + private String idName = ""; + private FlexibleMapAccessor<Map<String, ? extends Object>> mapAcsr = null; + private ModelForm modelForm = null; + private String name = ""; + private List<UpdateArea> onChangeUpdateAreas = new ArrayList<UpdateArea>(); + private List<UpdateArea> onClickUpdateAreas = new ArrayList<UpdateArea>(); + private String parameterName = ""; + private Integer position = null; + private String redWhen = ""; + private Boolean requiredField = null; + private String requiredFieldStyle = ""; + private boolean separateColumn = false; + private String serviceName = ""; + private Boolean sortField = null; + private String sortFieldAscStyle = ""; + private String sortFieldDescStyle = ""; + private String sortFieldHelpText = ""; + private String sortFieldStyle = ""; + private FlexibleStringExpander title = FlexibleStringExpander.getInstance("");; + private String titleAreaStyle = ""; + private String titleStyle = ""; + private FlexibleStringExpander tooltip = FlexibleStringExpander.getInstance("");; + private String tooltipStyle = ""; + private FlexibleStringExpander useWhen = FlexibleStringExpander.getInstance("");; + private String widgetAreaStyle = ""; + private String widgetStyle = ""; + + public ModelFormFieldBuilder() { + } + + /** XML Constructor */ + public ModelFormFieldBuilder(Element fieldElement, ModelForm modelForm, ModelReader entityModelReader, + DispatchContext dispatchContext) { + String name = fieldElement.getAttribute("name"); + this.action = FlexibleStringExpander.getInstance(fieldElement.getAttribute("action")); + this.attributeName = UtilXml.checkEmpty(fieldElement.getAttribute("attribute-name"), name); + this.encodeOutput = !"false".equals(fieldElement.getAttribute("encode-output")); + this.entityName = fieldElement.getAttribute("entity-name"); + this.entryAcsr = FlexibleMapAccessor.getInstance(UtilXml.checkEmpty(fieldElement.getAttribute("entry-name"), name)); + this.event = fieldElement.getAttribute("event"); + this.fieldName = UtilXml.checkEmpty(fieldElement.getAttribute("field-name"), name); + this.headerLink = fieldElement.getAttribute("header-link"); + this.headerLinkStyle = fieldElement.getAttribute("header-link-style"); + this.idName = fieldElement.getAttribute("id-name"); + this.mapAcsr = FlexibleMapAccessor.getInstance(fieldElement.getAttribute("map-name")); + this.modelForm = modelForm; + this.name = name; + this.parameterName = UtilXml.checkEmpty(fieldElement.getAttribute("parameter-name"), name); + String positionAtttr = fieldElement.getAttribute("position"); + Integer position = null; + if (!positionAtttr.isEmpty()) { + position = Integer.parseInt(positionAtttr); + } + this.position = position; + this.redWhen = fieldElement.getAttribute("red-when"); + String requiredField = fieldElement.getAttribute("required-field"); + this.requiredField = requiredField.isEmpty() ? null : "true".equals(requiredField); + this.requiredFieldStyle = fieldElement.getAttribute("required-field-style"); + this.separateColumn = "true".equals(fieldElement.getAttribute("separate-column")); + this.serviceName = fieldElement.getAttribute("service-name"); + String sortField = fieldElement.getAttribute("sort-field"); + this.sortField = sortField.isEmpty() ? null : "true".equals(sortField); + this.sortFieldAscStyle = fieldElement.getAttribute("sort-field-asc-style"); + this.sortFieldDescStyle = fieldElement.getAttribute("sort-field-desc-style"); + this.sortFieldHelpText = fieldElement.getAttribute("sort-field-help-text"); + this.sortFieldStyle = fieldElement.getAttribute("sort-field-style"); + this.title = FlexibleStringExpander.getInstance(fieldElement.getAttribute("title")); + this.titleAreaStyle = fieldElement.getAttribute("title-area-style"); + this.titleStyle = fieldElement.getAttribute("title-style"); + this.tooltip = FlexibleStringExpander.getInstance(fieldElement.getAttribute("tooltip")); + this.tooltipStyle = fieldElement.getAttribute("tooltip-style"); + this.useWhen = FlexibleStringExpander.getInstance(fieldElement.getAttribute("use-when")); + this.widgetAreaStyle = fieldElement.getAttribute("widget-area-style"); + this.widgetStyle = fieldElement.getAttribute("widget-style"); + Element childElement = null; + List<? extends Element> subElements = UtilXml.childElementList(fieldElement); + for (Element subElement : subElements) { + String subElementName = subElement.getTagName(); + if ("on-field-event-update-area".equals(subElementName)) { + UpdateArea updateArea = new UpdateArea(subElement); + if ("change".equals(updateArea.getEventType())) + onChangeUpdateAreas.add(updateArea); + else if ("click".equals(updateArea.getEventType())) + onClickUpdateAreas.add(updateArea); + } else { + if (this.fieldType != null) { + throw new IllegalArgumentException("Multiple field types found: " + this.fieldType + ", " + subElementName); + } + this.fieldType = subElementName; + childElement = subElement; + } + } + if (UtilValidate.isEmpty(this.fieldType)) { + this.induceFieldInfo(modelForm, null, entityModelReader, dispatchContext); + } else if ("display".equals(this.fieldType)) + this.fieldInfo = new DisplayField(childElement, null); + else if ("display-entity".equals(this.fieldType)) + this.fieldInfo = new DisplayEntityField(childElement, null); + else if ("hyperlink".equals(this.fieldType)) + this.fieldInfo = new HyperlinkField(childElement, null); + else if ("text".equals(this.fieldType)) + this.fieldInfo = new TextField(childElement, null); + else if ("textarea".equals(this.fieldType)) + this.fieldInfo = new TextareaField(childElement, null); + else if ("date-time".equals(this.fieldType)) + this.fieldInfo = new DateTimeField(childElement, null); + else if ("drop-down".equals(this.fieldType)) + this.fieldInfo = new DropDownField(childElement, null); + else if ("check".equals(this.fieldType)) + this.fieldInfo = new CheckField(childElement, null); + else if ("radio".equals(this.fieldType)) + this.fieldInfo = new RadioField(childElement, null); + else if ("submit".equals(this.fieldType)) + this.fieldInfo = new SubmitField(childElement, null); + else if ("reset".equals(this.fieldType)) + this.fieldInfo = new ResetField(childElement, null); + else if ("hidden".equals(this.fieldType)) + this.fieldInfo = new HiddenField(childElement, null); + else if ("ignored".equals(this.fieldType)) + this.fieldInfo = new IgnoredField(childElement, null); + else if ("text-find".equals(this.fieldType)) + this.fieldInfo = new TextFindField(childElement, null); + else if ("date-find".equals(this.fieldType)) + this.fieldInfo = new DateFindField(childElement, null); + else if ("range-find".equals(this.fieldType)) + this.fieldInfo = new RangeFindField(childElement, null); + else if ("lookup".equals(this.fieldType)) + this.fieldInfo = new LookupField(childElement, null); + else if ("file".equals(this.fieldType)) + this.fieldInfo = new FileField(childElement, null); + else if ("password".equals(this.fieldType)) + this.fieldInfo = new PasswordField(childElement, null); + else if ("image".equals(this.fieldType)) + this.fieldInfo = new ImageField(childElement, null); + else if ("container".equals(this.fieldType)) + this.fieldInfo = new ContainerField(childElement, null); + else + throw new IllegalArgumentException("The field sub-element with name " + this.fieldType + " is not supported"); + } + + public ModelFormFieldBuilder(ModelFormField modelFormField) { + this.action = modelFormField.getAction(); + this.attributeName = modelFormField.getAttributeName(); + this.encodeOutput = modelFormField.getEncodeOutput(); + this.entityName = modelFormField.getEntityName(); + this.entryAcsr = modelFormField.getEntryAcsr(); + this.event = modelFormField.getEvent(); + this.fieldInfo = modelFormField.getFieldInfo(); + this.fieldName = modelFormField.getFieldName(); + this.headerLink = modelFormField.getHeaderLink(); + this.headerLinkStyle = modelFormField.getHeaderLinkStyle(); + this.idName = modelFormField.getIdName(); + this.mapAcsr = modelFormField.getMapAcsr(); + this.modelForm = modelFormField.getModelForm(); + this.name = modelFormField.getName(); + this.onChangeUpdateAreas.addAll(modelFormField.getOnChangeUpdateAreas()); + this.onClickUpdateAreas.addAll(modelFormField.getOnClickUpdateAreas()); + this.parameterName = modelFormField.getParameterName(); + this.position = modelFormField.getPosition(); + this.redWhen = modelFormField.getRedWhen(); + this.requiredField = modelFormField.getRequiredField(); + this.requiredFieldStyle = modelFormField.getRequiredFieldStyle(); + this.separateColumn = modelFormField.getSeparateColumn(); + this.serviceName = modelFormField.getServiceName(); + this.sortField = modelFormField.getSortField(); + this.sortFieldAscStyle = modelFormField.getSortFieldAscStyle(); + this.sortFieldDescStyle = modelFormField.getSortFieldDescStyle(); + this.sortFieldHelpText = modelFormField.getSortFieldHelpText(); + this.sortFieldStyle = modelFormField.getSortFieldStyle(); + this.title = modelFormField.getTitle(); + this.titleAreaStyle = modelFormField.getTitleAreaStyle(); + this.titleStyle = modelFormField.getTitleStyle(); + this.tooltip = modelFormField.getTooltip(); + this.tooltipStyle = modelFormField.getTooltipStyle(); + this.useWhen = modelFormField.getUseWhen(); + this.widgetAreaStyle = modelFormField.getWidgetAreaStyle(); + this.widgetStyle = modelFormField.getWidgetStyle(); + } + + public ModelFormFieldBuilder(ModelFormFieldBuilder builder) { + this.action = builder.getAction(); + this.attributeName = builder.getAttributeName(); + this.encodeOutput = builder.getEncodeOutput(); + this.entityName = builder.getEntityName(); + this.entryAcsr = builder.getEntryAcsr(); + this.event = builder.getEvent(); + this.fieldInfo = builder.getFieldInfo(); + this.fieldName = builder.getFieldName(); + this.headerLink = builder.getHeaderLink(); + this.headerLinkStyle = builder.getHeaderLinkStyle(); + this.idName = builder.getIdName(); + this.mapAcsr = builder.getMapAcsr(); + this.modelForm = builder.getModelForm(); + this.name = builder.getName(); + this.onChangeUpdateAreas.addAll(builder.getOnChangeUpdateAreas()); + this.onClickUpdateAreas.addAll(builder.getOnClickUpdateAreas()); + this.parameterName = builder.getParameterName(); + this.position = builder.getPosition(); + this.redWhen = builder.getRedWhen(); + this.requiredField = builder.getRequiredField(); + this.requiredFieldStyle = builder.getRequiredFieldStyle(); + this.separateColumn = builder.getSeparateColumn(); + this.serviceName = builder.getServiceName(); + this.sortField = builder.getSortField(); + this.sortFieldAscStyle = builder.getSortFieldAscStyle(); + this.sortFieldDescStyle = builder.getSortFieldDescStyle(); + this.sortFieldHelpText = builder.getSortFieldHelpText(); + this.sortFieldStyle = builder.getSortFieldStyle(); + this.title = builder.getTitle(); + this.titleAreaStyle = builder.getTitleAreaStyle(); + this.titleStyle = builder.getTitleStyle(); + this.tooltip = builder.getTooltip(); + this.tooltipStyle = builder.getTooltipStyle(); + this.useWhen = builder.getUseWhen(); + this.widgetAreaStyle = builder.getWidgetAreaStyle(); + this.widgetStyle = builder.getWidgetStyle(); + } + + public ModelFormFieldBuilder addOnChangeUpdateArea(UpdateArea onChangeUpdateArea) { + this.onChangeUpdateAreas.add(onChangeUpdateArea); + return this; + } + + public ModelFormFieldBuilder addOnClickUpdateArea(UpdateArea onClickUpdateArea) { + this.onClickUpdateAreas.add(onClickUpdateArea); + return this; + } + + public ModelFormField build() { + return ModelFormField.from(this); + } + + public FlexibleStringExpander getAction() { + return action; + } + + public String getAttributeName() { + return attributeName; + } + + public boolean getEncodeOutput() { + return encodeOutput; + } + + public String getEntityName() { + return entityName; + } + + public FlexibleMapAccessor<Object> getEntryAcsr() { + return entryAcsr; + } + + public String getEvent() { + return event; + } + + public FieldInfo getFieldInfo() { + return fieldInfo; + } + + public String getFieldName() { + return fieldName; + } + + public String getFieldType() { + return fieldType; + } + + public String getHeaderLink() { + return headerLink; + } + + public String getHeaderLinkStyle() { + return headerLinkStyle; + } + + public String getIdName() { + return idName; + } + + public FlexibleMapAccessor<Map<String, ? extends Object>> getMapAcsr() { + return mapAcsr; + } + + public ModelForm getModelForm() { + return modelForm; + } + + public String getName() { + return name; + } + + public List<UpdateArea> getOnChangeUpdateAreas() { + return onChangeUpdateAreas; + } + + public List<UpdateArea> getOnClickUpdateAreas() { + return onClickUpdateAreas; + } + + public String getParameterName() { + return parameterName; + } + + public Integer getPosition() { + return position; + } + + public String getRedWhen() { + return redWhen; + } + + public Boolean getRequiredField() { + return requiredField; + } + + public String getRequiredFieldStyle() { + return requiredFieldStyle; + } + + public boolean getSeparateColumn() { + return separateColumn; + } + + public String getServiceName() { + return serviceName; + } + + public Boolean getSortField() { + return sortField; + } + + public String getSortFieldAscStyle() { + return sortFieldAscStyle; + } + + public String getSortFieldDescStyle() { + return sortFieldDescStyle; + } + + public String getSortFieldHelpText() { + return sortFieldHelpText; + } + + public String getSortFieldStyle() { + return sortFieldStyle; + } + + public FlexibleStringExpander getTitle() { + return title; + } + + public String getTitleAreaStyle() { + return titleAreaStyle; + } + + public String getTitleStyle() { + return titleStyle; + } + + public FlexibleStringExpander getTooltip() { + return tooltip; + } + + public String getTooltipStyle() { + return tooltipStyle; + } + + public FlexibleStringExpander getUseWhen() { + return useWhen; + } + + public String getWidgetAreaStyle() { + return widgetAreaStyle; + } + + public String getWidgetStyle() { + return widgetStyle; + } + + private boolean induceFieldInfo(ModelForm modelForm, String defaultFieldType, ModelReader entityModelReader, DispatchContext dispatchContext) { + if (induceFieldInfoFromEntityField(defaultFieldType, entityModelReader)) + return true; + if (induceFieldInfoFromServiceParam(defaultFieldType, entityModelReader, dispatchContext)) + return true; + return false; + } + + public boolean induceFieldInfoFromEntityField(ModelEntity modelEntity, ModelField modelField, String defaultFieldType) { + if (modelEntity == null || modelField == null) + return false; + this.entityName = modelEntity.getEntityName(); + this.fieldName = modelField.getName(); + if ("find".equals(defaultFieldType)) { + if ("id".equals(modelField.getType()) || "id-ne".equals(modelField.getType())) { + ModelFormField.TextFindField textField = new ModelFormField.TextFindField(FieldInfo.SOURCE_AUTO_ENTITY, 20, + Integer.valueOf(20), null); + this.setFieldInfo(textField); + } else if ("id-long".equals(modelField.getType()) || "id-long-ne".equals(modelField.getType())) { + ModelFormField.TextFindField textField = new ModelFormField.TextFindField(FieldInfo.SOURCE_AUTO_ENTITY, 40, + Integer.valueOf(60), null); + this.setFieldInfo(textField); + } else if ("id-vlong".equals(modelField.getType()) || "id-vlong-ne".equals(modelField.getType())) { + ModelFormField.TextFindField textField = new ModelFormField.TextFindField(FieldInfo.SOURCE_AUTO_ENTITY, 60, + Integer.valueOf(250), null); + this.setFieldInfo(textField); + } else if ("very-short".equals(modelField.getType())) { + ModelFormField.TextField textField = new ModelFormField.TextField(FieldInfo.SOURCE_AUTO_ENTITY, 6, + Integer.valueOf(10), null); + this.setFieldInfo(textField); + } else if ("name".equals(modelField.getType()) || "short-varchar".equals(modelField.getType())) { + ModelFormField.TextFindField textField = new ModelFormField.TextFindField(FieldInfo.SOURCE_AUTO_ENTITY, 40, + Integer.valueOf(60), null); + this.setFieldInfo(textField); + } else if ("value".equals(modelField.getType()) || "comment".equals(modelField.getType()) + || "description".equals(modelField.getType()) || "long-varchar".equals(modelField.getType()) + || "url".equals(modelField.getType()) || "email".equals(modelField.getType())) { + ModelFormField.TextFindField textField = new ModelFormField.TextFindField(FieldInfo.SOURCE_AUTO_ENTITY, 60, + Integer.valueOf(250), null); + this.setFieldInfo(textField); + } else if ("floating-point".equals(modelField.getType()) || "currency-amount".equals(modelField.getType()) + || "numeric".equals(modelField.getType())) { + ModelFormField.RangeFindField textField = new ModelFormField.RangeFindField(FieldInfo.SOURCE_AUTO_ENTITY, 6, null); + this.setFieldInfo(textField); + } else if ("date-time".equals(modelField.getType()) || "date".equals(modelField.getType()) + || "time".equals(modelField.getType())) { + String type = modelField.getType(); + if ("date-time".equals(modelField.getType())) { + type = "timestamp"; + } + ModelFormField.DateFindField dateTimeField = new ModelFormField.DateFindField(FieldInfo.SOURCE_AUTO_ENTITY, type); + this.setFieldInfo(dateTimeField); + } else { + ModelFormField.TextFindField textField = new ModelFormField.TextFindField(FieldInfo.SOURCE_AUTO_ENTITY, null); + this.setFieldInfo(textField); + } + } else if ("display".equals(defaultFieldType)) { + ModelFormField.DisplayField displayField = new ModelFormField.DisplayField(FieldInfo.SOURCE_AUTO_SERVICE, null); + this.setFieldInfo(displayField); + } else if ("hidden".equals(defaultFieldType)) { + ModelFormField.HiddenField hiddenField = new ModelFormField.HiddenField(FieldInfo.SOURCE_AUTO_SERVICE, null); + this.setFieldInfo(hiddenField); + } else { + if ("id".equals(modelField.getType()) || "id-ne".equals(modelField.getType())) { + ModelFormField.TextField textField = new ModelFormField.TextField(FieldInfo.SOURCE_AUTO_ENTITY, 20, + Integer.valueOf(20), null); + this.setFieldInfo(textField); + } else if ("id-long".equals(modelField.getType()) || "id-long-ne".equals(modelField.getType())) { + ModelFormField.TextField textField = new ModelFormField.TextField(FieldInfo.SOURCE_AUTO_ENTITY, 40, + Integer.valueOf(60), null); + this.setFieldInfo(textField); + } else if ("id-vlong".equals(modelField.getType()) || "id-vlong-ne".equals(modelField.getType())) { + ModelFormField.TextField textField = new ModelFormField.TextField(FieldInfo.SOURCE_AUTO_ENTITY, 60, + Integer.valueOf(250), null); + this.setFieldInfo(textField); + } else if ("indicator".equals(modelField.getType())) { + ArrayList<OptionSource> optionSources = new ArrayList<OptionSource>(); + optionSources.add(new ModelFormField.SingleOption("Y", null, null)); + optionSources.add(new ModelFormField.SingleOption("N", null, null)); + ModelFormField.DropDownField dropDownField = new ModelFormField.DropDownField(FieldInfo.SOURCE_AUTO_ENTITY, + optionSources); + this.setFieldInfo(dropDownField); + } else if ("very-short".equals(modelField.getType())) { + ModelFormField.TextField textField = new ModelFormField.TextField(FieldInfo.SOURCE_AUTO_ENTITY, 6, + Integer.valueOf(10), null); + this.setFieldInfo(textField); + } else if ("very-long".equals(modelField.getType())) { + ModelFormField.TextareaField textareaField = new ModelFormField.TextareaField(FieldInfo.SOURCE_AUTO_ENTITY, null); + this.setFieldInfo(textareaField); + } else if ("name".equals(modelField.getType()) || "short-varchar".equals(modelField.getType())) { + ModelFormField.TextField textField = new ModelFormField.TextField(FieldInfo.SOURCE_AUTO_ENTITY, 40, + Integer.valueOf(60), null); + this.setFieldInfo(textField); + } else if ("value".equals(modelField.getType()) || "comment".equals(modelField.getType()) + || "description".equals(modelField.getType()) || "long-varchar".equals(modelField.getType()) + || "url".equals(modelField.getType()) || "email".equals(modelField.getType())) { + ModelFormField.TextField textField = new ModelFormField.TextField(FieldInfo.SOURCE_AUTO_ENTITY, 60, + Integer.valueOf(250), null); + this.setFieldInfo(textField); + } else if ("floating-point".equals(modelField.getType()) || "currency-amount".equals(modelField.getType()) + || "numeric".equals(modelField.getType())) { + ModelFormField.TextField textField = new ModelFormField.TextField(FieldInfo.SOURCE_AUTO_ENTITY, 6, null, null); + this.setFieldInfo(textField); + } else if ("date-time".equals(modelField.getType()) || "date".equals(modelField.getType()) + || "time".equals(modelField.getType())) { + String type = modelField.getType(); + if ("date-time".equals(modelField.getType())) { + type = "timestamp"; + } + ModelFormField.DateTimeField dateTimeField = new ModelFormField.DateTimeField(FieldInfo.SOURCE_AUTO_ENTITY, type); + this.setFieldInfo(dateTimeField); + } else { + ModelFormField.TextField textField = new ModelFormField.TextField(FieldInfo.SOURCE_AUTO_ENTITY, null); + this.setFieldInfo(textField); + } + } + return true; + } + + private boolean induceFieldInfoFromEntityField(String defaultFieldType, ModelReader entityModelReader) { + if (UtilValidate.isEmpty(this.getEntityName()) || UtilValidate.isEmpty(this.getFieldName())) + return false; + try { + ModelEntity modelEntity = entityModelReader.getModelEntity(this.getEntityName()); + if (modelEntity != null) { + ModelField modelField = modelEntity.getField(this.getFieldName()); + if (modelField != null) { + // okay, populate using the entity field info... + this.induceFieldInfoFromEntityField(modelEntity, modelField, defaultFieldType); + return true; + } + } + } catch (GenericEntityException e) { + Debug.logError(e, 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(FieldInfo.SOURCE_AUTO_SERVICE, 6, + null); + this.setFieldInfo(textField); + } else if (modelParam.type.indexOf("Timestamp") != -1) { + ModelFormField.DateFindField dateTimeField = new ModelFormField.DateFindField(FieldInfo.SOURCE_AUTO_SERVICE, + "timestamp"); + this.setFieldInfo(dateTimeField); + } else if (modelParam.type.indexOf("Date") != -1) { + ModelFormField.DateFindField dateTimeField = new ModelFormField.DateFindField(FieldInfo.SOURCE_AUTO_SERVICE, + "date"); + this.setFieldInfo(dateTimeField); + } else if (modelParam.type.indexOf("Time") != -1) { + ModelFormField.DateFindField dateTimeField = new ModelFormField.DateFindField(FieldInfo.SOURCE_AUTO_SERVICE, + "time"); + this.setFieldInfo(dateTimeField); + } else { + ModelFormField.TextFindField textField = new ModelFormField.TextFindField(FieldInfo.SOURCE_AUTO_SERVICE, null); + this.setFieldInfo(textField); + } + } else if ("display".equals(defaultFieldType)) { + ModelFormField.DisplayField displayField = new ModelFormField.DisplayField(FieldInfo.SOURCE_AUTO_SERVICE, null); + 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(FieldInfo.SOURCE_AUTO_SERVICE, 6, null, null); + this.setFieldInfo(textField); + } else if (modelParam.type.indexOf("Timestamp") != -1) { + ModelFormField.DateTimeField dateTimeField = new ModelFormField.DateTimeField(FieldInfo.SOURCE_AUTO_SERVICE, + "timestamp"); + this.setFieldInfo(dateTimeField); + } else if (modelParam.type.indexOf("Date") != -1) { + ModelFormField.DateTimeField dateTimeField = new ModelFormField.DateTimeField(FieldInfo.SOURCE_AUTO_SERVICE, + "date"); + this.setFieldInfo(dateTimeField); + } else if (modelParam.type.indexOf("Time") != -1) { + ModelFormField.DateTimeField dateTimeField = new ModelFormField.DateTimeField(FieldInfo.SOURCE_AUTO_SERVICE, + "time"); + this.setFieldInfo(dateTimeField); + } else { + ModelFormField.TextField textField = new ModelFormField.TextField(FieldInfo.SOURCE_AUTO_SERVICE, null); + this.setFieldInfo(textField); + } + } + return true; + } + + private boolean induceFieldInfoFromServiceParam(String defaultFieldType, ModelReader entityModelReader, + DispatchContext dispatchContext) { + if (UtilValidate.isEmpty(this.getServiceName()) || UtilValidate.isEmpty(this.getAttributeName())) + return false; + try { + ModelService modelService = dispatchContext.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, entityModelReader)) { + 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 void mergeOverrideModelFormField(ModelFormFieldBuilder builder) { + if (builder == null) + return; + if (UtilValidate.isNotEmpty(builder.getName())) + this.name = builder.getName(); + if (UtilValidate.isNotEmpty(builder.getMapAcsr())) + this.mapAcsr = builder.getMapAcsr(); + if (UtilValidate.isNotEmpty(builder.getEntityName())) + this.entityName = builder.getEntityName(); + if (UtilValidate.isNotEmpty(builder.getServiceName())) + this.serviceName = builder.getServiceName(); + if (UtilValidate.isNotEmpty(builder.getEntryAcsr())) + this.entryAcsr = builder.getEntryAcsr(); + if (UtilValidate.isNotEmpty(builder.getParameterName())) + this.parameterName = builder.getParameterName(); + if (UtilValidate.isNotEmpty(builder.getFieldName())) + this.fieldName = builder.getFieldName(); + if (!builder.getAttributeName().isEmpty()) + this.attributeName = builder.getAttributeName(); + if (UtilValidate.isNotEmpty(builder.getTitle())) + this.title = builder.getTitle(); + if (UtilValidate.isNotEmpty(builder.getTooltip())) + this.tooltip = builder.getTooltip(); + if (builder.getSortField() != null) + this.sortField = builder.getSortField(); + if (UtilValidate.isNotEmpty(builder.getSortFieldHelpText())) + this.sortFieldHelpText = builder.getSortFieldHelpText(); + if (UtilValidate.isNotEmpty(builder.getTitleAreaStyle())) + this.titleAreaStyle = builder.getTitleAreaStyle(); + if (UtilValidate.isNotEmpty(builder.getWidgetAreaStyle())) + this.widgetAreaStyle = builder.getWidgetAreaStyle(); + if (UtilValidate.isNotEmpty(builder.getTitleStyle())) + this.titleStyle = builder.getTitleStyle(); + if (UtilValidate.isNotEmpty(builder.getWidgetStyle())) + this.widgetStyle = builder.getWidgetStyle(); + if (UtilValidate.isNotEmpty(builder.getRedWhen())) + this.redWhen = builder.getRedWhen(); + if (UtilValidate.isNotEmpty(builder.getEvent())) + this.event = builder.getEvent(); + if (!builder.getAction().isEmpty()) + this.action = builder.getAction(); + if (UtilValidate.isNotEmpty(builder.getUseWhen())) + this.useWhen = builder.getUseWhen(); + if (builder.getFieldInfo() != null) + this.setFieldInfo(builder.getFieldInfo()); + if (UtilValidate.isNotEmpty(builder.getHeaderLink())) + this.headerLink = builder.getHeaderLink(); + if (UtilValidate.isNotEmpty(builder.getHeaderLinkStyle())) + this.headerLinkStyle = builder.getHeaderLinkStyle(); + if (UtilValidate.isNotEmpty(builder.getIdName())) + this.idName = builder.getIdName(); + if (UtilValidate.isNotEmpty(builder.getOnChangeUpdateAreas())) + this.onChangeUpdateAreas.addAll(builder.getOnChangeUpdateAreas()); + if (UtilValidate.isNotEmpty(builder.getOnClickUpdateAreas())) + this.onClickUpdateAreas.addAll(builder.getOnClickUpdateAreas()); + this.encodeOutput = builder.getEncodeOutput(); + this.position = builder.getPosition(); + this.requiredField = builder.getRequiredField(); + this.separateColumn = builder.getSeparateColumn(); + } + + public ModelFormFieldBuilder setAction(String action) { + this.action = FlexibleStringExpander.getInstance(action); + return this; + } + + public ModelFormFieldBuilder setAttributeName(String attributeName) { + this.attributeName = attributeName; + return this; + } + + public ModelFormFieldBuilder setEncodeOutput(boolean encodeOutput) { + this.encodeOutput = encodeOutput; + return this; + } + + public ModelFormFieldBuilder setEntityName(String entityName) { + this.entityName = entityName; + return this; + } + + public ModelFormFieldBuilder setEntryName(String entryName) { + this.entryAcsr = FlexibleMapAccessor.getInstance(entryName); + return this; + } + + public ModelFormFieldBuilder setEvent(String event) { + this.event = event; + return this; + } + + public ModelFormFieldBuilder setFieldInfo(FieldInfo fieldInfo) { + if (fieldInfo != null && (this.fieldInfo == null || (fieldInfo.getFieldSource() <= this.fieldInfo.getFieldSource()))) { + this.fieldInfo = fieldInfo; + } + return this; + } + + public ModelFormFieldBuilder setFieldName(String fieldName) { + this.fieldName = fieldName; + return this; + } + + public ModelFormFieldBuilder setFieldType(String fieldType) { + this.fieldType = fieldType; + return this; + } + + public ModelFormFieldBuilder setHeaderLink(String headerLink) { + this.headerLink = headerLink; + return this; + } + + public ModelFormFieldBuilder setHeaderLinkStyle(String headerLinkStyle) { + this.headerLinkStyle = headerLinkStyle; + return this; + } + + public ModelFormFieldBuilder setIdName(String idName) { + this.idName = idName; + return this; + } + + public ModelFormFieldBuilder setMapName(String mapName) { + this.mapAcsr = FlexibleMapAccessor.getInstance(mapName); + return this; + } + + public ModelFormFieldBuilder setModelForm(ModelForm modelForm) { + this.modelForm = modelForm; + return this; + } + + public ModelFormFieldBuilder setName(String name) { + this.name = name; + return this; + } + + public ModelFormFieldBuilder setParameterName(String parameterName) { + this.parameterName = parameterName; + return this; + } + + public ModelFormFieldBuilder setPosition(Integer position) { + this.position = position; + return this; + } + + public ModelFormFieldBuilder setRedWhen(String redWhen) { + this.redWhen = redWhen; + return this; + } + + public ModelFormFieldBuilder setRequiredField(Boolean requiredField) { + this.requiredField = requiredField; + return this; + } + + public ModelFormFieldBuilder setRequiredFieldStyle(String requiredFieldStyle) { + this.requiredFieldStyle = requiredFieldStyle; + return this; + } + + public ModelFormFieldBuilder setSeparateColumn(boolean separateColumn) { + this.separateColumn = separateColumn; + return this; + } + + public ModelFormFieldBuilder setServiceName(String serviceName) { + this.serviceName = serviceName; + return this; + } + + public ModelFormFieldBuilder setSortField(Boolean sortField) { + this.sortField = sortField; + return this; + } + + public ModelFormFieldBuilder setSortFieldAscStyle(String sortFieldAscStyle) { + this.sortFieldAscStyle = sortFieldAscStyle; + return this; + } + + public ModelFormFieldBuilder setSortFieldDescStyle(String sortFieldDescStyle) { + this.sortFieldDescStyle = sortFieldDescStyle; + return this; + } + + public ModelFormFieldBuilder setSortFieldHelpText(String sortFieldHelpText) { + this.sortFieldHelpText = sortFieldHelpText; + return this; + } + + public ModelFormFieldBuilder setSortFieldStyle(String sortFieldStyle) { + this.sortFieldStyle = sortFieldStyle; + return this; + } + + public ModelFormFieldBuilder setTitle(String title) { + this.title = FlexibleStringExpander.getInstance(title); + return this; + } + + public ModelFormFieldBuilder setTitleAreaStyle(String titleAreaStyle) { + this.titleAreaStyle = titleAreaStyle; + return this; + } + + public ModelFormFieldBuilder setTitleStyle(String titleStyle) { + this.titleStyle = titleStyle; + return this; + } + + public ModelFormFieldBuilder setTooltip(String tooltip) { + this.tooltip = FlexibleStringExpander.getInstance(tooltip); + return this; + } + + public ModelFormFieldBuilder setTooltipStyle(String tooltipStyle) { + this.tooltipStyle = tooltipStyle; + return this; + } + + public ModelFormFieldBuilder setUseWhen(String useWhen) { + this.useWhen = FlexibleStringExpander.getInstance(useWhen); + return this; + } + + public ModelFormFieldBuilder setWidgetAreaStyle(String widgetAreaStyle) { + this.widgetAreaStyle = widgetAreaStyle; + return this; + } + + public ModelFormFieldBuilder setWidgetStyle(String widgetStyle) { + this.widgetStyle = widgetStyle; + return this; + } +} Added: ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelMenu.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelMenu.java?rev=1652852&view=auto ============================================================================== --- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelMenu.java (added) +++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelMenu.java Sun Jan 18 21:03:40 2015 @@ -0,0 +1,534 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.widget.model; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.UtilXml; +import org.ofbiz.base.util.collections.FlexibleMapAccessor; +import org.ofbiz.base.util.string.FlexibleStringExpander; +import org.ofbiz.widget.renderer.MenuStringRenderer; +import org.w3c.dom.Element; + +/** + * Models the <menu> element. + * + * @see <code>widget-menu.xsd</code> + */ +@SuppressWarnings("serial") +public class ModelMenu extends ModelWidget { + + /* + * ----------------------------------------------------------------------- * + * DEVELOPERS PLEASE READ + * ----------------------------------------------------------------------- * + * + * This model is intended to be a read-only data structure that represents + * an XML element. Outside of object construction, the class should not + * have any behaviors. + * + * Instances of this class will be shared by multiple threads - therefore + * it is immutable. DO NOT CHANGE THE OBJECT'S STATE AT RUN TIME! + * + */ + + public static final String module = ModelMenu.class.getName(); + + private final List<ModelAction> actions; + private final String defaultAlign; + private final String defaultAlignStyle; + private final FlexibleStringExpander defaultAssociatedContentId; + private final String defaultCellWidth; + private final String defaultDisabledTitleStyle; + private final String defaultEntityName; + private final Boolean defaultHideIfSelected; + private final String defaultMenuItemName; + private final String defaultPermissionEntityAction; + private final String defaultPermissionOperation; + private final String defaultSelectedStyle; + private final String defaultTitleStyle; + private final String defaultTooltipStyle; + private final String defaultWidgetStyle; + private final FlexibleStringExpander extraIndex; + private final String fillStyle; + private final String id; + private final FlexibleStringExpander menuContainerStyleExdr; + /** This List will contain one copy of each item for each item name in the order + * they were encountered in the service, entity, or menu definition; item definitions + * with constraints will also be in this list but may appear multiple times for the same + * item name. + * + * When rendering the menu the order in this list should be following and it should not be + * necessary to use the Map. The Map is used when loading the menu definition to keep the + * list clean and implement the override features for item definitions. + */ + private final List<ModelMenuItem> menuItemList; + /** This Map is keyed with the item name and has a ModelMenuItem for the value; items + * with conditions will not be put in this Map so item definition overrides for items + * with conditions is not possible. + */ + private final Map<String, ModelMenuItem> menuItemMap; + private final String menuLocation; + private final String menuWidth; + private final String orientation; + private final ModelMenu parentMenu; + private final FlexibleMapAccessor<String> selectedMenuItemContextFieldName; + private final String target; + private final FlexibleStringExpander title; + private final String tooltip; + private final String type; + + /** XML Constructor */ + public ModelMenu(Element menuElement, String menuLocation) { + super(menuElement); + ArrayList<ModelAction> actions = new ArrayList<ModelAction>(); + String defaultAlign = ""; + String defaultAlignStyle = ""; + FlexibleStringExpander defaultAssociatedContentId = FlexibleStringExpander.getInstance(""); + String defaultCellWidth = ""; + String defaultDisabledTitleStyle = ""; + String defaultEntityName = ""; + Boolean defaultHideIfSelected = Boolean.FALSE; + String defaultMenuItemName = ""; + String defaultPermissionEntityAction = ""; + String defaultPermissionOperation = ""; + String defaultSelectedStyle = ""; + String defaultTitleStyle = ""; + String defaultTooltipStyle = ""; + String defaultWidgetStyle = ""; + FlexibleStringExpander extraIndex = FlexibleStringExpander.getInstance(""); + String fillStyle = ""; + String id = ""; + FlexibleStringExpander menuContainerStyleExdr = FlexibleStringExpander.getInstance(""); + ArrayList<ModelMenuItem> menuItemList = new ArrayList<ModelMenuItem>(); + Map<String, ModelMenuItem> menuItemMap = new HashMap<String, ModelMenuItem>(); + String menuWidth = ""; + String orientation = "horizontal"; + FlexibleMapAccessor<String> selectedMenuItemContextFieldName = FlexibleMapAccessor.getInstance(""); + String target = ""; + FlexibleStringExpander title = FlexibleStringExpander.getInstance(""); + String tooltip = ""; + String type = ""; + // check if there is a parent menu to inherit from + ModelMenu parent = null; + String parentResource = menuElement.getAttribute("extends-resource"); + String parentMenu = menuElement.getAttribute("extends"); + if (!parentMenu.isEmpty()) { + if (!parentResource.isEmpty()) { + try { + parent = MenuFactory.getMenuFromLocation(parentResource, parentMenu); + } catch (Exception e) { + Debug.logError(e, "Failed to load parent menu definition '" + parentMenu + "' at resource '" + parentResource + + "'", module); + } + } else { + parentResource = menuLocation; + // try to find a menu definition in the same file + Element rootElement = menuElement.getOwnerDocument().getDocumentElement(); + List<? extends Element> menuElements = UtilXml.childElementList(rootElement, "menu"); + for (Element menuElementEntry : menuElements) { + if (menuElementEntry.getAttribute("name").equals(parentMenu)) { + parent = new ModelMenu(menuElementEntry, parentResource); + break; + } + } + if (parent == null) { + Debug.logError("Failed to find parent menu definition '" + parentMenu + "' in same document.", module); + } + } + if (parent != null) { + type = parent.type; + target = parent.target; + id = parent.id; + title = parent.title; + tooltip = parent.tooltip; + defaultEntityName = parent.defaultEntityName; + defaultTitleStyle = parent.defaultTitleStyle; + defaultSelectedStyle = parent.defaultSelectedStyle; + defaultWidgetStyle = parent.defaultWidgetStyle; + defaultTooltipStyle = parent.defaultTooltipStyle; + defaultMenuItemName = parent.defaultMenuItemName; + menuItemList.addAll(parent.menuItemList); + menuItemMap.putAll(parent.menuItemMap); + defaultPermissionOperation = parent.defaultPermissionOperation; + defaultPermissionEntityAction = parent.defaultPermissionEntityAction; + defaultAssociatedContentId = parent.defaultAssociatedContentId; + defaultHideIfSelected = parent.defaultHideIfSelected; + orientation = parent.orientation; + menuWidth = parent.menuWidth; + defaultCellWidth = parent.defaultCellWidth; + defaultDisabledTitleStyle = parent.defaultDisabledTitleStyle; + defaultAlign = parent.defaultAlign; + defaultAlignStyle = parent.defaultAlignStyle; + fillStyle = parent.fillStyle; + extraIndex = parent.extraIndex; + selectedMenuItemContextFieldName = parent.selectedMenuItemContextFieldName; + menuContainerStyleExdr = parent.menuContainerStyleExdr; + if (parent.actions != null) { + actions.addAll(parent.actions); + } + } + } + if (!menuElement.getAttribute("type").isEmpty()) + type = menuElement.getAttribute("type"); + if (!menuElement.getAttribute("target").isEmpty()) + target = menuElement.getAttribute("target"); + if (!menuElement.getAttribute("id").isEmpty()) + id = menuElement.getAttribute("id"); + if (!menuElement.getAttribute("title").isEmpty()) + title = FlexibleStringExpander.getInstance(menuElement.getAttribute("title")); + if (!menuElement.getAttribute("tooltip").isEmpty()) + tooltip = menuElement.getAttribute("tooltip"); + if (!menuElement.getAttribute("default-entity-name").isEmpty()) + defaultEntityName = menuElement.getAttribute("default-entity-name"); + if (!menuElement.getAttribute("default-title-style").isEmpty()) + defaultTitleStyle = menuElement.getAttribute("default-title-style"); + if (!menuElement.getAttribute("default-selected-style").isEmpty()) + defaultSelectedStyle = menuElement.getAttribute("default-selected-style"); + if (!menuElement.getAttribute("default-widget-style").isEmpty()) + defaultWidgetStyle = menuElement.getAttribute("default-widget-style"); + if (!menuElement.getAttribute("default-tooltip-style").isEmpty()) + defaultTooltipStyle = menuElement.getAttribute("default-tooltip-style"); + if (!menuElement.getAttribute("default-menu-item-name").isEmpty()) + defaultMenuItemName = menuElement.getAttribute("default-menu-item-name"); + if (!menuElement.getAttribute("default-permission-operation").isEmpty()) + defaultPermissionOperation = menuElement.getAttribute("default-permission-operation"); + if (!menuElement.getAttribute("default-permission-entity-action").isEmpty()) + defaultPermissionEntityAction = menuElement.getAttribute("default-permission-entity-action"); + if (!menuElement.getAttribute("default-associated-content-id").isEmpty()) + defaultAssociatedContentId = FlexibleStringExpander.getInstance(menuElement + .getAttribute("default-associated-content-id")); + if (!menuElement.getAttribute("orientation").isEmpty()) + orientation = menuElement.getAttribute("orientation"); + if (!menuElement.getAttribute("menu-width").isEmpty()) + menuWidth = menuElement.getAttribute("menu-width"); + if (!menuElement.getAttribute("default-cell-width").isEmpty()) + defaultCellWidth = menuElement.getAttribute("default-cell-width"); + if (!menuElement.getAttribute("default-hide-if-selected").isEmpty()) + defaultHideIfSelected = "true".equals(menuElement.getAttribute("default-hide-if-selected").isEmpty()); + if (!menuElement.getAttribute("default-disabled-title-style").isEmpty()) + defaultDisabledTitleStyle = menuElement.getAttribute("default-disabled-title-style"); + if (!menuElement.getAttribute("selected-menuitem-context-field-name").isEmpty()) + selectedMenuItemContextFieldName = FlexibleMapAccessor.getInstance(menuElement + .getAttribute("selected-menuitem-context-field-name")); + if (!menuElement.getAttribute("menu-container-style").isEmpty()) + menuContainerStyleExdr = FlexibleStringExpander.getInstance(menuElement.getAttribute("menu-container-style")); + if (!menuElement.getAttribute("default-align").isEmpty()) + defaultAlign = menuElement.getAttribute("default-align"); + if (!menuElement.getAttribute("default-align-style").isEmpty()) + defaultAlignStyle = menuElement.getAttribute("default-align-style"); + if (!menuElement.getAttribute("fill-style").isEmpty()) + fillStyle = menuElement.getAttribute("fill-style"); + if (!menuElement.getAttribute("extra-index").isEmpty()) + extraIndex = FlexibleStringExpander.getInstance(menuElement.getAttribute("extra-index")); + // read all actions under the "actions" element + Element actionsElement = UtilXml.firstChildElement(menuElement, "actions"); + if (actionsElement != null) { + actions.addAll(ModelMenuAction.readSubActions(this, actionsElement)); + } + actions.trimToSize(); + this.actions = Collections.unmodifiableList(actions); + this.defaultAlign = defaultAlign; + this.defaultAlignStyle = defaultAlignStyle; + this.defaultAssociatedContentId = defaultAssociatedContentId; + this.defaultCellWidth = defaultCellWidth; + this.defaultDisabledTitleStyle = defaultDisabledTitleStyle; + this.defaultEntityName = defaultEntityName; + this.defaultHideIfSelected = defaultHideIfSelected; + this.defaultMenuItemName = defaultMenuItemName; + this.defaultPermissionEntityAction = defaultPermissionEntityAction; + this.defaultPermissionOperation = defaultPermissionOperation; + this.defaultSelectedStyle = defaultSelectedStyle; + this.defaultTitleStyle = defaultTitleStyle; + this.defaultTooltipStyle = defaultTooltipStyle; + this.defaultWidgetStyle = defaultWidgetStyle; + this.extraIndex = extraIndex; + this.fillStyle = fillStyle; + this.id = id; + this.menuContainerStyleExdr = menuContainerStyleExdr; + List<? extends Element> itemElements = UtilXml.childElementList(menuElement, "menu-item"); + for (Element itemElement : itemElements) { + ModelMenuItem modelMenuItem = new ModelMenuItem(itemElement, this); + addUpdateMenuItem(modelMenuItem, menuItemList, menuItemMap); + } + menuItemList.trimToSize(); + this.menuItemList = Collections.unmodifiableList(menuItemList); + this.menuItemMap = Collections.unmodifiableMap(menuItemMap); + this.menuLocation = menuLocation; + this.menuWidth = menuWidth; + this.orientation = orientation; + this.parentMenu = parent; + this.selectedMenuItemContextFieldName = selectedMenuItemContextFieldName; + this.target = target; + this.title = title; + this.tooltip = tooltip; + this.type = type; + } + + @Override + public void accept(ModelWidgetVisitor visitor) throws Exception { + visitor.visit(this); + } + + /** + * add/override modelMenuItem using the menuItemList and menuItemMap + * + */ + private void addUpdateMenuItem(ModelMenuItem modelMenuItem, List<ModelMenuItem> menuItemList, + Map<String, ModelMenuItem> menuItemMap) { + ModelMenuItem existingMenuItem = menuItemMap.get(modelMenuItem.getName()); + if (existingMenuItem != null) { + // does exist, update the item by doing a merge/override + ModelMenuItem mergedMenuItem = existingMenuItem.mergeOverrideModelMenuItem(modelMenuItem); + int existingItemIndex = menuItemList.indexOf(existingMenuItem); + menuItemList.set(existingItemIndex, mergedMenuItem); + menuItemMap.put(modelMenuItem.getName(), mergedMenuItem); + } else { + // does not exist, add to Map + menuItemList.add(modelMenuItem); + menuItemMap.put(modelMenuItem.getName(), modelMenuItem); + } + } + + public List<ModelAction> getActions() { + return actions; + } + + @Override + public String getBoundaryCommentName() { + return menuLocation + "#" + getName(); + } + + public String getCurrentMenuName(Map<String, Object> context) { + return getName(); + } + + public String getDefaultAlign() { + return this.defaultAlign; + } + + public String getDefaultAlignStyle() { + return this.defaultAlignStyle; + } + + public FlexibleStringExpander getDefaultAssociatedContentId() { + return defaultAssociatedContentId; + } + + public String getDefaultAssociatedContentId(Map<String, Object> context) { + return defaultAssociatedContentId.expandString(context); + } + + public String getDefaultCellWidth() { + return this.defaultCellWidth; + } + + public String getDefaultDisabledTitleStyle() { + return this.defaultDisabledTitleStyle; + } + + public String getDefaultEntityName() { + return this.defaultEntityName; + } + + public Boolean getDefaultHideIfSelected() { + return this.defaultHideIfSelected; + } + + public String getDefaultMenuItemName() { + return this.defaultMenuItemName; + } + + public String getDefaultPermissionEntityAction() { + return this.defaultPermissionEntityAction; + } + + public String getDefaultPermissionOperation() { + return this.defaultPermissionOperation; + } + + public String getDefaultSelectedStyle() { + return this.defaultSelectedStyle; + } + + public String getDefaultTitleStyle() { + return this.defaultTitleStyle; + } + + public String getDefaultTooltipStyle() { + return this.defaultTooltipStyle; + } + + public String getDefaultWidgetStyle() { + return this.defaultWidgetStyle; + } + + public FlexibleStringExpander getExtraIndex() { + return extraIndex; + } + + public String getExtraIndex(Map<String, Object> context) { + try { + return extraIndex.expandString(context); + } catch (Exception ex) { + return ""; + } + } + + public String getFillStyle() { + return this.fillStyle; + } + + public String getId() { + return this.id; + } + + public String getMenuContainerStyle(Map<String, Object> context) { + return menuContainerStyleExdr.expandString(context); + } + + public FlexibleStringExpander getMenuContainerStyleExdr() { + return menuContainerStyleExdr; + } + + public List<ModelMenuItem> getMenuItemList() { + return menuItemList; + } + + public Map<String, ModelMenuItem> getMenuItemMap() { + return menuItemMap; + } + + public String getMenuLocation() { + return menuLocation; + } + + public String getMenuWidth() { + return this.menuWidth; + } + + public ModelMenuItem getModelMenuItemByName(String name) { + return this.menuItemMap.get(name); + } + + public String getOrientation() { + return this.orientation; + } + + public ModelMenu getParentMenu() { + return parentMenu; + } + + public FlexibleMapAccessor<String> getSelectedMenuItemContextFieldName() { + return selectedMenuItemContextFieldName; + } + + public String getSelectedMenuItemContextFieldName(Map<String, Object> context) { + String menuItemName = this.selectedMenuItemContextFieldName.get(context); + if (UtilValidate.isEmpty(menuItemName)) { + return this.defaultMenuItemName; + } + return menuItemName; + } + + public String getTarget() { + return target; + } + + public FlexibleStringExpander getTitle() { + return title; + } + + public String getTitle(Map<String, Object> context) { + return title.expandString(context); + } + + public String getTooltip() { + return this.tooltip; + } + + public String getType() { + return this.type; + } + + public int renderedMenuItemCount(Map<String, Object> context) { + int count = 0; + for (ModelMenuItem item : this.menuItemList) { + if (item.shouldBeRendered(context)) + count++; + } + return count; + } + + /** + * Renders this menu to a String, i.e. in a text format, as defined with the + * MenuStringRenderer implementation. + * + * @param writer The Writer that the menu text will be written to + * @param context Map containing the menu context; the following are + * reserved words in this context: parameters (Map), isError (Boolean), + * itemIndex (Integer, for lists only, otherwise null), bshInterpreter, + * menuName (String, optional alternate name for menu, defaults to the + * value of the name attribute) + * @param menuStringRenderer An implementation of the MenuStringRenderer + * interface that is responsible for the actual text generation for + * different menu elements; implementing you own makes it possible to + * use the same menu definitions for many types of menu UIs + */ + public void renderMenuString(Appendable writer, Map<String, Object> context, MenuStringRenderer menuStringRenderer) + throws IOException { + AbstractModelAction.runSubActions(this.actions, context); + if ("simple".equals(this.type)) { + this.renderSimpleMenuString(writer, context, menuStringRenderer); + } else { + throw new IllegalArgumentException("The type " + this.getType() + " is not supported for menu with name " + + this.getName()); + } + } + + public void renderSimpleMenuString(Appendable writer, Map<String, Object> context, MenuStringRenderer menuStringRenderer) + throws IOException { + // render menu open + menuStringRenderer.renderMenuOpen(writer, context, this); + + // render formatting wrapper open + menuStringRenderer.renderFormatSimpleWrapperOpen(writer, context, this); + + // render each menuItem row, except hidden & ignored rows + for (ModelMenuItem item : this.menuItemList) { + item.renderMenuItemString(writer, context, menuStringRenderer); + } + // render formatting wrapper close + menuStringRenderer.renderFormatSimpleWrapperClose(writer, context, this); + + // render menu close + menuStringRenderer.renderMenuClose(writer, context, this); + } + + public void runActions(Map<String, Object> context) { + AbstractModelAction.runSubActions(this.actions, context); + } +} Added: ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelMenuAction.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelMenuAction.java?rev=1652852&view=auto ============================================================================== --- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelMenuAction.java (added) +++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelMenuAction.java Sun Jan 18 21:03:40 2015 @@ -0,0 +1,232 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.widget.model; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.TimeZone; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpSession; + +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.GeneralException; +import org.ofbiz.base.util.ObjectType; +import org.ofbiz.base.util.UtilGenerics; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.UtilXml; +import org.ofbiz.base.util.collections.FlexibleMapAccessor; +import org.ofbiz.base.util.string.FlexibleStringExpander; +import org.w3c.dom.Element; + +/** + * Abstract menu action. + */ +public abstract class ModelMenuAction { + + public static final String module = ModelMenuAction.class.getName(); + + public static List<ModelAction> readSubActions(ModelMenu modelMenu, Element parentElement) { + List<? extends Element> actionElementList = UtilXml.childElementList(parentElement); + List<ModelAction> actions = new ArrayList<ModelAction>(actionElementList.size()); + for (Element actionElement : actionElementList) { + if ("set".equals(actionElement.getNodeName())) { + actions.add(new SetField(modelMenu, actionElement)); + } else { + actions.add(AbstractModelAction.newInstance(modelMenu, actionElement)); + } + } + return Collections.unmodifiableList(actions); + } + + /** + * Models the <set> element. + * + * @see <code>widget-common.xsd</code> + */ + @SuppressWarnings("serial") + public static class SetField extends AbstractModelAction { + private final FlexibleMapAccessor<Object> field; + private final FlexibleMapAccessor<Object> fromField; + private final FlexibleStringExpander valueExdr; + private final FlexibleStringExpander defaultExdr; + private final FlexibleStringExpander globalExdr; + private final String type; + private final String toScope; + private final String fromScope; + + public SetField(ModelMenu modelMenu, Element setElement) { + super (modelMenu, setElement); + this.field = FlexibleMapAccessor.getInstance(setElement.getAttribute("field")); + this.fromField = FlexibleMapAccessor.getInstance(setElement.getAttribute("from-field")); + this.valueExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("value")); + this.defaultExdr = UtilValidate.isNotEmpty(setElement.getAttribute("default-value")) ? FlexibleStringExpander.getInstance(setElement.getAttribute("default-value")) : null; + this.globalExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("global")); + this.type = setElement.getAttribute("type"); + this.toScope = setElement.getAttribute("to-scope"); + this.fromScope = setElement.getAttribute("from-scope"); + if (!this.fromField.isEmpty() && !this.valueExdr.isEmpty()) { + throw new IllegalArgumentException("Cannot specify a from-field [" + setElement.getAttribute("from-field") + "] and a value [" + setElement.getAttribute("value") + "] on the set action in a screen widget"); + } + } + + @SuppressWarnings("rawtypes") + @Override + public void runAction(Map<String, Object> context) { + String globalStr = this.globalExdr.expandString(context); + // default to false + boolean global = "true".equals(globalStr); + + Object newValue = null; + if (this.fromScope != null && this.fromScope.equals("user")) { + if (!this.fromField.isEmpty()) { + String originalName = this.fromField.getOriginalName(); + String currentWidgetTrail = (String)context.get("_WIDGETTRAIL_"); + String newKey = currentWidgetTrail + "|" + originalName; + HttpSession session = (HttpSession)context.get("session"); + newValue = session.getAttribute(newKey); + if (Debug.verboseOn()) Debug.logVerbose("In user getting value for field from [" + this.fromField.getOriginalName() + "]: " + newValue, module); + } else if (!this.valueExdr.isEmpty()) { + newValue = this.valueExdr.expandString(context); + } + + } else if (this.fromScope != null && this.fromScope.equals("application")) { + if (!this.fromField.isEmpty()) { + String originalName = this.fromField.getOriginalName(); + String currentWidgetTrail = (String)context.get("_WIDGETTRAIL_"); + String newKey = currentWidgetTrail + "|" + originalName; + ServletContext servletContext = (ServletContext)context.get("application"); + newValue = servletContext.getAttribute(newKey); + if (Debug.verboseOn()) Debug.logVerbose("In application getting value for field from [" + this.fromField.getOriginalName() + "]: " + newValue, module); + } else if (!this.valueExdr.isEmpty()) { + newValue = this.valueExdr.expandString(context); + } + + } else { + if (!this.fromField.isEmpty()) { + newValue = this.fromField.get(context); + if (Debug.verboseOn()) Debug.logVerbose("In screen getting value for field from [" + this.fromField.getOriginalName() + "]: " + newValue, module); + } else if (!this.valueExdr.isEmpty()) { + newValue = this.valueExdr.expandString(context); + } + } + + // If newValue is still empty, use the default value + if (this.defaultExdr != null) { + if (ObjectType.isEmpty(newValue)) { + newValue = this.defaultExdr.expandString(context); + } + } + + if (UtilValidate.isNotEmpty(this.type)) { + if ("NewMap".equals(this.type)) { + newValue = new HashMap(); + } else if ("NewList".equals(this.type)) { + newValue = new LinkedList(); + } else { + try { + newValue = ObjectType.simpleTypeConvert(newValue, this.type, null, (TimeZone) context.get("timeZone"), (Locale) context.get("locale"), true); + } catch (GeneralException e) { + String errMsg = "Could not convert field value for the field: [" + this.field.getOriginalName() + "] to the [" + this.type + "] type for the value [" + newValue + "]: " + e.toString(); + Debug.logError(e, errMsg, module); + throw new IllegalArgumentException(errMsg); + } + } + } + if (this.toScope != null && this.toScope.equals("user")) { + String originalName = this.field.getOriginalName(); + String currentWidgetTrail = (String)context.get("_WIDGETTRAIL_"); + String newKey = currentWidgetTrail + "|" + originalName; + HttpSession session = (HttpSession)context.get("session"); + session.setAttribute(newKey, newValue); + if (Debug.verboseOn()) Debug.logVerbose("In user setting value for field from [" + this.field.getOriginalName() + "]: " + newValue, module); + + } else if (this.toScope != null && this.toScope.equals("application")) { + String originalName = this.field.getOriginalName(); + String currentWidgetTrail = (String)context.get("_WIDGETTRAIL_"); + String newKey = currentWidgetTrail + "|" + originalName; + ServletContext servletContext = (ServletContext)context.get("application"); + servletContext.setAttribute(newKey, newValue); + if (Debug.verboseOn()) Debug.logVerbose("In application setting value for field from [" + this.field.getOriginalName() + "]: " + newValue, module); + + } else { + if (Debug.verboseOn()) Debug.logVerbose("In screen setting field [" + this.field.getOriginalName() + "] to value: " + newValue, module); + this.field.put(context, newValue); + } + + if (global) { + Map<String, Object> globalCtx = UtilGenerics.checkMap(context.get("globalContext")); + if (globalCtx != null) { + this.field.put(globalCtx, newValue); + } + } + + // this is a hack for backward compatibility with the JPublish page object + Map<String, Object> page = UtilGenerics.checkMap(context.get("page")); + if (page != null) { + this.field.put(page, newValue); + } + } + + @Override + public void accept(ModelActionVisitor visitor) throws Exception { + visitor.visit(this); + } + + public FlexibleMapAccessor<Object> getField() { + return field; + } + + public FlexibleMapAccessor<Object> getFromField() { + return fromField; + } + + public FlexibleStringExpander getValueExdr() { + return valueExdr; + } + + public FlexibleStringExpander getDefaultExdr() { + return defaultExdr; + } + + public FlexibleStringExpander getGlobalExdr() { + return globalExdr; + } + + public String getType() { + return type; + } + + public String getToScope() { + return toScope; + } + + public String getFromScope() { + return fromScope; + } + } +} + + + Added: ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelMenuCondition.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelMenuCondition.java?rev=1652852&view=auto ============================================================================== --- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelMenuCondition.java (added) +++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelMenuCondition.java Sun Jan 18 21:03:40 2015 @@ -0,0 +1,68 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.widget.model; + +import org.ofbiz.base.util.string.FlexibleStringExpander; +import org.w3c.dom.Element; + +/** + * Models the <condition> element. + * + * @see <code>widget-menu.xsd</code> + */ +public final class ModelMenuCondition { + + /* + * ----------------------------------------------------------------------- * + * DEVELOPERS PLEASE READ + * ----------------------------------------------------------------------- * + * + * This model is intended to be a read-only data structure that represents + * an XML element. Outside of object construction, the class should not + * have any behaviors. + * + * Instances of this class will be shared by multiple threads - therefore + * it is immutable. DO NOT CHANGE THE OBJECT'S STATE AT RUN TIME! + * + */ + + public static final String module = ModelMenuCondition.class.getName(); + + private final FlexibleStringExpander passStyleExdr; + private final FlexibleStringExpander failStyleExdr; + private final ModelCondition condition; + + public ModelMenuCondition(ModelMenuItem modelMenuItem, Element conditionElement) { + this.passStyleExdr = FlexibleStringExpander.getInstance(conditionElement.getAttribute("pass-style")); + this.failStyleExdr = FlexibleStringExpander.getInstance(conditionElement.getAttribute("disabled-style")); + this.condition = AbstractModelCondition.DEFAULT_CONDITION_FACTORY.newInstance(modelMenuItem, conditionElement); + } + + public ModelCondition getCondition() { + return condition; + } + + public FlexibleStringExpander getFailStyleExdr() { + return failStyleExdr; + } + + public FlexibleStringExpander getPassStyleExdr() { + return passStyleExdr; + } +} |
Free forum by Nabble | Edit this page |