001: /* $Id: AbstractPropertyData.java 812 2007-01-15 20:48:12Z hengels $ */
002: package org.conform;
003:
004: import java.util.*;
005:
006: /**
007: * @version $Revision: 812 $
008: */
009: public abstract class AbstractPropertyData extends AbstractData
010: implements PropertyData {
011: PropertyMeta propertyMeta;
012: AbstractBeanData beanData;
013: Object invalidValue;
014: boolean invalid = true;
015: private final Issue requiredIssue;
016:
017: protected AbstractPropertyData(AbstractBeanData beanData,
018: PropertyMeta propertyMeta) {
019: this .beanData = beanData;
020: this .propertyMeta = propertyMeta;
021: requiredIssue = new Issue(0, this .propertyMeta,
022: "validation.required");
023: }
024:
025: public PropertyMeta getPropertyMeta() {
026: return propertyMeta;
027: }
028:
029: public Meta getMeta() {
030: return propertyMeta;
031: }
032:
033: public BeanData getBeanData() {
034: return beanData;
035: }
036:
037: public void setValue(Object value) {
038: Object oldValue = getValue();
039: if (!isDifferent(oldValue, value)) {
040: return;
041: }
042:
043: try {
044: if (propertyMeta.isWritable())
045: value = validate(value);
046:
047: doSetValue(value);
048: unsetInvalidValue();
049: } catch (ValidationException e) {
050: setInvalidValue(value);
051: }
052: beanData.changeSupport.firePropertyChange(propertyMeta
053: .getName(), oldValue, value);
054: }
055:
056: private boolean isDifferent(Object oldObject, Object newObject) {
057: if (oldObject == newObject)
058: return false;
059:
060: if (oldObject == null)
061: return true;
062:
063: return !oldObject.equals(newObject);
064: }
065:
066: public Object getValue() {
067: return isInvalid() ? getInvalidValue() : doGetValue();
068: }
069:
070: public Object getInvalidValue() {
071: return invalidValue;
072: }
073:
074: public void setInvalidValue(Object invalidValue) {
075: this .invalidValue = invalidValue;
076: invalid = true;
077: }
078:
079: public void unsetInvalidValue() {
080: invalidValue = null;
081: invalid = false;
082: }
083:
084: public boolean isInvalid() {
085: return invalid;
086: }
087:
088: public void revalidate() {
089: try {
090: Object value = doGetValue();
091: doSetValue(validate(value));
092: } catch (ValidationException e) {
093: }
094: }
095:
096: public Object validate(Object value) throws ValidationException {
097: ValidationEvent addEvent = new ValidationEvent(this );
098: ValidationEvent removeEvent = new ValidationEvent(this );
099: List<ValidationException.Message> messages = new LinkedList<ValidationException.Message>();
100:
101: if (propertyMeta.isMandatory() && propertyMeta.isWritable()) {
102: if (value == null) {
103: addEvent.addIssue(requiredIssue);
104: messages.add(new ValidationException.Message(
105: "validation.required"));
106: } else {
107: removeEvent.addIssue(requiredIssue);
108: }
109: }
110:
111: Collection validators = propertyMeta.getValidators();
112: if (validators != null) {
113: Iterator iter = validators.iterator();
114: while (iter.hasNext()) {
115: Validator validator = (Validator) iter.next();
116: try {
117: value = validator.validate(value);
118: removeEvent
119: .addIssue(new Issue(System
120: .identityHashCode(validator),
121: propertyMeta));
122: } catch (ValidationException e) {
123: messages.addAll(e.getMessages());
124: for (ValidationException.Message message : e
125: .getMessages()) {
126: addEvent.addIssue(new Issue(System
127: .identityHashCode(validator),
128: propertyMeta, message.getCode(),
129: message.getArguments()));
130: }
131: }
132: }
133: }
134:
135: // fireRemoveIssue sends the issue to be removed
136: if (removeEvent.getIssues().size() != 0) {
137: fireRemoveIssue(removeEvent);
138: }
139:
140: // fireAddIssue sends an update of the issue
141: if (addEvent.getIssues().size() != 0) {
142: fireAddIssue(addEvent);
143: throw new ValidationException(messages);
144: }
145:
146: return value;
147: }
148:
149: public void initialize() {
150: Initializer initializer = propertyMeta.getInitializer();
151: if (initializer != null) {
152: Object oldValue = getValue();
153: Object value = initializer.getValue();
154: doSetValue(value);
155: unsetInvalidValue();
156: beanData.changeSupport.firePropertyChange(propertyMeta
157: .getName(), oldValue, value);
158: }
159: unsetInvalidValue();
160: }
161:
162: public void update() {
163: unsetInvalidValue();
164: doSetValue(doGetValue());
165: }
166:
167: public void clear() {
168: }
169:
170: public void flush() {
171: }
172:
173: protected abstract void doSetValue(Object value);
174:
175: protected abstract Object doGetValue();
176: }
|