KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > eventops > RequestToField


1 /*
2  * $Id: RequestToField.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 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.eventops;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.ofbiz.base.util.Debug;
30 import org.ofbiz.base.util.collections.FlexibleServletAccessor;
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  * Copies a Servlet request attribute to a map field
39  *
40  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
41  * @version $Rev: 5462 $
42  * @since 2.0
43  */

44 public class RequestToField extends MethodOperation {
45     
46     public static final String JavaDoc module = RequestToField.class.getName();
47     
48     ContextAccessor mapAcsr;
49     ContextAccessor fieldAcsr;
50     FlexibleServletAccessor requestAcsr;
51     String JavaDoc defaultVal;
52
53     public RequestToField(Element JavaDoc element, SimpleMethod simpleMethod) {
54         super(element, simpleMethod);
55         mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
56         fieldAcsr = new ContextAccessor(element.getAttribute("field-name"));
57         requestAcsr = new FlexibleServletAccessor(element.getAttribute("request-name"), element.getAttribute("field-name"));
58         defaultVal = element.getAttribute("default");
59     }
60
61     public boolean exec(MethodContext methodContext) {
62         String JavaDoc defaultVal = methodContext.expandString(this.defaultVal);
63
64         Object JavaDoc fieldVal = null;
65         // only run this if it is in an EVENT context
66
if (methodContext.getMethodType() == MethodContext.EVENT) {
67             fieldVal = requestAcsr.get(methodContext.getRequest(), methodContext.getEnvMap());
68             if (fieldVal == null) {
69                 Debug.logWarning("Request attribute value not found with name " + requestAcsr, module);
70             }
71         }
72
73         // if fieldVal is null, or is a String and has zero length, use defaultVal
74
if (fieldVal == null) {
75             fieldVal = defaultVal;
76         } else if (fieldVal instanceof String JavaDoc) {
77             String JavaDoc strVal = (String JavaDoc) fieldVal;
78
79             if (strVal.length() == 0) {
80                 fieldVal = defaultVal;
81             }
82         }
83
84         if (!mapAcsr.isEmpty()) {
85             Map JavaDoc fromMap = (Map JavaDoc) mapAcsr.get(methodContext);
86
87             if (fromMap == null) {
88                 Debug.logWarning("Map not found with name " + mapAcsr + " creating a new map", module);
89                 fromMap = new HashMap JavaDoc();
90                 mapAcsr.put(methodContext, fromMap);
91             }
92
93             fieldAcsr.put(fromMap, fieldVal, methodContext);
94         } else {
95             fieldAcsr.put(methodContext, fieldVal);
96         }
97         return true;
98     }
99
100     public String JavaDoc rawString() {
101         // TODO: add all attributes and other info
102
return "<request-to-field request-name=\"" + this.requestAcsr + "\" field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>";
103     }
104     public String JavaDoc expandedString(MethodContext methodContext) {
105         // TODO: something more than a stub/dummy
106
return this.rawString();
107     }
108 }
109
Popular Tags