| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
package org.kuali.rice.kns.uif.util; |
| 12 | |
|
| 13 | |
import java.util.Collection; |
| 14 | |
import java.util.Map; |
| 15 | |
|
| 16 | |
import org.apache.commons.lang.StringUtils; |
| 17 | |
import org.kuali.rice.kns.uif.container.View; |
| 18 | |
import org.kuali.rice.kns.uif.core.Component; |
| 19 | |
import org.kuali.rice.kns.uif.core.BindingInfo; |
| 20 | |
import org.kuali.rice.kns.uif.field.AttributeField; |
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | 0 | public class ViewModelUtils { |
| 41 | |
|
| 42 | |
public static Class<?> getPropertyType(View view, String propertyPath) { |
| 43 | 0 | Class<?> propertyType = null; |
| 44 | |
|
| 45 | 0 | if (StringUtils.isBlank(propertyPath)) { |
| 46 | 0 | return propertyType; |
| 47 | |
} |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | 0 | Class<?> modelClass = view.getFormClass(); |
| 52 | 0 | String modelProperty = propertyPath; |
| 53 | |
|
| 54 | 0 | int bestMatchLength = 0; |
| 55 | |
|
| 56 | |
|
| 57 | 0 | String flattenedPropertyPath = propertyPath.replaceAll("\\[.+\\]", ""); |
| 58 | |
|
| 59 | |
|
| 60 | 0 | Map<String, Class<?>> modelClasses = view.getAbstractTypeClasses(); |
| 61 | 0 | for (String path : modelClasses.keySet()) { |
| 62 | |
|
| 63 | 0 | if (StringUtils.equals(path, flattenedPropertyPath)) { |
| 64 | 0 | propertyType = modelClasses.get(path); |
| 65 | 0 | break; |
| 66 | |
} |
| 67 | |
|
| 68 | |
|
| 69 | 0 | if (flattenedPropertyPath.startsWith(path) && (path.length() > bestMatchLength)) { |
| 70 | 0 | bestMatchLength = path.length(); |
| 71 | |
|
| 72 | 0 | modelClass = modelClasses.get(path); |
| 73 | 0 | modelProperty = StringUtils.removeStart(flattenedPropertyPath, path); |
| 74 | 0 | modelProperty = StringUtils.removeStart(modelProperty, "."); |
| 75 | |
} |
| 76 | |
} |
| 77 | |
|
| 78 | |
|
| 79 | 0 | if (propertyType == null) { |
| 80 | 0 | propertyType = ObjectPropertyUtils.getPropertyType(modelClass, modelProperty); |
| 81 | |
} |
| 82 | |
|
| 83 | 0 | return propertyType; |
| 84 | |
} |
| 85 | |
|
| 86 | |
public static String getParentObjectPath(AttributeField field) { |
| 87 | 0 | String parentObjectPath = ""; |
| 88 | |
|
| 89 | 0 | String objectPath = field.getBindingInfo().getBindingObjectPath(); |
| 90 | 0 | String propertyPrefix = field.getBindingInfo().getBindByNamePrefix(); |
| 91 | |
|
| 92 | 0 | if (!field.getBindingInfo().isBindToForm() && StringUtils.isNotBlank(objectPath)) { |
| 93 | 0 | parentObjectPath = objectPath; |
| 94 | |
} |
| 95 | |
|
| 96 | 0 | if (StringUtils.isNotBlank(propertyPrefix)) { |
| 97 | 0 | if (StringUtils.isNotBlank(parentObjectPath)) { |
| 98 | 0 | parentObjectPath += "."; |
| 99 | |
} |
| 100 | |
|
| 101 | 0 | parentObjectPath += propertyPrefix; |
| 102 | |
} |
| 103 | |
|
| 104 | 0 | return parentObjectPath; |
| 105 | |
} |
| 106 | |
|
| 107 | |
public static Class<?> getParentObjectClassForMetadata(View view, AttributeField field) { |
| 108 | 0 | String parentObjectPath = getParentObjectPath(field); |
| 109 | |
|
| 110 | 0 | return getPropertyType(view, parentObjectPath); |
| 111 | |
} |
| 112 | |
|
| 113 | |
public static Object getParentObjectForMetadata(View view, Object model, AttributeField field) { |
| 114 | |
|
| 115 | 0 | Object parentObject = model; |
| 116 | |
|
| 117 | 0 | String parentObjectPath = getParentObjectPath(field); |
| 118 | 0 | if (StringUtils.isNotBlank(parentObjectPath)) { |
| 119 | 0 | parentObject = ObjectPropertyUtils.getPropertyValue(model, parentObjectPath); |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | 0 | if ((parentObject == null) || Collection.class.isAssignableFrom(parentObject.getClass()) |
| 124 | |
|| Map.class.isAssignableFrom(parentObject.getClass())) { |
| 125 | |
try { |
| 126 | 0 | Class<?> parentObjectClass = getPropertyType(view, parentObjectPath); |
| 127 | 0 | parentObject = parentObjectClass.newInstance(); |
| 128 | |
} |
| 129 | 0 | catch (InstantiationException e) { |
| 130 | |
|
| 131 | |
} |
| 132 | 0 | catch (IllegalAccessException e) { |
| 133 | |
|
| 134 | 0 | } |
| 135 | |
} |
| 136 | |
} |
| 137 | |
|
| 138 | 0 | return parentObject; |
| 139 | |
} |
| 140 | |
|
| 141 | |
public static Object getValue(View view, Object model, String propertyName, BindingInfo bindingInfo){ |
| 142 | 0 | Object value = null; |
| 143 | 0 | if(bindingInfo == null && StringUtils.isNotBlank(propertyName)){ |
| 144 | 0 | if(StringUtils.isNotBlank(view.getDefaultBindingObjectPath())){ |
| 145 | 0 | value = ObjectPropertyUtils.getPropertyValue(model, view.getDefaultBindingObjectPath() + "." + propertyName); |
| 146 | |
} |
| 147 | |
else{ |
| 148 | 0 | value = ObjectPropertyUtils.getPropertyValue(model, propertyName); |
| 149 | |
} |
| 150 | |
|
| 151 | |
} |
| 152 | 0 | else if(bindingInfo != null){ |
| 153 | 0 | if(StringUtils.isNotBlank(bindingInfo.getBindingPath()) && !bindingInfo.getBindingPath().equals("null")){ |
| 154 | 0 | value = ObjectPropertyUtils.getPropertyValue(model, bindingInfo.getBindingPath()); |
| 155 | |
} |
| 156 | |
} |
| 157 | 0 | return value; |
| 158 | |
} |
| 159 | |
|
| 160 | |
} |