Coverage report

  %line %branch
org.apache.portals.graffito.jcr.mapper.model.ClassDescriptor
0% 
0% 

 1  
 /*
 2  
  * Copyright 2000-2005 The Apache Software Foundation.
 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
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.apache.portals.graffito.jcr.mapper.model;
 17  
 
 18  
 
 19  
 import java.util.Collection;
 20  
 import java.util.HashMap;
 21  
 import java.util.Iterator;
 22  
 import java.util.Map;
 23  
 import org.apache.portals.graffito.jcr.exception.JcrMappingException;
 24  
 
 25  
 /**
 26  
  *
 27  
  * ClassDescriptor is used by the mapper to read general information on a class
 28  
  *
 29  
  * @author <a href="mailto:christophe.lombart@sword-technologies.com">Lombart Christophe </a>
 30  
  * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
 31  
  */
 32  0
 public class ClassDescriptor {
 33  
     private static final String NODETYPE_PER_HIERARCHY = "nodetypeperhierarchy";
 34  
     private static final String NODETYPE_PER_CONCRETECLASS = "nodetypeperconcreteclass";
 35  
 
 36  
     private MappingDescriptor mappingDescriptor;
 37  
     private ClassDescriptor superClassDescriptor;
 38  
 
 39  
     private String className;
 40  
     private String jcrNodeType;
 41  
     private String jcrSuperTypes;
 42  0
     private String[] jcrMixinTypes = new String[0];
 43  
     private FieldDescriptor idFieldDescriptor;
 44  
     private FieldDescriptor pathFieldDescriptor;
 45  
     private FieldDescriptor discriminatorFieldDescriptor;
 46  
 
 47  0
     private Map fieldDescriptors = new HashMap();
 48  
     // collects all super field descriptors
 49  0
     private Map allFields = null;
 50  0
     private Map beanDescriptors = new HashMap();
 51  
     // collects all super bean descriptors
 52  0
     private Map allBeans = null;
 53  0
     private Map collectionDescriptors = new HashMap();
 54  
     // collects all super collection descriptors
 55  0
     private Map allCollections = null;
 56  0
     private Map fieldNames = new HashMap();
 57  
 
 58  
     private String superClassName;
 59  
     private String extendsStrategy;
 60  0
     private boolean abstractClass = false;
 61  
     private String descriminatorValue;
 62  
 
 63  
 
 64  
     public void setAbstract(boolean flag) {
 65  0
         this.abstractClass = flag;
 66  0
     }
 67  
 
 68  
     public boolean isAbstract() {
 69  0
         return this.abstractClass;
 70  
     }
 71  
 
 72  
     /**
 73  
      * @return Returns the descriminatorValue.
 74  
      */
 75  
     public String getDiscriminatorValue() {
 76  0
         return descriminatorValue;
 77  
     }
 78  
 
 79  
     /**
 80  
      * In case this class is part of an hierarchy which is
 81  
      * using <tt>NODETYPE_PER_HIERARCHY</tt> strategy the returned
 82  
      * value represents the value of the <tt>discriminator</tt>
 83  
      * property than identifies uniquely this type.
 84  
      * 
 85  
      * @param descriminatorValue The descriminatorValue to set.
 86  
      */
 87  
     public void setDiscriminatorValue(String descriminatorValue) {
 88  0
         this.descriminatorValue= descriminatorValue;
 89  0
     }
 90  
 
 91  
     /**
 92  
      * Returns the inheritance strategy used by this descriptor. It can be either
 93  
      * <tt>NODETYPE_PER_CONCRETECLASS</tt>, <tt>NODETYPE_PER_HIERARCHY</tt> or
 94  
      * <tt>null</tt> if no inheritance mechanism is used.
 95  
      *
 96  
      * @return Returns {@link #NODETYPE_PER_HIERARCHY} or {@link #NODETYPE_PER_CONCRETECLASS} or
 97  
      * <tt>null</tt>
 98  
      */
 99  
 /*
 100  
     public String getExtendsStrategy() {
 101  
         return extendsStrategy;
 102  
     }
 103  
 */
 104  
 
 105  
     public boolean usesNodeTypePerHierarchyStrategy() {
 106  0
         return NODETYPE_PER_HIERARCHY.equals(this.extendsStrategy);
 107  
     }
 108  
 
 109  
     public boolean usesNodeTypePerConcreteClassStrategy() {
 110  0
         return NODETYPE_PER_CONCRETECLASS.equals(this.extendsStrategy);
 111  
     }
 112  
     /**
 113  
      * @return Returns the className.
 114  
      */
 115  
     public String getClassName() {
 116  0
         return className;
 117  
     }
 118  
 
 119  
     /**
 120  
      * @param className The className to set.
 121  
      */
 122  
     public void setClassName(String className) {
 123  0
         this.className = className;
 124  0
     }
 125  
 
 126  
     /**
 127  
      * @return Returns the jcrNodeType.
 128  
      */
 129  
     public String getJcrNodeType() {
 130  0
         return jcrNodeType;
 131  
     }
 132  
 
 133  
     /**
 134  
      * @param jcrNodeType The jcrNodeType to set.
 135  
      */
 136  
     public void setJcrNodeType(String jcrNodeType) {
 137  0
         this.jcrNodeType = jcrNodeType;
 138  0
     }
 139  
 
 140  
     /**
 141  
      * Add a new FielDescriptor
 142  
      * @param fieldDescriptor the new field descriptor to add
 143  
      */
 144  
     public void addFieldDescriptor(FieldDescriptor fieldDescriptor) {
 145  0
         fieldDescriptor.setClassDescriptor(this);
 146  0
         if (fieldDescriptor.isId()) {
 147  0
             this.idFieldDescriptor = fieldDescriptor;
 148  
         }
 149  0
         if (fieldDescriptor.isPath()) {
 150  0
             this.pathFieldDescriptor = fieldDescriptor;
 151  
         }
 152  0
         if (fieldDescriptor.isDiscriminator()) {
 153  0
             this.discriminatorFieldDescriptor = fieldDescriptor;
 154  
         }
 155  
 
 156  0
         fieldDescriptors.put(fieldDescriptor.getFieldName(), fieldDescriptor);
 157  0
         fieldNames.put(fieldDescriptor.getFieldName(), fieldDescriptor.getJcrName());
 158  0
     }
 159  
 
 160  
     /**
 161  
      * Get the FieldDescriptor to used for a specific java bean attribute
 162  
      * @param fieldName The java bean attribute name
 163  
      *
 164  
      * @return the {@link FieldDescriptor} found or null
 165  
      */
 166  
     public FieldDescriptor getFieldDescriptor(String fieldName) {
 167  0
         return (FieldDescriptor) this.allFields.get(fieldName);
 168  
     }
 169  
 
 170  
     /**
 171  
      *
 172  
      * @return all {@link FieldDescriptor} defined in this ClassDescriptor
 173  
      */
 174  
     public Collection getFieldDescriptors() {
 175  0
         return this.allFields.values();
 176  
     }
 177  
 
 178  
     /**
 179  
      * Add a new BeanDescriptor
 180  
      * @param beanDescriptor the new bean descriptor to add
 181  
      */
 182  
 
 183  
     public void addBeanDescriptor(BeanDescriptor beanDescriptor) {
 184  0
         beanDescriptor.setClassDescriptor(this);
 185  0
         beanDescriptors.put(beanDescriptor.getFieldName(), beanDescriptor);
 186  0
         fieldNames.put(beanDescriptor.getFieldName(), beanDescriptor.getJcrName());
 187  0
     }
 188  
 
 189  
     /**
 190  
      * Get the BeanDescriptor to used for a specific java bean attribute
 191  
      * @param fieldName The java bean attribute name
 192  
      *
 193  
      * @return the {@link BeanDescriptor} found or null
 194  
      */
 195  
     public BeanDescriptor getBeanDescriptor(String fieldName) {
 196  0
         return (BeanDescriptor) this.allBeans.get(fieldName);
 197  
     }
 198  
 
 199  
     /**
 200  
      * @return all {@link BeanDescriptor} defined in this ClassDescriptor
 201  
      */
 202  
     public Collection getBeanDescriptors() {
 203  0
         return this.allBeans.values();
 204  
     }
 205  
 
 206  
     /**
 207  
      * Add a new CollectionDescriptor
 208  
      * @param collectionDescriptor the new collection descriptor to add
 209  
      */
 210  
 
 211  
     public void addCollectionDescriptor(CollectionDescriptor collectionDescriptor) {
 212  0
         collectionDescriptor.setClassDescriptor(this);
 213  0
         collectionDescriptors.put(collectionDescriptor.getFieldName(), collectionDescriptor);
 214  0
         fieldNames.put(collectionDescriptor.getFieldName(), collectionDescriptor.getJcrName());
 215  0
     }
 216  
 
 217  
     /**
 218  
      * Get the CollectionDescriptor to used for a specific java bean attribute
 219  
      * @param fieldName The java bean attribute name
 220  
      *
 221  
      * @return the {@link CollectionDescriptor} found or null
 222  
      */
 223  
     public CollectionDescriptor getCollectionDescriptor(String fieldName) {
 224  0
         return (CollectionDescriptor) this.allCollections.get(fieldName);
 225  
     }
 226  
 
 227  
     /**
 228  
      * @return all {@link BeanDescriptor} defined in this ClassDescriptor
 229  
      */
 230  
     public Collection getCollectionDescriptors() {
 231  0
         return this.allCollections.values();
 232  
     }
 233  
 
 234  
     /**
 235  
      * @return the fieldDescriptor ID
 236  
      */
 237  
     public FieldDescriptor getIdFieldDescriptor() {
 238  0
         return idFieldDescriptor;
 239  
     }
 240  
 
 241  
     /**
 242  
      * @return the fieldDescriptor path
 243  
      */
 244  
     public FieldDescriptor getPathFieldDescriptor() {
 245  0
         return pathFieldDescriptor;
 246  
     }
 247  
 
 248  
     public FieldDescriptor getDiscriminatorFieldDescriptor() {
 249  0
         return this.discriminatorFieldDescriptor;
 250  
     }
 251  
 
 252  
     public boolean hasDiscriminatorField() {
 253  0
         return this.discriminatorFieldDescriptor != null;
 254  
     }
 255  
 
 256  
     /**
 257  
      * Check if this class has an ID
 258  
      * @return true if the class has an ID
 259  
      */
 260  
     public boolean hasIdField() {
 261  0
         return this.idFieldDescriptor != null;
 262  
     }
 263  
 
 264  
     /**
 265  
      * Get the JCR name used for one of the object attributes
 266  
      * @param fieldName the object attribute name (can be an atomic field, bean field or a collection field)
 267  
      * @return the JCR name found
 268  
      */
 269  
     public String getJcrName(String fieldName) {
 270  0
         return (String) this.fieldNames.get(fieldName);
 271  
     }
 272  
 
 273  
     /** Get the JCR node super types.
 274  
      *
 275  
      * @return jcrSuperTypes
 276  
      */
 277  
     public String getJcrSuperTypes() {
 278  0
         return jcrSuperTypes;
 279  
     }
 280  
 
 281  
     /** Setter for JCR super types.
 282  
      *
 283  
      * @param superTypes Comma separated list of JCR node super types
 284  
      */
 285  
     public void setJcrSuperTypes(String superTypes) {
 286  0
         this.jcrSuperTypes = superTypes;
 287  0
     }
 288  
 
 289  
     /**
 290  
      * Retrieve the mixin types.
 291  
      *
 292  
      * @return array of mixin types
 293  
      */
 294  
     public String[] getJcrMixinTypes() {
 295  0
         return this.jcrMixinTypes;
 296  
     }
 297  
 
 298  
     /**
 299  
      * Sets a comma separated list of mixin types.
 300  
      *
 301  
      * @param mixinTypes command separated list of mixins
 302  
      */
 303  
 //    public void setJcrMixinTypesList(String[] mixinTypes) {
 304  
 //        if (null != mixinTypes) {
 305  
 //            setJcrMixinTypes(mixinTypes[0].split(","));
 306  
 //        }
 307  
 //    }
 308  
 
 309  
     public void setJcrMixinTypes(String[] mixinTypes) {
 310  0
         if (null != mixinTypes && mixinTypes.length == 1) {
 311  0
             jcrMixinTypes = mixinTypes[0].split(" *, *");
 312  
         }
 313  0
     }
 314  
 
 315  
     /**
 316  
      * @return Returns the mappingDescriptor.
 317  
      */
 318  
     public MappingDescriptor getMappingDescriptor() {
 319  0
         return mappingDescriptor;
 320  
     }
 321  
 
 322  
     /**
 323  
      * @param mappingDescriptor The mappingDescriptor to set.
 324  
      */
 325  
     public void setMappingDescriptor(MappingDescriptor mappingDescriptor) {
 326  0
         this.mappingDescriptor = mappingDescriptor;
 327  0
     }
 328  
 
 329  
     /**
 330  
      * Revisit information in this descriptor and fills in more.
 331  
      */
 332  
     public void afterPropertiesSet() {
 333  0
         lookupSuperDescriptor();
 334  
 
 335  0
         lookupInheritanceSettings();
 336  
 
 337  0
         validateInheritanceSettings();
 338  0
     }
 339  
 
 340  
     private void validateInheritanceSettings() {
 341  0
         if (NODETYPE_PER_CONCRETECLASS.equals(this.extendsStrategy)) {
 342  0
             this.discriminatorFieldDescriptor = findDiscriminatorField();
 343  
 
 344  0
             if (null != this.discriminatorFieldDescriptor) {
 345  0
                 throw new JcrMappingException("");
 346  
             }
 347  
         }
 348  0
         else if (NODETYPE_PER_HIERARCHY.equals(this.extendsStrategy)) {
 349  0
             this.discriminatorFieldDescriptor = findDiscriminatorField();
 350  
 
 351  0
             if (null == this.discriminatorFieldDescriptor) {
 352  0
                 throw new JcrMappingException("");
 353  
             }
 354  
         }
 355  0
     }
 356  
 
 357  
     private FieldDescriptor findDiscriminatorField() {
 358  0
         if (null != this.discriminatorFieldDescriptor) {
 359  0
             return this.discriminatorFieldDescriptor;
 360  
         }
 361  
 
 362  0
         if (null != this.superClassDescriptor) {
 363  0
             return this.superClassDescriptor.findDiscriminatorField();
 364  
         }
 365  
 
 366  0
         return null;
 367  
     }
 368  
 
 369  
     private void lookupInheritanceSettings() {
 370  0
         if (null != this.superClassDescriptor) {
 371  0
             if (this.jcrNodeType.equals(class="keyword">this.superClassDescriptor.jcrNodeType)) {
 372  0
                 this.extendsStrategy = NODETYPE_PER_HIERARCHY;
 373  
             }
 374  
             else {
 375  0
                 this.extendsStrategy = NODETYPE_PER_CONCRETECLASS;
 376  
             }
 377  
         }
 378  0
     }
 379  
 
 380  
     private void lookupSuperDescriptor() {
 381  0
         if (null == this.superClassDescriptor) {
 382  0
             this.allFields = class="keyword">this.fieldDescriptors;
 383  0
             this.allBeans = class="keyword">this.beanDescriptors;
 384  0
             this.allCollections = class="keyword">this.collectionDescriptors;
 385  
         }
 386  
         else {
 387  0
             this.allFields = merge(class="keyword">this.fieldDescriptors, class="keyword">this.superClassDescriptor.getFieldDescriptors());
 388  0
             this.allBeans = merge(class="keyword">this.beanDescriptors, class="keyword">this.superClassDescriptor.getBeanDescriptors());
 389  0
             this.allCollections = merge(class="keyword">this.collectionDescriptors, class="keyword">this.superClassDescriptor.getCollectionDescriptors());
 390  
         }
 391  0
     }
 392  
 
 393  
     /**
 394  
      * @return return the super class name if defined in mapping, or
 395  
      * <tt>null</tt> if not set
 396  
      */
 397  
     public String getSuperClass() {
 398  0
         return this.superClassName;
 399  
     }
 400  
 
 401  
     /**
 402  
      * @param className
 403  
      */
 404  
     public void setSuperClass(String className) {
 405  0
         this.superClassName = className;
 406  0
     }
 407  
 
 408  
     /**
 409  
      * @return Returns the superClassDescriptor.
 410  
      */
 411  
     public ClassDescriptor getSuperClassDescriptor() {
 412  0
         return superClassDescriptor;
 413  
     }
 414  
 
 415  
     /**
 416  
      * @param superClassDescriptor The superClassDescriptor to set.
 417  
      */
 418  
     public void setSuperClassDescriptor(ClassDescriptor superClassDescriptor) {
 419  0
         this.superClassDescriptor= superClassDescriptor;
 420  0
     }
 421  
 
 422  
     private Map merge(Map existing, Collection superSource) {
 423  0
         if (null == superSource) {
 424  0
             return existing;
 425  
         }
 426  
 
 427  0
         Map merged = new HashMap(existing);
 428  0
         for(Iterator it = superSource.iterator(); it.hasNext();) {
 429  0
             FieldDescriptor fd = (FieldDescriptor) it.next();
 430  0
             if (!merged.containsKey(fd.getFieldName())) {
 431  0
                 merged.put(fd.getFieldName(), fd);
 432  
             }
 433  
         }
 434  
 
 435  0
         return merged;
 436  
     }
 437  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.