View Javadoc

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  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          SortedMap<String, String> validations = new TreeMap<String, String>();
40  
41          Column c = prop.getAnnotation( Column.class );
42          if( c != null ) {
43              if( c.nullable() == false ) {
44                  validations.put( REQUIRED, REQUIRED );
45              }
46              if( prop.isString() ) {
47                  int maxLength = c.length();
48                  validations.put( MAX_LENGTH, MAX_LENGTH + "=" + maxLength );
49              }
50          }
51  
52          Basic b = prop.getAnnotation( Basic.class );
53          if( b != null ) {
54              if( b.optional() == false ) {
55                  validations.put( REQUIRED, REQUIRED );
56              }
57          }
58  
59          return validations;
60      }
61  
62      public Integer getMaxLength( BeanProperty prop ) {
63  
64          Integer maxLength = null;
65  
66          Column c = prop.getAnnotation( Column.class );
67          if( c != null ) {
68              maxLength = c.length();
69          }
70  
71          return maxLength;
72      }
73  
74      public boolean isNullable( BeanProperty prop ) {
75  
76          boolean nullable = true;
77  
78          Column c = prop.getAnnotation( Column.class );
79          if( c != null ) {
80              if( c.nullable() == false ) {
81                  nullable = false;
82              }
83          }
84  
85          Basic b = prop.getAnnotation( Basic.class );
86          if( b != null ) {
87              if( b.optional() == false ) {
88                  nullable = false;
89              }
90          }
91  
92          return nullable;
93      }
94  
95  }