|
| 1 | +// Copyright 2010 The Apache Software Foundation |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +package org.apache.tapestry5.internal.beanvalidator; |
| 15 | + |
| 16 | +import static java.lang.String.format; |
| 17 | + |
| 18 | +import java.lang.annotation.Annotation; |
| 19 | +import java.util.Iterator; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +import javax.validation.ConstraintViolation; |
| 23 | +import javax.validation.MessageInterpolator; |
| 24 | +import javax.validation.Validator; |
| 25 | +import javax.validation.ValidatorFactory; |
| 26 | +import javax.validation.MessageInterpolator.Context; |
| 27 | +import javax.validation.metadata.BeanDescriptor; |
| 28 | +import javax.validation.metadata.ConstraintDescriptor; |
| 29 | +import javax.validation.metadata.PropertyDescriptor; |
| 30 | + |
| 31 | +import org.apache.tapestry5.Field; |
| 32 | +import org.apache.tapestry5.FieldValidator; |
| 33 | +import org.apache.tapestry5.MarkupWriter; |
| 34 | +import org.apache.tapestry5.ValidationException; |
| 35 | +import org.apache.tapestry5.beanvalidator.BeanValidatorGroupSource; |
| 36 | +import org.apache.tapestry5.beanvalidator.ClientConstraintDescriptor; |
| 37 | +import org.apache.tapestry5.beanvalidator.ClientConstraintDescriptorSource; |
| 38 | +import org.apache.tapestry5.json.JSONObject; |
| 39 | +import org.apache.tapestry5.services.BeanValidationContext; |
| 40 | +import org.apache.tapestry5.services.Environment; |
| 41 | +import org.apache.tapestry5.services.FormSupport; |
| 42 | + |
| 43 | + |
| 44 | +public class BeanFieldValidator implements FieldValidator |
| 45 | +{ |
| 46 | + private final Field field; |
| 47 | + private final String propertyName; |
| 48 | + private final ValidatorFactory validatorFactory; |
| 49 | + private final BeanValidatorGroupSource beanValidationGroupSource; |
| 50 | + private final ClientConstraintDescriptorSource clientValidatorSource; |
| 51 | + private final FormSupport formSupport; |
| 52 | + private final Environment environment; |
| 53 | + |
| 54 | + public BeanFieldValidator(Field field, String propertyName, |
| 55 | + ValidatorFactory validatorFactory, |
| 56 | + BeanValidatorGroupSource beanValidationGroupSource, |
| 57 | + ClientConstraintDescriptorSource clientValidatorSource, |
| 58 | + FormSupport formSupport, |
| 59 | + Environment environment) |
| 60 | + { |
| 61 | + this.field = field; |
| 62 | + this.propertyName = propertyName; |
| 63 | + this.validatorFactory = validatorFactory; |
| 64 | + this.beanValidationGroupSource = beanValidationGroupSource; |
| 65 | + this.clientValidatorSource = clientValidatorSource; |
| 66 | + this.formSupport = formSupport; |
| 67 | + this.environment = environment; |
| 68 | + } |
| 69 | + |
| 70 | + public boolean isRequired() |
| 71 | + { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + public void render(final MarkupWriter writer) |
| 76 | + { |
| 77 | + final BeanValidationContext beanValidationContext = environment.peek(BeanValidationContext.class); |
| 78 | + |
| 79 | + if (beanValidationContext == null) |
| 80 | + { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + final Validator validator = validatorFactory.getValidator(); |
| 85 | + |
| 86 | + BeanDescriptor beanDescriptor = validator.getConstraintsForClass(beanValidationContext.getBeanType()); |
| 87 | + |
| 88 | + PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty(propertyName); |
| 89 | + |
| 90 | + for (final ConstraintDescriptor<?> descriptor :propertyDescriptor.getConstraintDescriptors()) |
| 91 | + { |
| 92 | + Class<? extends Annotation> annotationType = descriptor.getAnnotation().annotationType(); |
| 93 | + |
| 94 | + ClientConstraintDescriptor clientConstraintDescriptor = clientValidatorSource.getConstraintDescriptor(annotationType); |
| 95 | + |
| 96 | + if(clientConstraintDescriptor != null) |
| 97 | + { |
| 98 | + String message = interpolateMessage(descriptor); |
| 99 | + |
| 100 | + JSONObject specs = new JSONObject(); |
| 101 | + |
| 102 | + for (String attribute : clientConstraintDescriptor.getAttributes()) |
| 103 | + { |
| 104 | + Object object = descriptor.getAttributes().get(attribute); |
| 105 | + |
| 106 | + if (object == null) |
| 107 | + { |
| 108 | + throw new RuntimeException("Expected attribute is null"); |
| 109 | + } |
| 110 | + specs.put(attribute, object); |
| 111 | + } |
| 112 | + |
| 113 | + formSupport.addValidation(field, clientConstraintDescriptor.getValidatorName(), message, specs); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @SuppressWarnings("unchecked") |
| 119 | + public void validate(final Object value) throws ValidationException |
| 120 | + { |
| 121 | + |
| 122 | + final BeanValidationContext beanValidationContext = environment.peek(BeanValidationContext.class); |
| 123 | + |
| 124 | + if (beanValidationContext == null) |
| 125 | + { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + final Validator validator = validatorFactory.getValidator(); |
| 130 | + |
| 131 | + final Set<ConstraintViolation<Object>> violations = validator.validateValue( |
| 132 | + (Class<Object>) beanValidationContext.getBeanType(), propertyName, |
| 133 | + value, beanValidationGroupSource.get()); |
| 134 | + |
| 135 | + if (violations.isEmpty()) |
| 136 | + { |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + final StringBuilder builder = new StringBuilder(); |
| 141 | + |
| 142 | + for (Iterator iterator = violations.iterator(); iterator.hasNext();) |
| 143 | + { |
| 144 | + ConstraintViolation<?> violation = (ConstraintViolation<Object>) iterator.next(); |
| 145 | + |
| 146 | + builder.append(format("%s %s", field.getLabel(), violation.getMessage())); |
| 147 | + |
| 148 | + if(iterator.hasNext()) |
| 149 | + builder.append(", "); |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + throw new ValidationException(builder.toString()); |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | + private String interpolateMessage(final ConstraintDescriptor<?> descriptor) |
| 158 | + { |
| 159 | + String messageTemplate = (String) descriptor.getAttributes().get("message"); |
| 160 | + |
| 161 | + MessageInterpolator messageInterpolator = validatorFactory.getMessageInterpolator(); |
| 162 | + |
| 163 | + return messageInterpolator.interpolate(messageTemplate, new Context() |
| 164 | + { |
| 165 | + |
| 166 | + public ConstraintDescriptor<?> getConstraintDescriptor() |
| 167 | + { |
| 168 | + return descriptor; |
| 169 | + } |
| 170 | + |
| 171 | + public Object getValidatedValue() |
| 172 | + { |
| 173 | + return null; |
| 174 | + } |
| 175 | + }); |
| 176 | + } |
| 177 | +} |
0 commit comments