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: ValidationGroup.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.site;
009:
010: import java.util.ArrayList;
011: import java.util.List;
012: import java.util.logging.Logger;
013:
014: import com.uwyn.rife.site.exceptions.ValidationException;
015: import com.uwyn.rife.tools.BeanUtils;
016: import com.uwyn.rife.tools.ExceptionUtils;
017: import com.uwyn.rife.tools.exceptions.BeanUtilsException;
018:
019: public class ValidationGroup<C extends ConstrainedProperty> implements
020: Cloneable {
021: private String mName = null;
022: private ValidatedConstrained mValidation = null;
023: private ArrayList<String> mSubjects = null;
024: private ValidationGroup mParent = null;
025: private ArrayList<String> mPropertyNames = null;
026:
027: ValidationGroup(String name, Validation validation) {
028: mName = name;
029: mValidation = validation;
030: mSubjects = new ArrayList<String>();
031: }
032:
033: void setParent(ValidationGroup parent) {
034: mParent = parent;
035: }
036:
037: void setValidation(ValidatedConstrained validation) {
038: mValidation = validation;
039: }
040:
041: public void reinitializeProperties(Object bean)
042: throws ValidationException {
043: if (null == bean || null == mPropertyNames
044: || 0 == mPropertyNames.size()) {
045: return;
046: }
047:
048: Object new_bean;
049: try {
050: new_bean = bean.getClass().newInstance();
051: } catch (Throwable e) {
052: throw new ValidationException(e);
053: }
054:
055: String[] property_names = new String[mPropertyNames.size()];
056: mPropertyNames.toArray(property_names);
057: try {
058: for (String name : BeanUtils.getPropertyNames(bean
059: .getClass(), property_names, null, null)) {
060: BeanUtils.setPropertyValue(bean, name, BeanUtils
061: .getPropertyValue(new_bean, name));
062: }
063: } catch (BeanUtilsException e) {
064: throw new ValidationException(e);
065: }
066: }
067:
068: public String getName() {
069: return mName;
070: }
071:
072: public List<String> getPropertyNames() {
073: return mPropertyNames;
074: }
075:
076: public List<String> getSubjects() {
077: return mSubjects;
078: }
079:
080: public ValidatedConstrained getValidation() {
081: return mValidation;
082: }
083:
084: public ValidationGroup<C> addSubject(String subject) {
085: addPropertyName(subject);
086:
087: if (mSubjects.contains(subject)) {
088: return this ;
089: }
090:
091: mSubjects.add(subject);
092:
093: if (mParent != null) {
094: mParent.addSubject(subject);
095: }
096:
097: return this ;
098: }
099:
100: private void addPropertyName(String name) {
101: if (null == mPropertyNames) {
102: mPropertyNames = new ArrayList<String>();
103: }
104:
105: if (!mPropertyNames.contains(name)) {
106: mPropertyNames.add(name);
107: }
108: }
109:
110: public ValidationGroup<C> addRule(ValidationRule rule) {
111: mValidation.addRule(rule);
112: addSubject(rule.getSubject());
113:
114: return this ;
115: }
116:
117: public ValidationGroup<C> addConstraint(C constrainedProperty) {
118: addPropertyName(constrainedProperty.getPropertyName());
119:
120: List<PropertyValidationRule> rules = mValidation
121: .addConstrainedPropertyRules(constrainedProperty);
122: for (ValidationRule rule : rules) {
123: addSubject(rule.getSubject());
124: }
125:
126: return this ;
127: }
128:
129: public ValidationGroup<C> addGroup(String name) {
130: ValidationGroup<C> group = mValidation.addGroup(name);
131: group.setParent(this );
132: return group;
133: }
134:
135: public ValidationGroup<C> clone() {
136: ValidationGroup<C> new_validationgroup = null;
137: try {
138: new_validationgroup = (ValidationGroup<C>) super .clone();
139:
140: if (mSubjects != null) {
141: new_validationgroup.mSubjects = new ArrayList<String>(
142: mSubjects);
143: }
144: if (mPropertyNames != null) {
145: new_validationgroup.mPropertyNames = new ArrayList<String>(
146: mPropertyNames);
147: }
148: } catch (CloneNotSupportedException e) {
149: ///CLOVER:OFF
150: // this should never happen
151: Logger.getLogger("com.uwyn.rife.site").severe(
152: ExceptionUtils.getExceptionStackTrace(e));
153: ///CLOVER:ON
154: }
155:
156: return new_validationgroup;
157: }
158: }
|