Coverage Report - net.sf.beanform.integration.HibernateIntegrator
 
Classes in this File Line Coverage Branch Coverage Complexity
HibernateIntegrator
100% 
100% 
0
 
 1  
 // Copyright 2006 Daniel Gredler
 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  
 
 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  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  
 }