001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.generator;
021:
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.HashSet;
025: import java.util.Iterator;
026:
027: import org.apache.log4j.Logger;
028:
029: import de.schlund.pfixxml.RequestParam;
030: import de.schlund.pfixxml.RequestParamType;
031: import de.schlund.pfixxml.SimpleRequestParam;
032: import de.schlund.util.statuscodes.StatusCode;
033: import de.schlund.util.statuscodes.StatusCodeLib;
034:
035: /**
036: * Describe class <code>IWrapperParam</code> here.
037: *
038: * @author <a href="mailto:jtl@schlund.de">Jens Lautenbacher</a>
039: *
040: *
041: */
042:
043: public class IWrapperParam implements IWrapperParamCheck,
044: IWrapperParamDefinition, Comparable<IWrapperParamDefinition> {
045:
046: private static final String TYPE_OPTIONAL = "optional";
047: private static final String TYPE_MANDATORY = "mandatory";
048: private static final String TYPE_MULTIPLE = "multiple";
049: private static final String TYPE_SINGLE = "single";
050: private String name;
051: private String type;
052: private boolean trim;
053: private boolean optional;
054: private boolean multiple;
055: private String[] stringval = null;
056: private Object[] value = null;
057: private RequestParam[] defaultval = null;
058: private IWrapperParamCaster caster;
059: private ArrayList<IWrapperParamPreCheck> precheck = new ArrayList<IWrapperParamPreCheck>();
060: private ArrayList<IWrapperParamPostCheck> postcheck = new ArrayList<IWrapperParamPostCheck>();
061: private HashSet<StatusCodeInfo> scodeinfos = new HashSet<StatusCodeInfo>();
062: private Logger LOG = Logger.getLogger(this .getClass());
063: private StatusCodeInfo missing = new StatusCodeInfo(
064: StatusCodeLib.PFIXCORE_GENERATOR_MISSING_PARAM, null, null);
065: private boolean inrequest = false;
066:
067: public IWrapperParam(String name, boolean multiple,
068: boolean optional, RequestParam[] defaultval, String type,
069: boolean trim) {
070: this .type = type;
071: this .name = name;
072: this .optional = optional;
073: this .multiple = multiple;
074: this .caster = null;
075: this .defaultval = defaultval;
076: this .trim = trim;
077: }
078:
079: @Deprecated
080: public IWrapperParam(String name, boolean multiple,
081: boolean optional, RequestParam[] defaultval, String type) {
082: this (name, multiple, optional, defaultval, type, true);
083: }
084:
085: public void setValue(Object[] value) {
086: this .value = value;
087: }
088:
089: public String getOccurance() {
090: return optional ? TYPE_OPTIONAL : TYPE_MANDATORY;
091: }
092:
093: public String getFrequency() {
094: return multiple ? TYPE_MULTIPLE : TYPE_SINGLE;
095: }
096:
097: public IWrapperParamCaster getCaster() {
098: return caster;
099: }
100:
101: public IWrapperParamPreCheck[] getPreChecks() {
102: return (IWrapperParamPreCheck[]) precheck
103: .toArray(new IWrapperParamPreCheck[] {});
104: }
105:
106: public IWrapperParamPostCheck[] getPostChecks() {
107: return (IWrapperParamPostCheck[]) postcheck
108: .toArray(new IWrapperParamPostCheck[] {});
109: }
110:
111: public void setCustomSCode(String scode) {
112: missing = new StatusCodeInfo(StatusCodeLib
113: .getStatusCodeByName(scode), null, null);
114: }
115:
116: public void setParamCaster(IWrapperParamCaster caster) {
117: this .caster = caster;
118: }
119:
120: public void addPreChecker(IWrapperParamPreCheck check) {
121: precheck.add(check);
122: }
123:
124: public void addPostChecker(IWrapperParamPostCheck check) {
125: postcheck.add(check);
126: }
127:
128: public boolean suppliedInRequest() {
129: return inrequest;
130: }
131:
132: public boolean errorHappened() {
133: return !scodeinfos.isEmpty();
134: }
135:
136: public StatusCodeInfo[] getStatusCodeInfos() {
137: return (StatusCodeInfo[]) scodeinfos
138: .toArray(new StatusCodeInfo[] {});
139: }
140:
141: public void addSCode(StatusCode scode, String[] args, String level) {
142: StatusCodeInfo scinfo = new StatusCodeInfo(scode, args, level);
143: scodeinfos.add(scinfo);
144: }
145:
146: public String getType() {
147: return type;
148: }
149:
150: public String getName() {
151: return name;
152: }
153:
154: public Object getValue() {
155: if (value != null && value.length > 0) {
156: return value[0];
157: } else {
158: return null;
159: }
160: }
161:
162: public Object[] getValueArr() {
163: return value;
164: }
165:
166: public String[] getStringValue() {
167: return stringval;
168: }
169:
170: public void setStringValue(Object[] values) {
171: if (values != null) {
172: stringval = new String[values.length];
173: for (int i = 0; i < values.length; i++) {
174: if (values[i] == null) {
175: stringval[i] = null;
176: } else if (values[i] instanceof RequestParam) {
177: stringval[i] = ((RequestParam) values[i])
178: .getValue();
179: } else {
180: stringval[i] = values[i].toString();
181: }
182: }
183: } else {
184: stringval = null;
185: }
186: }
187:
188: public void initValueFromRequest(String prefix, RequestData reqdata) {
189: String thename = prefix + "." + name;
190: RequestParam[] rparamv = reqdata.getParameters(thename);
191:
192: LOG.debug(">>> [" + thename + "] Optional: " + optional);
193:
194: if (rparamv != null) {
195: inrequest = true;
196: ArrayList<RequestParam> in = new ArrayList<RequestParam>(
197: Arrays.asList(rparamv));
198: ArrayList<RequestParam> out = new ArrayList<RequestParam>();
199: for (Iterator<RequestParam> i = in.iterator(); i.hasNext();) {
200: RequestParam val = i.next();
201: LOG.debug(">>> [" + thename + "] Input: >" + val + "<");
202: if (val.getType().equals(RequestParamType.SIMPLE)
203: || val.getType().equals(
204: RequestParamType.FIELDDATA)) {
205: String tmp = val.getValue().trim();
206: if (!tmp.equals("")) {
207: if (trim)
208: val.setValue(tmp);
209: out.add(val);
210: }
211: } else if (val.getType().equals(
212: RequestParamType.FILEDATA)) {
213: String fname = val.getValue();
214: if (fname != null)
215: out.add(val);
216: } else {
217: LOG.error("RequestParam " + thename
218: + " is of unknown type: " + val.getType());
219: }
220: }
221: if (out.size() > 0) {
222: rparamv = (RequestParam[]) out
223: .toArray(new RequestParam[] {});
224: } else {
225: LOG
226: .debug(">>> ["
227: + thename
228: + "] Outlist empty, setting InputArray to null!!!");
229: rparamv = null;
230: }
231: } else {
232: inrequest = false;
233: LOG.debug(">>> [" + thename + "] InputArray is null!!!");
234: }
235:
236: setStringValue(rparamv);
237:
238: checkValue(rparamv);
239:
240: }
241:
242: private void checkValue(RequestParam[] rparamv) {
243:
244: // Default values are _not_ echoed back to the UI, so we set them only AFTER
245: // we set the normalized string values.
246: if (rparamv == null && defaultval != null) {
247: LOG.debug(">>> Param '" + name
248: + "' is empty, but using supplied default value.");
249: rparamv = defaultval;
250: }
251:
252: if (rparamv == null && !optional) {
253: synchronized (scodeinfos) {
254: scodeinfos.add(missing);
255: }
256: } else if (rparamv != null) {
257: for (Iterator<IWrapperParamPreCheck> i = precheck
258: .iterator(); i.hasNext();) {
259: IWrapperParamPreCheck pre = i.next();
260: pre.check(rparamv);
261: if (pre.errorHappened()) {
262: synchronized (scodeinfos) {
263: scodeinfos.addAll(Arrays.asList(pre
264: .getStatusCodeInfos()));
265: }
266: }
267: }
268: if (!errorHappened()) {
269: // Now we try to cast the RequestParam according to it's classname
270: if (caster != null) {
271: caster.castValue(rparamv);
272: }
273: if (caster != null && caster.errorHappened()) {
274: synchronized (scodeinfos) {
275: scodeinfos.addAll(Arrays.asList(caster
276: .getStatusCodeInfos()));
277: }
278: } else {
279: Object[] tmp;
280: if (caster != null) {
281: tmp = caster.getValue();
282: } else {
283: // Build up an Array with the String values of the rparamv
284: tmp = new String[rparamv.length];
285: for (int i = 0; i < rparamv.length; i++) {
286: tmp[i] = rparamv[i].getValue();
287: }
288: }
289: for (Iterator<IWrapperParamPostCheck> i = postcheck
290: .iterator(); i.hasNext();) {
291: IWrapperParamPostCheck post = i.next();
292: post.check(tmp);
293: if (post.errorHappened()) {
294: synchronized (scodeinfos) {
295: scodeinfos.addAll(Arrays.asList(post
296: .getStatusCodeInfos()));
297: }
298: }
299: }
300: if (!errorHappened()) {
301: setValue(tmp);
302: }
303: }
304: }
305: } else {
306: setValue(null);
307: }
308: }
309:
310: protected void initFromStringValue() {
311: RequestParam[] rparamv = null;
312: if (stringval != null) {
313: rparamv = new RequestParam[stringval.length];
314: for (int i = 0; i < stringval.length; i++) {
315: RequestParam rp = new SimpleRequestParam(stringval[i]);
316: rparamv[i] = rp;
317: }
318: }
319: checkValue(rparamv);
320: }
321:
322: public int compareTo(IWrapperParamDefinition in) {
323: return name.compareTo(in.getName());
324: }
325: }
|