001 // Copyright 2006-2007 Daniel Gredler
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package net.sf.beanform.integration;
016
017 import java.util.SortedMap;
018 import java.util.TreeMap;
019
020 import javax.persistence.Basic;
021 import javax.persistence.Column;
022
023 import net.sf.beanform.prop.BeanProperty;
024
025 /**
026 * Provides integration with EJB3 annotations.
027 *
028 * @see http://java.sun.com/javaee/5/docs/api/javax/persistence/package-summary.html
029 * @author Daniel Gredler
030 */
031 public class Ejb3Integrator implements Integrator {
032
033 public static final boolean COLUMN_NULLABLE_DEFAULT = true;
034 public static final int COLUMN_LENGTH_DEFAULT = 255;
035 public static final boolean BASIC_OPTIONAL_DEFAULT = true;
036
037 public SortedMap<String, String> getValidation( BeanProperty prop ) {
038
039 SortedMap<String, String> validations = new TreeMap<String, String>();
040
041 Column c = prop.getAnnotation( Column.class );
042 if( c != null ) {
043 if( c.nullable() == false ) {
044 validations.put( REQUIRED, REQUIRED );
045 }
046 if( prop.isString() ) {
047 int maxLength = c.length();
048 validations.put( MAX_LENGTH, MAX_LENGTH + "=" + maxLength );
049 }
050 }
051
052 Basic b = prop.getAnnotation( Basic.class );
053 if( b != null ) {
054 if( b.optional() == false ) {
055 validations.put( REQUIRED, REQUIRED );
056 }
057 }
058
059 return validations;
060 }
061
062 public Integer getMaxLength( BeanProperty prop ) {
063
064 Integer maxLength = null;
065
066 Column c = prop.getAnnotation( Column.class );
067 if( c != null ) {
068 maxLength = c.length();
069 }
070
071 return maxLength;
072 }
073
074 public boolean isNullable( BeanProperty prop ) {
075
076 boolean nullable = true;
077
078 Column c = prop.getAnnotation( Column.class );
079 if( c != null ) {
080 if( c.nullable() == false ) {
081 nullable = false;
082 }
083 }
084
085 Basic b = prop.getAnnotation( Basic.class );
086 if( b != null ) {
087 if( b.optional() == false ) {
088 nullable = false;
089 }
090 }
091
092 return nullable;
093 }
094
095 }