KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > envops > SetOperation


1 /*
2  * $Id: EnvToEnv.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2005-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.minilang.method.envops;
25
26 import org.ofbiz.base.util.Debug;
27 import org.ofbiz.base.util.GeneralException;
28 import org.ofbiz.base.util.ObjectType;
29 import org.ofbiz.base.util.UtilValidate;
30 import org.ofbiz.base.util.string.FlexibleStringExpander;
31 import org.ofbiz.minilang.SimpleMethod;
32 import org.ofbiz.minilang.method.ContextAccessor;
33 import org.ofbiz.minilang.method.MethodContext;
34 import org.ofbiz.minilang.method.MethodOperation;
35 import org.w3c.dom.Element JavaDoc;
36
37 /**
38  * A general set operation to set a field from another field or from a value. Also supports a default-value, and type conversion.
39  *
40  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
41  * @version $Rev: 5842 $
42  * @since 3.5
43  */

44 public class SetOperation extends MethodOperation {
45     public static final String JavaDoc module = SetOperation.class.getName();
46     
47     protected ContextAccessor field;
48     protected ContextAccessor fromField;
49     protected FlexibleStringExpander valueExdr;
50     protected FlexibleStringExpander defaultExdr;
51     protected String JavaDoc type;
52     protected boolean setIfNull; // default to false
53
protected boolean setIfEmpty; // default to true
54

55     public SetOperation(Element JavaDoc element, SimpleMethod simpleMethod) {
56         super(element, simpleMethod);
57         this.field = new ContextAccessor(element.getAttribute("field"));
58         this.fromField = new ContextAccessor(element.getAttribute("from-field"));
59         this.valueExdr = new FlexibleStringExpander(element.getAttribute("value"));
60         this.defaultExdr = new FlexibleStringExpander(element.getAttribute("default-value"));
61         this.type = element.getAttribute("type");
62         // default to false, anything but true is false
63
this.setIfNull = "true".equals(element.getAttribute("set-if-null"));
64         // default to true, anything but false is true
65
this.setIfEmpty = !"false".equals(element.getAttribute("set-if-empty"));
66
67         if (!this.fromField.isEmpty() && !this.valueExdr.isEmpty()) {
68             throw new IllegalArgumentException JavaDoc("Cannot specify a from-field [" + element.getAttribute("from-field") + "] and a value [" + element.getAttribute("value") + "] on the set action in a screen widget");
69         }
70     }
71
72     public boolean exec(MethodContext methodContext) {
73         Object JavaDoc newValue = null;
74         if (!this.fromField.isEmpty()) {
75             newValue = this.fromField.get(methodContext);
76             if (Debug.verboseOn()) Debug.logVerbose("In screen getting value for field from [" + this.fromField.toString() + "]: " + newValue, module);
77         } else if (!this.valueExdr.isEmpty()) {
78             newValue = methodContext.expandString(this.valueExdr);
79         }
80
81         // If newValue is still empty, use the default value
82
if (ObjectType.isEmpty(newValue) && !this.defaultExdr.isEmpty()) {
83             newValue = methodContext.expandString(this.defaultExdr);
84         }
85
86         if (!setIfNull && newValue == null) {
87             if (Debug.verboseOn()) Debug.logVerbose("Field value not found (null) with name [" + fromField + "] and value [" + valueExdr + "], and there was not default value, not setting field", module);
88             return true;
89         }
90         if (!setIfEmpty && ObjectType.isEmpty(newValue)) {
91             if (Debug.verboseOn()) Debug.logVerbose("Field value not found (empty) with name [" + fromField + "] and value [" + valueExdr + "], and there was not default value, not setting field", module);
92             return true;
93         }
94
95         if (UtilValidate.isNotEmpty(this.type)) {
96             try {
97                 newValue = ObjectType.simpleTypeConvert(newValue, this.type, null, null);
98             } catch (GeneralException e) {
99                 String JavaDoc errMsg = "Could not convert field value for the field: [" + this.field.toString() + "] to the [" + this.type + "] type for the value [" + newValue + "]: " + e.toString();
100                 Debug.logError(e, errMsg, module);
101                 methodContext.setErrorReturn(errMsg, simpleMethod);
102                 return false;
103             }
104         }
105         
106         if (Debug.verboseOn()) Debug.logVerbose("In screen setting field [" + this.field.toString() + "] to value: " + newValue, module);
107         this.field.put(methodContext, newValue);
108         return true;
109     }
110
111     public String JavaDoc rawString() {
112         return "<set field=\"" + this.field
113                 + (this.valueExdr.isEmpty() ? "" : "\" value=\"" + this.valueExdr.getOriginal())
114                 + (this.fromField.isEmpty() ? "" : "\" from-field=\"" + this.fromField)
115                 + (this.defaultExdr.isEmpty() ? "" : "\" default-value=\"" + this.defaultExdr.getOriginal())
116                 + (this.type == null || this.type.length() == 0 ? "" : "\" type=\"" + this.type)
117                 + "\"/>";
118     }
119     public String JavaDoc expandedString(MethodContext methodContext) {
120         // TODO: something more than a stub/dummy
121
return this.rawString();
122     }
123 }
124
Popular Tags