KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > pseudotag > InputValue


1 /*
2  * $Id: InputValue.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2003 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.webapp.pseudotag;
25
26 import java.io.IOException JavaDoc;
27 import java.util.Map JavaDoc;
28 import javax.servlet.jsp.PageContext JavaDoc;
29
30 import org.ofbiz.base.util.UtilFormatOut;
31 import org.ofbiz.entity.GenericValue;
32
33 /**
34  * InputValue Pseudo-Tag
35  * Outputs a string for an input box from either an entity field or
36  * a request parameter. Decides which to use by checking to see if the entityattr exist and
37  * using the specified field if it does. If the Boolean object referred to by the tryentityattr
38  * attribute is false, always tries to use the request parameter and ignores the entity 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 InputValue {
45
46     PageContext JavaDoc pageContextInternal = null;
47
48     public InputValue(PageContext JavaDoc pageContextInternal) {
49         this.pageContextInternal = pageContextInternal;
50     }
51
52     public void run(String JavaDoc field, String JavaDoc entityAttr)
53         throws IOException JavaDoc {
54         run(field, null, entityAttr, null, null, null, pageContextInternal);
55     }
56
57     public void run(String JavaDoc field, String JavaDoc entityAttr, String JavaDoc tryEntityAttr)
58         throws IOException JavaDoc {
59         run(field, null, entityAttr, tryEntityAttr, null, null, pageContextInternal);
60     }
61
62     public void run(String JavaDoc field, String JavaDoc entityAttr, String JavaDoc tryEntityAttr,
63         String JavaDoc fullattrsStr) throws IOException JavaDoc {
64         run(field, null, entityAttr, tryEntityAttr, null, fullattrsStr, pageContextInternal);
65     }
66
67     /** Run the InputValue Pseudo-Tag, all fields except field, and entityAttr can be null */
68     public void run(String JavaDoc field, String JavaDoc param, String JavaDoc entityAttr, String JavaDoc tryEntityAttr,
69         String JavaDoc defaultStr, String JavaDoc fullattrsStr) throws IOException JavaDoc {
70         run(field, param, entityAttr, tryEntityAttr, defaultStr, fullattrsStr, pageContextInternal);
71     }
72
73     /* --- STATIC METHODS --- */
74
75     public static void run(String JavaDoc field, String JavaDoc entityAttr,
76         PageContext JavaDoc pageContext) throws IOException JavaDoc {
77         run(field, null, entityAttr, null, null, null, pageContext);
78     }
79
80     public static void run(String JavaDoc field, String JavaDoc entityAttr, String JavaDoc tryEntityAttr,
81         PageContext JavaDoc pageContext) throws IOException JavaDoc {
82         run(field, null, entityAttr, tryEntityAttr, null, null, pageContext);
83     }
84
85     public static void run(String JavaDoc field, String JavaDoc entityAttr, String JavaDoc tryEntityAttr,
86         String JavaDoc fullattrsStr, PageContext JavaDoc pageContext) throws IOException JavaDoc {
87         run(field, null, entityAttr, tryEntityAttr, null, fullattrsStr, pageContext);
88     }
89
90     /** Run the InputValue Pseudo-Tag, all fields except field, entityAttr, and pageContext can be null */
91     public static void run(String JavaDoc field, String JavaDoc param, String JavaDoc entityAttr, String JavaDoc tryEntityAttr,
92         String JavaDoc defaultStr, String JavaDoc fullattrsStr, PageContext JavaDoc pageContext) throws IOException JavaDoc {
93         if (field == null || entityAttr == null || pageContext == null) {
94             throw new RuntimeException JavaDoc("Required parameter (field or entityAttr or pageContext) missing");
95         }
96
97         if (defaultStr == null) defaultStr = "";
98         String JavaDoc inputValue = null;
99         boolean tryEntity = true;
100         boolean fullattrs = false;
101
102         String JavaDoc paramName = param;
103
104         if (paramName == null || paramName.length() == 0)
105             paramName = field;
106
107         Boolean JavaDoc tempBool = null;
108
109         if (tryEntityAttr != null)
110             tempBool = (Boolean JavaDoc) pageContext.findAttribute(tryEntityAttr);
111         if (tempBool != null)
112             tryEntity = tempBool.booleanValue();
113
114         // if anything but true, it will be false, ie default is false
115
fullattrs = "true".equals(fullattrsStr);
116
117         if (tryEntity) {
118             Object JavaDoc entTemp = pageContext.findAttribute(entityAttr);
119
120             if (entTemp != null) {
121                 if (entTemp instanceof GenericValue) {
122                     GenericValue entity = (GenericValue) entTemp;
123                     Object JavaDoc fieldVal = entity.get(field);
124
125                     if (fieldVal != null)
126                         inputValue = fieldVal.toString();
127                 } else if (entTemp instanceof Map JavaDoc) {
128                     Map JavaDoc map = (Map JavaDoc) entTemp;
129                     Object JavaDoc fieldVal = map.get(field);
130
131                     if (fieldVal != null)
132                         inputValue = fieldVal.toString();
133                 } // else do nothing
134
}
135         } else {
136             // Current code will only get a parameter if we are not trying to get
137
// fields from the entity/map
138
// OLD WAY:
139
// if nothing found in entity, or if not checked, try a parameter
140
// if (inputValue == null) {
141
inputValue = pageContext.getRequest().getParameter(paramName);
142         }
143
144         if (inputValue == null || inputValue.length() == 0)
145             inputValue = defaultStr;
146
147         if (fullattrs) {
148             inputValue = UtilFormatOut.replaceString(inputValue, "\"", "&quot;");
149             pageContext.getOut().print("name=\"" + paramName + "\" value=\"" +
150                 inputValue + "\"");
151         } else {
152             pageContext.getOut().print(inputValue);
153         }
154     }
155 }
156
Popular Tags