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.webapp.pseudotag;
019:
020: import java.io.IOException;
021: import java.util.Map;
022: import javax.servlet.jsp.PageContext;
023:
024: import org.ofbiz.base.util.UtilFormatOut;
025: import org.ofbiz.entity.GenericValue;
026:
027: /**
028: * InputValue Pseudo-Tag
029: * Outputs a string for an input box from either an entity field or
030: * a request parameter. Decides which to use by checking to see if the entityattr exist and
031: * using the specified field if it does. If the Boolean object referred to by the tryentityattr
032: * attribute is false, always tries to use the request parameter and ignores the entity field.
033: */
034: public class InputValue {
035:
036: PageContext pageContextInternal = null;
037:
038: public InputValue(PageContext pageContextInternal) {
039: this .pageContextInternal = pageContextInternal;
040: }
041:
042: public void run(String field, String entityAttr) throws IOException {
043: run(field, null, entityAttr, null, null, null,
044: pageContextInternal);
045: }
046:
047: public void run(String field, String entityAttr,
048: String tryEntityAttr) throws IOException {
049: run(field, null, entityAttr, tryEntityAttr, null, null,
050: pageContextInternal);
051: }
052:
053: public void run(String field, String entityAttr,
054: String tryEntityAttr, String fullattrsStr)
055: throws IOException {
056: run(field, null, entityAttr, tryEntityAttr, null, fullattrsStr,
057: pageContextInternal);
058: }
059:
060: /** Run the InputValue Pseudo-Tag, all fields except field, and entityAttr can be null */
061: public void run(String field, String param, String entityAttr,
062: String tryEntityAttr, String defaultStr, String fullattrsStr)
063: throws IOException {
064: run(field, param, entityAttr, tryEntityAttr, defaultStr,
065: fullattrsStr, pageContextInternal);
066: }
067:
068: /* --- STATIC METHODS --- */
069:
070: public static void run(String field, String entityAttr,
071: PageContext pageContext) throws IOException {
072: run(field, null, entityAttr, null, null, null, pageContext);
073: }
074:
075: public static void run(String field, String entityAttr,
076: String tryEntityAttr, PageContext pageContext)
077: throws IOException {
078: run(field, null, entityAttr, tryEntityAttr, null, null,
079: pageContext);
080: }
081:
082: public static void run(String field, String entityAttr,
083: String tryEntityAttr, String fullattrsStr,
084: PageContext pageContext) throws IOException {
085: run(field, null, entityAttr, tryEntityAttr, null, fullattrsStr,
086: pageContext);
087: }
088:
089: /** Run the InputValue Pseudo-Tag, all fields except field, entityAttr, and pageContext can be null */
090: public static void run(String field, String param,
091: String entityAttr, String tryEntityAttr, String defaultStr,
092: String fullattrsStr, PageContext pageContext)
093: throws IOException {
094: if (field == null || entityAttr == null || pageContext == null) {
095: throw new RuntimeException(
096: "Required parameter (field or entityAttr or pageContext) missing");
097: }
098:
099: if (defaultStr == null)
100: defaultStr = "";
101: String inputValue = null;
102: boolean tryEntity = true;
103: boolean fullattrs = false;
104:
105: String paramName = param;
106:
107: if (paramName == null || paramName.length() == 0)
108: paramName = field;
109:
110: Boolean tempBool = null;
111:
112: if (tryEntityAttr != null)
113: tempBool = (Boolean) pageContext
114: .findAttribute(tryEntityAttr);
115: if (tempBool != null)
116: tryEntity = tempBool.booleanValue();
117:
118: // if anything but true, it will be false, ie default is false
119: fullattrs = "true".equals(fullattrsStr);
120:
121: if (tryEntity) {
122: Object entTemp = pageContext.findAttribute(entityAttr);
123:
124: if (entTemp != null) {
125: if (entTemp instanceof GenericValue) {
126: GenericValue entity = (GenericValue) entTemp;
127: Object fieldVal = entity.get(field);
128:
129: if (fieldVal != null)
130: inputValue = fieldVal.toString();
131: } else if (entTemp instanceof Map) {
132: Map map = (Map) entTemp;
133: Object fieldVal = map.get(field);
134:
135: if (fieldVal != null)
136: inputValue = fieldVal.toString();
137: } // else do nothing
138: }
139: } else {
140: // Current code will only get a parameter if we are not trying to get
141: // fields from the entity/map
142: // OLD WAY:
143: // if nothing found in entity, or if not checked, try a parameter
144: // if (inputValue == null) {
145: inputValue = pageContext.getRequest().getParameter(
146: paramName);
147: }
148:
149: if (inputValue == null || inputValue.length() == 0)
150: inputValue = defaultStr;
151:
152: if (fullattrs) {
153: inputValue = UtilFormatOut.replaceString(inputValue, "\"",
154: """);
155: pageContext.getOut().print(
156: "name=\"" + paramName + "\" value=\"" + inputValue
157: + "\"");
158: } else {
159: pageContext.getOut().print(inputValue);
160: }
161: }
162: }
|