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.HashMap;
024: import java.util.Iterator;
025:
026: import org.apache.log4j.Logger;
027:
028: import de.schlund.util.statuscodes.StatusCode;
029:
030: /**
031: * IWrapperIndexedParam.java
032: *
033: *
034: * Created: Mon Aug 20 18:45:30 2001
035: *
036: * @author <a href="mailto:jtl@schlund.de">Jens Lautenbacher</a>
037: *
038: *
039: */
040:
041: public class IWrapperIndexedParam implements IWrapperParamDefinition,
042: Comparable<IWrapperParamDefinition> {
043:
044: private static final String TYPE_INDEXED = "indexed";
045: private static final String TYPE_MULTIPLE = "multiple";
046: private static final String TYPE_SINGLE = "single";
047: private String name;
048: private String type;
049: private boolean trim;
050: private boolean multiple;
051: private IWrapperParamCaster caster;
052: private ArrayList<IWrapperParamPreCheck> precheck = new ArrayList<IWrapperParamPreCheck>();
053: private ArrayList<IWrapperParamPostCheck> postcheck = new ArrayList<IWrapperParamPostCheck>();
054: private HashMap<String, IWrapperParam> params = new HashMap<String, IWrapperParam>();
055: private HashMap<String, IWrapperParam> errors = new HashMap<String, IWrapperParam>();
056: // private String prefix;
057: private Logger LOG = Logger.getLogger(this .getClass());
058:
059: public IWrapperIndexedParam(String name, boolean multiple,
060: String type, boolean trim) {
061: this .type = type;
062: this .name = name;
063: this .caster = null;
064: this .multiple = multiple;
065: this .trim = trim;
066: }
067:
068: @Deprecated
069: public IWrapperIndexedParam(String name, boolean multiple,
070: String type) {
071: this (name, multiple, type, true);
072: }
073:
074: public String getOccurance() {
075: return TYPE_INDEXED;
076: }
077:
078: public String getFrequency() {
079: return multiple ? TYPE_MULTIPLE : TYPE_SINGLE;
080: }
081:
082: public IWrapperParamCaster getCaster() {
083: return caster;
084: }
085:
086: public IWrapperParamPreCheck[] getPreChecks() {
087: return (IWrapperParamPreCheck[]) precheck
088: .toArray(new IWrapperParamPreCheck[] {});
089: }
090:
091: public IWrapperParamPostCheck[] getPostChecks() {
092: return (IWrapperParamPostCheck[]) postcheck
093: .toArray(new IWrapperParamPostCheck[] {});
094: }
095:
096: protected void initValueFromRequest(String prefix, RequestData req) {
097: String wholename = prefix + "." + name;
098: for (Iterator<String> i = req.getParameterNames(); i.hasNext();) {
099: String pname = i.next();
100: if (pname.startsWith(wholename + ".")) {
101: // Now initialize the IWrapperParams
102: String idx = pname.substring(wholename.length() + 1);
103: LOG.debug("~~~ Found index: " + idx
104: + " for IndexedParam " + name);
105: IWrapperParam pinfo = new IWrapperParam(name + "."
106: + idx, multiple, true, null, type, trim);
107: pinfo.setParamCaster(caster);
108: synchronized (params) {
109: params.put(pinfo.getName(), pinfo);
110: }
111: for (Iterator<IWrapperParamPreCheck> j = precheck
112: .iterator(); j.hasNext();) {
113: pinfo.addPreChecker(j.next());
114: }
115: for (Iterator<IWrapperParamPostCheck> j = postcheck
116: .iterator(); j.hasNext();) {
117: pinfo.addPostChecker(j.next());
118: }
119: pinfo.initValueFromRequest(prefix, req);
120: if (pinfo.errorHappened()) {
121: LOG.debug("*** ERROR happened for Param: "
122: + pinfo.getName());
123: synchronized (errors) {
124: errors.put(pinfo.getName(), pinfo);
125: }
126: }
127: }
128: }
129: }
130:
131: protected void initFromStringValue() {
132: Iterator<?> it = params.values().iterator();
133: while (it.hasNext()) {
134: IWrapperParam pinfo = (IWrapperParam) it.next();
135: pinfo.initFromStringValue();
136: }
137: }
138:
139: public String getType() {
140: return type;
141: }
142:
143: public String getName() {
144: return name;
145: }
146:
147: public void setParamCaster(IWrapperParamCaster caster) {
148: this .caster = caster;
149: }
150:
151: public void addPreChecker(IWrapperParamPreCheck check) {
152: precheck.add(check);
153: }
154:
155: public void addPostChecker(IWrapperParamPostCheck check) {
156: postcheck.add(check);
157: }
158:
159: public boolean errorHappened() {
160: return !errors.isEmpty();
161: }
162:
163: public IWrapperParam[] getAllParamsWithErrors() {
164: synchronized (errors) {
165: return (IWrapperParam[]) errors.values().toArray(
166: new IWrapperParam[] {});
167: }
168: }
169:
170: public IWrapperParam[] getAllParams() {
171: synchronized (params) {
172: return (IWrapperParam[]) params.values().toArray(
173: new IWrapperParam[] {});
174: }
175: }
176:
177: public IWrapperParam getParamForIndex(String idx) {
178: String key = name + "." + idx;
179: synchronized (params) {
180: IWrapperParam pinfo = (IWrapperParam) params.get(key);
181: if (pinfo == null) {
182: pinfo = new IWrapperParam(key, multiple, true, null,
183: type, trim);
184: params.put(pinfo.getName(), pinfo);
185: }
186: return pinfo;
187: }
188: }
189:
190: public void addSCode(StatusCode scode, String[] args, String level,
191: String idx) {
192: IWrapperParam pinfo = getParamForIndex(idx);
193: pinfo.addSCode(scode, args, level);
194: synchronized (errors) {
195: errors.put(pinfo.getName(), pinfo);
196: }
197: }
198:
199: public String[] getKeys() {
200: String[] keys;
201: synchronized (params) {
202: keys = new String[params.size()];
203: int j = 0;
204: for (Iterator<String> i = params.keySet().iterator(); i
205: .hasNext();) {
206: keys[j] = i.next().substring(getName().length() + 1);
207: j++;
208: }
209: }
210: return keys;
211: }
212:
213: public int compareTo(IWrapperParamDefinition in) {
214: return name.compareTo(in.getName());
215: }
216: }// IWrapperIndexedParam
|