001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: FormBuilderXml.java 3697 2007-03-16 16:59:32Z gbevin $
007: */
008: package com.uwyn.rife.site;
009:
010: import com.uwyn.rife.template.Template;
011: import com.uwyn.rife.tools.ExceptionUtils;
012: import com.uwyn.rife.tools.exceptions.BeanUtilsException;
013: import java.util.ArrayList;
014: import java.util.Collection;
015: import java.util.Collections;
016: import java.util.Map;
017: import java.util.Set;
018: import java.util.logging.Logger;
019:
020: public class FormBuilderXml implements FormBuilder {
021: private ValidationBuilder mValidationBuilder = new ValidationBuilderXml();
022:
023: public Collection<String> generateForm(Template template,
024: Class beanClass, Map<String, String[]> values, String prefix)
025: throws BeanUtilsException {
026: if (null == beanClass) {
027: return Collections.EMPTY_LIST;
028: }
029:
030: // create an instance of the bean
031: Object bean;
032: try {
033: bean = beanClass.newInstance();
034: } catch (Throwable e) {
035: bean = null;
036: }
037:
038: return _generateForm(template, bean, values, prefix);
039: }
040:
041: public Collection<String> generateForm(Template template,
042: Object bean, Map<String, String[]> values, String prefix)
043: throws BeanUtilsException {
044: if (null == bean) {
045: return Collections.EMPTY_LIST;
046: }
047:
048: if (bean instanceof Class)
049: throw new IllegalArgumentException(
050: "bean should be a bean instance, not a bean class.");
051:
052: return _generateForm(template, bean, values, prefix);
053: }
054:
055: private Collection<String> _generateForm(Template template,
056: Object bean, Map<String, String[]> values, String prefix) {
057: ArrayList<String> set_values = new ArrayList<String>();
058:
059: if (null == template) {
060: return set_values;
061: }
062:
063: if (null == values && bean != null) {
064: // check if the bean is validated
065: Validated validated = null;
066: Set<ValidationError> previous_errors = null;
067: if (bean instanceof Validated) {
068: validated = (Validated) bean;
069:
070: // check if validation errors are already present in the bean,
071: // and generate the formatted errors
072: set_values.addAll(mValidationBuilder
073: .generateValidationErrors(template, validated
074: .getValidationErrors(), validated
075: .getValidatedSubjects(), prefix));
076: set_values.addAll(mValidationBuilder
077: .generateErrorMarkings(template, validated
078: .getValidationErrors(), validated
079: .getValidatedSubjects(), prefix));
080:
081: // store the validation errors and revalidate
082: previous_errors = validated.getValidationErrors();
083:
084: // revalidate the bean to check which fields have to be displayed
085: validated.resetValidation();
086: validated.validate();
087: }
088:
089: if (validated != null) {
090: // restore the previous validation errors
091: validated.replaceValidationErrors(previous_errors);
092: }
093: }
094:
095: return set_values;
096: }
097:
098: public Collection<String> generateField(Template template,
099: ConstrainedProperty property, String[] values, String prefix) {
100: return Collections.EMPTY_LIST;
101: }
102:
103: public Collection<String> generateField(Template template,
104: Class propertyType, ConstrainedProperty property,
105: String[] values, String prefix) {
106: return Collections.EMPTY_LIST;
107: }
108:
109: public Collection<String> replaceField(Template template,
110: String templateFieldName, ConstrainedProperty property,
111: String[] values, String prefix) {
112: return Collections.EMPTY_LIST;
113: }
114:
115: public Collection<String> replaceField(Template template,
116: String templateFieldName, Class propertyType,
117: ConstrainedProperty property, String[] values, String prefix) {
118: return Collections.EMPTY_LIST;
119: }
120:
121: public Collection<String> generateField(Template template,
122: String name, String[] values, String prefix) {
123: return Collections.EMPTY_LIST;
124: }
125:
126: public Collection<String> generateField(Template template,
127: Class propertyType, String name, String[] values,
128: String prefix) {
129: return Collections.EMPTY_LIST;
130: }
131:
132: public Collection<String> replaceField(Template template,
133: String templateFieldName, String name, String[] values,
134: String prefix) {
135: return Collections.EMPTY_LIST;
136: }
137:
138: public Collection<String> replaceField(Template template,
139: String templateFieldName, Class propertyType, String name,
140: String[] values, String prefix) {
141: return Collections.EMPTY_LIST;
142: }
143:
144: public void removeForm(Template template, Class beanClass,
145: String prefix) throws BeanUtilsException {
146: if (null == template || null == beanClass) {
147: return;
148: }
149:
150: // create an instance of the bean
151: Object bean;
152: try {
153: bean = beanClass.newInstance();
154: } catch (Throwable e) {
155: bean = null;
156: }
157:
158: Validated validated;
159: if (bean != null && bean instanceof Validated) {
160: validated = (Validated) bean;
161:
162: mValidationBuilder.removeValidationErrors(template,
163: validated.getValidatedSubjects(), prefix);
164: mValidationBuilder.removeErrorMarkings(template, validated
165: .getValidatedSubjects(), prefix);
166: }
167: }
168:
169: public void removeField(Template template, String name,
170: String prefix) {
171: }
172:
173: public void removeField(Template template, String templateFieldName) {
174: }
175:
176: public Collection<String> selectParameter(Template template,
177: String name, String[] values) {
178: return Collections.EMPTY_LIST;
179: }
180:
181: public void unselectParameter(Template template, String name,
182: String[] values) {
183: }
184:
185: public ValidationBuilder getValidationBuilder() {
186: return mValidationBuilder;
187: }
188:
189: public Object clone() {
190: try {
191: FormBuilderXml other = (FormBuilderXml) super .clone();
192:
193: other.mValidationBuilder = (ValidationBuilder) this .mValidationBuilder
194: .clone();
195:
196: return other;
197: } catch (CloneNotSupportedException e) {
198: ///CLOVER:OFF
199: // this should never happen
200: Logger.getLogger("com.uwyn.rife.site").severe(
201: ExceptionUtils.getExceptionStackTrace(e));
202: return null;
203: ///CLOVER:ON
204: }
205: }
206: }
|