Coverage Report - org.kuali.student.datadictionary.util.Bean2DictionaryConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
Bean2DictionaryConverter
87%
58/66
77%
34/44
5
 
 1  
 /*
 2  
  * Copyright 2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 1.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.opensource.org/licenses/ecl1.php
 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.kuali.student.datadictionary.util;
 17  
 
 18  
 import java.beans.BeanInfo;
 19  
 import java.beans.IntrospectionException;
 20  
 import java.beans.Introspector;
 21  
 import java.beans.PropertyDescriptor;
 22  
 import java.util.Date;
 23  
 import java.util.List;
 24  
 import java.util.Stack;
 25  
 import org.kuali.rice.kns.datadictionary.AttributeDefinition;
 26  
 import org.kuali.rice.kns.datadictionary.DataObjectEntry;
 27  
 import org.kuali.rice.kns.datadictionary.validation.DataType;
 28  
 
 29  
 public class Bean2DictionaryConverter {
 30  
 
 31  
     private Class<?> clazz;
 32  
     private Stack<AttributeDefinition> parents;
 33  
 
 34  14
     public Bean2DictionaryConverter(Class<?> clazz, Stack<AttributeDefinition> parents) {
 35  14
         this.clazz = clazz;
 36  14
         this.parents = parents;
 37  14
     }
 38  
 
 39  
     public DataObjectEntry convert() {
 40  4
         DataObjectEntry ode = new DataObjectEntry();
 41  4
         ode.setObjectClass(clazz);
 42  4
         addAttributeDefinitions(ode);
 43  4
         return ode;
 44  
     }
 45  
 
 46  
     public void addAttributeDefinitions(DataObjectEntry ode) {
 47  
         BeanInfo beanInfo;
 48  
         try {
 49  14
             beanInfo = Introspector.getBeanInfo(clazz);
 50  0
         } catch (IntrospectionException ex) {
 51  0
             throw new RuntimeException(ex);
 52  14
         }
 53  97
         for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
 54  83
             if (Class.class.equals(pd.getPropertyType())) {
 55  10
                 continue;
 56  
             }
 57  73
             Class<?> actualClass = calcActualClass(clazz, pd);
 58  73
             AttributeDefinition ad = calcAttributeDefinition(clazz, pd, actualClass);
 59  73
             ode.getAttributes().add(ad);
 60  73
             if (ad.getDataType().equals(DataType.COMPLEX)) {
 61  10
                 parents.push(ad);
 62  10
                 Bean2DictionaryConverter subConverter = new Bean2DictionaryConverter(actualClass, parents);
 63  10
                 subConverter.addAttributeDefinitions(ode);
 64  10
                 parents.pop();
 65  
             }
 66  
         }
 67  14
     }
 68  
 
 69  
     private AttributeDefinition calcAttributeDefinition(Class<?> clazz, PropertyDescriptor pd, Class<?> actualClass) {
 70  73
         AttributeDefinition ad = new AttributeDefinition();
 71  73
         ad.setName(calcName(pd.getName()));
 72  73
         Class<?> pt = pd.getPropertyType();
 73  73
         if (List.class.equals(pt)) {
 74  
 //            TODO: fix this to use a CollectionDefinition
 75  
 //            ad.setMaxOccurs(DictionaryConstants.UNBOUNDED);
 76  4
             ad.setDataType(calcDataType(actualClass));
 77  
         } else {
 78  
 //            ad.setMaxOccurs(DictionaryConstants.SINGLE);
 79  69
             ad.setDataType(calcDataType(actualClass));
 80  
         }
 81  73
         return ad;
 82  
     }
 83  
 
 84  
     private String calcName(String leafName) {
 85  73
         StringBuilder bldr = new StringBuilder();
 86  73
         for (AttributeDefinition parent : parents) {
 87  36
             bldr.append(parent.getName());
 88  36
             bldr.append(".");
 89  
         }
 90  73
         bldr.append(initLower(leafName));
 91  73
         return bldr.toString();
 92  
     }
 93  
 
 94  
     private String initLower(String name) {
 95  73
         return name.substring(0, 1).toLowerCase() + name.substring(1);
 96  
     }
 97  
 
 98  
     public static Class<?> calcActualClass(Class<?> clazz, PropertyDescriptor pd) {
 99  423
         Class<?> pt = pd.getPropertyType();
 100  423
         if (List.class.equals(pt)) {
 101  40
             pt = ComplexSubstructuresHelper.getActualClassFromList(clazz, pd.getName());
 102  
         }
 103  423
         return pt;
 104  
     }
 105  
 
 106  
     private DataType calcDataType(Class<?> pt) {
 107  73
         return calcDataType(clazz, pt);
 108  
     }
 109  
 
 110  
     public static DataType calcDataType(Class<?> clazz, Class<?> pt) {
 111  248
         if (int.class.equals(pt) || Integer.class.equals(pt)) {
 112  5
             return DataType.INTEGER;
 113  243
         } else if (long.class.equals(pt) || Long.class.equals(pt)) {
 114  0
             return DataType.LONG;
 115  243
         } else if (double.class.equals(pt) || Double.class.equals(pt)) {
 116  0
             return DataType.DOUBLE;
 117  243
         } else if (float.class.equals(pt) || Float.class.equals(pt)) {
 118  0
             return DataType.FLOAT;
 119  243
         } else if (boolean.class.equals(pt) || Boolean.class.equals(pt)) {
 120  7
             return DataType.BOOLEAN;
 121  236
         } else if (Date.class.equals(pt)) {
 122  54
             return DataType.DATE;
 123  182
         } else if (String.class.equals(pt)) {
 124  136
             return DataType.STRING;
 125  46
         } else if (List.class.equals(pt)) {
 126  0
             throw new RuntimeException("Can't have a list of lists, List<List<?>>");
 127  46
         } else if (Enum.class.isAssignableFrom(pt)) {
 128  0
             return DataType.STRING;
 129  46
         } else if (Object.class.equals(pt)) {
 130  1
             return DataType.STRING;
 131  45
         } else if (pt.getName().startsWith("org.kuali.student.")) {
 132  45
             return DataType.COMPLEX;
 133  
         } else {
 134  0
             throw new RuntimeException("unknown/unhandled type of object in bean " + clazz.getSimpleName() + "." + pt.getName());
 135  
         }
 136  
     }
 137  
 }
 138