| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
package net.sf.beanform.integration; |
| 16 |
|
|
| 17 |
|
import java.util.ArrayList; |
| 18 |
|
import java.util.Collections; |
| 19 |
|
import java.util.HashMap; |
| 20 |
|
import java.util.Iterator; |
| 21 |
|
import java.util.List; |
| 22 |
|
import java.util.Map; |
| 23 |
|
|
| 24 |
|
import org.apache.commons.logging.Log; |
| 25 |
|
import org.apache.commons.logging.LogFactory; |
| 26 |
|
|
| 27 |
|
import net.sf.beanform.prop.BeanProperty; |
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
public final class IntegratorChain { |
| 39 |
|
|
| 40 |
1 |
private final static Log LOG = LogFactory.getLog( IntegratorChain.class ); |
| 41 |
|
private final static List<Integrator> INTEGRATORS; |
| 42 |
|
|
| 43 |
|
static { |
| 44 |
|
|
| 45 |
|
|
| 46 |
1 |
List<Integrator> list = new ArrayList<Integrator>(); |
| 47 |
1 |
if( isSupported( "Hibernate", "org.hibernate.validator.Max" ) ) { |
| 48 |
1 |
list.add( new HibernateIntegrator() ); |
| 49 |
|
} |
| 50 |
1 |
if( isSupported( "EJB3", "javax.persistence.Column" ) ) { |
| 51 |
1 |
list.add( new Ejb3Integrator() ); |
| 52 |
|
} |
| 53 |
1 |
INTEGRATORS = Collections.unmodifiableList( list ); |
| 54 |
1 |
} |
| 55 |
|
|
| 56 |
|
static boolean isSupported( String featureName, String className ) { |
| 57 |
|
Class c; |
| 58 |
|
try { |
| 59 |
4 |
c = Class.forName( className ); |
| 60 |
|
} |
| 61 |
1 |
catch( ClassNotFoundException e ) { |
| 62 |
1 |
c = null; |
| 63 |
|
} |
| 64 |
0 |
catch( NoClassDefFoundError e ) { |
| 65 |
0 |
c = null; |
| 66 |
4 |
} |
| 67 |
4 |
boolean supported = ( c != null ); |
| 68 |
4 |
if( supported ) LOG.debug( featureName + " support detected." ); |
| 69 |
4 |
return supported; |
| 70 |
|
} |
| 71 |
|
|
| 72 |
1 |
IntegratorChain() { |
| 73 |
|
|
| 74 |
1 |
} |
| 75 |
|
|
| 76 |
|
public static String getValidation( BeanProperty prop ) { |
| 77 |
175 |
Map<String, String> validations = new HashMap<String, String>(); |
| 78 |
175 |
for( Integrator integrator : INTEGRATORS ) { |
| 79 |
350 |
Map<String, String> temp = integrator.getValidation( prop ); |
| 80 |
350 |
validations.putAll( temp ); |
| 81 |
350 |
} |
| 82 |
175 |
StringBuilder sb = new StringBuilder(); |
| 83 |
175 |
for( Iterator<String> i = validations.values().iterator(); i.hasNext(); ) { |
| 84 |
40 |
sb.append( i.next() ); |
| 85 |
40 |
if( i.hasNext() ) sb.append( "," ); |
| 86 |
|
} |
| 87 |
175 |
if( sb.length() == 0 ) return null; |
| 88 |
24 |
else return sb.toString(); |
| 89 |
|
} |
| 90 |
|
|
| 91 |
|
public static Integer getMaxLength( BeanProperty prop ) { |
| 92 |
73 |
Integer maxLength = null; |
| 93 |
73 |
for( Integrator integrator : INTEGRATORS ) { |
| 94 |
146 |
Integer temp = integrator.getMaxLength( prop ); |
| 95 |
146 |
if( temp != null ) maxLength = temp; |
| 96 |
146 |
} |
| 97 |
73 |
return maxLength; |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
public static boolean isNullable( BeanProperty prop ) { |
| 101 |
9 |
boolean nullable = true; |
| 102 |
9 |
for( Integrator integrator : INTEGRATORS ) { |
| 103 |
18 |
boolean temp = integrator.isNullable( prop ); |
| 104 |
18 |
nullable = nullable && temp; |
| 105 |
18 |
} |
| 106 |
9 |
return nullable; |
| 107 |
|
} |
| 108 |
|
|
| 109 |
|
} |