| 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 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
16 |
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 |
186 |
SortedMap<String, String> validations = new TreeMap<String, String>(); |
| 59 |
|
|
| 60 |
186 |
Length length = prop.getAnnotation( Length.class ); |
| 61 |
186 |
if( length != null ) { |
| 62 |
22 |
int minLength = length.min(); |
| 63 |
22 |
if( minLength > LENGTH_MIN_DEFAULT ) { |
| 64 |
16 |
validations.put( MIN_LENGTH, MIN_LENGTH + "=" + minLength ); |
| 65 |
|
} |
| 66 |
22 |
int maxLength = length.max(); |
| 67 |
22 |
if( maxLength < LENGTH_MAX_DEFAULT ) { |
| 68 |
21 |
validations.put( MAX_LENGTH, MAX_LENGTH + "=" + maxLength ); |
| 69 |
|
} |
| 70 |
|
} |
| 71 |
|
|
| 72 |
186 |
Min min = prop.getAnnotation( Min.class ); |
| 73 |
186 |
if( min != null ) { |
| 74 |
1 |
int minValue = min.value(); |
| 75 |
1 |
validations.put( MIN, MIN + "=" + minValue ); |
| 76 |
|
} |
| 77 |
|
|
| 78 |
186 |
Max max = prop.getAnnotation( Max.class ); |
| 79 |
186 |
if( max != null ) { |
| 80 |
1 |
int maxValue = max.value(); |
| 81 |
1 |
validations.put( MAX, MAX + "=" + maxValue ); |
| 82 |
|
} |
| 83 |
|
|
| 84 |
186 |
NotNull notNull = prop.getAnnotation( NotNull.class ); |
| 85 |
186 |
if( notNull != null ) { |
| 86 |
1 |
validations.put( REQUIRED, REQUIRED ); |
| 87 |
|
} |
| 88 |
|
|
| 89 |
186 |
Past past = prop.getAnnotation( Past.class ); |
| 90 |
186 |
if( past != null ) { |
| 91 |
1 |
SimpleDateFormat sdf = new SimpleDateFormat( DATE_FORMAT ); |
| 92 |
1 |
String date = sdf.format( new Date() ); |
| 93 |
1 |
validations.put( MAX_DATE, MAX_DATE + "=" + date ); |
| 94 |
|
} |
| 95 |
|
|
| 96 |
186 |
Future future = prop.getAnnotation( Future.class ); |
| 97 |
186 |
if( future != null ) { |
| 98 |
1 |
SimpleDateFormat sdf = new SimpleDateFormat( DATE_FORMAT ); |
| 99 |
1 |
String date = sdf.format( new Date() ); |
| 100 |
1 |
validations.put( MIN_DATE, MIN_DATE + "=" + date ); |
| 101 |
|
} |
| 102 |
|
|
| 103 |
186 |
Pattern pattern = prop.getAnnotation( Pattern.class ); |
| 104 |
186 |
if( pattern != null ) { |
| 105 |
1 |
String regex = pattern.regex(); |
| 106 |
1 |
validations.put( PATTERN, PATTERN + "=" + regex ); |
| 107 |
|
} |
| 108 |
|
|
| 109 |
186 |
Range range = prop.getAnnotation( Range.class ); |
| 110 |
186 |
if( range != null ) { |
| 111 |
1 |
long minValue = range.min(); |
| 112 |
1 |
long maxValue = range.max(); |
| 113 |
1 |
validations.put( MIN, MIN + "=" + minValue ); |
| 114 |
1 |
validations.put( MAX, MAX + "=" + maxValue ); |
| 115 |
|
} |
| 116 |
|
|
| 117 |
186 |
Email email = prop.getAnnotation( Email.class ); |
| 118 |
186 |
if( email != null ) { |
| 119 |
1 |
validations.put( EMAIL, EMAIL ); |
| 120 |
|
} |
| 121 |
|
|
| 122 |
186 |
return validations; |
| 123 |
|
} |
| 124 |
|
|
| 125 |
|
public Integer getMaxLength( BeanProperty prop ) { |
| 126 |
|
|
| 127 |
75 |
Integer maxLength = null; |
| 128 |
|
|
| 129 |
75 |
Length length = prop.getAnnotation( Length.class ); |
| 130 |
75 |
if( length != null ) { |
| 131 |
33 |
maxLength = length.max(); |
| 132 |
|
} |
| 133 |
|
|
| 134 |
75 |
return maxLength; |
| 135 |
|
} |
| 136 |
|
|
| 137 |
|
public boolean isNullable( BeanProperty prop ) { |
| 138 |
|
|
| 139 |
11 |
boolean nullable = true; |
| 140 |
|
|
| 141 |
11 |
NotNull notNull = prop.getAnnotation( NotNull.class ); |
| 142 |
11 |
if( notNull != null ) { |
| 143 |
1 |
nullable = false; |
| 144 |
|
} |
| 145 |
|
|
| 146 |
11 |
return nullable; |
| 147 |
|
} |
| 148 |
|
|
| 149 |
|
} |