001: package org.conform.hibernate;
002:
003: import org.conform.*;
004: import org.conform.validator.StringLengthValidator;
005: import org.conform.validator.PrecisionScaleValidator;
006: import org.hibernate.mapping.*;
007: import org.hibernate.mapping.Property;
008: import org.hibernate.cfg.Configuration;
009: import org.apache.commons.logging.LogFactory;
010:
011: import java.util.*;
012: import java.util.Set;
013: import java.math.BigDecimal;
014: import java.math.BigInteger;
015:
016: public class HibernateMappingModifier implements Modifier {
017: private static org.apache.commons.logging.Log LOG = LogFactory
018: .getLog(Meta.class);
019:
020: private static final Set integerClasses = new HashSet<Class>(Arrays
021: .asList(new Class[] { Byte.class, Short.class,
022: Integer.class, Long.class, byte.class, short.class,
023: int.class, long.class, BigInteger.class, }));
024: private static final Set decimalClasses = new HashSet<Class>(Arrays
025: .asList(new Class[] { Float.class, Double.class,
026: float.class, double.class, BigDecimal.class, }));
027:
028: BeanMetaProvider beanMetaProvider;
029: private Configuration configuration;
030:
031: public HibernateMappingModifier() {
032: }
033:
034: public HibernateMappingModifier(Configuration configuration,
035: BeanMetaProvider beanMetaProvider) {
036: this .configuration = configuration;
037: this .beanMetaProvider = beanMetaProvider;
038: }
039:
040: public Configuration getConfiguration() {
041: return configuration;
042: }
043:
044: public void setConfiguration(Configuration configuration) {
045: this .configuration = configuration;
046: }
047:
048: public BeanMetaProvider getBeanMetaProvider() {
049: return beanMetaProvider;
050: }
051:
052: public void setBeanMetaProvider(BeanMetaProvider beanMetaProvider) {
053: this .beanMetaProvider = beanMetaProvider;
054: }
055:
056: public void modify(BeanMeta beanMeta) {
057: PersistentClass classMapping = configuration
058: .getClassMapping(beanMeta.getType().getName());
059: if (classMapping == null)
060: return;
061:
062: Property property = (Property) classMapping
063: .getIdentifierProperty();
064: PropertyMeta propertyMeta = beanMeta.getProperty(property
065: .getName());
066: if (propertyMeta != null) {
067: apply(propertyMeta, property);
068: propertyMeta.setAttribute(
069: PropertyMeta.ATTRIBUTE_IDENTIFIER, Boolean.TRUE);
070: }
071:
072: Iterator propertyIterator = classMapping.getPropertyIterator();
073: while (propertyIterator.hasNext()) {
074: property = (Property) propertyIterator.next();
075: propertyMeta = beanMeta.getProperty(property.getName());
076: if (propertyMeta != null)
077: apply(propertyMeta, property);
078: }
079: }
080:
081: private void apply(PropertyMeta propertyMeta, Property property) {
082: if (property.getColumnSpan() == 1) {
083: Column column = (Column) property.getColumnIterator()
084: .next();
085: Class type = propertyMeta.getType();
086: if (String.class.equals(type)) {
087: int length = column.getLength();
088: propertyMeta.setAttribute(
089: PropertyMeta.ATTRIBUTE_LENGTH, new Integer(
090: length));
091: propertyMeta.addValidator(new StringLengthValidator(
092: length));
093: } else {
094: if (integerClasses.contains(type)) {
095: int precision = column.getPrecision();
096: propertyMeta.setAttribute(
097: PropertyMeta.ATTRIBUTE_PRECISION,
098: new Integer(precision));
099: if (BigInteger.class.equals(type))
100: propertyMeta
101: .addValidator(new PrecisionScaleValidator(
102: precision));
103: } else if (decimalClasses.contains(type)) {
104: int precision = column.getPrecision();
105: int scale = column.getScale();
106: propertyMeta.setAttribute(
107: PropertyMeta.ATTRIBUTE_PRECISION,
108: new Integer(precision));
109: propertyMeta.setAttribute(
110: PropertyMeta.ATTRIBUTE_SCALE, new Integer(
111: scale));
112: if (BigDecimal.class.equals(type))
113: propertyMeta
114: .addValidator(new PrecisionScaleValidator(
115: precision, scale));
116: }
117: }
118: }
119: }
120: }
|