KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: WebappPropertyToField.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.net.*;
27 import java.util.*;
28 import javax.servlet.*;
29
30 import org.w3c.dom.*;
31 import org.ofbiz.base.util.*;
32 import org.ofbiz.minilang.*;
33 import org.ofbiz.minilang.method.*;
34
35 /**
36  * Copies a property value from a properties file in a ServletContext resource to a field
37  *
38  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
39  * @version $Rev: 5462 $
40  * @since 2.0
41  */

42 public class WebappPropertyToField extends MethodOperation {
43     
44     public static final String JavaDoc module = WebappPropertyToField.class.getName();
45     
46     String JavaDoc resource;
47     String JavaDoc property;
48     String JavaDoc defaultVal;
49     ContextAccessor mapAcsr;
50     ContextAccessor fieldAcsr;
51
52     public WebappPropertyToField(Element element, SimpleMethod simpleMethod) {
53         super(element, simpleMethod);
54         resource = element.getAttribute("resource");
55         property = element.getAttribute("property");
56         defaultVal = element.getAttribute("default");
57         mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
58         fieldAcsr = new ContextAccessor(element.getAttribute("field-name"));
59     }
60
61     public boolean exec(MethodContext methodContext) {
62         String JavaDoc resource = methodContext.expandString(this.resource);
63         String JavaDoc property = methodContext.expandString(this.property);
64         String JavaDoc defaultVal = methodContext.expandString(this.defaultVal);
65                 
66         String JavaDoc fieldVal = null;
67
68         // only run this if it is in an EVENT context
69
if (methodContext.getMethodType() == MethodContext.EVENT) {
70             ServletContext servletContext = (ServletContext) methodContext.getRequest().getAttribute("servletContext");
71             URL propsUrl = null;
72
73             try {
74                 propsUrl = servletContext.getResource(resource);
75             } catch (java.net.MalformedURLException JavaDoc e) {
76                 Debug.logWarning(e, "Error finding webapp resource (properties file) not found with name " + resource, module);
77             }
78
79             if (propsUrl == null) {
80                 Debug.logWarning("Webapp resource (properties file) not found with name " + resource, module);
81             } else {
82                 fieldVal = UtilProperties.getPropertyValue(propsUrl, property);
83                 if (fieldVal == null || fieldVal.length() == 0) {
84                     Debug.logWarning("Webapp resource property value not found with name " + property + " in resource " + resource, module);
85                 }
86             }
87         }
88
89         // if fieldVal is null, or is a String and has zero length, use defaultVal
90
if (fieldVal == null) {
91             fieldVal = defaultVal;
92         } else if (fieldVal instanceof String JavaDoc) {
93             String JavaDoc strVal = (String JavaDoc) fieldVal;
94
95             if (strVal.length() == 0) {
96                 fieldVal = defaultVal;
97             }
98         }
99
100         if (!mapAcsr.isEmpty()) {
101             Map fromMap = (Map) mapAcsr.get(methodContext);
102
103             if (fromMap == null) {
104                 Debug.logWarning("Map not found with name " + mapAcsr + " creating a new map", module);
105                 fromMap = new HashMap();
106                 mapAcsr.put(methodContext, fromMap);
107             }
108
109             fieldAcsr.put(fromMap, fieldVal, methodContext);
110         } else {
111             fieldAcsr.put(methodContext, fieldVal);
112         }
113         return true;
114     }
115
116     public String JavaDoc rawString() {
117         // TODO: add all attributes and other info
118
return "<webapp-property-to-field field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>";
119     }
120     public String JavaDoc expandedString(MethodContext methodContext) {
121         // TODO: something more than a stub/dummy
122
return this.rawString();
123     }
124 }
125
Popular Tags