Coverage Report - org.trails.hibernate.HibernateDescriptorDecorator
 
Classes in this File Line Coverage Branch Coverage Complexity
HibernateDescriptorDecorator
70% 
82% 
0
 
 1  
 /*
 2  
  * Copyright 2004 Chris Nelson
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 7  
  * Unless required by applicable law or agreed to in writing,
 8  
  * software distributed under the License is distributed on an "AS IS" BASIS,
 9  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 10  
  * See the License for the specific language governing permissions and limitations under the License.
 11  
  */
 12  
 package org.trails.hibernate;
 13  
 
 14  
 import java.beans.IntrospectionException;
 15  
 import java.beans.Introspector;
 16  
 import java.beans.PropertyDescriptor;
 17  
 import java.lang.reflect.Field;
 18  
 import java.lang.reflect.Method;
 19  
 import java.util.ArrayList;
 20  
 import java.util.Collection;
 21  
 import java.util.HashMap;
 22  
 import java.util.Iterator;
 23  
 import java.util.List;
 24  
 
 25  
 import ognl.Ognl;
 26  
 import ognl.OgnlException;
 27  
 
 28  
 import org.apache.commons.logging.Log;
 29  
 import org.apache.commons.logging.LogFactory;
 30  
 import org.hibernate.HibernateException;
 31  
 import org.hibernate.SessionFactory;
 32  
 import org.hibernate.cfg.Configuration;
 33  
 import org.hibernate.mapping.Column;
 34  
 import org.hibernate.mapping.Component;
 35  
 import org.hibernate.mapping.PersistentClass;
 36  
 import org.hibernate.mapping.Property;
 37  
 import org.hibernate.mapping.Selectable;
 38  
 import org.hibernate.mapping.SimpleValue;
 39  
 import org.hibernate.metadata.ClassMetadata;
 40  
 import org.hibernate.metadata.CollectionMetadata;
 41  
 import org.hibernate.type.ComponentType;
 42  
 import org.hibernate.type.Type;
 43  
 import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
 44  
 import org.trails.TrailsRuntimeException;
 45  
 import org.trails.descriptor.CollectionDescriptor;
 46  
 import org.trails.descriptor.DescriptorDecorator;
 47  
 import org.trails.descriptor.DescriptorFactory;
 48  
 import org.trails.descriptor.EmbeddedDescriptor;
 49  
 import org.trails.descriptor.EnumReferenceDescriptor;
 50  
 import org.trails.descriptor.IClassDescriptor;
 51  
 import org.trails.descriptor.IPropertyDescriptor;
 52  
 import org.trails.descriptor.IdentifierDescriptor;
 53  
 import org.trails.descriptor.ObjectReferenceDescriptor;
 54  
 import org.trails.descriptor.OwningObjectReferenceDescriptor;
 55  
 import org.trails.descriptor.TrailsPropertyDescriptor;
 56  
 
 57  
 /**
 58  
  * This decorator will add metadata information. It will replace simple
 59  
  * reflection based TrailsPropertyIPropertyDescriptors with appropriate
 60  
  * Hibernate descriptors <p/> Background... TrailsDescriptorService operates one
 61  
  * ReflectorDescriptorFactory - TrailsDescriptorService iterates/scans all class
 62  
  * types encountered - ReflectorDescriptorFactory allocates property descriptor
 63  
  * instance for the class type - TrailsDescriptorService decorates property
 64  
  * descriptor by calling this module HibernateDescriptorDecorator -
 65  
  * HibernateDescriptorDecorator caches the decorated property descriptor into a
 66  
  * decorated descriptor list - decorated descriptor list gets populated into
 67  
  * class descriptor for class type - TrailsDescriptorService finally populates
 68  
  * decorated class descriptor and it's aggregated list of decorated property
 69  
  * descriptors into it's own list/cache of referenced class descriptors
 70  
  * 
 71  
  * @see TrailsPropertyDescriptor
 72  
  * @see ObjectReferenceDescriptor
 73  
  * @see OwningObjectReferenceDescriptor
 74  
  * @see CollectionDescriptor
 75  
  * @see EmbeddedDescriptor
 76  
  */
 77  25
 public class HibernateDescriptorDecorator implements DescriptorDecorator
 78  
 {
 79  1
         protected static final Log LOG = LogFactory.getLog(HibernateDescriptorDecorator.class);
 80  
 
 81  
         private LocalSessionFactoryBean localSessionFactoryBean;
 82  
 
 83  
         private List types;
 84  
 
 85  
         private DescriptorFactory descriptorFactory;
 86  
 
 87  12
         private HashMap<Class, IClassDescriptor> descriptors = new HashMap<Class, IClassDescriptor>();
 88  
 
 89  12
         private int largeColumnLength = 100;
 90  
 
 91  
         public IClassDescriptor decorate(IClassDescriptor descriptor)
 92  
         {
 93  145
                 ArrayList<IPropertyDescriptor> decoratedPropertyDescriptors = new ArrayList<IPropertyDescriptor>();
 94  923
                 for (IPropertyDescriptor propertyDescriptor : descriptor.getPropertyDescriptors())
 95  
                 {
 96  633
                         Class type = descriptor.getType();
 97  
 
 98  
                         IPropertyDescriptor descriptorReference;
 99  
                         try
 100  
                         {
 101  633
                                 ClassMetadata classMetaData = findMetadata(type);
 102  633
                                 if (propertyDescriptor.getName().equals(getIdentifierProperty(type)))
 103  
                                 {
 104  143
                                         descriptorReference = createIdentifierDescriptor(type, propertyDescriptor);
 105  490
                                 } else if (notAHibernateProperty(classMetaData, propertyDescriptor))
 106  
                                 {
 107  
                                         // If this is not a hibernate property (i.e. marked
 108  
                                         // Transient), it's certainly not searchable
 109  
                                         // Are the any other properties like this?
 110  25
                                         propertyDescriptor.setSearchable(false);
 111  25
                                         descriptorReference = propertyDescriptor;
 112  
                                 } else
 113  
                                 {
 114  465
                                         Property mappingProperty = getMapping(type).getProperty(propertyDescriptor.getName());
 115  930
                                         descriptorReference = decoratePropertyDescriptor(type, mappingProperty, propertyDescriptor,
 116  465
                                                         descriptor);
 117  
                                 }
 118  
 
 119  0
                         } catch (HibernateException e)
 120  
                         {
 121  0
                                 throw new TrailsRuntimeException(e);
 122  
                         }
 123  633
                         decoratedPropertyDescriptors.add(descriptorReference);
 124  
                 }
 125  145
                 descriptor.setPropertyDescriptors(decoratedPropertyDescriptors);
 126  145
                 return descriptor;
 127  
         }
 128  
 
 129  
         protected IPropertyDescriptor decoratePropertyDescriptor(Class type, Property mappingProperty,
 130  
                         IPropertyDescriptor descriptor, IClassDescriptor parentClassDescriptor)
 131  
         {
 132  491
                 if (isFormula(mappingProperty))
 133  
                 {
 134  22
                         descriptor.setReadOnly(true);
 135  22
                         return descriptor;
 136  
                 }
 137  469
                 descriptor.setLength(findColumnLength(mappingProperty));
 138  469
                 descriptor.setLarge(isLarge(mappingProperty));
 139  469
                 if (!mappingProperty.isOptional())
 140  
                 {
 141  34
                         descriptor.setRequired(true);
 142  
                 }
 143  
 
 144  469
                 if (!mappingProperty.isInsertable() && !mappingProperty.isUpdateable())
 145  
                 {
 146  0
                         descriptor.setReadOnly(true);
 147  
                 }
 148  
 
 149  469
                 IPropertyDescriptor descriptorReference = descriptor;
 150  469
                 Type hibernateType = mappingProperty.getType();
 151  469
                 if (mappingProperty.getType() instanceof ComponentType)
 152  
                 {
 153  26
                         EmbeddedDescriptor embeddedDescriptor = buildEmbeddedDescriptor(type, mappingProperty, descriptor,
 154  13
                                         parentClassDescriptor);
 155  13
                         descriptorReference = embeddedDescriptor;
 156  456
                 } else if (Collection.class.isAssignableFrom(descriptor.getPropertyType()))
 157  
                 {
 158  81
                         descriptorReference = decorateCollectionDescriptor(type, descriptor, parentClassDescriptor);
 159  375
                 } else if (hibernateType.isAssociationType())
 160  
                 {
 161  92
                         descriptorReference = decorateAssociationDescriptor(type, mappingProperty, descriptor,
 162  46
                                         parentClassDescriptor);
 163  329
                 } else if (hibernateType.getReturnedClass().isEnum())
 164  
                 {
 165  0
                         descriptor.addExtension(EnumReferenceDescriptor.class.getName(), new EnumReferenceDescriptor(hibernateType
 166  0
                                         .getReturnedClass()));
 167  
                 }
 168  
 
 169  469
                 return descriptorReference;
 170  
         }
 171  
 
 172  
         private EmbeddedDescriptor buildEmbeddedDescriptor(Class type, Property mappingProperty,
 173  
                         IPropertyDescriptor descriptor, IClassDescriptor parentClassDescriptor)
 174  
         {
 175  13
                 Component componentMapping = (Component) mappingProperty.getValue();
 176  13
                 IClassDescriptor baseDescriptor = getDescriptorFactory().buildClassDescriptor(descriptor.getPropertyType());
 177  
                 // build from base descriptor
 178  13
                 EmbeddedDescriptor embeddedDescriptor = new EmbeddedDescriptor(type, baseDescriptor);
 179  
                 // and copy from property descriptor
 180  13
                 embeddedDescriptor.copyFrom(descriptor);
 181  13
                 ArrayList<IPropertyDescriptor> decoratedProperties = new ArrayList<IPropertyDescriptor>();
 182  
                 // go thru each property and decorate it with Hibernate info
 183  65
                 for (IPropertyDescriptor propertyDescriptor : embeddedDescriptor.getPropertyDescriptors())
 184  
                 {
 185  39
                         if (notAHibernateProperty(componentMapping, propertyDescriptor))
 186  
                         {
 187  13
                                 decoratedProperties.add(propertyDescriptor);
 188  
                         } else
 189  
                         {
 190  26
                                 Property property = componentMapping.getProperty(propertyDescriptor.getName());
 191  52
                                 IPropertyDescriptor iPropertyDescriptor = decoratePropertyDescriptor(embeddedDescriptor.getBeanType(),
 192  26
                                                 property, propertyDescriptor, parentClassDescriptor);
 193  26
                                 decoratedProperties.add(iPropertyDescriptor);
 194  
                         }
 195  
                 }
 196  13
                 embeddedDescriptor.setPropertyDescriptors(decoratedProperties);
 197  13
                 return embeddedDescriptor;
 198  
         }
 199  
 
 200  
         /**
 201  
          * The default way to order our property descriptors is by the order they
 202  
          * appear in the hibernate config, with id first. Any non-mapped properties
 203  
          * are tacked on at the end, til I think of a better way.
 204  
          * 
 205  
          * @param propertyDescriptors
 206  
          * @return
 207  
          */
 208  
         protected List sortPropertyDescriptors(Class type, List propertyDescriptors)
 209  
         {
 210  0
                 ArrayList sortedPropertyDescriptors = new ArrayList();
 211  
 
 212  
                 try
 213  
                 {
 214  0
                         sortedPropertyDescriptors.add(Ognl.getValue("#this.{? identifier == true}[0]", propertyDescriptors));
 215  0
                         for (Iterator iter = getMapping(type).getPropertyIterator(); iter.hasNext();)
 216  
                         {
 217  0
                                 Property mapping = (Property) iter.next();
 218  0
                                 sortedPropertyDescriptors.addAll((List) Ognl.getValue("#this.{ ? name == \"" + mapping.getName()
 219  0
                                                 + "\"}", propertyDescriptors));
 220  
                         }
 221  0
                 } catch (Exception ex)
 222  
                 {
 223  0
                         throw new TrailsRuntimeException(ex);
 224  
                 }
 225  0
                 return sortedPropertyDescriptors;
 226  
         }
 227  
 
 228  
         /**
 229  
          * Find the Hibernate metadata for this type, traversing up the hierarchy to
 230  
          * supertypes if necessary
 231  
          * 
 232  
          * @param type
 233  
          * @return
 234  
          */
 235  
         protected ClassMetadata findMetadata(Class type) throws MetadataNotFoundException
 236  
         {
 237  633
                 ClassMetadata metaData = getSessionFactory().getClassMetadata(type);
 238  633
                 if (metaData != null)
 239  
                 {
 240  633
                         return metaData;
 241  
                 }
 242  0
                 if (!type.equals(Object.class))
 243  
                 {
 244  0
                         return findMetadata(type.getSuperclass());
 245  
                 } else
 246  
                 {
 247  0
                         throw new MetadataNotFoundException("Failed to find metadata.");
 248  
                 }
 249  
         }
 250  
 
 251  
         private boolean isFormula(Property mappingProperty)
 252  
         {
 253  1383
                 for (Iterator iter = mappingProperty.getColumnIterator(); iter.hasNext();)
 254  
                 {
 255  423
                         Selectable selectable = (Selectable) iter.next();
 256  423
                         if (selectable.isFormula())
 257  
                         {
 258  22
                                 return true;
 259  
                         }
 260  
                 }
 261  469
                 return false;
 262  
         }
 263  
 
 264  
         /**
 265  
          * Checks to see if a property descriptor is in a component mapping
 266  
          * 
 267  
          * @param componentMapping
 268  
          * @param propertyDescriptor
 269  
          * @return true if the propertyDescriptor property is in componentMapping
 270  
          */
 271  
         protected boolean notAHibernateProperty(Component componentMapping, IPropertyDescriptor propertyDescriptor)
 272  
         {
 273  117
                 for (Iterator iter = componentMapping.getPropertyIterator(); iter.hasNext();)
 274  
                 {
 275  65
                         Property property = (Property) iter.next();
 276  65
                         if (property.getName().equals(propertyDescriptor.getName()))
 277  
                         {
 278  26
                                 return false;
 279  
                         }
 280  
                 }
 281  13
                 return true;
 282  
         }
 283  
 
 284  
         private boolean isLarge(Property mappingProperty)
 285  
         {
 286  
                 // Hack to avoid setting large property if length
 287  
                 // is exactly equal to Hibernate default column length
 288  903
                 return findColumnLength(mappingProperty) != Column.DEFAULT_LENGTH
 289  128
                                 && findColumnLength(mappingProperty) > getLargeColumnLength();
 290  
         }
 291  
 
 292  
         private int findColumnLength(Property mappingProperty)
 293  
         {
 294  1066
                 int length = 0;
 295  2994
                 for (Iterator iter = mappingProperty.getColumnIterator(); iter.hasNext();)
 296  
                 {
 297  862
                         Column column = (Column) iter.next();
 298  862
                         length += column.getLength();
 299  
                 }
 300  1066
                 return length;
 301  
         }
 302  
 
 303  
         /**
 304  
          * @param classMetaData
 305  
          * @param type
 306  
          * @return
 307  
          */
 308  
         protected boolean notAHibernateProperty(ClassMetadata classMetaData, IPropertyDescriptor descriptor)
 309  
         {
 310  
                 try
 311  
                 {
 312  980
                         return (Boolean) Ognl.getValue("propertyNames.{ ? #this == \"" + descriptor.getName() + "\"}.size() == 0",
 313  490
                                         classMetaData);
 314  0
                 } catch (OgnlException oe)
 315  
                 {
 316  0
                         throw new TrailsRuntimeException(oe);
 317  
                 }
 318  
         }
 319  
 
 320  
         /**
 321  
          * @param type
 322  
          * @param descriptor
 323  
          * @param parentClassDescriptor
 324  
          * @return
 325  
          */
 326  
         private IdentifierDescriptor createIdentifierDescriptor(Class type, IPropertyDescriptor descriptor)
 327  
         {
 328  143
                 IdentifierDescriptor identifierDescriptor = new IdentifierDescriptor(type, descriptor);
 329  143
                 PersistentClass mapping = getMapping(type);
 330  
 
 331  143
                 if (((SimpleValue) mapping.getIdentifier()).getIdentifierGeneratorStrategy().equals("assigned"))
 332  
                 {
 333  22
                         identifierDescriptor.setGenerated(false);
 334  
                 }
 335  
 
 336  143
                 return identifierDescriptor;
 337  
         }
 338  
 
 339  
         /**
 340  
          * @param type
 341  
          * @return
 342  
          */
 343  
         protected PersistentClass getMapping(Class type)
 344  
         {
 345  608
                 Configuration cfg = getLocalSessionFactoryBean().getConfiguration();
 346  
 
 347  608
                 return cfg.getClassMapping(type.getName());
 348  
         }
 349  
 
 350  
         /**
 351  
          * @param type
 352  
          * @param newDescriptor
 353  
          */
 354  
         private CollectionDescriptor decorateCollectionDescriptor(Class type, IPropertyDescriptor descriptor,
 355  
                         IClassDescriptor parentClassDescriptor)
 356  
         {
 357  
                 try
 358  
                 {
 359  81
                         CollectionDescriptor collectionDescriptor = new CollectionDescriptor(type, descriptor);
 360  81
                         org.hibernate.mapping.Collection collectionMapping = findCollectionMapping(type, descriptor.getName());
 361  
                         // It is a child relationship if it has delete-orphan specified in
 362  
                         // the mapping
 363  81
                         collectionDescriptor.setChildRelationship(collectionMapping.hasOrphanDelete());
 364  162
                         CollectionMetadata collectionMetaData = getSessionFactory().getCollectionMetadata(
 365  81
                                         collectionMapping.getRole());
 366  
 
 367  81
                         collectionDescriptor.setElementType(collectionMetaData.getElementType().getReturnedClass());
 368  
 
 369  81
                         collectionDescriptor.setOneToMany(collectionMapping.isOneToMany());
 370  
 
 371  81
                         decorateOneToManyCollection(parentClassDescriptor, collectionDescriptor, collectionMapping);
 372  
 
 373  81
                         return collectionDescriptor;
 374  
 
 375  0
                 } catch (HibernateException e)
 376  
                 {
 377  0
                         throw new TrailsRuntimeException(e);
 378  
                 }
 379  
         }
 380  
 
 381  
         public IPropertyDescriptor decorateAssociationDescriptor(Class type, Property mappingProperty,
 382  
                         IPropertyDescriptor descriptor, IClassDescriptor parentClassDescriptor)
 383  
         {
 384  46
                 Type hibernateType = mappingProperty.getType();
 385  46
                 Class parentClassType = parentClassDescriptor.getType();
 386  92
                 ObjectReferenceDescriptor descriptorReference = new ObjectReferenceDescriptor(type, descriptor, hibernateType
 387  46
                                 .getReturnedClass());
 388  
 
 389  
                 try
 390  
                 {
 391  46
                         Field propertyField = parentClassType.getDeclaredField(descriptor.getName());
 392  46
                         PropertyDescriptor beanPropDescriptor = (PropertyDescriptor) Ognl.getValue(
 393  46
                                         "propertyDescriptors.{? name == '" + descriptor.getName() + "'}[0]", Introspector
 394  46
                                                         .getBeanInfo(parentClassType));
 395  46
                         Method readMethod = beanPropDescriptor.getReadMethod();
 396  
 
 397  
                         // Start by checking for and retrieving mappedBy attribute inside
 398  
                         // the annotation
 399  46
                         String inverseProperty = "";
 400  46
                         if (readMethod.isAnnotationPresent(javax.persistence.OneToOne.class))
 401  
                         {
 402  0
                                 inverseProperty = readMethod.getAnnotation(javax.persistence.OneToOne.class).mappedBy();
 403  46
                         } else if (propertyField.isAnnotationPresent(javax.persistence.OneToOne.class))
 404  
                         {
 405  0
                                 inverseProperty = propertyField.getAnnotation(javax.persistence.OneToOne.class).mappedBy();
 406  
                         } else
 407  
                         {
 408  
                                 // If there is none then just return the
 409  
                                 // ObjectReferenceDescriptor
 410  46
                                 return descriptorReference;
 411  
                         }
 412  
 
 413  0
                         if ("".equals(inverseProperty))
 414  
                         {
 415  
                                 // http://forums.hibernate.org/viewtopic.php?t=974287&sid=12d018b08dffe07e263652190cfc4e60
 416  
                                 // Caution... this does not support multiple
 417  
                                 // class references across the OneToOne relationship
 418  0
                                 Class returnType = readMethod.getReturnType();
 419  0
                                 for (int i = 0; i < returnType.getDeclaredMethods().length; i++)
 420  
                                 {
 421  0
                                         if (returnType.getDeclaredMethods()[i].getReturnType().equals(propertyField.getDeclaringClass()))
 422  
                                         {
 423  0
                                                 Method theProperty = returnType.getDeclaredMethods()[i];
 424  
                                                 /* strips preceding 'get' */
 425  0
                                                 inverseProperty = theProperty.getName().substring(3).toLowerCase();
 426  0
                                                 break;
 427  
                                         }
 428  
                                 }
 429  
                         }
 430  
 
 431  0
                 } catch (SecurityException e)
 432  
                 {
 433  0
                         LOG.error(e.getMessage());
 434  0
                 } catch (NoSuchFieldException e)
 435  
                 {
 436  0
                         LOG.error(e.getMessage());
 437  0
                 } catch (OgnlException e)
 438  
                 {
 439  0
                         LOG.error(e.getMessage());
 440  0
                 } catch (IntrospectionException e)
 441  
                 {
 442  0
                         LOG.error(e.getMessage());
 443  
                 }
 444  0
                 return descriptorReference;
 445  
         }
 446  
 
 447  
         /**
 448  
          * I couldn't find a way to get the "mappedBy" value from the collection
 449  
          * metadata, so I'm getting it from the OneToMany annotation.
 450  
          */
 451  
         private void decorateOneToManyCollection(IClassDescriptor parentClassDescriptor,
 452  
                         CollectionDescriptor collectionDescriptor, org.hibernate.mapping.Collection collectionMapping)
 453  
         {
 454  81
                 Class type = parentClassDescriptor.getType();
 455  81
                 if (collectionDescriptor.isOneToMany() && collectionMapping.isInverse())
 456  
                 {
 457  
                         try
 458  
                         {
 459  
 
 460  22
                                 Field propertyField = type.getDeclaredField(collectionDescriptor.getName());
 461  22
                                 PropertyDescriptor beanPropDescriptor = (PropertyDescriptor) Ognl.getValue(
 462  22
                                                 "propertyDescriptors.{? name == '" + collectionDescriptor.getName() + "'}[0]", Introspector
 463  22
                                                                 .getBeanInfo(type));
 464  22
                                 Method readMethod = beanPropDescriptor.getReadMethod();
 465  22
                                 String mappedBy = "";
 466  22
                                 if (readMethod.isAnnotationPresent(javax.persistence.OneToMany.class))
 467  
                                 {
 468  22
                                         mappedBy = readMethod.getAnnotation(javax.persistence.OneToMany.class).mappedBy();
 469  0
                                 } else if (propertyField.isAnnotationPresent(javax.persistence.OneToMany.class))
 470  
                                 {
 471  0
                                         mappedBy = propertyField.getAnnotation(javax.persistence.OneToMany.class).mappedBy();
 472  
                                 }
 473  
 
 474  22
                                 if (!"".equals(mappedBy))
 475  
                                 {
 476  22
                                         collectionDescriptor.setInverseProperty(mappedBy);
 477  
                                 }
 478  
 
 479  22
                                 parentClassDescriptor.setHasCyclicRelationships(true);
 480  
 
 481  0
                         } catch (SecurityException e)
 482  
                         {
 483  0
                                 LOG.error(e.getMessage());
 484  0
                         } catch (NoSuchFieldException e)
 485  
                         {
 486  0
                                 LOG.error(e.getMessage());
 487  0
                         } catch (OgnlException e)
 488  
                         {
 489  0
                                 LOG.error(e.getMessage());
 490  0
                         } catch (IntrospectionException e)
 491  
                         {
 492  0
                                 LOG.error(e.getMessage());
 493  
                         }
 494  
                 }
 495  81
         }
 496  
 
 497  
         protected org.hibernate.mapping.Collection findCollectionMapping(Class type, String name)
 498  
         {
 499  94
                 String roleName = type.getName() + "." + name;
 500  188
                 org.hibernate.mapping.Collection collectionMapping = getLocalSessionFactoryBean().getConfiguration()
 501  94
                                 .getCollectionMapping(roleName);
 502  94
                 if (collectionMapping != null)
 503  
                 {
 504  81
                         return collectionMapping;
 505  13
                 } else if (!type.equals(Object.class))
 506  
                 {
 507  13
                         return findCollectionMapping(type.getSuperclass(), name);
 508  
                 } else
 509  
                 {
 510  0
                         throw new MetadataNotFoundException("Metadata not found.");
 511  
                 }
 512  
 
 513  
         }
 514  
 
 515  
         /*
 516  
          * (non-Javadoc)
 517  
          * 
 518  
          * @see org.trails.descriptor.PropertyDescriptorService#getIdentifierProperty(java.lang.Class)
 519  
          */
 520  
         public String getIdentifierProperty(Class type)
 521  
         {
 522  
                 try
 523  
                 {
 524  633
                         return getSessionFactory().getClassMetadata(type).getIdentifierPropertyName();
 525  0
                 } catch (HibernateException e)
 526  
                 {
 527  0
                         throw new TrailsRuntimeException(e);
 528  
                 }
 529  
         }
 530  
 
 531  
         /**
 532  
          * @return Returns the sessionFactory.
 533  
          */
 534  
         public SessionFactory getSessionFactory()
 535  
         {
 536  1347
                 return (SessionFactory) getLocalSessionFactoryBean().getObject();
 537  
         }
 538  
 
 539  
         public IClassDescriptor getClassDescriptor(Class type)
 540  
         {
 541  0
                 return descriptors.get(type);
 542  
         }
 543  
 
 544  
         /**
 545  
          * @return Returns the localSessionFactoryBean.
 546  
          */
 547  
         public LocalSessionFactoryBean getLocalSessionFactoryBean()
 548  
         {
 549  2049
                 return localSessionFactoryBean;
 550  
         }
 551  
 
 552  
         /**
 553  
          * @param localSessionFactoryBean
 554  
          *            The localSessionFactoryBean to set.
 555  
          */
 556  
         public void setLocalSessionFactoryBean(LocalSessionFactoryBean localSessionFactoryBean)
 557  
         {
 558  12
                 this.localSessionFactoryBean = localSessionFactoryBean;
 559  12
         }
 560  
 
 561  
         /*
 562  
          * (non-Javadoc)
 563  
          * 
 564  
          * @see org.trails.descriptor.TrailsDescriptorService#getAllDescriptors()
 565  
          */
 566  
         public List<IClassDescriptor> getAllDescriptors()
 567  
         {
 568  0
                 return new ArrayList<IClassDescriptor>(descriptors.values());
 569  
         }
 570  
 
 571  
         public List getTypes()
 572  
         {
 573  0
                 return types;
 574  
         }
 575  
 
 576  
         public void setTypes(List types)
 577  
         {
 578  0
                 this.types = types;
 579  0
         }
 580  
 
 581  
         public int getLargeColumnLength()
 582  
         {
 583  128
                 return largeColumnLength;
 584  
         }
 585  
 
 586  
         /**
 587  
          * Columns longer than this will have their large property set to true.
 588  
          * 
 589  
          * @param largeColumnLength
 590  
          */
 591  
         public void setLargeColumnLength(int largeColumnLength)
 592  
         {
 593  0
                 this.largeColumnLength = largeColumnLength;
 594  0
         }
 595  
 
 596  
         public DescriptorFactory getDescriptorFactory()
 597  
         {
 598  13
                 return descriptorFactory;
 599  
         }
 600  
 
 601  
         public void setDescriptorFactory(DescriptorFactory descriptorFactory)
 602  
         {
 603  12
                 this.descriptorFactory = descriptorFactory;
 604  12
         }
 605  
 
 606  
 }