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