Coverage Report - org.kuali.student.enrollment.lpr.mock.DataProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
DataProvider
0%
0/33
0%
0/12
2.286
 
 1  
 /**
 2  
  * Copyright 2011 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 package org.kuali.student.enrollment.lpr.mock;
 16  
 
 17  
 import java.beans.BeanInfo;
 18  
 import java.beans.IntrospectionException;
 19  
 import java.beans.Introspector;
 20  
 import java.beans.PropertyDescriptor;
 21  
 import java.util.HashMap;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 import org.apache.log4j.Logger;
 25  
 import org.kuali.student.common.infc.AttributeInfc;
 26  
 
 27  
 /**
 28  
  * Provides values to compare
 29  
  * @author nwright
 30  
  */
 31  
 public class DataProvider implements DataProviderInfc {
 32  
 
 33  0
     final static Logger LOG = Logger.getLogger(DataProvider.class);
 34  
     private static final String DYNAMIC_ATTRIBUTE = "attributes";
 35  0
     Map<String, Object> dataMap = null;
 36  
 
 37  0
     public DataProvider() {
 38  0
     }
 39  
 
 40  
     //TODO fix it later.
 41  
     @Override
 42  
     public String getPath() {
 43  0
         return "";
 44  
     }
 45  
 
 46  
     @SuppressWarnings("unchecked")
 47  
     @Override
 48  
     public void initialize(Object o) {
 49  
 
 50  0
         dataMap = new HashMap<String, Object>();
 51  
 
 52  0
         Map<String, PropertyDescriptor> beanInfo = getBeanInfo(o.getClass());
 53  
 
 54  0
         for (String propName : beanInfo.keySet()) {
 55  
 
 56  0
             PropertyDescriptor pd = beanInfo.get(propName);
 57  0
             Object value = null;
 58  
             try {
 59  0
                 value = pd.getReadMethod().invoke(o);
 60  0
             } catch (Exception e) {
 61  0
                 LOG.warn("Method invokation failed", e);
 62  0
             }
 63  
 
 64  
             // Extract dynamic attributes
 65  0
             if (DYNAMIC_ATTRIBUTE.equals(propName)) {
 66  0
                 for (AttributeInfc attr: (List<AttributeInfc>) value)
 67  
                 {
 68  0
                  dataMap.put(attr.getKey(), attr.getValue());
 69  
                 }
 70  
             } else {
 71  0
                 dataMap.put(propName, value);
 72  
             }
 73  0
         }
 74  0
     }
 75  
 
 76  
     @Override
 77  
     public String getObjectId() {
 78  0
         return (dataMap.containsKey("id") && null != dataMap.get("id")) ? dataMap.get("id").toString() : null;
 79  
     }
 80  
 
 81  
     @Override
 82  
     public Object getValue(String fieldKey) {
 83  0
         return dataMap.get(fieldKey);
 84  
     }
 85  
 
 86  
     @Override
 87  
     public Boolean hasField(String fieldKey) {
 88  0
         return dataMap.containsKey(fieldKey);
 89  
     }
 90  
 
 91  
     private Map<String, PropertyDescriptor> getBeanInfo(Class<?> clazz) {
 92  0
         Map<String, PropertyDescriptor> properties = new HashMap<String, PropertyDescriptor>();
 93  0
         BeanInfo beanInfo = null;
 94  
         try {
 95  0
             beanInfo = Introspector.getBeanInfo(clazz);
 96  0
         } catch (IntrospectionException e) {
 97  0
             throw new RuntimeException(e);
 98  0
         }
 99  0
         PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
 100  0
         for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
 101  0
             properties.put(propertyDescriptor.getName(), propertyDescriptor);
 102  
         }
 103  0
         return properties;
 104  
     }
 105  
 }