Coverage Report - org.kuali.rice.kns.uif.util.ViewModelUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ViewModelUtils
0%
0/34
0%
0/24
4.75
 
 1  
 /*
 2  
  * Copyright 2007 The Kuali Foundation Licensed under the Educational Community
 3  
  * License, Version 1.0 (the "License"); you may not use this file except in
 4  
  * compliance with the License. You may obtain a copy of the License at
 5  
  * http://www.opensource.org/licenses/ecl1.php Unless required by applicable law
 6  
  * or agreed to in writing, software distributed under the License is
 7  
  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 8  
  * KIND, either express or implied. See the License for the specific language
 9  
  * governing permissions and limitations under the License.
 10  
  */
 11  
 package org.kuali.rice.kns.uif.util;
 12  
 
 13  
 import java.util.Map;
 14  
 
 15  
 import org.apache.commons.lang.StringUtils;
 16  
 import org.hibernate.mapping.Collection;
 17  
 import org.kuali.rice.kns.uif.container.View;
 18  
 import org.kuali.rice.kns.uif.field.AttributeField;
 19  
 
 20  
 /**
 21  
  * This is a description of what this class does - jkneal don't forget to fill
 22  
  * this in.
 23  
  * 
 24  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 25  
  */
 26  0
 public class ViewModelUtils {
 27  
 
 28  
     public static Class<?> getPropertyType(View view, String propertyPath) {
 29  0
         Class<?> propertyType = null;
 30  
 
 31  0
         if (StringUtils.isBlank(propertyPath)) {
 32  0
             return propertyType;
 33  
         }
 34  
 
 35  
         // TODO: make this do partial matching & collection matching
 36  
 
 37  
         // check if property path matches one of the modelClass entries
 38  0
         Map<String, Class<?>> modelClasses = view.getAbstractTypeClasses();
 39  0
         for (String path : modelClasses.keySet()) {
 40  0
             if (StringUtils.equals(path, propertyPath)) {
 41  0
                 propertyType = modelClasses.get(path);
 42  
             }
 43  
         }
 44  
 
 45  
         // if not found in the view's map, get the type based on the form
 46  0
         if (propertyType == null) {
 47  0
             propertyType = ObjectPropertyUtils.getPropertyType(view.getFormClass(), propertyPath);
 48  
         }
 49  
 
 50  0
         return propertyType;
 51  
     }
 52  
 
 53  
     public static String getParentObjectPath(AttributeField field) {
 54  0
         String parentObjectPath = "";
 55  
 
 56  0
         String objectPath = field.getBindingInfo().getBindingObjectPath();
 57  0
         String propertyPrefix = field.getBindingInfo().getBindByNamePrefix();
 58  
 
 59  0
         if (!field.getBindingInfo().isBindToForm() && StringUtils.isNotBlank(objectPath)) {
 60  0
             parentObjectPath = objectPath;
 61  
         }
 62  
 
 63  0
         if (StringUtils.isNotBlank(propertyPrefix)) {
 64  0
             if (StringUtils.isNotBlank(parentObjectPath)) {
 65  0
                 parentObjectPath += ".";
 66  
             }
 67  
 
 68  0
             parentObjectPath += propertyPrefix;
 69  
         }
 70  
 
 71  0
         return parentObjectPath;
 72  
     }
 73  
 
 74  
     public static Class<?> getParentObjectClassForMetadata(View view, AttributeField field) {
 75  0
         String parentObjectPath = getParentObjectPath(field);
 76  
 
 77  0
         return getPropertyType(view, parentObjectPath);
 78  
     }
 79  
 
 80  
     public static Object getParentObjectForMetadata(View view, Object model, AttributeField field) {
 81  
         // default to model as parent
 82  0
         Object parentObject = model;
 83  
 
 84  0
         String parentObjectPath = getParentObjectPath(field);
 85  0
         if (StringUtils.isNotBlank(parentObjectPath)) {
 86  0
             parentObject = ObjectPropertyUtils.getPropertyValue(model, parentObjectPath);
 87  
 
 88  
             // attempt to create new instance if parent is null or is a
 89  
             // collection or map
 90  0
             if ((parentObject == null) || Collection.class.isAssignableFrom(parentObject.getClass())
 91  
                     || Map.class.isAssignableFrom(parentObject.getClass())) {
 92  
                 try {
 93  0
                     Class<?> parentObjectClass = getPropertyType(view, parentObjectPath);
 94  0
                     parentObject = parentObjectClass.newInstance();
 95  
                 }
 96  0
                 catch (InstantiationException e) {
 97  
                     // swallow exception and let null be returned
 98  
                 }
 99  0
                 catch (IllegalAccessException e) {
 100  
                     // swallow exception and let null be returned
 101  0
                 }
 102  
             }
 103  
         }
 104  
 
 105  0
         return parentObject;
 106  
     }
 107  
 
 108  
 }