001: package org.osbl;
002:
003: import org.conform.*;
004: import org.conform.format.DefaultFormatFactory;
005: import org.conform.modifier.AnnotationModifier;
006:
007: import java.util.*;
008:
009: public class ValidationHelper {
010: private Set<Issue> issues = new HashSet<Issue>();
011:
012: private ValidationListener forwarder = new ValidationListener() {
013: public void addIssues(ValidationEvent event) {
014: issues.addAll(event.getIssues());
015: }
016:
017: public void removeIssues(ValidationEvent event) {
018: }
019:
020: public void clearIssues(Meta meta) {
021: }
022: };
023:
024: private BeanMetaProvider getBeanMetaProvider() {
025: return (BeanMetaProvider) ServiceProvider.getInstance()
026: .getService("StaticBeanMetaProvider");
027: }
028:
029: public void staticValidation(Object object) {
030: if (object == null)
031: throw new NullPointerException("Object must not be null!");
032:
033: BeanMeta beanMeta = getBeanMetaProvider().getBeanMeta(
034: object.getClass());
035: DefaultBeanData beanData = new DefaultBeanData(beanMeta);
036: beanData.setValue(object);
037:
038: beanData.addValidationListener(forwarder);
039: beanData.setIssuePublishingActive(true);
040: beanData.revalidateAll();
041: }
042:
043: public void validation(Object object, Validator validator,
044: String... affectedPropertyNames) {
045: if (object == null)
046: throw new NullPointerException("Object must not be null!");
047: if (affectedPropertyNames == null
048: || affectedPropertyNames.length == 0)
049: throw new NullPointerException(
050: "Affected property names are required!");
051:
052: try {
053: validator.validate(object);
054: } catch (ValidationException e) {
055: PropertyMeta[] affectedPropertyMetas = propertyMetas(object
056: .getClass(), affectedPropertyNames);
057:
058: for (ValidationException.Message message : e.getMessages()) {
059: issues.add(new Issue(
060: System.identityHashCode(validator),
061: affectedPropertyMetas, message.getCode(),
062: message.getArguments()));
063: }
064: }
065: }
066:
067: public void validation(Object object, BeanValidator validator) {
068: if (object == null)
069: throw new NullPointerException("Object must not be null!");
070:
071: try {
072: validator.validate(object);
073: } catch (ValidationException e) {
074: PropertyMeta[] affectedPropertyMetas = propertyMetas(object
075: .getClass(), validator.getAffectedPropertyNames());
076:
077: for (ValidationException.Message message : e.getMessages()) {
078: issues.add(new Issue(
079: System.identityHashCode(validator),
080: affectedPropertyMetas, message.getCode(),
081: message.getArguments()));
082: }
083: }
084: }
085:
086: public void addIssue(Class type, String[] propertyNames,
087: String code, Object... arguments) {
088: issues.add(new Issue(-issues.size(), propertyMetas(type,
089: propertyNames), code, arguments));
090: }
091:
092: public void addIssue(Class type, String propertyName, String code,
093: Object... arguments) {
094: issues.add(new Issue(-issues.size(), propertyMeta(type,
095: propertyName), code, arguments));
096: }
097:
098: public void throwIfIssues() throws ValidationIssueException {
099: if (issues.size() > 0)
100: throw new ValidationIssueException(issues);
101: }
102:
103: private PropertyMeta[] propertyMetas(Class type,
104: String[] affectedPropertyNames) {
105: BeanMeta beanMeta = getBeanMetaProvider().getBeanMeta(type);
106: PropertyMeta[] affectedPropertyMetas = new PropertyMeta[affectedPropertyNames.length];
107: for (int i = 0; i < affectedPropertyNames.length; i++) {
108: String affectedPropertyName = affectedPropertyNames[i];
109: affectedPropertyMetas[i] = beanMeta
110: .getProperty(affectedPropertyName);
111: }
112: return affectedPropertyMetas;
113: }
114:
115: private PropertyMeta propertyMeta(Class type,
116: String affectedPropertyName) {
117: BeanMeta beanMeta = getBeanMetaProvider().getBeanMeta(type);
118: return beanMeta.getProperty(affectedPropertyName);
119: }
120:
121: public static void main(String[] args) {
122: new DefaultFormatFactory();
123: ServiceProvider.INSTANCE = new ServiceProvider() {
124: public List<String> getServiceNames() {
125: return null;
126: }
127:
128: public Object getService(String name) {
129: VariationBeanMetaProvider metaProvider = new VariationBeanMetaProvider(
130: new Beans());
131: metaProvider.addModifier(new AnnotationModifier(
132: metaProvider));
133: return metaProvider;
134: }
135:
136: public <T> T getService(Class<T> type) {
137: return null;
138: }
139:
140: public void autowireServices(Object bean) {
141: }
142: };
143: Address address = new Address();
144:
145: ValidationHelper validationHelper = new ValidationHelper();
146: validationHelper.staticValidation(address);
147:
148: // callback validator
149: validationHelper.validation(address, new Validator() {
150: public Object validate(Object value)
151: throws ValidationException {
152: throw new ValidationException(
153: new ValidationException.Message("blub",
154: new Object[] { value }));
155: }
156: }, "city", "postcode");
157:
158: // callback bean validator
159: validationHelper.validation(address, new BeanValidator() {
160: public Object validate(Object value)
161: throws ValidationException {
162: throw new ValidationException(
163: new ValidationException.Message("bla",
164: new Object[] { value }));
165: }
166:
167: public String[] getAffectedPropertyNames() {
168: return new String[] { "street" };
169: }
170: });
171:
172: // inline
173: if (true)
174: validationHelper.addIssue(Address.class, new String[] {
175: "street", "postcode", "city" }, "fasel", 7);
176:
177: System.out.println("Issues: " + validationHelper.issues);
178: }
179:
180: public static class Address {
181: Long id;
182:
183: String street;
184: String postcode;
185: String city;
186:
187: public Long getId() {
188: return id;
189: }
190:
191: public void setId(Long id) {
192: this .id = id;
193: }
194:
195: public String getStreet() {
196: return street;
197: }
198:
199: public void setStreet(String street) {
200: this .street = street;
201: }
202:
203: public String getPostcode() {
204: return postcode;
205: }
206:
207: public void setPostcode(String postcode) {
208: this .postcode = postcode;
209: }
210:
211: public String getCity() {
212: return city;
213: }
214:
215: public void setCity(String city) {
216: this.city = city;
217: }
218: }
219: }
|