001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.method.envops;
019:
020: import org.ofbiz.base.util.Debug;
021: import org.ofbiz.base.util.GeneralException;
022: import org.ofbiz.base.util.ObjectType;
023: import org.ofbiz.base.util.UtilValidate;
024: import org.ofbiz.base.util.string.FlexibleStringExpander;
025: import org.ofbiz.minilang.SimpleMethod;
026: import org.ofbiz.minilang.method.ContextAccessor;
027: import org.ofbiz.minilang.method.MethodContext;
028: import org.ofbiz.minilang.method.MethodOperation;
029: import org.w3c.dom.Element;
030:
031: /**
032: * A general set operation to set a field from another field or from a value. Also supports a default-value, and type conversion.
033: */
034: public class SetOperation extends MethodOperation {
035: public static final String module = SetOperation.class.getName();
036:
037: protected ContextAccessor field;
038: protected ContextAccessor fromField;
039: protected FlexibleStringExpander valueExdr;
040: protected FlexibleStringExpander defaultExdr;
041: protected String type;
042: protected boolean setIfNull; // default to false
043: protected boolean setIfEmpty; // default to true
044:
045: public SetOperation(Element element, SimpleMethod simpleMethod) {
046: super (element, simpleMethod);
047: this .field = new ContextAccessor(element.getAttribute("field"));
048: this .fromField = new ContextAccessor(element
049: .getAttribute("from-field"));
050: this .valueExdr = new FlexibleStringExpander(element
051: .getAttribute("value"));
052: this .defaultExdr = new FlexibleStringExpander(element
053: .getAttribute("default-value"));
054: this .type = element.getAttribute("type");
055: // default to false, anything but true is false
056: this .setIfNull = "true".equals(element
057: .getAttribute("set-if-null"));
058: // default to true, anything but false is true
059: this .setIfEmpty = !"false".equals(element
060: .getAttribute("set-if-empty"));
061:
062: if (!this .fromField.isEmpty() && !this .valueExdr.isEmpty()) {
063: throw new IllegalArgumentException(
064: "Cannot specify a from-field ["
065: + element.getAttribute("from-field")
066: + "] and a value ["
067: + element.getAttribute("value")
068: + "] on the set action in a screen widget");
069: }
070: }
071:
072: public boolean exec(MethodContext methodContext) {
073: Object newValue = null;
074: if (!this .fromField.isEmpty()) {
075: newValue = this .fromField.get(methodContext);
076: if (Debug.verboseOn())
077: Debug.logVerbose(
078: "In screen getting value for field from ["
079: + this .fromField.toString() + "]: "
080: + newValue, module);
081: } else if (!this .valueExdr.isEmpty()) {
082: newValue = methodContext.expandString(this .valueExdr);
083: }
084:
085: // If newValue is still empty, use the default value
086: if (ObjectType.isEmpty(newValue) && !this .defaultExdr.isEmpty()) {
087: newValue = methodContext.expandString(this .defaultExdr);
088: }
089:
090: if (!setIfNull && newValue == null) {
091: if (Debug.verboseOn())
092: Debug
093: .logVerbose(
094: "Field value not found (null) with name ["
095: + fromField
096: + "] and value ["
097: + valueExdr
098: + "], and there was not default value, not setting field",
099: module);
100: return true;
101: }
102: if (!setIfEmpty && ObjectType.isEmpty(newValue)) {
103: if (Debug.verboseOn())
104: Debug
105: .logVerbose(
106: "Field value not found (empty) with name ["
107: + fromField
108: + "] and value ["
109: + valueExdr
110: + "], and there was not default value, not setting field",
111: module);
112: return true;
113: }
114:
115: if (UtilValidate.isNotEmpty(this .type)) {
116: try {
117: newValue = ObjectType.simpleTypeConvert(newValue,
118: this .type, null, null);
119: } catch (GeneralException e) {
120: String errMsg = "Could not convert field value for the field: ["
121: + this .field.toString()
122: + "] to the ["
123: + this .type
124: + "] type for the value ["
125: + newValue + "]: " + e.toString();
126: Debug.logError(e, errMsg, module);
127: methodContext.setErrorReturn(errMsg, simpleMethod);
128: return false;
129: }
130: }
131:
132: if (Debug.verboseOn())
133: Debug.logVerbose(
134: "In screen setting field [" + this .field.toString()
135: + "] to value: " + newValue, module);
136: this .field.put(methodContext, newValue);
137: return true;
138: }
139:
140: public String rawString() {
141: return "<set field=\""
142: + this .field
143: + (this .valueExdr.isEmpty() ? "" : "\" value=\""
144: + this .valueExdr.getOriginal())
145: + (this .fromField.isEmpty() ? "" : "\" from-field=\""
146: + this .fromField)
147: + (this .defaultExdr.isEmpty() ? ""
148: : "\" default-value=\""
149: + this .defaultExdr.getOriginal())
150: + (this .type == null || this .type.length() == 0 ? ""
151: : "\" type=\"" + this .type) + "\"/>";
152: }
153:
154: public String expandedString(MethodContext methodContext) {
155: // TODO: something more than a stub/dummy
156: return this.rawString();
157: }
158: }
|