Coverage Report - net.sf.beanform.integration.Ejb3Integrator
 
Classes in this File Line Coverage Branch Coverage Complexity
Ejb3Integrator
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.util.SortedMap;
 18  
 import java.util.TreeMap;
 19  
 
 20  
 import javax.persistence.Basic;
 21  
 import javax.persistence.Column;
 22  
 
 23  
 import net.sf.beanform.prop.BeanProperty;
 24  
 
 25  
 /**
 26  
  * Provides integration with EJB3 annotations.
 27  
  *
 28  
  * @see http://java.sun.com/javaee/5/docs/api/javax/persistence/package-summary.html
 29  
  * @author Daniel Gredler
 30  
  */
 31  13
 public class Ejb3Integrator implements Integrator {
 32  
 
 33  
     public static final boolean COLUMN_NULLABLE_DEFAULT = true;
 34  
     public static final int     COLUMN_LENGTH_DEFAULT = 255;
 35  
     public static final boolean BASIC_OPTIONAL_DEFAULT = true;
 36  
 
 37  
     public SortedMap<String, String> getValidation( BeanProperty prop ) {
 38  
 
 39  180
         SortedMap<String, String> validations = new TreeMap<String, String>();
 40  
 
 41  180
         Column c = prop.getAnnotation( Column.class );
 42  180
         if( c != null ) {
 43  6
             if( c.nullable() == false ) {
 44  2
                 validations.put( REQUIRED, REQUIRED );
 45  
             }
 46  6
             if( prop.isString() ) {
 47  6
                 int maxLength = c.length();
 48  6
                 validations.put( MAX_LENGTH, MAX_LENGTH + "=" + maxLength );
 49  
             }
 50  
         }
 51  
 
 52  180
         Basic b = prop.getAnnotation( Basic.class );
 53  180
         if( b != null ) {
 54  2
             if( b.optional() == false ) {
 55  1
                 validations.put( REQUIRED, REQUIRED );
 56  
             }
 57  
         }
 58  
 
 59  180
         return validations;
 60  
     }
 61  
 
 62  
     public Integer getMaxLength( BeanProperty prop ) {
 63  
 
 64  75
         Integer maxLength = null;
 65  
 
 66  75
         Column c = prop.getAnnotation( Column.class );
 67  75
         if( c != null ) {
 68  17
             maxLength = c.length();
 69  
         }
 70  
 
 71  75
         return maxLength;
 72  
     }
 73  
 
 74  
     public boolean isNullable( BeanProperty prop ) {
 75  
 
 76  14
         boolean nullable = true;
 77  
 
 78  14
         Column c = prop.getAnnotation( Column.class );
 79  14
         if( c != null ) {
 80  3
             if( c.nullable() == false ) {
 81  1
                 nullable = false;
 82  
             }
 83  
         }
 84  
 
 85  14
         Basic b = prop.getAnnotation( Basic.class );
 86  14
         if( b != null ) {
 87  2
             if( b.optional() == false ) {
 88  1
                 nullable = false;
 89  
             }
 90  
         }
 91  
 
 92  14
         return nullable;
 93  
     }
 94  
 
 95  
 }