001: /*
002: * Created on Jan 22, 2004
003: *
004: * To change the template for this generated file go to
005: * Window - Preferences - Java - Code Generation - Code and Comments
006: */
007: package org.vfny.geoserver.form.validation;
008:
009: import org.apache.struts.action.ActionError;
010: import org.apache.struts.action.ActionErrors;
011: import org.apache.struts.action.ActionForm;
012: import org.apache.struts.action.ActionMapping;
013: import org.vfny.geoserver.config.validation.ArgumentConfig;
014: import org.vfny.geoserver.config.validation.PlugInConfig;
015: import org.vfny.geoserver.config.validation.TestConfig;
016: import org.vfny.geoserver.config.validation.ValidationConfig;
017: import java.beans.PropertyDescriptor;
018: import java.util.ArrayList;
019: import java.util.HashMap;
020: import java.util.List;
021: import java.util.Map;
022: import javax.servlet.http.HttpServletRequest;
023:
024: /**
025: * ValidationTestEditorForm purpose.
026: * <p>
027: * Description of ValidationTestEditorForm ...
028: * </p>
029: *
030: * @author rgould, Refractions Research, Inc.
031: * @author $Author: emperorkefka $ (last modification)
032: * @version $Id: ValidationTestEditorForm.java 6177 2007-02-19 10:11:27Z aaime $
033: */
034: public class ValidationTestEditorForm extends ActionForm {
035: private String name;
036: private String description;
037: private String plugInName;
038: private HttpServletRequest request;
039: private List attributeKeys;
040: private List attributeHelps;
041: private List attributeValues;
042:
043: public void reset(ActionMapping arg0, HttpServletRequest request) {
044: super .reset(arg0, request);
045: this .request = request;
046:
047: TestConfig testConfig = (TestConfig) request.getSession()
048: .getAttribute(TestConfig.CURRENTLY_SELECTED_KEY);
049:
050: name = testConfig.getName();
051: description = testConfig.getDescription();
052: plugInName = testConfig.getPlugIn().getName();
053:
054: attributeKeys = new ArrayList();
055: attributeHelps = new ArrayList();
056: attributeValues = new ArrayList();
057: ArgumentConfig.loadPropertyLists(testConfig, request
058: .getLocale(), attributeKeys, attributeHelps,
059: attributeValues);
060: }
061:
062: public ActionErrors validate(ActionMapping mapping,
063: HttpServletRequest request) {
064: final TestConfig victim = (TestConfig) request.getSession()
065: .getAttribute(TestConfig.CURRENTLY_SELECTED_KEY);
066:
067: // Only used to validate values!
068: ActionErrors errors = new ActionErrors();
069:
070: for (int i = 0; i < attributeKeys.size(); i++) {
071: String key = (String) attributeKeys.get(i);
072: String text = (String) attributeValues.get(i);
073: PropertyDescriptor property = victim
074: .getPropertyDescriptor(key);
075:
076: if ((text == null) || (text.length() == 0)) {
077: if (property.isPreferred()) {
078: errors.add(key, new ActionError(
079: "validation.test.property.required", key));
080: } else {
081: // assume null is ok
082: }
083: } else {
084: try {
085: Object value = victim.createArg(key, text);
086:
087: if ((value == null) && property.isPreferred()) {
088: errors.add(key, new ActionError(
089: "validation.test.property.required",
090: key));
091: }
092: } catch (Throwable t) {
093: errors
094: .add(key, new ActionError(
095: "validation.test.property.invalid",
096: key, t));
097: }
098: }
099: }
100:
101: return errors;
102: }
103:
104: /**
105: * Translate text representation of arguments to real values.
106: * <p>
107: * Victim is required for access to BeanInfo
108: * </p>
109: */
110: public Map toArgumentMap(TestConfig victim) throws Exception {
111: Map map = new HashMap();
112:
113: for (int i = 0; i < attributeKeys.size(); i++) {
114: String key = (String) attributeKeys.get(i);
115: String text = (String) attributeValues.get(i);
116: PropertyDescriptor property = victim
117: .getPropertyDescriptor(name);
118:
119: if ((text == null) || (text.length() == 0)) {
120: if (property.isPreferred()) {
121: throw new IllegalArgumentException(
122: "Required non empty value for " + key);
123: }
124: }
125:
126: Object value = victim.createArg(key, text);
127:
128: if (value == null) {
129: if (property.isPreferred()) {
130: throw new IllegalArgumentException(
131: "Required non empty value for " + key);
132: }
133: } else {
134: map.put(key, value);
135: }
136: }
137:
138: return map;
139: }
140:
141: /**
142: * List of attribtue keys as text.
143: * <p>
144: * These keys are really the propertyName associated with a BeanInfo
145: * </p>
146: */
147: public List getAttributeKeys() {
148: return attributeKeys;
149: }
150:
151: /**
152: * List of attribtue vales as text.
153: * <p>
154: * To convert this value to a real java object you will need to use
155: * a BeanInfo Property descriptor.
156: * </p>
157: */
158: public List getAttributeValues() {
159: return attributeValues;
160: }
161:
162: /** Help text gernated from PropertyDescriptor.getShortDescription() */
163: public String[] getAttributeHelps() {
164: return (String[]) attributeHelps
165: .toArray(new String[attributeHelps.size()]);
166: }
167:
168: public String getAttributeKey(int index) {
169: return (String) attributeKeys.get(index);
170: }
171:
172: public void setAttributeValues(List list) {
173: attributeValues = list;
174: }
175:
176: public void setAttributeKeys(List list) {
177: attributeKeys = list;
178: }
179:
180: public void setAttributeHelps(List list) {
181: attributeHelps = list;
182: }
183:
184: /**
185: * Access description property.
186: *
187: * @return Returns the description.
188: */
189: public String getDescription() {
190: return description;
191: }
192:
193: /**
194: * Set description to description.
195: *
196: * @param description The description to set.
197: */
198: public void setDescription(String description) {
199: this .description = description;
200: }
201:
202: /**
203: * Access name property.
204: *
205: * @return Returns the name.
206: */
207: public String getName() {
208: return name;
209: }
210:
211: /**
212: * Set name to name.
213: *
214: * @param name The name to set.
215: */
216: public void setName(String name) {
217: this .name = name;
218: }
219:
220: /**
221: * Access plugInName property.
222: *
223: * @return Returns the plugInName.
224: */
225: public String getPlugInName() {
226: return plugInName;
227: }
228:
229: public String getPlugInDescription() {
230: ValidationConfig validationConfig = (ValidationConfig) this
231: .getServlet().getServletContext().getAttribute(
232: ValidationConfig.CONFIG_KEY);
233: PlugInConfig config = validationConfig.getPlugIn(plugInName);
234:
235: return config.getDescription();
236: }
237: }
|