001    package org.tynamo.descriptor.factories;
002    
003    import org.apache.commons.beanutils.BeanUtils;
004    import org.tynamo.descriptor.TynamoPropertyDescriptor;
005    import org.tynamo.descriptor.TynamoPropertyDescriptorImpl;
006    import org.tynamo.util.Utils;
007    
008    import java.beans.BeanInfo;
009    import java.beans.PropertyDescriptor;
010    import java.util.ArrayList;
011    import java.util.Collection;
012    
013    
014    public class PropertyDescriptorFactoryImpl implements PropertyDescriptorFactory
015    {
016    
017            private final Collection<String> propertyExcludes;
018    
019            public PropertyDescriptorFactoryImpl(Collection<String> propertyExcludes)
020            {
021                    this.propertyExcludes = propertyExcludes;
022            }
023    
024    
025            /**
026             * Build the set of property descriptors for this type
027             *
028             * @param beanType the aggregating class
029             * @param beanInfo the BeanInfo, already gathered
030             * @return
031             * @throws Exception
032             */
033            public ArrayList<TynamoPropertyDescriptor> buildPropertyDescriptors(Class beanType, BeanInfo beanInfo) throws Exception
034            {
035                    ArrayList<TynamoPropertyDescriptor> result = new ArrayList<TynamoPropertyDescriptor>();
036                    for (PropertyDescriptor beanPropDescriptor : beanInfo.getPropertyDescriptors())
037                    {
038                            if (!Utils.isExcluded(beanPropDescriptor.getName(), propertyExcludes))
039                            {
040                                    Class<?> propertyType = beanPropDescriptor.getPropertyType();
041                                    TynamoPropertyDescriptorImpl propDescriptor = new TynamoPropertyDescriptorImpl(beanType, propertyType);
042                                    BeanUtils.copyProperties(propDescriptor, beanPropDescriptor);
043                                    result.add(propDescriptor);
044                            }
045                    }
046                    return result;
047            }
048    }