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: AbstractBeanHandler.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.template;
009:
010: import com.uwyn.rife.cmf.MimeType;
011: import com.uwyn.rife.site.Constrained;
012: import com.uwyn.rife.site.ConstrainedProperty;
013: import com.uwyn.rife.site.ConstrainedUtils;
014: import com.uwyn.rife.template.exceptions.BeanRemovalErrorException;
015: import com.uwyn.rife.template.exceptions.BeanSettingErrorException;
016: import com.uwyn.rife.template.exceptions.TemplateException;
017: import com.uwyn.rife.tools.ArrayUtils;
018: import com.uwyn.rife.tools.exceptions.BeanUtilsException;
019: import java.util.Map;
020:
021: public abstract class AbstractBeanHandler implements BeanHandler {
022: protected abstract MimeType getMimeType();
023:
024: protected abstract Map<String, Object> getPropertyValues(
025: Template template, Object bean, String prefix)
026: throws BeanUtilsException;
027:
028: public void setBean(Template template, Object bean, String prefix,
029: boolean encode) throws TemplateException {
030: if (null == template)
031: throw new IllegalArgumentException(
032: "template can't be null.");
033: if (null == bean) {
034: return;
035: }
036:
037: Constrained constrained = ConstrainedUtils
038: .makeConstrainedInstance(bean);
039:
040: try {
041: Map<String, Object> property_values = getPropertyValues(
042: template, bean, prefix);
043: Object property_value = null;
044: String[] property_value_strings = null;
045: String[] property_values_encoded = null;
046: TemplateEncoder encoder = template.getEncoder();
047: ConstrainedProperty constrained_property = null;
048: for (String property_name : property_values.keySet()) {
049: property_value = property_values.get(property_name);
050: property_values_encoded = null;
051:
052: if (null == property_value) {
053: property_values_encoded = new String[1];
054: property_values_encoded[0] = "";
055: } else {
056: // get the constrained property if that's appropriate
057: if (constrained != null) {
058: if (prefix != null) {
059: constrained_property = constrained
060: .getConstrainedProperty(property_name
061: .substring(prefix.length()));
062: } else {
063: constrained_property = constrained
064: .getConstrainedProperty(property_name);
065: }
066: }
067:
068: // handle multiple values
069: property_value_strings = ArrayUtils
070: .createStringArray(property_value,
071: constrained_property);
072: if (property_value_strings != null
073: && property_value_strings.length > 0) {
074: // encode the value if that's necessary
075: property_values_encoded = new String[property_value_strings.length];
076: for (int i = 0; i < property_values_encoded.length; i++) {
077: if (null == encoder
078: || !encode
079: || (constrained_property != null && constrained_property
080: .isDisplayedRaw())) {
081: // still encode if the mime type of the constrained property is different
082: // from the mime type of the bean handler
083: if (encoder != null
084: && getMimeType() != null
085: && constrained_property != null
086: && constrained_property
087: .getMimeType() != null
088: && constrained_property
089: .getMimeType() != getMimeType()) {
090: property_values_encoded[i] = encoder
091: .encode(property_value_strings[i]);
092: } else {
093: property_values_encoded[i] = property_value_strings[i];
094: }
095: } else {
096: property_values_encoded[i] = encoder
097: .encode(property_value_strings[i]);
098: }
099: }
100: }
101: }
102:
103: if (property_values_encoded != null) {
104: // plain values
105: if (template.hasValueId(property_name)) {
106: template.setValue(property_name,
107: property_values_encoded[0]);
108: }
109:
110: if (getFormBuilder() != null) {
111: // handle form values
112: getFormBuilder().selectParameter(template,
113: property_name, property_value_strings);
114: }
115: }
116: }
117: } catch (BeanUtilsException e) {
118: throw new BeanSettingErrorException(bean, e);
119: }
120: }
121:
122: public void removeBean(Template template, Object bean, String prefix)
123: throws TemplateException {
124: if (null == template)
125: throw new IllegalArgumentException(
126: "template can't be null.");
127: if (null == bean) {
128: return;
129: }
130:
131: try {
132: Constrained constrained = ConstrainedUtils
133: .makeConstrainedInstance(bean);
134: ConstrainedProperty constrained_property = null;
135:
136: Map<String, Object> property_values = getPropertyValues(
137: template, bean, prefix);
138: Object property_value = null;
139: String[] property_value_strings = null;
140: for (String property_name : property_values.keySet()) {
141: property_value = property_values.get(property_name);
142:
143: if (property_name != null) {
144: // plain values
145: if (template.hasValueId(property_name)) {
146: template.removeValue(property_name);
147: }
148: }
149:
150: if (getFormBuilder() != null) {
151: if (property_value != null) {
152: // get the constrained property if that's appropriate
153: if (constrained != null) {
154: if (prefix != null) {
155: constrained_property = constrained
156: .getConstrainedProperty(property_name
157: .substring(prefix
158: .length()));
159: } else {
160: constrained_property = constrained
161: .getConstrainedProperty(property_name);
162: }
163: }
164: // handle multiple values
165: property_value_strings = ArrayUtils
166: .createStringArray(property_value,
167: constrained_property);
168: if (property_value_strings.length > 0) {
169: // handle form values
170: getFormBuilder().unselectParameter(
171: template, property_name,
172: property_value_strings);
173: }
174: }
175: }
176: }
177: } catch (BeanUtilsException e) {
178: throw new BeanRemovalErrorException(bean, e);
179: }
180: }
181: }
|