Coverage Report - org.trails.descriptor.CollectionDescriptor
 
Classes in this File Line Coverage Branch Coverage Complexity
CollectionDescriptor
84% 
100% 
1.474
 
 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.descriptor;
 13  
 
 14  
 import java.lang.reflect.InvocationTargetException;
 15  
 import java.lang.reflect.Method;
 16  
 import java.util.List;
 17  
 
 18  
 import org.apache.commons.beanutils.BeanUtils;
 19  
 import org.apache.commons.logging.Log;
 20  
 import org.apache.commons.logging.LogFactory;
 21  
 import org.trails.TrailsRuntimeException;
 22  
 
 23  
 
 24  1
 public class CollectionDescriptor extends TrailsPropertyDescriptor
 25  
 {
 26  1
         protected static final Log LOG = LogFactory.getLog(CollectionDescriptor.class);
 27  
 
 28  
         private Class elementType;
 29  
 
 30  55
         private boolean childRelationship = false;
 31  
 
 32  55
         private String inverseProperty = null;
 33  
 
 34  55
         private boolean oneToMany = false;
 35  
 
 36  
         public CollectionDescriptor(Class beanType, IPropertyDescriptor descriptor)
 37  
         {
 38  0
                 super(beanType, descriptor);
 39  0
         }
 40  
 
 41  
         public CollectionDescriptor(Class beanType, CollectionDescriptor collectionDescriptor)
 42  
         {
 43  1
                 super(beanType, collectionDescriptor.getBeanType());
 44  1
                 this.copyFrom(collectionDescriptor);
 45  1
         }
 46  
 
 47  
         public CollectionDescriptor(Class beanType, Class type)
 48  
         {
 49  54
                 super(beanType, type);
 50  
                 // TODO Auto-generated constructor stub
 51  54
         }
 52  
 
 53  
         public CollectionDescriptor(Class beanType, String name, Class type)
 54  
         {
 55  42
                 this(beanType, type);
 56  42
                 this.setName(name);
 57  42
         }
 58  
 
 59  
         /* (non-Javadoc)
 60  
                  * @see org.trails.descriptor.PropertyDescriptor#isCollection()
 61  
                  */
 62  
         public boolean isCollection()
 63  
         {
 64  10
                 return true;
 65  
         }
 66  
 
 67  
         /**
 68  
          * @return Returns the elementType.
 69  
          */
 70  
         public Class getElementType()
 71  
         {
 72  21
                 return elementType;
 73  
         }
 74  
 
 75  
         /**
 76  
          * @param elementType The elementType to set.
 77  
          */
 78  
         public void setElementType(Class elementType)
 79  
         {
 80  30
                 this.elementType = elementType;
 81  30
         }
 82  
 
 83  
         public String getInverseProperty()
 84  
         {
 85  4
                 return inverseProperty;
 86  
         }
 87  
 
 88  
         public void setInverseProperty(String inverseProperty)
 89  
         {
 90  3
                 this.inverseProperty = inverseProperty;
 91  3
         }
 92  
 
 93  
         /**
 94  
          * Is this a OneToMany collection? or a ManyToMany collection?
 95  
          */
 96  
         public boolean isOneToMany()
 97  
         {
 98  3
                 return oneToMany;
 99  
         }
 100  
 
 101  
         public void setOneToMany(boolean oneToMany)
 102  
         {
 103  3
                 this.oneToMany = oneToMany;
 104  3
         }
 105  
 
 106  
 
 107  
         /**
 108  
          * @return Returns the childRelationship.
 109  
          */
 110  
         public boolean isChildRelationship()
 111  
         {
 112  12
                 return childRelationship;
 113  
         }
 114  
 
 115  
         /**
 116  
          * @param childRelationship The childRelationship to set.
 117  
          */
 118  
         public void setChildRelationship(boolean childRelationship)
 119  
         {
 120  12
                 this.childRelationship = childRelationship;
 121  12
                 if (this.childRelationship)
 122  
                 {
 123  12
                         setSearchable(false);
 124  
                 }
 125  12
         }
 126  
 
 127  
         public Object clone()
 128  
         {
 129  1
                 return new CollectionDescriptor(getBeanType(), this);
 130  
         }
 131  
 
 132  
         public String findAddExpression()
 133  
         {
 134  
                 final String method = "add";
 135  
 
 136  
                 /**
 137  
                  * Awful patch for TRAILS-78 bug.
 138  
                  * It evaluates if the object is in the list before adding it.
 139  
                  * If it is already there then do nothing else add it.
 140  
                  * eg: "bazzes.contains(#member) ? bazzes.size() : bazzes.add" 
 141  
                  *
 142  
                  */
 143  6
                 if (isChildRelationship() && List.class.isAssignableFrom(getType())) {
 144  2
                         return findExpression(method, getName() + ".contains(#member) ? " + getName() + ".size() : " + getName() + "." + method);
 145  
                 } else {
 146  4
                         return findExpression(method);
 147  
                 }
 148  
 
 149  
         }
 150  
 
 151  
         public String findRemoveExpression()
 152  
         {
 153  3
                 return findExpression("remove");
 154  
         }
 155  
 
 156  
         /**
 157  
          * @param method the method to look for, usually add or remove
 158  
          * @return the ogln expression to use to add or remove a member to the
 159  
          *         collection.  Will look for a addName method where Name is
 160  
          *         the unqualified element class name, if there isn't one it will use
 161  
          *         the collection's add method.
 162  
          */
 163  
         private String findExpression(String method, String defaultValue)
 164  
         {
 165  9
                 Method addMethod = null;
 166  
 
 167  
                 try
 168  
                 {
 169  9
                         addMethod = getBeanType().getMethod(method + getElementType().getSimpleName(), new Class[]{getElementType()});
 170  3
                 } catch (NoSuchMethodException ex)
 171  
                 {
 172  
                         // if we don't have one...
 173  3
                         return defaultValue;
 174  0
                 } catch (Exception e)
 175  
                 {
 176  0
                         throw new TrailsRuntimeException(e);
 177  
                 }
 178  
 
 179  6
                 return addMethod.getName();
 180  
         }
 181  
 
 182  
         String findExpression(String method)
 183  
         {
 184  7
                 return findExpression(method, getName() + "." + method);
 185  
         }
 186  
 
 187  
         private void copyFrom(CollectionDescriptor collectionDescriptor)
 188  
         {
 189  1
                 LOG.debug("Cloning CollectionDescriptor");
 190  
                 try
 191  
                 {
 192  1
                         BeanUtils.copyProperties(this, collectionDescriptor);
 193  0
                 } catch (IllegalAccessException e)
 194  
                 {
 195  0
                         LOG.error(e.getMessage());
 196  0
                 } catch (InvocationTargetException e)
 197  
                 {
 198  0
                         LOG.error(e.getMessage());
 199  
                 }
 200  1
         }
 201  
 }