01: /*
02: * Copyright 2006 Davide Deidda
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: /*
18: * PersistentValidator.java
19: *
20: * Created on 26 novembre 2003, 21.40
21: */
22:
23: package it.biobytes.ammentos.validation;
24:
25: import it.biobytes.ammentos.*;
26: import java.util.logging.*;
27:
28: /**
29: * Describes objects which are able to perform validations against persistent
30: * objects.
31: *
32: * @author davide
33: */
34: public abstract class Validator<E> {
35: private ValidationReport m_report; // Validation report
36: private Logger m_logger = Logger.getLogger("ammentos");
37:
38: /**
39: * Performs validation operations on the provided Persistent Object
40: */
41: public final ValidationReport validate(E obj) {
42: m_report = new ValidationReport();
43: performValidation(obj);
44: return m_report;
45: }
46:
47: /**
48: * Performs validation on the provided persistent object
49: */
50: public abstract void performValidation(E obj);
51:
52: protected void checkTrue(boolean expression, String errorMessage) {
53: if (!expression)
54: addReportError(errorMessage);
55: }
56:
57: protected void checkNotNull(Object obj, String errorMessage) {
58: if (obj == null)
59: addReportError(errorMessage);
60: }
61:
62: protected void addReportError(String errorMessage) {
63: m_report.addError(errorMessage);
64: }
65:
66: protected boolean isPersistent(Object obj) {
67: boolean res;
68: try {
69: res = (Ammentos.getMetadata(obj.getClass()) != null);
70: } catch (PersistenceException e) {
71: res = false;
72: }
73: return res;
74: }
75:
76: protected void checkPersistent(Object obj, String errorMessage) {
77: if (!isPersistent(obj)) {
78: addReportError(errorMessage);
79: }
80: }
81: }
|