1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package net.sf.beanform.integration;
16
17 import java.text.SimpleDateFormat;
18 import java.util.Date;
19 import java.util.SortedMap;
20 import java.util.TreeMap;
21
22 import net.sf.beanform.prop.BeanProperty;
23
24 import org.hibernate.validator.Email;
25 import org.hibernate.validator.Future;
26 import org.hibernate.validator.Length;
27 import org.hibernate.validator.Max;
28 import org.hibernate.validator.Min;
29 import org.hibernate.validator.NotNull;
30 import org.hibernate.validator.Past;
31 import org.hibernate.validator.Pattern;
32 import org.hibernate.validator.Range;
33
34 /***
35 * Provides integration with Hibernate Validator annotations.
36 *
37 * Hibernate Validator annotations that are ignored:
38 * <ul>
39 * <li>Size</li>
40 * <li>AssertFalse</li>
41 * <li>AssertTrue</li>
42 * <li>Valid</li>
43 * </ul>
44 *
45 * @see http://www.hibernate.org/hib_docs/annotations/reference/en/html/validator.html
46 * @see http://docs.jboss.org/ejb3/app-server/HibernateAnnotations/api/org/hibernate/validator/package-summary.html
47 * @author Daniel Gredler
48 */
49 public class HibernateIntegrator implements Integrator {
50
51 public static final int LENGTH_MAX_DEFAULT = Integer.MAX_VALUE;
52 public static final int LENGTH_MIN_DEFAULT = 0;
53
54 private static final String DATE_FORMAT = "MM/dd/yyyy";
55
56 public SortedMap<String, String> getValidation( BeanProperty prop ) {
57
58 SortedMap<String, String> validations = new TreeMap<String, String>();
59
60 Length length = prop.getAnnotation( Length.class );
61 if( length != null ) {
62 int minLength = length.min();
63 if( minLength > LENGTH_MIN_DEFAULT ) {
64 validations.put( MIN_LENGTH, MIN_LENGTH + "=" + minLength );
65 }
66 int maxLength = length.max();
67 if( maxLength < LENGTH_MAX_DEFAULT ) {
68 validations.put( MAX_LENGTH, MAX_LENGTH + "=" + maxLength );
69 }
70 }
71
72 Min min = prop.getAnnotation( Min.class );
73 if( min != null ) {
74 int minValue = min.value();
75 validations.put( MIN, MIN + "=" + minValue );
76 }
77
78 Max max = prop.getAnnotation( Max.class );
79 if( max != null ) {
80 int maxValue = max.value();
81 validations.put( MAX, MAX + "=" + maxValue );
82 }
83
84 NotNull notNull = prop.getAnnotation( NotNull.class );
85 if( notNull != null ) {
86 validations.put( REQUIRED, REQUIRED );
87 }
88
89 Past past = prop.getAnnotation( Past.class );
90 if( past != null ) {
91 SimpleDateFormat sdf = new SimpleDateFormat( DATE_FORMAT );
92 String date = sdf.format( new Date() );
93 validations.put( MAX_DATE, MAX_DATE + "=" + date );
94 }
95
96 Future future = prop.getAnnotation( Future.class );
97 if( future != null ) {
98 SimpleDateFormat sdf = new SimpleDateFormat( DATE_FORMAT );
99 String date = sdf.format( new Date() );
100 validations.put( MIN_DATE, MIN_DATE + "=" + date );
101 }
102
103 Pattern pattern = prop.getAnnotation( Pattern.class );
104 if( pattern != null ) {
105 String regex = pattern.regex();
106 validations.put( PATTERN, PATTERN + "=" + regex );
107 }
108
109 Range range = prop.getAnnotation( Range.class );
110 if( range != null ) {
111 long minValue = range.min();
112 long maxValue = range.max();
113 validations.put( MIN, MIN + "=" + minValue );
114 validations.put( MAX, MAX + "=" + maxValue );
115 }
116
117 Email email = prop.getAnnotation( Email.class );
118 if( email != null ) {
119 validations.put( EMAIL, EMAIL );
120 }
121
122 return validations;
123 }
124
125 public Integer getMaxLength( BeanProperty prop ) {
126
127 Integer maxLength = null;
128
129 Length length = prop.getAnnotation( Length.class );
130 if( length != null ) {
131 maxLength = length.max();
132 }
133
134 return maxLength;
135 }
136
137 public boolean isNullable( BeanProperty prop ) {
138
139 boolean nullable = true;
140
141 NotNull notNull = prop.getAnnotation( NotNull.class );
142 if( notNull != null ) {
143 nullable = false;
144 }
145
146 return nullable;
147 }
148
149 }