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 | 0 | public class ViewModelUtils { |
40 | |
|
41 | |
public static Class<?> getPropertyType(View view, String propertyPath) { |
42 | 0 | Class<?> propertyType = null; |
43 | |
|
44 | 0 | if (StringUtils.isBlank(propertyPath)) { |
45 | 0 | return propertyType; |
46 | |
} |
47 | |
|
48 | |
|
49 | |
|
50 | 0 | Class<?> modelClass = view.getFormClass(); |
51 | 0 | String modelProperty = propertyPath; |
52 | |
|
53 | 0 | int bestMatchLength = 0; |
54 | |
|
55 | |
|
56 | 0 | String flattenedPropertyPath = propertyPath.replaceAll("\\[.+\\]", ""); |
57 | |
|
58 | |
|
59 | 0 | Map<String, Class<?>> modelClasses = view.getAbstractTypeClasses(); |
60 | 0 | for (String path : modelClasses.keySet()) { |
61 | |
|
62 | 0 | if (StringUtils.equals(path, flattenedPropertyPath)) { |
63 | 0 | propertyType = modelClasses.get(path); |
64 | 0 | break; |
65 | |
} |
66 | |
|
67 | |
|
68 | 0 | if (flattenedPropertyPath.startsWith(path) && (path.length() > bestMatchLength)) { |
69 | 0 | bestMatchLength = path.length(); |
70 | |
|
71 | 0 | modelClass = modelClasses.get(path); |
72 | 0 | modelProperty = StringUtils.removeStart(flattenedPropertyPath, path); |
73 | 0 | modelProperty = StringUtils.removeStart(modelProperty, "."); |
74 | |
} |
75 | |
} |
76 | |
|
77 | |
|
78 | 0 | if (propertyType == null) { |
79 | 0 | propertyType = ObjectPropertyUtils.getPropertyType(modelClass, modelProperty); |
80 | |
} |
81 | |
|
82 | 0 | return propertyType; |
83 | |
} |
84 | |
|
85 | |
public static String getParentObjectPath(AttributeField field) { |
86 | 0 | String parentObjectPath = ""; |
87 | |
|
88 | 0 | String objectPath = field.getBindingInfo().getBindingObjectPath(); |
89 | 0 | String propertyPrefix = field.getBindingInfo().getBindByNamePrefix(); |
90 | |
|
91 | 0 | if (!field.getBindingInfo().isBindToForm() && StringUtils.isNotBlank(objectPath)) { |
92 | 0 | parentObjectPath = objectPath; |
93 | |
} |
94 | |
|
95 | 0 | if (StringUtils.isNotBlank(propertyPrefix)) { |
96 | 0 | if (StringUtils.isNotBlank(parentObjectPath)) { |
97 | 0 | parentObjectPath += "."; |
98 | |
} |
99 | |
|
100 | 0 | parentObjectPath += propertyPrefix; |
101 | |
} |
102 | |
|
103 | 0 | return parentObjectPath; |
104 | |
} |
105 | |
|
106 | |
public static Class<?> getParentObjectClassForMetadata(View view, AttributeField field) { |
107 | 0 | String parentObjectPath = getParentObjectPath(field); |
108 | |
|
109 | 0 | return getPropertyType(view, parentObjectPath); |
110 | |
} |
111 | |
|
112 | |
public static Object getParentObjectForMetadata(View view, Object model, AttributeField field) { |
113 | |
|
114 | 0 | Object parentObject = model; |
115 | |
|
116 | 0 | String parentObjectPath = getParentObjectPath(field); |
117 | 0 | if (StringUtils.isNotBlank(parentObjectPath)) { |
118 | 0 | parentObject = ObjectPropertyUtils.getPropertyValue(model, parentObjectPath); |
119 | |
|
120 | |
|
121 | |
|
122 | 0 | if ((parentObject == null) || Collection.class.isAssignableFrom(parentObject.getClass()) |
123 | |
|| Map.class.isAssignableFrom(parentObject.getClass())) { |
124 | |
try { |
125 | 0 | Class<?> parentObjectClass = getPropertyType(view, parentObjectPath); |
126 | 0 | parentObject = parentObjectClass.newInstance(); |
127 | |
} |
128 | 0 | catch (InstantiationException e) { |
129 | |
|
130 | |
} |
131 | 0 | catch (IllegalAccessException e) { |
132 | |
|
133 | 0 | } |
134 | |
} |
135 | |
} |
136 | |
|
137 | 0 | return parentObject; |
138 | |
} |
139 | |
|
140 | |
public static Object getValue(View view, Object model, String propertyName, BindingInfo bindingInfo){ |
141 | 0 | Object value = null; |
142 | 0 | if(bindingInfo == null && StringUtils.isNotBlank(propertyName)){ |
143 | 0 | if(StringUtils.isNotBlank(view.getDefaultBindingObjectPath())){ |
144 | 0 | value = ObjectPropertyUtils.getPropertyValue(model, view.getDefaultBindingObjectPath() + "." + propertyName); |
145 | |
} |
146 | |
else{ |
147 | 0 | value = ObjectPropertyUtils.getPropertyValue(model, propertyName); |
148 | |
} |
149 | |
|
150 | |
} |
151 | 0 | else if(bindingInfo != null){ |
152 | 0 | if(StringUtils.isNotBlank(bindingInfo.getBindingPath()) && !bindingInfo.getBindingPath().equals("null")){ |
153 | 0 | value = ObjectPropertyUtils.getPropertyValue(model, bindingInfo.getBindingPath()); |
154 | |
} |
155 | |
} |
156 | 0 | return value; |
157 | |
} |
158 | |
|
159 | |
} |