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