| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 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 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 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 | |
|
| 36 | |
|
| 37 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 89 | |
|
| 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 | |
|
| 98 | |
} |
| 99 | 0 | catch (IllegalAccessException e) { |
| 100 | |
|
| 101 | 0 | } |
| 102 | |
} |
| 103 | |
} |
| 104 | |
|
| 105 | 0 | return parentObject; |
| 106 | |
} |
| 107 | |
|
| 108 | |
} |