1 /* 2 * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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 package de.smartics.properties.api.core.domain; 17 18 import java.util.List; 19 import java.util.Locale; 20 21 import de.smartics.exceptions.i18n.message.MessageParam; 22 import de.smartics.properties.api.core.app.PropertiesCode; 23 24 /** 25 * Provides context information for errors concerning a property. 26 */ 27 public class PropertyValidationMessageBean extends PropertyValueMessageBean 28 { 29 // ********************************* Fields ********************************* 30 31 // --- constants ------------------------------------------------------------ 32 33 /** 34 * The class version identifier. 35 */ 36 private static final long serialVersionUID = 1L; 37 38 // --- members -------------------------------------------------------------- 39 40 /** 41 * The property constraints that failed to be met. 42 * 43 * @serial 44 */ 45 @MessageParam 46 private final List<? extends PropertyConstraint<?>> constraints; 47 48 // ****************************** Initializer ******************************* 49 50 // ****************************** Constructors ****************************** 51 52 /** 53 * Convenience constructor with no message and no root cause. 54 * 55 * @param propertyDescriptor the descriptor of the property raising the 56 * exception. 57 * @param constraints the property constraints that failed to be met. 58 * @param value the value of the property that does not meet the constraints. 59 * If the value is not serializable, its string representation is 60 * stored. 61 */ 62 public PropertyValidationMessageBean( 63 final PropertyDescriptor propertyDescriptor, 64 final List<? extends PropertyConstraint<?>> constraints, 65 final Object value) 66 { 67 this(null, propertyDescriptor, constraints, value); 68 } 69 70 /** 71 * Convenience constructor with cause. 72 * 73 * @param cause the cause (which is saved for later retrieval by the 74 * {@link #getCause()} method). (A <tt>null</tt> value is permitted, 75 * and indicates that the cause is nonexistent or unknown.) 76 * @param propertyDescriptor the descriptor of the property raising the 77 * exception. 78 * @param constraints the property constraints that failed to be met. 79 * @param value the value of the property that does not meet the constraints. 80 * If the value is not serializable, its string representation is 81 * stored. 82 */ 83 public PropertyValidationMessageBean(final Throwable cause, 84 final PropertyDescriptor propertyDescriptor, 85 final List<? extends PropertyConstraint<?>> constraints, 86 final Object value) 87 { 88 this(PropertyCode.INVALID_VALUE, cause, propertyDescriptor, constraints, 89 value); 90 } 91 92 /** 93 * Default constructor. 94 * 95 * @param code the error or exception code of the exception. 96 * @param cause the cause (which is saved for later retrieval by the 97 * {@link #getCause()} method). (A <tt>null</tt> value is permitted, 98 * and indicates that the cause is nonexistent or unknown.) 99 * @param propertyDescriptor the descriptor of the property raising the 100 * exception. 101 * @param constraints the property constraints that failed to be met. 102 * @param value the value of the property that does not meet the constraints. 103 * If the value is not serializable, its string representation is 104 * stored. 105 */ 106 public PropertyValidationMessageBean(final PropertiesCode code, 107 final Throwable cause, final PropertyDescriptor propertyDescriptor, 108 final List<? extends PropertyConstraint<?>> constraints, 109 final Object value) 110 { 111 super(code, cause, propertyDescriptor, value); 112 113 this.constraints = constraints; 114 } 115 116 // ****************************** Inner Classes ***************************** 117 118 // ********************************* Methods ******************************** 119 120 // --- init ----------------------------------------------------------------- 121 122 private String createMessage(final Locale locale) 123 { 124 final StringBuilder buffer = new StringBuilder(); 125 126 if (constraints != null && !constraints.isEmpty()) 127 { 128 for (final PropertyConstraint<?> constraint : constraints) 129 { 130 buffer.append("\n > ").append( 131 constraint.getViolationMessage(locale, getValue())); 132 } 133 } 134 135 return buffer.toString(); 136 } 137 138 // --- get&set -------------------------------------------------------------- 139 140 /** 141 * Returns the property constraints that failed to be met. 142 * 143 * @return the property constraints that failed to be met. 144 */ 145 public final List<? extends PropertyConstraint<?>> getConstraints() 146 { 147 return constraints; 148 } 149 150 /** 151 * Returns the validation message provided by the validator. 152 * 153 * @param locale the locale to use for message construction. 154 * @return the validation message provided by the validator. 155 */ 156 @MessageParam("validationMessage") 157 public final String getValidationMessage(final Locale locale) 158 { 159 return createMessage(locale); 160 } 161 162 // --- business ------------------------------------------------------------- 163 164 // --- object basics -------------------------------------------------------- 165 166 }