Coverage Report - org.trails.descriptor.TrailsClassDescriptor
 
Classes in this File Line Coverage Branch Coverage Complexity
TrailsClassDescriptor
72% 
100% 
0
 
 1  44
 /*
 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.util.ArrayList;
 16  
 import java.util.List;
 17  
 
 18  
 import ognl.Ognl;
 19  
 import ognl.OgnlException;
 20  
 import org.apache.commons.beanutils.BeanUtils;
 21  
 import org.trails.util.Utils;
 22  
 
 23  
 /**
 24  
  * This represents all the Trails metadata for a single class.
 25  
  */
 26  
 public class TrailsClassDescriptor extends TrailsDescriptor implements
 27  
         IClassDescriptor
 28  
 {
 29  540
         private List<IPropertyDescriptor> propertyDescriptors = new ArrayList<IPropertyDescriptor>();
 30  
 
 31  540
         private List<IMethodDescriptor> methodDescriptors = new ArrayList<IMethodDescriptor>();
 32  
 
 33  
         // private BeanDescriptor beanDescriptor;
 34  
         private boolean child;
 35  
 
 36  
         boolean hasCyclicRelationships;
 37  
 
 38  540
         boolean allowRemove = true;
 39  
 
 40  540
         boolean allowSave = true;
 41  
 
 42  
         // ///////////////////////////////////////////////////////////////////////////////////////////////////////
 43  
         // Constructors
 44  
         // ///////////////////////////////////////////////////////////////////////////////////////////////////////
 45  
         /**
 46  
          * This is a copy constructor. These need to be clonable for the security
 47  
          * aspect to be able to copy them, so if new properties are added they
 48  
          * should be added here too.
 49  
          */
 50  
         public TrailsClassDescriptor(IClassDescriptor descriptor)
 51  
         {
 52  16
                 super(descriptor);
 53  16
                 copyPropertyDescriptorsFrom(descriptor);
 54  16
                 copyMethodDescriptorsFrom(descriptor);
 55  16
         }
 56  
 
 57  
         public TrailsClassDescriptor(Class type)
 58  
         {
 59  284
                 super(type);
 60  284
         }
 61  
 
 62  
         public TrailsClassDescriptor(Class type, String displayName)
 63  
         {
 64  228
                 super(type);
 65  228
                 this.setDisplayName(displayName);
 66  228
         }
 67  
 
 68  
         /**
 69  
          * @param dto
 70  
          */
 71  
         public TrailsClassDescriptor(TrailsClassDescriptor dto)
 72  
         {
 73  12
                 super(dto);
 74  
 
 75  
                 try
 76  
                 {
 77  12
                         BeanUtils.copyProperties(this, dto);
 78  0
                 } catch (IllegalAccessException e)
 79  
                 {
 80  0
                         LOG.error(e.getMessage());
 81  0
                         e.printStackTrace();
 82  0
                 } catch (InvocationTargetException e)
 83  
                 {
 84  0
                         LOG.error(e.getMessage());
 85  0
                         e.printStackTrace();
 86  0
                 } catch (Exception e)
 87  
                 {
 88  0
                         LOG.error(e.toString());
 89  0
                         e.printStackTrace();
 90  
                 }
 91  12
         }
 92  
 
 93  
         // ///////////////////////////////////////////////////////////////////////////////////////////////////////
 94  
         // Methods
 95  
         // ///////////////////////////////////////////////////////////////////////////////////////////////////////
 96  
         private void copyMethodDescriptorsFrom(IClassDescriptor descriptor)
 97  
         {
 98  36
                 for (IMethodDescriptor methodDescriptor : descriptor
 99  16
                         .getMethodDescriptors())
 100  
                 {
 101  8
                         getMethodDescriptors().add(
 102  4
                                 IMethodDescriptor.class.cast(methodDescriptor.clone()));
 103  
                 }
 104  16
         }
 105  
 
 106  
         protected void copyPropertyDescriptorsFrom(IClassDescriptor descriptor)
 107  
         {
 108  60
                 for (IPropertyDescriptor iPropertyDescriptor : descriptor
 109  16
                         .getPropertyDescriptors())
 110  
                 {
 111  28
                         getPropertyDescriptors()
 112  28
                                 .add(
 113  56
                                         IPropertyDescriptor.class.cast(iPropertyDescriptor
 114  28
                                                 .clone()));
 115  
                 }
 116  16
         }
 117  
 
 118  
         /**
 119  
          * @param ognl
 120  
          * @return
 121  
          */
 122  
         private IPropertyDescriptor findDescriptor(String ognl)
 123  
         {
 124  
                 try
 125  
                 {
 126  168
                         return (IPropertyDescriptor) Ognl.getValue(ognl, this);
 127  0
                 } catch (OgnlException oe)
 128  
                 {
 129  
                         // oe.printStackTrace();
 130  
 
 131  0
                         return null;
 132  4
                 } catch (IndexOutOfBoundsException ie)
 133  
                 {
 134  48
                         return null;
 135  88
                 }
 136  12
         }
 137  32
 
 138  
         /**
 139  
          * @param string
 140  
          * @return
 141  
          */
 142  
         public IPropertyDescriptor getPropertyDescriptor(String name)
 143  
         {
 144  92
                 return findDescriptor("propertyDescriptors.{? name == '" + name + "'}[0]");
 145  
         }
 146  
 
 147  
         /**
 148  
          * @return
 149  
          */
 150  
         public String getPluralDisplayName()
 151  
         {
 152  44
                 return Utils.pluralize(getDisplayName() );
 153  
         }
 154  
 
 155  
         public List<IPropertyDescriptor> getPropertyDescriptors(List<String> properties) {
 156  16
                 ArrayList<IPropertyDescriptor> descriptors = new ArrayList<IPropertyDescriptor>();
 157  64
                 for (String property : properties) {
 158  32
                         descriptors.add(getPropertyDescriptor(property));
 159  
                 }
 160  16
                 return descriptors;
 161  
         }
 162  
 
 163  
         // ///////////////////////////////////////////////////////////////////////////////////////////////////////
 164  
         // bean getters / setters
 165  
         // ///////////////////////////////////////////////////////////////////////////////////////////////////////
 166  
         /**
 167  
          * @return Returns the methodDescriptors.
 168  
          */
 169  
         public List<IMethodDescriptor> getMethodDescriptors()
 170  
         {
 171  108
                 return methodDescriptors;
 172  
         }
 173  
 
 174  
         /**
 175  
          * @param methodDescriptors The methodDescriptors to set.
 176  
          */
 177  
         public void setMethodDescriptors(List<IMethodDescriptor> methodDescriptors)
 178  
         {
 179  244
                 this.methodDescriptors = methodDescriptors;
 180  244
         }
 181  
 
 182  
         /**
 183  
          * @return Returns the propertyDescriptors.
 184  
          */
 185  
         public List<IPropertyDescriptor> getPropertyDescriptors()
 186  
         {
 187  660
                 return propertyDescriptors;
 188  
         }
 189  
 
 190  
         /**
 191  
          * @param propertyDescriptors The propertyDescriptors to set.
 192  
          */
 193  
         public void setPropertyDescriptors(
 194  
                 List<IPropertyDescriptor> propertyDescriptors)
 195  
         {
 196  260
                 this.propertyDescriptors = propertyDescriptors;
 197  260
         }
 198  
 
 199  
         public IPropertyDescriptor getIdentifierDescriptor()
 200  
         {
 201  76
                 String ognl = "propertyDescriptors.{? identifier}[0]";
 202  
 
 203  76
                 return findDescriptor(ognl);
 204  
         }
 205  
 
 206  
         /**
 207  
          * @return Returns the child.
 208  
          */
 209  
         public boolean isChild()
 210  
         {
 211  40
                 return child;
 212  
         }
 213  
 
 214  
         /**
 215  
          * @param child The child to set.
 216  
          */
 217  
         public void setChild(boolean child)
 218  
         {
 219  40
                 this.child = child;
 220  40
         }
 221  
 
 222  
         @Override
 223  
         public Object clone()
 224  
         {
 225  12
                 return new TrailsClassDescriptor(this);
 226  
         }
 227  
 
 228  
         @Override
 229  
         public void copyFrom(IDescriptor descriptor)
 230  
         {
 231  4
                 super.copyFrom(descriptor);
 232  
 
 233  4
                 if (descriptor instanceof TrailsClassDescriptor)
 234  
                 {
 235  
 
 236  
                         try
 237  
                         {
 238  0
                                 BeanUtils.copyProperties(this,
 239  0
                                         (TrailsClassDescriptor) descriptor);
 240  0
                                 copyPropertyDescriptorsFrom((TrailsClassDescriptor) descriptor);
 241  0
                                 copyMethodDescriptorsFrom((TrailsClassDescriptor) descriptor);
 242  0
                         } catch (IllegalAccessException e)
 243  
                         {
 244  0
                                 LOG.error(e.getMessage());
 245  0
                                 e.printStackTrace();
 246  0
                         } catch (InvocationTargetException e)
 247  
                         {
 248  0
                                 LOG.error(e.getMessage());
 249  0
                                 e.printStackTrace();
 250  0
                         } catch (Exception e)
 251  
                         {
 252  0
                                 LOG.error(e.toString());
 253  0
                                 e.printStackTrace();
 254  
                         }
 255  
                 }
 256  4
         }
 257  
 
 258  
         public boolean isAllowRemove()
 259  
         {
 260  56
                 return allowRemove;
 261  
         }
 262  
 
 263  
         public void setAllowRemove(boolean allowRemove)
 264  
         {
 265  56
                 this.allowRemove = allowRemove;
 266  56
         }
 267  
 
 268  
         public boolean isAllowSave()
 269  
         {
 270  60
                 return allowSave;
 271  
         }
 272  
 
 273  
         public void setAllowSave(boolean allowSave)
 274  
         {
 275  40
                 this.allowSave = allowSave;
 276  40
         }
 277  
 
 278  
         public boolean getHasCyclicRelationships()
 279  
         {
 280  56
                 return hasCyclicRelationships;
 281  
         }
 282  
 
 283  
         public void setHasCyclicRelationships(boolean hasBidirectionalRelationship)
 284  
         {
 285  80
                 this.hasCyclicRelationships = hasBidirectionalRelationship;
 286  80
         }
 287  
 
 288  
         /**
 289  
          * Added toString method to help with unit testing debugging.
 290  
          */
 291  
         public String toString()
 292  
         {
 293  0
                 return "{TrailsClassDescriptor - Type: " + getType() + "}";
 294  
         }
 295  
 
 296  
 }