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