Modified: ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/dtd/widget-tree.xsd
URL: http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/dtd/widget-tree.xsd?rev=1649482&r1=1649481&r2=1649482&view=diff ============================================================================== --- ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/dtd/widget-tree.xsd (original) +++ ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/dtd/widget-tree.xsd Mon Jan 5 08:50:30 2015 @@ -18,6 +18,7 @@ specific language governing permissions under the License. --> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> + <xs:include schemaLocation="http://ofbiz.apache.org/dtds/widget-common.xsd" /> <xs:element name="trees"> <xs:complexType> <xs:sequence> @@ -61,8 +62,9 @@ under the License. <xs:element name="node"> <xs:complexType> <xs:sequence> - <xs:element minOccurs="0" ref="condition" /> + <xs:element name="condition" type="ConditionRootType" minOccurs="0" /> <xs:choice minOccurs="0"> + <xs:element ref="actions" /> <xs:element ref="entity-one" /> <xs:element ref="service" /> </xs:choice> @@ -94,6 +96,7 @@ under the License. <xs:complexType> <xs:sequence> <xs:choice> + <xs:element ref="actions" /> <xs:element ref="entity-and" /> <xs:element ref="service" /> <xs:element ref="entity-condition" /> @@ -103,7 +106,6 @@ under the License. <xs:attribute type="xs:string" name="node-name" use="required" /> </xs:complexType> </xs:element> - <xs:element name="include-screen"> <xs:complexType> <xs:attribute type="xs:string" name="name" use="required" /> @@ -201,7 +203,13 @@ under the License. </xs:attribute> </xs:complexType> </xs:element> - + <xs:element name="actions"> + <xs:complexType> + <xs:sequence> + <xs:element minOccurs="0" maxOccurs="unbounded" ref="AllActions"/> + </xs:sequence> + </xs:complexType> + </xs:element> <xs:element name="service"> <xs:complexType> <xs:sequence> @@ -215,12 +223,10 @@ under the License. </xs:annotation> </xs:attribute> <xs:attribute type="xs:string" name="result-map-list" /> - <!-- deprecated, use result-map-list instead <xs:attribute type="xs:string" name="result-map-list-iterator-name"/> --> <xs:attribute type="xs:string" name="result-map-value" /> <xs:attribute type="xs:string" name="value" /> </xs:complexType> </xs:element> - <xs:element name="entity-and"> <xs:complexType> <xs:sequence> @@ -311,355 +317,10 @@ under the License. </xs:attribute> </xs:complexType> </xs:element> - <xs:element name="entity-one"> - <xs:complexType> - <xs:sequence> - <xs:element minOccurs="0" maxOccurs="unbounded" ref="field-map" /> - <xs:element minOccurs="0" maxOccurs="unbounded" ref="select-field" /> - </xs:sequence> - <xs:attribute name="entity-name" type="xs:string" use="required" /> - <xs:attribute name="value-field" type="xs:string" /> - <xs:attribute name="use-cache" default="false"> - <xs:simpleType> - <xs:restriction base="xs:token"> - <xs:enumeration value="true" /> - <xs:enumeration value="false" /> - </xs:restriction> - </xs:simpleType> - </xs:attribute> - <xs:attribute name="auto-field-map" default="true"> - <xs:simpleType> - <xs:restriction base="xs:token"> - <xs:enumeration value="true" /> - <xs:enumeration value="false" /> - </xs:restriction> - </xs:simpleType> - </xs:attribute> - </xs:complexType> - </xs:element> - - <xs:element name="order-by"> - <xs:complexType> - <xs:attribute type="xs:string" name="field-name" use="required" /> - </xs:complexType> - </xs:element> - <xs:element name="select-field"> - <xs:complexType> - <xs:attribute type="xs:string" name="field-name" use="required" /> - </xs:complexType> - </xs:element> - <xs:element name="limit-range"> - <xs:complexType> - <xs:attribute name="start" type="xs:string" use="required"> - <xs:annotation> - <xs:documentation>Should resolve into a non-negative integer.</xs:documentation> - </xs:annotation> - </xs:attribute> - <xs:attribute name="size" type="xs:string" use="required"> - <xs:annotation> - <xs:documentation>Should resolve into a non-negative integer.</xs:documentation> - </xs:annotation> - </xs:attribute> - </xs:complexType> - </xs:element> - <xs:element name="limit-view"> - <xs:complexType> - <xs:attribute name="view-index" type="xs:string" use="required"> - <xs:annotation> - <xs:documentation>Should resolve into a non-negative integer.</xs:documentation> - </xs:annotation> - </xs:attribute> - <xs:attribute name="view-size" type="xs:string" use="required"> - <xs:annotation> - <xs:documentation>Should resolve into a positive integer.</xs:documentation> - </xs:annotation> - </xs:attribute> - </xs:complexType> - </xs:element> - <xs:element name="use-iterator" /> - <xs:element name="field-map"> - <xs:complexType> - <xs:attribute type="xs:string" name="field-name" use="required" /> - <xs:attribute type="xs:string" name="from-field" /> - <xs:attribute type="xs:string" name="value" /> - </xs:complexType> - </xs:element> <xs:element name="out-field-map"> <xs:complexType> <xs:attribute type="xs:string" name="field-name" use="required" /> <xs:attribute type="xs:string" name="to-field-name" /> </xs:complexType> </xs:element> - - <xs:element name="condition-list"> - <xs:complexType> - <xs:choice maxOccurs="unbounded"> - <xs:element ref="condition-expr" /> - <xs:element ref="condition-list" /> - </xs:choice> - <xs:attribute name="combine" default="and"> - <xs:simpleType> - <xs:restriction base="xs:token"> - <xs:enumeration value="and" /> - <xs:enumeration value="or" /> - </xs:restriction> - </xs:simpleType> - </xs:attribute> - </xs:complexType> - </xs:element> - <xs:element name="having-condition-list"> - <xs:complexType> - <xs:choice maxOccurs="unbounded"> - <xs:element ref="condition-expr" /> - <xs:element ref="condition-list" /> - </xs:choice> - <xs:attribute name="combine" default="and"> - <xs:simpleType> - <xs:restriction base="xs:token"> - <xs:enumeration value="and" /> - <xs:enumeration value="or" /> - </xs:restriction> - </xs:simpleType> - </xs:attribute> - </xs:complexType> - </xs:element> - <xs:element name="condition-expr"> - <xs:complexType> - <xs:attribute name="field-name" type="xs:string" use="required" /> - <xs:attribute name="operator" default="equals"> - <xs:simpleType> - <xs:restriction base="xs:token"> - <xs:enumeration value="less" /> - <xs:enumeration value="greater" /> - <xs:enumeration value="less-equals" /> - <xs:enumeration value="greater-equals" /> - <xs:enumeration value="equals" /> - <xs:enumeration value="not-equals" /> - <xs:enumeration value="in" /> - <xs:enumeration value="between" /> - <xs:enumeration value="like" /> - </xs:restriction> - </xs:simpleType> - </xs:attribute> - <xs:attribute name="from-field" type="xs:string" /> - <xs:attribute name="value" type="xs:string" /> - <xs:attribute name="ignore-if-null" default="false"> - <xs:simpleType> - <xs:restriction base="xs:token"> - <xs:enumeration value="true" /> - <xs:enumeration value="false" /> - </xs:restriction> - </xs:simpleType> - </xs:attribute> - <xs:attribute name="ignore-if-empty" default="false"> - <xs:simpleType> - <xs:restriction base="xs:token"> - <xs:enumeration value="true" /> - <xs:enumeration value="false" /> - </xs:restriction> - </xs:simpleType> - </xs:attribute> - <xs:attribute name="ignore-case" default="false"> - <xs:simpleType> - <xs:restriction base="xs:token"> - <xs:enumeration value="true" /> - <xs:enumeration value="false" /> - </xs:restriction> - </xs:simpleType> - </xs:attribute> - <xs:attribute name="ignore" default="false"> - <xs:annotation> - <xs:documentation> - Ignore the condition if flag is true. - Defaults to false. - </xs:documentation> - </xs:annotation> - </xs:attribute> - </xs:complexType> - </xs:element> - - <!-- ================ CONDITIONS ================ --> - <xs:element name="AllConditionals" abstract="true"/> - <xs:element name="condition"> - <xs:complexType> - <xs:sequence> - <xs:element minOccurs="1" maxOccurs="1" ref="AllConditionals"/> - </xs:sequence> - </xs:complexType> - </xs:element> - <xs:element name="and" substitutionGroup="AllConditionals"> - <xs:complexType> - <xs:sequence> - <xs:element maxOccurs="unbounded" ref="AllConditionals"/> - </xs:sequence> - </xs:complexType> - </xs:element> - <xs:element name="xor" substitutionGroup="AllConditionals"> - <xs:complexType> - <xs:sequence> - <xs:element maxOccurs="unbounded" ref="AllConditionals"/> - </xs:sequence> - </xs:complexType> - </xs:element> - <xs:element name="or" substitutionGroup="AllConditionals"> - <xs:complexType> - <xs:sequence> - <xs:element maxOccurs="unbounded" ref="AllConditionals"/> - </xs:sequence> - </xs:complexType> - </xs:element> - <xs:element name="not" substitutionGroup="AllConditionals"> - <xs:complexType> - <xs:sequence> - <xs:element ref="AllConditionals"/> - </xs:sequence> - </xs:complexType> - </xs:element> - <xs:element name="if-has-permission" substitutionGroup="AllConditionals"> - <xs:complexType> - <xs:attribute type="xs:string" name="permission" use="required" /> - <xs:attribute type="xs:string" name="action" /> - </xs:complexType> - </xs:element> - <xs:element name="if-entity-permission" substitutionGroup="AllConditionals"> - <xs:complexType> - <xs:choice minOccurs="0"> - <xs:element minOccurs="0" maxOccurs="1" ref="permission-condition-getter" /> - <xs:element minOccurs="0" maxOccurs="1" ref="related-role-getter" /> - <xs:element minOccurs="0" maxOccurs="1" ref="auxiliary-value-getter" /> - </xs:choice> - <xs:attribute type="xs:string" name="entity-name" use="required" /> - <xs:attribute type="xs:string" name="entity-id" use="required" /> - <xs:attribute type="xs:string" name="target-operation" use="required" /> - <xs:attribute name="display-fail-cond" default="false"> - <xs:simpleType> - <xs:restriction base="xs:token"> - <xs:enumeration value="true" /> - <xs:enumeration value="false" /> - </xs:restriction> - </xs:simpleType> - </xs:attribute> - </xs:complexType> - </xs:element> - <xs:element name="permission-condition-getter"> - <xs:complexType> - <xs:attribute type="xs:string" name="entity-name" /> - <xs:attribute type="xs:string" name="operation-field-name" /> - <xs:attribute type="xs:string" name="role-field-name" /> - <xs:attribute type="xs:string" name="auxiliary-field-name" /> - <xs:attribute type="xs:string" name="status-field-name" /> - <xs:attribute type="xs:string" name="privilege-field-name" /> - </xs:complexType> - </xs:element> - <xs:element name="related-role-getter"> - <xs:complexType> - <xs:attribute type="xs:string" name="entity-name" /> - <xs:attribute type="xs:string" name="role-entity-name" /> - <xs:attribute type="xs:string" name="role-type-field-name" /> - <xs:attribute type="xs:string" name="party-field-name" /> - <xs:attribute type="xs:string" name="owner-entity-field-name" /> - <xs:attribute type="xs:string" name="entity-id-name" /> - </xs:complexType> - </xs:element> - <xs:element name="auxiliary-value-getter"> - <xs:complexType> - <xs:attribute type="xs:string" name="entity-name" /> - <xs:attribute type="xs:string" name="auxiliary-field-name" /> - <xs:attribute type="xs:string" name="entity-id-name" /> - </xs:complexType> - </xs:element> - <xs:element name="if-validate-method" substitutionGroup="AllConditionals"> - <xs:complexType> - <xs:attribute type="xs:string" name="field" use="required" /> - <xs:attribute type="xs:string" name="method" use="required" /> - <xs:attribute type="xs:string" name="class" default="org.ofbiz.base.util.UtilValidate" /> - </xs:complexType> - </xs:element> - <xs:element name="if-compare" substitutionGroup="AllConditionals"> - <xs:complexType> - <xs:attribute name="field" type="xs:string" use="required" /> - <xs:attribute name="operator" use="required"> - <xs:simpleType> - <xs:restriction base="xs:token"> - <xs:enumeration value="less" /> - <xs:enumeration value="greater" /> - <xs:enumeration value="less-equals" /> - <xs:enumeration value="greater-equals" /> - <xs:enumeration value="equals" /> - <xs:enumeration value="not-equals" /> - <xs:enumeration value="contains" /> - </xs:restriction> - </xs:simpleType> - </xs:attribute> - <xs:attribute type="xs:string" name="value" use="required" /> - <xs:attribute name="type" default="String"> - <xs:simpleType> - <xs:restriction base="xs:token"> - <xs:enumeration value="PlainString" /> - <xs:enumeration value="String" /> - <xs:enumeration value="BigDecimal" /> - <xs:enumeration value="Double" /> - <xs:enumeration value="Float" /> - <xs:enumeration value="Long" /> - <xs:enumeration value="Integer" /> - <xs:enumeration value="Date" /> - <xs:enumeration value="Time" /> - <xs:enumeration value="Timestamp" /> - <xs:enumeration value="Boolean" /> - <xs:enumeration value="Object" /> - </xs:restriction> - </xs:simpleType> - </xs:attribute> - <xs:attribute name="format" type="xs:string" /> - </xs:complexType> - </xs:element> - <xs:element name="if-compare-field" substitutionGroup="AllConditionals"> - <xs:complexType> - <xs:attribute name="field" type="xs:string" use="required" /> - <xs:attribute name="operator" use="required"> - <xs:simpleType> - <xs:restriction base="xs:token"> - <xs:enumeration value="less" /> - <xs:enumeration value="greater" /> - <xs:enumeration value="less-equals" /> - <xs:enumeration value="greater-equals" /> - <xs:enumeration value="equals" /> - <xs:enumeration value="not-equals" /> - <xs:enumeration value="contains" /> - </xs:restriction> - </xs:simpleType> - </xs:attribute> - <xs:attribute name="to-field" type="xs:string" /> - <xs:attribute name="type" default="String"> - <xs:simpleType> - <xs:restriction base="xs:token"> - <xs:enumeration value="PlainString" /> - <xs:enumeration value="String" /> - <xs:enumeration value="BigDecimal" /> - <xs:enumeration value="Double" /> - <xs:enumeration value="Float" /> - <xs:enumeration value="Long" /> - <xs:enumeration value="Integer" /> - <xs:enumeration value="Date" /> - <xs:enumeration value="Time" /> - <xs:enumeration value="Timestamp" /> - <xs:enumeration value="Boolean" /> - <xs:enumeration value="Object" /> - </xs:restriction> - </xs:simpleType> - </xs:attribute> - <xs:attribute type="xs:string" name="format" /> - </xs:complexType> - </xs:element> - <xs:element name="if-regexp" substitutionGroup="AllConditionals"> - <xs:complexType> - <xs:attribute type="xs:string" name="field" use="required" /> - <xs:attribute type="xs:string" name="expr" use="required" /> - </xs:complexType> - </xs:element> - <xs:element name="if-empty" substitutionGroup="AllConditionals"> - <xs:complexType> - <xs:attribute type="xs:string" name="field" use="required"/> - </xs:complexType> - </xs:element> </xs:schema> \ No newline at end of file Modified: ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/ModelActionVisitor.java URL: http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/ModelActionVisitor.java?rev=1649482&r1=1649481&r2=1649482&view=diff ============================================================================== --- ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/ModelActionVisitor.java (original) +++ ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/ModelActionVisitor.java Mon Jan 5 08:50:30 2015 @@ -51,10 +51,6 @@ public interface ModelActionVisitor { void visit(ModelFormAction.Service service); - void visit(ModelFormAction.EntityAnd entityAnd); - - void visit(ModelFormAction.EntityCondition entityCondition); - void visit(ModelMenuAction.SetField setField); void visit(ModelTreeAction.Script script); Modified: ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/ModelWidgetAction.java URL: http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/ModelWidgetAction.java?rev=1649482&r1=1649481&r2=1649482&view=diff ============================================================================== --- ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/ModelWidgetAction.java (original) +++ ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/ModelWidgetAction.java Mon Jan 5 08:50:30 2015 @@ -60,24 +60,36 @@ import org.ofbiz.service.GenericServiceE import org.ofbiz.service.ModelService; import org.w3c.dom.Element; +/** + * Abstract widget action. + */ @SuppressWarnings("serial") public abstract class ModelWidgetAction implements Serializable { - public static final String module = ModelWidgetAction.class.getName(); - - protected ModelWidget modelWidget; - - protected ModelWidgetAction() {} - - public ModelWidgetAction(ModelWidget modelWidget, Element actionElement) { - this.modelWidget = modelWidget; - if (Debug.verboseOn()) Debug.logVerbose("Reading widget action with name: " + actionElement.getNodeName(), module); - } - public abstract void runAction(Map<String, Object> context) throws GeneralException; + /* + * ----------------------------------------------------------------------- * + * 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 abstract void accept(ModelActionVisitor visitor); + public static final String module = ModelWidgetAction.class.getName(); - public static ModelWidgetAction toModelWidgetAction(ModelWidget modelWidget, Element actionElement) { + /** + * Returns a new <code>ModelWidgetAction</code> instance, built from <code>actionElement</code>. + * + * @param modelWidget The <code>ModelWidget</code> that contains the <actions> element + * @param actionElement + * @return A new <code>ModelWidgetAction</code> instance + */ + public static ModelWidgetAction newInstance(ModelWidget modelWidget, Element actionElement) { if ("set".equals(actionElement.getNodeName())) { return new SetField(modelWidget, actionElement); } else if ("property-map".equals(actionElement.getNodeName())) { @@ -102,18 +114,25 @@ public abstract class ModelWidgetAction throw new IllegalArgumentException("Action element not supported with name: " + actionElement.getNodeName()); } } - + public static List<ModelWidgetAction> readSubActions(ModelWidget modelWidget, Element parentElement) { List<? extends Element> actionElementList = UtilXml.childElementList(parentElement); List<ModelWidgetAction> actions = new ArrayList<ModelWidgetAction>(actionElementList.size()); - for (Element actionElement: actionElementList) { - actions.add(toModelWidgetAction(modelWidget, actionElement)); + for (Element actionElement : actionElementList) { + actions.add(newInstance(modelWidget, actionElement)); } return Collections.unmodifiableList(actions); } + /** + * Executes the actions contained in <code>actions</code>. + * + * @param actions + * @param context + */ public static void runSubActions(List<ModelWidgetAction> actions, Map<String, Object> context) { - if (actions == null) return; + if (actions == null) + return; for (ModelWidgetAction action : actions) { if (Debug.verboseOn()) Debug.logVerbose("Running action " + action.getClass().getName(), module); @@ -125,305 +144,403 @@ public abstract class ModelWidgetAction } } - public static class SetField extends ModelWidgetAction { - protected FlexibleMapAccessor<Object> field; - protected FlexibleMapAccessor<Object> fromField; - protected FlexibleStringExpander valueExdr; - protected FlexibleStringExpander defaultExdr; - protected FlexibleStringExpander globalExdr; - protected String type; - protected String toScope; - protected String fromScope; - - public SetField(ModelWidget modelWidget, Element setElement) { - super (modelWidget, 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 = FlexibleStringExpander.getInstance(setElement.getAttribute("default-value")); - 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 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()) { - HttpSession session = (HttpSession) context.get("session"); - newValue = getInMemoryPersistedFromField(session, context); - 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.expand(context); - } - } else if (this.fromScope != null && this.fromScope.equals("application")) { - if (!this.fromField.isEmpty()) { - ServletContext servletContext = (ServletContext) context.get("application"); - newValue = getInMemoryPersistedFromField(servletContext, context); - 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("Getting value for field from [" + this.fromField.getOriginalName() + "]: " + newValue, module); - } else if (!this.valueExdr.isEmpty()) { - newValue = this.valueExdr.expand(context); - } - } - - // If newValue is still empty, use the default value - if (ObjectType.isEmpty(newValue) && !this.defaultExdr.isEmpty()) { - newValue = this.defaultExdr.expand(context); - } + private final ModelWidget modelWidget; - 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); - } - } - } + protected ModelWidgetAction() { + // FIXME: This should not be null. + this.modelWidget = null; + } - if (this.toScope != null && this.toScope.equals("user")) { - String originalName = this.field.getOriginalName(); - List<String> currentWidgetTrail = UtilGenerics.toList(context.get("_WIDGETTRAIL_")); - String newKey = ""; - if (currentWidgetTrail != null) { - newKey = StringUtil.join(currentWidgetTrail, "|"); - } - if (UtilValidate.isNotEmpty(newKey)) { - newKey += "|"; - } - newKey += 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(); - List<String> currentWidgetTrail = UtilGenerics.toList(context.get("_WIDGETTRAIL_")); - String newKey = ""; - if (currentWidgetTrail != null) { - newKey = StringUtil.join(currentWidgetTrail, "|"); - } - if (UtilValidate.isNotEmpty(newKey)) { - newKey += "|"; - } - newKey += 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 { - // only do this if it is not global, if global ONLY put it in the global context - if (!global) { - if (Debug.verboseOn()) Debug.logVerbose("Setting field [" + this.field.getOriginalName() + "] to value: " + newValue, module); - this.field.put(context, newValue); - } - } + protected ModelWidgetAction(ModelWidget modelWidget, Element actionElement) { + this.modelWidget = modelWidget; + if (Debug.verboseOn()) + Debug.logVerbose("Reading widget action with name: " + actionElement.getNodeName(), module); + } - if (global) { - Map<String, Object> globalCtx = UtilGenerics.checkMap(context.get("globalContext")); - if (globalCtx != null) { - this.field.put(globalCtx, newValue); - } else { - this.field.put(context, newValue); - } - } + public abstract void accept(ModelActionVisitor visitor); - // 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); - } - } + /** + * Returns the <code>ModelWidget</code> that contains the <actions> element. + * + * @return The <code>ModelWidget</code> that contains the <actions> element + */ + public ModelWidget getModelWidget() { + return modelWidget; + } - public Object getInMemoryPersistedFromField(Object storeAgent, Map<String, Object> context) { - Object newValue = null; - String originalName = this.fromField.getOriginalName(); - List<String> currentWidgetTrail = UtilGenerics.toList(context.get("_WIDGETTRAIL_")); - List<String> trailList = new ArrayList<String>(); - if (currentWidgetTrail != null) { - trailList.addAll(currentWidgetTrail); - } + /** + * Executes this action. + * + * @param context + * @throws GeneralException + */ + public abstract void runAction(Map<String, Object> context) throws GeneralException; - for (int i=trailList.size(); i >= 0; i--) { - List<String> subTrail = trailList.subList(0,i); - String newKey = null; - if (subTrail.size() > 0) - newKey = StringUtil.join(subTrail, "|") + "|" + originalName; - else - newKey = originalName; + /** + * Models the <entity-and> element. + * + * @see <code>widget-screen.xsd</code> + */ + public static class EntityAnd extends ModelWidgetAction { + private final ByAndFinder finder; - if (storeAgent instanceof ServletContext) { - newValue = ((ServletContext)storeAgent).getAttribute(newKey); - } else if (storeAgent instanceof HttpSession) { - newValue = ((HttpSession)storeAgent).getAttribute(newKey); - } - if (newValue != null) { - break; - } - } - return newValue; + public EntityAnd(ModelWidget modelWidget, Element entityAndElement) { + super(modelWidget, entityAndElement); + finder = new ByAndFinder(entityAndElement); } @Override public void accept(ModelActionVisitor visitor) { visitor.visit(this); } - } - - public static class PropertyMap extends ModelWidgetAction { - protected FlexibleStringExpander resourceExdr; - protected FlexibleMapAccessor<ResourceBundleMapWrapper> mapNameAcsr; - protected FlexibleStringExpander globalExdr; - public PropertyMap(ModelWidget modelWidget, Element setElement) { - super (modelWidget, setElement); - this.resourceExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("resource")); - this.mapNameAcsr = FlexibleMapAccessor.getInstance(setElement.getAttribute("map-name")); - this.globalExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("global")); + public ByAndFinder getFinder() { + return this.finder; } @Override public void runAction(Map<String, Object> context) { - String globalStr = this.globalExdr.expandString(context); - // default to false - boolean global = "true".equals(globalStr); - - Locale locale = (Locale) context.get("locale"); - String resource = this.resourceExdr.expandString(context, locale); - - ResourceBundleMapWrapper existingPropMap = this.mapNameAcsr.get(context); - if (existingPropMap == null) { - this.mapNameAcsr.put(context, UtilProperties.getResourceBundleMap(resource, locale, context)); - } else { - try { - existingPropMap.addBottomResourceBundle(resource); - } catch (IllegalArgumentException e) { - // log the error, but don't let it kill everything just for a typo or bad char in an l10n file - Debug.logError(e, "Error adding resource bundle [" + resource + "]: " + e.toString(), module); - } + try { + finder.runFind(context, WidgetWorker.getDelegator(context)); + } catch (GeneralException e) { + String errMsg = "Error doing entity query by condition: " + e.toString(); + Debug.logError(e, errMsg, module); + throw new IllegalArgumentException(errMsg); } + } + } - if (global) { - Map<String, Object> globalCtx = UtilGenerics.checkMap(context.get("globalContext")); - if (globalCtx != null) { - ResourceBundleMapWrapper globalExistingPropMap = this.mapNameAcsr.get(globalCtx); - if (globalExistingPropMap == null) { - this.mapNameAcsr.put(globalCtx, UtilProperties.getResourceBundleMap(resource, locale, context)); - } else { - // is it the same object? if not add it in here too... - if (existingPropMap != globalExistingPropMap) { - try { - globalExistingPropMap.addBottomResourceBundle(resource); - } catch (IllegalArgumentException e) { - // log the error, but don't let it kill everything just for a typo or bad char in an l10n file - Debug.logError(e, "Error adding resource bundle [" + resource + "]: " + e.toString(), module); - } - } - } - } - } + /** + * Models the <entity-condition> element. + * + * @see <code>widget-screen.xsd</code> + */ + public static class EntityCondition extends ModelWidgetAction { + private final ByConditionFinder finder; + + public EntityCondition(ModelWidget modelWidget, Element entityConditionElement) { + super(modelWidget, entityConditionElement); + finder = new ByConditionFinder(entityConditionElement); } @Override public void accept(ModelActionVisitor visitor) { visitor.visit(this); } - } - public static class PropertyToField extends ModelWidgetAction { - - protected FlexibleStringExpander resourceExdr; - protected FlexibleStringExpander propertyExdr; - protected FlexibleMapAccessor<Object> fieldAcsr; - protected FlexibleStringExpander defaultExdr; - protected boolean noLocale; - protected FlexibleMapAccessor<List<? extends Object>> argListAcsr; - protected FlexibleStringExpander globalExdr; - - public PropertyToField(ModelWidget modelWidget, Element setElement) { - super (modelWidget, setElement); - this.resourceExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("resource")); - this.propertyExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("property")); - this.fieldAcsr = FlexibleMapAccessor.getInstance(setElement.getAttribute("field")); - this.defaultExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("default")); - this.noLocale = "true".equals(setElement.getAttribute("no-locale")); - this.argListAcsr = FlexibleMapAccessor.getInstance(setElement.getAttribute("arg-list-name")); - this.globalExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("global")); + public ByConditionFinder getFinder() { + return this.finder; } @Override public void runAction(Map<String, Object> context) { - //String globalStr = this.globalExdr.expandString(context); - // default to false - //boolean global = "true".equals(globalStr); - - Locale locale = (Locale) context.get("locale"); - String resource = this.resourceExdr.expandString(context, locale); - String property = this.propertyExdr.expandString(context, locale); - - String value = null; - if (noLocale) { - value = EntityUtilProperties.getPropertyValue(resource, property, WidgetWorker.getDelegator(context)); - } else { - value = EntityUtilProperties.getMessage(resource, property, locale, WidgetWorker.getDelegator(context)); - } - if (UtilValidate.isEmpty(value)) { - value = this.defaultExdr.expandString(context); + try { + finder.runFind(context, WidgetWorker.getDelegator(context)); + } catch (GeneralException e) { + String errMsg = "Error doing entity query by condition: " + e.toString(); + Debug.logError(e, errMsg, module); + throw new IllegalArgumentException(errMsg); } + } + } - // note that expanding the value string here will handle defaultValue and the string from - // the properties file; if we decide later that we don't want the string from the properties - // file to be expanded we should just expand the defaultValue at the beginning of this method. - value = FlexibleStringExpander.expandString(value, context); + /** + * Models the <entity-one> element. + * + * @see <code>widget-common.xsd</code> + */ + public static class EntityOne extends ModelWidgetAction { + private final PrimaryKeyFinder finder; - if (!argListAcsr.isEmpty()) { - List<? extends Object> argList = argListAcsr.get(context); - if (UtilValidate.isNotEmpty(argList)) { - value = MessageFormat.format(value, argList.toArray()); - } - } - fieldAcsr.put(context, value); + public EntityOne(ModelWidget modelWidget, Element entityOneElement) { + super(modelWidget, entityOneElement); + finder = new PrimaryKeyFinder(entityOneElement); } @Override public void accept(ModelActionVisitor visitor) { visitor.visit(this); } - } + public PrimaryKeyFinder getFinder() { + return this.finder; + } + + @Override + public void runAction(Map<String, Object> context) { + try { + finder.runFind(context, WidgetWorker.getDelegator(context)); + } catch (GeneralException e) { + String errMsg = "Error doing entity query by condition: " + e.toString(); + Debug.logError(e, errMsg, module); + throw new IllegalArgumentException(errMsg); + } + } + } + + /** + * Models the <get-related> element. + * + * @see <code>widget-common.xsd</code> + */ + public static class GetRelated extends ModelWidgetAction { + private final FlexibleMapAccessor<List<GenericValue>> listNameAcsr; + private final FlexibleMapAccessor<Map<String, Object>> mapAcsr; + private final FlexibleMapAccessor<List<String>> orderByListAcsr; + private final String relationName; + private final boolean useCache; + private final FlexibleMapAccessor<Object> valueNameAcsr; + + public GetRelated(ModelWidget modelWidget, Element getRelatedElement) { + super(modelWidget, getRelatedElement); + this.valueNameAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("value-field")); + this.listNameAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("list")); + this.relationName = getRelatedElement.getAttribute("relation-name"); + this.mapAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("map")); + this.orderByListAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("order-by-list")); + this.useCache = "true".equals(getRelatedElement.getAttribute("use-cache")); + } + + @Override + public void accept(ModelActionVisitor visitor) { + visitor.visit(this); + } + + public String getRelationName() { + return this.relationName; + } + + @Override + public void runAction(Map<String, Object> context) { + Object valueObject = valueNameAcsr.get(context); + if (valueObject == null) { + Debug.logVerbose("Value not found with name: " + valueNameAcsr + ", not getting related...", module); + return; + } + if (!(valueObject instanceof GenericValue)) { + String errMsg = "Env variable for value-name " + valueNameAcsr.toString() + + " is not a GenericValue object; for the relation-name: " + relationName + "]"; + Debug.logError(errMsg, module); + throw new IllegalArgumentException(errMsg); + } + GenericValue value = (GenericValue) valueObject; + List<String> orderByNames = null; + if (!orderByListAcsr.isEmpty()) { + orderByNames = orderByListAcsr.get(context); + } + Map<String, Object> constraintMap = null; + if (!mapAcsr.isEmpty()) { + constraintMap = mapAcsr.get(context); + } + try { + listNameAcsr.put(context, value.getRelated(relationName, constraintMap, orderByNames, useCache)); + } catch (GenericEntityException e) { + String errMsg = "Problem getting related from entity with name " + value.getEntityName() + + " for the relation-name: " + relationName + ": " + e.getMessage(); + Debug.logError(e, errMsg, module); + throw new IllegalArgumentException(errMsg); + } + } + } + + /** + * Models the <get-related-one> element. + * + * @see <code>widget-common.xsd</code> + */ + public static class GetRelatedOne extends ModelWidgetAction { + private final String relationName; + private final FlexibleMapAccessor<Object> toValueNameAcsr; + private final boolean useCache; + private final FlexibleMapAccessor<Object> valueNameAcsr; + + public GetRelatedOne(ModelWidget modelWidget, Element getRelatedOneElement) { + super(modelWidget, getRelatedOneElement); + this.valueNameAcsr = FlexibleMapAccessor.getInstance(getRelatedOneElement.getAttribute("value-field")); + this.toValueNameAcsr = FlexibleMapAccessor.getInstance(getRelatedOneElement.getAttribute("to-value-field")); + this.relationName = getRelatedOneElement.getAttribute("relation-name"); + this.useCache = "true".equals(getRelatedOneElement.getAttribute("use-cache")); + } + + @Override + public void accept(ModelActionVisitor visitor) { + visitor.visit(this); + } + + public String getRelationName() { + return this.relationName; + } + + @Override + public void runAction(Map<String, Object> context) { + Object valueObject = valueNameAcsr.get(context); + if (valueObject == null) { + Debug.logVerbose("Value not found with name: " + valueNameAcsr + ", not getting related...", module); + return; + } + if (!(valueObject instanceof GenericValue)) { + String errMsg = "Env variable for value-name " + valueNameAcsr.toString() + + " is not a GenericValue object; for the relation-name: " + relationName + "]"; + Debug.logError(errMsg, module); + throw new IllegalArgumentException(errMsg); + } + GenericValue value = (GenericValue) valueObject; + try { + toValueNameAcsr.put(context, value.getRelatedOne(relationName, useCache)); + } catch (GenericEntityException e) { + String errMsg = "Problem getting related one from entity with name " + value.getEntityName() + + " for the relation-name: " + relationName + ": " + e.getMessage(); + Debug.logError(e, errMsg, module); + throw new IllegalArgumentException(errMsg); + } + } + } + + /** + * Models the <property-map> element. + * + * @see <code>widget-common.xsd</code> + */ + public static class PropertyMap extends ModelWidgetAction { + private final FlexibleStringExpander globalExdr; + private final FlexibleMapAccessor<ResourceBundleMapWrapper> mapNameAcsr; + private final FlexibleStringExpander resourceExdr; + + public PropertyMap(ModelWidget modelWidget, Element setElement) { + super(modelWidget, setElement); + this.resourceExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("resource")); + this.mapNameAcsr = FlexibleMapAccessor.getInstance(setElement.getAttribute("map-name")); + this.globalExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("global")); + } + + @Override + public void accept(ModelActionVisitor visitor) { + visitor.visit(this); + } + + @Override + public void runAction(Map<String, Object> context) { + String globalStr = this.globalExdr.expandString(context); + // default to false + boolean global = "true".equals(globalStr); + Locale locale = (Locale) context.get("locale"); + String resource = this.resourceExdr.expandString(context, locale); + ResourceBundleMapWrapper existingPropMap = this.mapNameAcsr.get(context); + if (existingPropMap == null) { + this.mapNameAcsr.put(context, UtilProperties.getResourceBundleMap(resource, locale, context)); + } else { + try { + existingPropMap.addBottomResourceBundle(resource); + } catch (IllegalArgumentException e) { + // log the error, but don't let it kill everything just for a typo or bad char in an l10n file + Debug.logError(e, "Error adding resource bundle [" + resource + "]: " + e.toString(), module); + } + } + if (global) { + Map<String, Object> globalCtx = UtilGenerics.checkMap(context.get("globalContext")); + if (globalCtx != null) { + ResourceBundleMapWrapper globalExistingPropMap = this.mapNameAcsr.get(globalCtx); + if (globalExistingPropMap == null) { + this.mapNameAcsr.put(globalCtx, UtilProperties.getResourceBundleMap(resource, locale, context)); + } else { + // is it the same object? if not add it in here too... + if (existingPropMap != globalExistingPropMap) { + try { + globalExistingPropMap.addBottomResourceBundle(resource); + } catch (IllegalArgumentException e) { + // log the error, but don't let it kill everything just for a typo or bad char in an l10n file + Debug.logError(e, "Error adding resource bundle [" + resource + "]: " + e.toString(), module); + } + } + } + } + } + } + } + + /** + * Models the <property-to-field> element. + * + * @see <code>widget-common.xsd</code> + */ + public static class PropertyToField extends ModelWidgetAction { + private final FlexibleMapAccessor<List<? extends Object>> argListAcsr; + private final FlexibleStringExpander defaultExdr; + private final FlexibleMapAccessor<Object> fieldAcsr; + private final FlexibleStringExpander globalExdr; + private final boolean noLocale; + private final FlexibleStringExpander propertyExdr; + private final FlexibleStringExpander resourceExdr; + + public PropertyToField(ModelWidget modelWidget, Element setElement) { + super(modelWidget, setElement); + this.resourceExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("resource")); + this.propertyExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("property")); + this.fieldAcsr = FlexibleMapAccessor.getInstance(setElement.getAttribute("field")); + this.defaultExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("default")); + this.noLocale = "true".equals(setElement.getAttribute("no-locale")); + this.argListAcsr = FlexibleMapAccessor.getInstance(setElement.getAttribute("arg-list-name")); + this.globalExdr = FlexibleStringExpander.getInstance(setElement.getAttribute("global")); + } + + @Override + public void accept(ModelActionVisitor visitor) { + visitor.visit(this); + } + + @Override + public void runAction(Map<String, Object> context) { + //String globalStr = this.globalExdr.expandString(context); + // default to false + //boolean global = "true".equals(globalStr); + Locale locale = (Locale) context.get("locale"); + String resource = this.resourceExdr.expandString(context, locale); + String property = this.propertyExdr.expandString(context, locale); + String value = null; + if (noLocale) { + value = EntityUtilProperties.getPropertyValue(resource, property, WidgetWorker.getDelegator(context)); + } else { + value = EntityUtilProperties.getMessage(resource, property, locale, WidgetWorker.getDelegator(context)); + } + if (UtilValidate.isEmpty(value)) { + value = this.defaultExdr.expandString(context); + } + // note that expanding the value string here will handle defaultValue and the string from + // the properties file; if we decide later that we don't want the string from the properties + // file to be expanded we should just expand the defaultValue at the beginning of this method. + value = FlexibleStringExpander.expandString(value, context); + if (!argListAcsr.isEmpty()) { + List<? extends Object> argList = argListAcsr.get(context); + if (UtilValidate.isNotEmpty(argList)) { + value = MessageFormat.format(value, argList.toArray()); + } + } + fieldAcsr.put(context, value); + } + } + + /** + * Models the <script> element. + * + * @see <code>widget-common.xsd</code> + */ public static class Script extends ModelWidgetAction { - protected String location; - protected String method; + private final String location; + private final String method; public Script(ModelWidget modelWidget, Element scriptElement) { - super (modelWidget, scriptElement); + super(modelWidget, scriptElement); String scriptLocation = scriptElement.getAttribute("location"); this.location = WidgetWorker.getScriptLocation(scriptLocation); this.method = WidgetWorker.getScriptMethodName(scriptLocation); } @Override + public void accept(ModelActionVisitor visitor) { + visitor.visit(this); + } + + @Override public void runAction(Map<String, Object> context) throws GeneralException { if (location.endsWith(".xml")) { Map<String, Object> localContext = new HashMap<String, Object>(); @@ -440,37 +557,43 @@ public abstract class ModelWidgetAction ScriptUtil.executeScript(this.location, this.method, context); } } - - @Override - public void accept(ModelActionVisitor visitor) { - visitor.visit(this); - } } + /** + * Models the <service> element. + * + * @see <code>widget-screen.xsd</code> + */ public static class Service extends ModelWidgetAction { - protected FlexibleStringExpander serviceNameExdr; - protected FlexibleMapAccessor<Map<String, Object>> resultMapNameAcsr; - protected FlexibleStringExpander autoFieldMapExdr; - protected Map<FlexibleMapAccessor<Object>, Object> fieldMap; + private final FlexibleStringExpander autoFieldMapExdr; + private final Map<FlexibleMapAccessor<Object>, Object> fieldMap; + private final FlexibleMapAccessor<Map<String, Object>> resultMapNameAcsr; + private final FlexibleStringExpander serviceNameExdr; public Service(ModelWidget modelWidget, Element serviceElement) { - super (modelWidget, serviceElement); + super(modelWidget, serviceElement); this.serviceNameExdr = FlexibleStringExpander.getInstance(serviceElement.getAttribute("service-name")); this.resultMapNameAcsr = FlexibleMapAccessor.getInstance(serviceElement.getAttribute("result-map")); - if (this.resultMapNameAcsr.isEmpty()) this.resultMapNameAcsr = FlexibleMapAccessor.getInstance(serviceElement.getAttribute("result-map-name")); this.autoFieldMapExdr = FlexibleStringExpander.getInstance(serviceElement.getAttribute("auto-field-map")); this.fieldMap = EntityFinderUtil.makeFieldMap(serviceElement); } @Override + public void accept(ModelActionVisitor visitor) { + visitor.visit(this); + } + + public FlexibleStringExpander getServiceNameExdr() { + return this.serviceNameExdr; + } + + @Override public void runAction(Map<String, Object> context) { String serviceNameExpanded = this.serviceNameExdr.expandString(context); if (UtilValidate.isEmpty(serviceNameExpanded)) { throw new IllegalArgumentException("Service name was empty, expanded from: " + this.serviceNameExdr.getOriginal()); } - String autoFieldMapString = this.autoFieldMapExdr.expandString(context); - try { Map<String, Object> serviceContext = null; if ("true".equals(autoFieldMapString)) { @@ -487,22 +610,20 @@ public abstract class ModelWidgetAction FlexibleMapAccessor<Object> fieldFma = FlexibleMapAccessor.getInstance(autoFieldMapString); Map<String, Object> autoFieldMap = UtilGenerics.toMap(fieldFma.get(context)); if (autoFieldMap != null) { - serviceContext = WidgetWorker.getDispatcher(context).getDispatchContext().makeValidContext(serviceNameExpanded, ModelService.IN_PARAM, autoFieldMap); + serviceContext = WidgetWorker.getDispatcher(context).getDispatchContext() + .makeValidContext(serviceNameExpanded, ModelService.IN_PARAM, autoFieldMap); } } if (serviceContext == null) { serviceContext = new HashMap<String, Object>(); } - if (this.fieldMap != null) { EntityFinderUtil.expandFieldMapToContext(this.fieldMap, context, serviceContext); } - Map<String, Object> result = WidgetWorker.getDispatcher(context).runSync(serviceNameExpanded, serviceContext); - if (!this.resultMapNameAcsr.isEmpty()) { this.resultMapNameAcsr.put(context, result); - String queryString = (String)result.get("queryString"); + String queryString = (String) result.get("queryString"); context.put("queryString", queryString); context.put("queryStringMap", result.get("queryStringMap")); if (UtilValidate.isNotEmpty(queryString)) { @@ -522,211 +643,182 @@ public abstract class ModelWidgetAction throw new IllegalArgumentException(errMsg); } } - - public FlexibleStringExpander getServiceNameExdr() { - return this.serviceNameExdr; - } - - @Override - public void accept(ModelActionVisitor visitor) { - visitor.visit(this); - } } - public static class EntityOne extends ModelWidgetAction { - protected PrimaryKeyFinder finder; - - public EntityOne(ModelWidget modelWidget, Element entityOneElement) { - super (modelWidget, entityOneElement); - finder = new PrimaryKeyFinder(entityOneElement); - } + /** + * Models the <set> element. + * + * @see <code>widget-common.xsd</code> + */ + public static class SetField extends ModelWidgetAction { + private final FlexibleStringExpander defaultExdr; + private final FlexibleMapAccessor<Object> field; + private final FlexibleMapAccessor<Object> fromField; + private final String fromScope; + private final FlexibleStringExpander globalExdr; + private final String toScope; + private final String type; + private final FlexibleStringExpander valueExdr; - @Override - public void runAction(Map<String, Object> context) { - try { - finder.runFind(context, WidgetWorker.getDelegator(context)); - } catch (GeneralException e) { - String errMsg = "Error doing entity query by condition: " + e.toString(); - Debug.logError(e, errMsg, module); - throw new IllegalArgumentException(errMsg); + public SetField(ModelWidget modelWidget, Element setElement) { + super(modelWidget, 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 = FlexibleStringExpander.getInstance(setElement.getAttribute("default-value")); + 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 widget"); } } - public PrimaryKeyFinder getFinder() { - return this.finder; - } - @Override public void accept(ModelActionVisitor visitor) { visitor.visit(this); } - } - public static class EntityAnd extends ModelWidgetAction { - protected ByAndFinder finder; - - public EntityAnd(ModelWidget modelWidget, Element entityAndElement) { - super (modelWidget, entityAndElement); - finder = new ByAndFinder(entityAndElement); - } - - @Override - public void runAction(Map<String, Object> context) { - try { - finder.runFind(context, WidgetWorker.getDelegator(context)); - } catch (GeneralException e) { - String errMsg = "Error doing entity query by condition: " + e.toString(); - Debug.logError(e, errMsg, module); - throw new IllegalArgumentException(errMsg); + public Object getInMemoryPersistedFromField(Object storeAgent, Map<String, Object> context) { + Object newValue = null; + String originalName = this.fromField.getOriginalName(); + List<String> currentWidgetTrail = UtilGenerics.toList(context.get("_WIDGETTRAIL_")); + List<String> trailList = new ArrayList<String>(); + if (currentWidgetTrail != null) { + trailList.addAll(currentWidgetTrail); } - } - - public ByAndFinder getFinder() { - return this.finder; - } - - @Override - public void accept(ModelActionVisitor visitor) { - visitor.visit(this); - } - } - - public static class EntityCondition extends ModelWidgetAction { - ByConditionFinder finder; - - public EntityCondition(ModelWidget modelWidget, Element entityConditionElement) { - super (modelWidget, entityConditionElement); - finder = new ByConditionFinder(entityConditionElement); - } - - @Override - public void runAction(Map<String, Object> context) { - try { - finder.runFind(context, WidgetWorker.getDelegator(context)); - } catch (GeneralException e) { - String errMsg = "Error doing entity query by condition: " + e.toString(); - Debug.logError(e, errMsg, module); - throw new IllegalArgumentException(errMsg); + for (int i = trailList.size(); i >= 0; i--) { + List<String> subTrail = trailList.subList(0, i); + String newKey = null; + if (subTrail.size() > 0) + newKey = StringUtil.join(subTrail, "|") + "|" + originalName; + else + newKey = originalName; + if (storeAgent instanceof ServletContext) { + newValue = ((ServletContext) storeAgent).getAttribute(newKey); + } else if (storeAgent instanceof HttpSession) { + newValue = ((HttpSession) storeAgent).getAttribute(newKey); + } + if (newValue != null) { + break; + } } + return newValue; } - public ByConditionFinder getFinder() { - return this.finder; - } - - @Override - public void accept(ModelActionVisitor visitor) { - visitor.visit(this); - } - } - - public static class GetRelatedOne extends ModelWidgetAction { - protected FlexibleMapAccessor<Object> valueNameAcsr; - protected FlexibleMapAccessor<Object> toValueNameAcsr; - protected String relationName; - protected boolean useCache; - - public GetRelatedOne(ModelWidget modelWidget, Element getRelatedOneElement) { - super (modelWidget, getRelatedOneElement); - this.valueNameAcsr = FlexibleMapAccessor.getInstance(getRelatedOneElement.getAttribute("value-field")); - if (this.valueNameAcsr.isEmpty()) this.valueNameAcsr = FlexibleMapAccessor.getInstance(getRelatedOneElement.getAttribute("value-name")); - this.toValueNameAcsr = FlexibleMapAccessor.getInstance(getRelatedOneElement.getAttribute("to-value-field")); - if (this.toValueNameAcsr.isEmpty()) this.toValueNameAcsr = FlexibleMapAccessor.getInstance(getRelatedOneElement.getAttribute("to-value-name")); - this.relationName = getRelatedOneElement.getAttribute("relation-name"); - this.useCache = "true".equals(getRelatedOneElement.getAttribute("use-cache")); - } - + @SuppressWarnings("rawtypes") @Override public void runAction(Map<String, Object> context) { - Object valueObject = valueNameAcsr.get(context); - if (valueObject == null) { - Debug.logVerbose("Value not found with name: " + valueNameAcsr + ", not getting related...", module); - return; - } - if (!(valueObject instanceof GenericValue)) { - String errMsg = "Env variable for value-name " + valueNameAcsr.toString() + " is not a GenericValue object; for the relation-name: " + relationName + "]"; - Debug.logError(errMsg, module); - throw new IllegalArgumentException(errMsg); - } - GenericValue value = (GenericValue) valueObject; - try { - toValueNameAcsr.put(context, value.getRelatedOne(relationName, useCache)); - } catch (GenericEntityException e) { - String errMsg = "Problem getting related one from entity with name " + value.getEntityName() + " for the relation-name: " + relationName + ": " + e.getMessage(); - Debug.logError(e, errMsg, module); - throw new IllegalArgumentException(errMsg); + 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()) { + HttpSession session = (HttpSession) context.get("session"); + newValue = getInMemoryPersistedFromField(session, context); + 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.expand(context); + } + } else if (this.fromScope != null && this.fromScope.equals("application")) { + if (!this.fromField.isEmpty()) { + ServletContext servletContext = (ServletContext) context.get("application"); + newValue = getInMemoryPersistedFromField(servletContext, context); + 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("Getting value for field from [" + this.fromField.getOriginalName() + "]: " + newValue, + module); + } else if (!this.valueExdr.isEmpty()) { + newValue = this.valueExdr.expand(context); + } } - } - - public String getRelationName() { - return this.relationName; - } - - @Override - public void accept(ModelActionVisitor visitor) { - visitor.visit(this); - } - } - - public static class GetRelated extends ModelWidgetAction { - protected FlexibleMapAccessor<Object> valueNameAcsr; - protected FlexibleMapAccessor<List<GenericValue>> listNameAcsr; - protected FlexibleMapAccessor<Map<String, Object>> mapAcsr; - protected FlexibleMapAccessor<List<String>> orderByListAcsr; - protected String relationName; - protected boolean useCache; - - public GetRelated(ModelWidget modelWidget, Element getRelatedElement) { - super (modelWidget, getRelatedElement); - this.valueNameAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("value-field")); - if (this.valueNameAcsr.isEmpty()) this.valueNameAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("value-name")); - this.listNameAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("list")); - if (this.listNameAcsr.isEmpty()) this.listNameAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("list-name")); - this.relationName = getRelatedElement.getAttribute("relation-name"); - this.mapAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("map")); - if (this.mapAcsr.isEmpty()) this.mapAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("map-name")); - this.orderByListAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("order-by-list")); - if (this.orderByListAcsr.isEmpty()) this.orderByListAcsr = FlexibleMapAccessor.getInstance(getRelatedElement.getAttribute("order-by-list-name")); - this.useCache = "true".equals(getRelatedElement.getAttribute("use-cache")); - } - - @Override - public void runAction(Map<String, Object> context) { - Object valueObject = valueNameAcsr.get(context); - if (valueObject == null) { - Debug.logVerbose("Value not found with name: " + valueNameAcsr + ", not getting related...", module); - return; + // If newValue is still empty, use the default value + if (ObjectType.isEmpty(newValue) && !this.defaultExdr.isEmpty()) { + newValue = this.defaultExdr.expand(context); } - if (!(valueObject instanceof GenericValue)) { - String errMsg = "Env variable for value-name " + valueNameAcsr.toString() + " is not a GenericValue object; for the relation-name: " + relationName + "]"; - Debug.logError(errMsg, module); - throw new IllegalArgumentException(errMsg); + 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); + } + } } - GenericValue value = (GenericValue) valueObject; - List<String> orderByNames = null; - if (!orderByListAcsr.isEmpty()) { - orderByNames = orderByListAcsr.get(context); + if (this.toScope != null && this.toScope.equals("user")) { + String originalName = this.field.getOriginalName(); + List<String> currentWidgetTrail = UtilGenerics.toList(context.get("_WIDGETTRAIL_")); + String newKey = ""; + if (currentWidgetTrail != null) { + newKey = StringUtil.join(currentWidgetTrail, "|"); + } + if (UtilValidate.isNotEmpty(newKey)) { + newKey += "|"; + } + newKey += 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(); + List<String> currentWidgetTrail = UtilGenerics.toList(context.get("_WIDGETTRAIL_")); + String newKey = ""; + if (currentWidgetTrail != null) { + newKey = StringUtil.join(currentWidgetTrail, "|"); + } + if (UtilValidate.isNotEmpty(newKey)) { + newKey += "|"; + } + newKey += 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 { + // only do this if it is not global, if global ONLY put it in the global context + if (!global) { + if (Debug.verboseOn()) + Debug.logVerbose("Setting field [" + this.field.getOriginalName() + "] to value: " + newValue, module); + this.field.put(context, newValue); + } } - Map<String, Object> constraintMap = null; - if (!mapAcsr.isEmpty()) { - constraintMap = mapAcsr.get(context); + if (global) { + Map<String, Object> globalCtx = UtilGenerics.checkMap(context.get("globalContext")); + if (globalCtx != null) { + this.field.put(globalCtx, newValue); + } else { + this.field.put(context, newValue); + } } - try { - listNameAcsr.put(context, value.getRelated(relationName, constraintMap, orderByNames, useCache)); - } catch (GenericEntityException e) { - String errMsg = "Problem getting related from entity with name " + value.getEntityName() + " for the relation-name: " + relationName + ": " + e.getMessage(); - Debug.logError(e, errMsg, module); - throw new IllegalArgumentException(errMsg); + // 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); } } - - public String getRelationName() { - return this.relationName; - } - - @Override - public void accept(ModelActionVisitor visitor) { - visitor.visit(this); - } } } Modified: ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/PortalPageWorker.java URL: http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/PortalPageWorker.java?rev=1649482&r1=1649481&r2=1649482&view=diff ============================================================================== --- ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/PortalPageWorker.java (original) +++ ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/PortalPageWorker.java Mon Jan 5 08:50:30 2015 @@ -32,6 +32,7 @@ import org.ofbiz.entity.GenericEntityExc import org.ofbiz.entity.GenericValue; import org.ofbiz.entity.condition.EntityCondition; import org.ofbiz.entity.condition.EntityOperator; +import org.ofbiz.entity.util.EntityQuery; import org.ofbiz.entity.util.EntityUtil; import org.ofbiz.security.Security; @@ -67,17 +68,16 @@ public class PortalPageWorker { EntityCondition.makeCondition("parentPortalPageId", EntityOperator.EQUALS, parentPortalPageId)), EntityOperator.OR)), EntityOperator.AND); - portalPages = delegator.findList("PortalPage", cond, null, null, null, false); + portalPages = EntityQuery.use(delegator).from("PortalPage").where(cond).queryList(); List<GenericValue> userPortalPages = new ArrayList<GenericValue>(); if (UtilValidate.isNotEmpty(context.get("userLogin"))) { // check if a user is logged in String userLoginId = ((GenericValue)context.get("userLogin")).getString("userLoginId"); // replace with private pages for (GenericValue portalPage : portalPages) { - cond = EntityCondition.makeCondition(UtilMisc.toList( - EntityCondition.makeCondition("ownerUserLoginId", EntityOperator.EQUALS, userLoginId), - EntityCondition.makeCondition("originalPortalPageId", EntityOperator.EQUALS, portalPage.getString("portalPageId"))), - EntityOperator.AND); - List <GenericValue> privatePortalPages = delegator.findList("PortalPage", cond, null, null, null, false); + List<GenericValue> privatePortalPages = EntityQuery.use(delegator) + .from("PortalPage") + .where("ownerUserLoginId", userLoginId, "originalPortalPageId", portalPage.getString("portalPageId")) + .queryList(); if (UtilValidate.isNotEmpty(privatePortalPages)) { userPortalPages.add(privatePortalPages.get(0)); } else { @@ -85,12 +85,10 @@ public class PortalPageWorker { } } // add any other created private pages - cond = EntityCondition.makeCondition(UtilMisc.toList( - EntityCondition.makeCondition("ownerUserLoginId", EntityOperator.EQUALS, userLoginId), - EntityCondition.makeCondition("originalPortalPageId", EntityOperator.EQUALS, null), - EntityCondition.makeCondition("parentPortalPageId", EntityOperator.EQUALS, parentPortalPageId)), - EntityOperator.AND); - userPortalPages.addAll(delegator.findList("PortalPage", cond, null, null, null, false)); + userPortalPages.addAll(EntityQuery.use(delegator) + .from("PortalPage") + .where("ownerUserLoginId", userLoginId, "originalPortalPageId", null, "parentPortalPageId", parentPortalPageId) + .queryList()); } portalPages = EntityUtil.orderBy(userPortalPages, UtilMisc.toList("sequenceNum")); } catch (GenericEntityException e) { @@ -123,7 +121,7 @@ public class PortalPageWorker { EntityCondition.makeCondition("ownerUserLoginId", EntityOperator.EQUALS, userLoginId)), EntityOperator.OR)), EntityOperator.AND); - List <GenericValue> portalPages = delegator.findList("PortalPage", cond, null, null, null, false); + List <GenericValue> portalPages = EntityQuery.use(delegator).from("PortalPage").where(cond).queryList(); if (UtilValidate.isNotEmpty(portalPages)) { portalPage = EntityUtil.getFirst(portalPages); } @@ -133,7 +131,7 @@ public class PortalPageWorker { EntityCondition.makeCondition("originalPortalPageId", EntityOperator.EQUALS, portalPageId), EntityCondition.makeCondition("ownerUserLoginId", EntityOperator.EQUALS, userLoginId)), EntityOperator.AND); - List <GenericValue> privateDerivedPortalPages = delegator.findList("PortalPage", cond, null, null, null, false); + List <GenericValue> privateDerivedPortalPages = EntityQuery.use(delegator).from("PortalPage").where(cond).queryList(); if (UtilValidate.isNotEmpty(privateDerivedPortalPages)) { portalPage = EntityUtil.getFirst(privateDerivedPortalPages); } @@ -160,8 +158,7 @@ public class PortalPageWorker { Boolean hasPortalAdminPermission = security.hasPermission("PORTALPAGE_ADMIN", userLogin); try { Delegator delegator = WidgetWorker.getDelegator(context); - GenericValue portalPage = delegator.findOne("PortalPage", UtilMisc.toMap("portalPageId", portalPageId),false); - + GenericValue portalPage = EntityQuery.use(delegator).from("PortalPage").where("portalPageId", portalPageId).queryOne(); if (UtilValidate.isNotEmpty(portalPage)) { String ownerUserLoginId = (String) portalPage.get("ownerUserLoginId"); // Users with PORTALPAGE_ADMIN permission can configure every Portal Page Modified: ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/WidgetWorker.java URL: http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/WidgetWorker.java?rev=1649482&r1=1649481&r2=1649482&view=diff ============================================================================== --- ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/WidgetWorker.java (original) +++ ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/WidgetWorker.java Mon Jan 5 08:50:30 2015 @@ -36,7 +36,7 @@ import javax.servlet.http.HttpServletReq import javax.servlet.http.HttpServletResponse; import org.ofbiz.base.util.Debug; -import org.ofbiz.base.util.StringUtil; +import org.ofbiz.base.util.UtilCodec; import org.ofbiz.base.util.UtilDateTime; import org.ofbiz.base.util.UtilGenerics; import org.ofbiz.base.util.UtilHttp; @@ -71,7 +71,7 @@ public class WidgetWorker { // We may get an encoded request like: /projectmgr/control/EditTaskContents?workEffortId=10003 // Try to reducing a possibly encoded string down to its simplest form: /projectmgr/control/EditTaskContents?workEffortId=10003 // This step make sure the following appending externalLoginKey operation to work correctly - localRequestName = StringUtil.defaultWebEncoder.canonicalize(localRequestName); + localRequestName = UtilCodec.canonicalize(localRequestName); Appendable localWriter = new StringWriter(); if ("intra-app".equals(targetType)) { @@ -143,7 +143,7 @@ public class WidgetWorker { } externalWriter.append(parameter.getKey()); externalWriter.append('='); - StringUtil.SimpleEncoder simpleEncoder = (StringUtil.SimpleEncoder) context.get("simpleEncoder"); + UtilCodec.SimpleEncoder simpleEncoder = (UtilCodec.SimpleEncoder) context.get("simpleEncoder"); if (simpleEncoder != null && parameterValue != null) { externalWriter.append(simpleEncoder.encode(URLEncoder.encode(parameterValue, Charset.forName("UTF-8").displayName()))); } else { @@ -300,7 +300,7 @@ public class WidgetWorker { writer.append("<input name=\""); writer.append(parameter.getKey()); writer.append("\" value=\""); - writer.append(StringUtil.htmlEncoder.encode(parameter.getValue())); + writer.append(UtilCodec.getEncoder("html").encode(parameter.getValue())); writer.append("\" type=\"hidden\"/>"); } } @@ -330,6 +330,11 @@ public class WidgetWorker { } } + /** + * Models the <parameter> element. + * + * @see <code>widget-form.xsd</code> + */ public static class Parameter { protected String name; protected FlexibleStringExpander value; |
Free forum by Nabble | Edit this page |