Coverage Report - org.trails.descriptor.TrailsDescriptorService
 
Classes in this File Line Coverage Branch Coverage Complexity
TrailsDescriptorService
78% 
75% 
0
 
 1  64
 package org.trails.descriptor;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.Collections;
 5  
 import java.util.Comparator;
 6  
 import java.util.HashMap;
 7  
 import java.util.List;
 8  
 import java.util.Map;
 9  
 
 10  
 import ognl.OgnlException;
 11  
 
 12  
 /**
 13  
  * This class builds and caches IClassDescriptors.  Descriptors are build during the init
 14  
  * method which is called by Spring during application startup
 15  
  *
 16  
  * @author cnelson
 17  
  * @see IClassDescriptor
 18  
  */
 19  72
 public class TrailsDescriptorService implements DescriptorService
 20  
 {
 21  
         protected List<Class> types;
 22  36
         protected Map<Class, IClassDescriptor> descriptors = new HashMap<Class, IClassDescriptor>();
 23  36
         private List<DescriptorDecorator> decorators = new ArrayList<DescriptorDecorator>();
 24  
         private DescriptorFactory descriptorFactory;
 25  
 
 26  
         /**
 27  
          * For each class in types, a descriptor is built by the DescriptorFactory.  Next it is decorated
 28  
          * by each DescriptorDecorator in turn.  Finally it is cached.
 29  
          *
 30  
          * @throws OgnlException
 31  
          * @see DescriptorFactory
 32  
          * @see DescriptorDecorator
 33  
          */
 34  
         public void init() throws OgnlException
 35  
         {
 36  24
                 descriptors.clear();
 37  192
                 for (Class type : types)
 38  
                 {
 39  144
                         IClassDescriptor descriptor = getDescriptorFactory().buildClassDescriptor(type);
 40  144
                         descriptor = applyDecorators(descriptor);
 41  144
                         descriptors.put(type, descriptor);
 42  
                 }
 43  
                 // second pass to find children and set up descriptor parents
 44  192
                 for (IClassDescriptor iClassDescriptor : descriptors.values())
 45  
                 {
 46  144
                         findChildren(iClassDescriptor);
 47  
                 }
 48  24
         }
 49  
 
 50  
         public List<IClassDescriptor> getAllDescriptors()
 51  
         {
 52  4
                 List<IClassDescriptor> allDescriptors = new ArrayList<IClassDescriptor>(descriptors.values());
 53  4
                 Collections.sort(allDescriptors, new Comparator<IClassDescriptor>()
 54  
                 {
 55  
                         public int compare(IClassDescriptor o1, IClassDescriptor o2)
 56  
                         {
 57  60
                                 return o1.getDisplayName().compareTo(o2.getDisplayName());
 58  
                         }
 59  
                 });
 60  4
                 return allDescriptors;
 61  
         }
 62  
 
 63  
         /* (non-Javadoc)
 64  
                  * @see org.trails.descriptor.IDescriptorFactory#buildClassDescriptor(java.lang.Class)
 65  
                  */
 66  
         public IClassDescriptor getClassDescriptor(Class type)
 67  
         {
 68  24
                 if (type.getName().contains("CGLIB"))
 69  
                 {
 70  0
                         return descriptors.get(type.getSuperclass());
 71  
                 } else
 72  
                 {
 73  24
                         return descriptors.get(type);
 74  
                 }
 75  
         }
 76  
 
 77  
         protected void findChildren(IClassDescriptor iClassDescriptor)
 78  
         {
 79  576
                 for (IPropertyDescriptor propertyDescriptor : iClassDescriptor.getPropertyDescriptors())
 80  
                 {
 81  288
                         if (propertyDescriptor.isCollection())
 82  
                         {
 83  0
                                 if (((CollectionDescriptor) propertyDescriptor).isChildRelationship())
 84  
                                 {
 85  0
                                         IClassDescriptor collectionClassDescriptor = getClassDescriptor(((CollectionDescriptor) propertyDescriptor).getElementType());
 86  0
                                         collectionClassDescriptor.setChild(true);
 87  
                                 }
 88  0
                                 if (((CollectionDescriptor) propertyDescriptor).getInverseProperty() != null)
 89  
                                 {
 90  0
                                         iClassDescriptor.setHasCyclicRelationships(true);
 91  
                                 }
 92  
                         }
 93  
                 }
 94  144
         }
 95  
 
 96  
         /**
 97  
          * Have the decorators decorate this descriptor  todo what are decorators for and/or why would I want to call this
 98  
          * menthod?
 99  
          *
 100  
          * @param descriptor
 101  
          * @return The resulting descriptor after all decorators are applied
 102  
          */
 103  
         protected IClassDescriptor applyDecorators(IClassDescriptor descriptor)
 104  
         {
 105  144
                 IClassDescriptor currDescriptor = descriptor;
 106  292
                 for (DescriptorDecorator decorator : getDecorators())
 107  
                 {
 108  4
                         currDescriptor = decorator.decorate(currDescriptor);
 109  
                 }
 110  144
                 return currDescriptor;
 111  
         }
 112  
 
 113  
         /**
 114  
          * In the Trails default configuration this will be set
 115  
          * to all classes in the Hibernate config
 116  
          *
 117  
          * @return
 118  
          */
 119  
         public List getTypes()
 120  
         {
 121  0
                 return types;
 122  
         }
 123  
 
 124  
         /**
 125  
          * @param types all the classes this service should describe
 126  
          */
 127  
         public void setTypes(List<Class> types)
 128  
         {
 129  20
                 this.types = types;
 130  20
         }
 131  
 
 132  
         /**
 133  
          * In the default Trails configuration this will contain a HibernateDescriptorDecorator
 134  
          * and an AnnotationDecorator
 135  
          *
 136  
          * @return
 137  
          * @see org.trails.hibernate.HibernateDescriptorDecorator
 138  
          * @see org.trails.descriptor.annotation.AnnotationDecorator
 139  
          */
 140  
         public List<DescriptorDecorator> getDecorators()
 141  
         {
 142  148
                 return decorators;
 143  
         }
 144  
 
 145  
         public void setDecorators(List<DescriptorDecorator> decorators)
 146  
         {
 147  0
                 this.decorators = decorators;
 148  0
         }
 149  
 
 150  
         /**
 151  
          * In default Trails this will be a ReflectionDescriptorFactory
 152  
          *
 153  
          * @return
 154  
          * @see ReflectionDescriptorFactory
 155  
          */
 156  
         public DescriptorFactory getDescriptorFactory()
 157  
         {
 158  148
                 return descriptorFactory;
 159  
         }
 160  
 
 161  
         public void setDescriptorFactory(DescriptorFactory descriptorFactory)
 162  
         {
 163  20
                 this.descriptorFactory = descriptorFactory;
 164  20
         }
 165  
 }