001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.persistency.rule.impl;
010:
011: import com.completex.objective.components.persistency.PersistentEntry;
012: import com.completex.objective.components.persistency.PersistentObject;
013: import com.completex.objective.components.persistency.Record;
014: import com.completex.objective.components.persistency.Rule;
015: import com.completex.objective.components.persistency.rule.FieldValidator;
016: import com.completex.objective.components.persistency.rule.RuleException;
017: import com.completex.objective.components.persistency.rule.RecordValidator;
018:
019: import java.util.ArrayList;
020:
021: /**
022: * @author Gennady Krizhevsky
023: */
024: public class RecordValidatorImpl implements Rule, FieldValidator,
025: RecordValidator {
026: private Record record;
027: protected static final String NL = "\r\n";
028:
029: protected boolean verbose;
030:
031: private int size;
032:
033: private final FieldValidator[][] fieldValidators;
034:
035: public RecordValidatorImpl(PersistentObject persistentObject,
036: boolean verbose) {
037: if (persistentObject == null) {
038: throw new NullPointerException(
039: "Validated persistentObject is null");
040: }
041: int size = persistentObject.record().size();
042: // assert size >= 0;
043: this .record = persistentObject.record();
044: this .size = size;
045: this .verbose = verbose;
046: fieldValidators = new FieldValidator[size][];
047: }
048:
049: protected Record getRecord() {
050: return record;
051: }
052:
053: public FieldValidator[][] getFieldValidators() {
054: return fieldValidators;
055: }
056:
057: public RecordValidator addFieldValidator(int fieldIndex,
058: FieldValidator fieldValidator) {
059: assertIndex(fieldIndex);
060: //
061: // See if validators exists, if not - create,
062: // if yes - add to FieldValidator[]
063: FieldValidator[] old = fieldValidators[fieldIndex];
064: if (old == null) {
065: old = new FieldValidator[0];
066: }
067:
068: FieldValidator[] collection = new FieldValidator[old.length + 1];
069: System.arraycopy(old, 0, collection, 0, old.length);
070: collection[old.length] = fieldValidator;
071: fieldValidators[fieldIndex] = collection;
072: return this ;
073: }
074:
075: private void assertIndex(int fieldIndex) {
076: if (fieldIndex >= size || fieldIndex < 0) {
077: throw new IllegalArgumentException(
078: "fieldIndex >= size || fieldIndex < 0 : "
079: + fieldIndex);
080: }
081: }
082:
083: public void setFieldValidators(int fieldIndex,
084: FieldValidator[] validators) {
085: // assert convertors != null;
086: fieldValidators[fieldIndex] = validators;
087: }
088:
089: protected String validate(Record record) throws Exception {
090: if (record == null) {
091: return "";
092: }
093: boolean objectInitialized = record.isInitialized();
094: String resultMessage = "";
095: ArrayList elementExceptions = null;
096:
097: for (int k = 0; k < size; k++) {
098: FieldValidator[] validators = fieldValidators[k];
099: if (validators != null) {
100: boolean fieldDirty = record.isFieldDirty(k);
101: if (!objectInitialized
102: || (objectInitialized && fieldDirty)) {
103: for (int i = 0; i < validators.length; i++) {
104: if (validators[i] != null) {
105: try {
106: String message = validators[i]
107: .validate(record.getEntry(k),
108: verbose);
109: if (message.length() > 0) {
110: resultMessage += message + NL;
111: }
112: } catch (RuntimeException e) {
113: throw e;
114: } catch (Exception e) {
115: if (elementExceptions == null) {
116: elementExceptions = new ArrayList();
117: }
118: if (e instanceof RuleException) {
119: elementExceptions.add(e);
120: } else {
121: elementExceptions
122: .add(new RuleException(e, e
123: .getMessage())
124: .setFieldName(record
125: .getColumn(
126: k)
127: .getColumnName()));
128: }
129: }
130: }
131: }
132: }
133: }
134: }
135:
136: if (elementExceptions != null && elementExceptions.size() > 0) {
137: throw new RuleException(
138: (RuleException[]) elementExceptions
139: .toArray(new RuleException[elementExceptions
140: .size()]));
141: }
142:
143: if (resultMessage.length() > 0 && verbose) {
144: throw new RuleException(resultMessage);
145: }
146: return resultMessage;
147: }
148:
149: public void validateField(PersistentEntry entry) throws Exception {
150: if (entry == null) {
151: return;
152: }
153: String resultMessage = "";
154:
155: int k = entry.getColumn().getColumnIndex();
156: FieldValidator[] validators = fieldValidators[k];
157: if (validators != null) {
158: boolean fieldDirty = entry.isDirty();
159: if (fieldDirty) {
160: for (int i = 0; i < validators.length; i++) {
161: if (validators[i] != null) {
162:
163: String message = validators[i].validate(entry,
164: verbose);
165: if (message.length() > 0) {
166: resultMessage += message + NL;
167: // Go to the next iteration to ensure policy : for
168: // the same field stop validation when the 1st error encountered:
169: break;
170: }
171: }
172: }
173: }
174: }
175:
176: if (resultMessage.length() > 0) {
177: throw new RuleException(resultMessage);
178: }
179: }
180:
181: public Object executeRule(PersistentObject subject)
182: throws Exception {
183: return validate(subject.record());
184: }
185:
186: public String validate(PersistentEntry entry) throws Exception {
187: validateField(entry);
188: return null;
189: }
190:
191: public String validate(PersistentEntry entry, boolean verbose)
192: throws Exception {
193: validateField(entry);
194: return null;
195: }
196:
197: public void executeFieldRule(PersistentEntry entry)
198: throws Exception {
199: validateField(entry);
200: }
201:
202: public Object clone() throws CloneNotSupportedException {
203: return super.clone();
204: }
205: }
|