| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.krad.uif.modifier; |
| 17 | |
|
| 18 | |
import org.apache.commons.lang.StringUtils; |
| 19 | |
import org.apache.log4j.Logger; |
| 20 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
| 21 | |
import org.kuali.rice.krad.uif.UifConstants; |
| 22 | |
import org.kuali.rice.krad.uif.UifPropertyPaths; |
| 23 | |
import org.kuali.rice.krad.uif.container.Group; |
| 24 | |
import org.kuali.rice.krad.uif.field.DataField; |
| 25 | |
import org.kuali.rice.krad.uif.view.View; |
| 26 | |
import org.kuali.rice.krad.uif.component.Component; |
| 27 | |
import org.kuali.rice.krad.uif.field.HeaderField; |
| 28 | |
import org.kuali.rice.krad.uif.util.ComponentUtils; |
| 29 | |
import org.kuali.rice.krad.uif.util.ObjectPropertyUtils; |
| 30 | |
|
| 31 | |
import java.util.ArrayList; |
| 32 | |
import java.util.HashMap; |
| 33 | |
import java.util.HashSet; |
| 34 | |
import java.util.List; |
| 35 | |
import java.util.Map; |
| 36 | |
import java.util.Set; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
public class CompareFieldCreateModifier extends ComponentModifierBase { |
| 54 | 0 | private static final Logger LOG = Logger.getLogger(CompareFieldCreateModifier.class); |
| 55 | |
|
| 56 | |
private static final long serialVersionUID = -6285531580512330188L; |
| 57 | |
|
| 58 | |
private int defaultOrderSequence; |
| 59 | |
private boolean generateCompareHeaders; |
| 60 | |
|
| 61 | |
private HeaderField headerFieldPrototype; |
| 62 | |
private List<ComparableInfo> comparables; |
| 63 | |
|
| 64 | 0 | public CompareFieldCreateModifier() { |
| 65 | 0 | defaultOrderSequence = 1; |
| 66 | 0 | generateCompareHeaders = true; |
| 67 | |
|
| 68 | 0 | comparables = new ArrayList<ComparableInfo>(); |
| 69 | 0 | } |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
@Override |
| 78 | |
public void performInitialization(View view, Object model, Component component) { |
| 79 | 0 | super.performInitialization(view, model, component); |
| 80 | |
|
| 81 | 0 | if (headerFieldPrototype != null) { |
| 82 | 0 | view.getViewHelperService().performComponentInitialization(view, model, headerFieldPrototype); |
| 83 | |
} |
| 84 | 0 | } |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
@SuppressWarnings("unchecked") |
| 104 | |
@Override |
| 105 | |
public void performModification(View view, Object model, Component component) { |
| 106 | 0 | if ((component != null) && !(component instanceof Group)) { |
| 107 | 0 | throw new IllegalArgumentException( |
| 108 | |
"Compare field initializer only support Group components, found type: " + component.getClass()); |
| 109 | |
} |
| 110 | |
|
| 111 | 0 | if (component == null) { |
| 112 | 0 | return; |
| 113 | |
} |
| 114 | |
|
| 115 | |
|
| 116 | 0 | List<Component> comparisonItems = new ArrayList<Component>(); |
| 117 | |
|
| 118 | |
|
| 119 | 0 | List<ComparableInfo> groupComparables = (List<ComparableInfo>) ComponentUtils.sort(comparables, |
| 120 | |
defaultOrderSequence); |
| 121 | |
|
| 122 | |
|
| 123 | 0 | Map<String, Object> context = new HashMap<String, Object>(); |
| 124 | 0 | context.putAll(view.getContext()); |
| 125 | 0 | context.put(UifConstants.ContextVariableNames.COMPONENT, component); |
| 126 | |
|
| 127 | 0 | for (ComparableInfo comparable : groupComparables) { |
| 128 | 0 | KRADServiceLocatorWeb.getExpressionEvaluatorService().evaluateObjectExpressions(comparable, model, |
| 129 | |
context); |
| 130 | |
} |
| 131 | |
|
| 132 | |
|
| 133 | 0 | if (isGenerateCompareHeaders()) { |
| 134 | 0 | for (ComparableInfo comparable : groupComparables) { |
| 135 | 0 | HeaderField compareHeaderField = ComponentUtils.copy(headerFieldPrototype, comparable.getIdSuffix()); |
| 136 | 0 | compareHeaderField.setHeaderText(comparable.getHeaderText()); |
| 137 | |
|
| 138 | 0 | comparisonItems.add(compareHeaderField); |
| 139 | 0 | } |
| 140 | |
} |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | 0 | boolean performValueChangeComparison = false; |
| 145 | 0 | String compareValueObjectBindingPath = null; |
| 146 | 0 | for (ComparableInfo comparable : groupComparables) { |
| 147 | 0 | if (comparable.isCompareToForValueChange()) { |
| 148 | 0 | performValueChangeComparison = true; |
| 149 | 0 | compareValueObjectBindingPath = comparable.getBindingObjectPath(); |
| 150 | |
} |
| 151 | |
} |
| 152 | |
|
| 153 | |
|
| 154 | 0 | Group group = (Group) component; |
| 155 | 0 | for (Component item : group.getItems()) { |
| 156 | 0 | int defaultSuffix = 0; |
| 157 | 0 | for (ComparableInfo comparable : groupComparables) { |
| 158 | 0 | String idSuffix = comparable.getIdSuffix(); |
| 159 | 0 | if (StringUtils.isBlank(idSuffix)) { |
| 160 | 0 | idSuffix = UifConstants.IdSuffixes.COMPARE + defaultSuffix; |
| 161 | |
} |
| 162 | |
|
| 163 | 0 | Component compareItem = ComponentUtils.copy(item, idSuffix); |
| 164 | |
|
| 165 | 0 | ComponentUtils.setComponentPropertyDeep(compareItem, UifPropertyPaths.BIND_OBJECT_PATH, |
| 166 | |
comparable.getBindingObjectPath()); |
| 167 | 0 | if (comparable.isReadOnly()) { |
| 168 | 0 | compareItem.setReadOnly(true); |
| 169 | 0 | if (compareItem.getPropertyExpressions().containsKey("readOnly")) { |
| 170 | 0 | compareItem.getPropertyExpressions().remove("readOnly"); |
| 171 | |
} |
| 172 | |
} |
| 173 | |
|
| 174 | |
|
| 175 | 0 | if (performValueChangeComparison && comparable.isHighlightValueChange() && !comparable |
| 176 | |
.isCompareToForValueChange()) { |
| 177 | 0 | performValueComparison(group, compareItem, model, compareValueObjectBindingPath); |
| 178 | |
} |
| 179 | |
|
| 180 | 0 | comparisonItems.add(compareItem); |
| 181 | 0 | defaultSuffix++; |
| 182 | 0 | } |
| 183 | 0 | } |
| 184 | |
|
| 185 | |
|
| 186 | 0 | group.setItems(comparisonItems); |
| 187 | 0 | } |
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
protected void performValueComparison(Group group, Component compareItem, Object model, |
| 200 | |
String compareValueObjectBindingPath) { |
| 201 | |
|
| 202 | 0 | List<DataField> itemFields = ComponentUtils.getComponentsOfTypeDeep(compareItem, DataField.class); |
| 203 | 0 | for (DataField field : itemFields) { |
| 204 | 0 | String fieldBindingPath = field.getBindingInfo().getBindingPath(); |
| 205 | 0 | Object fieldValue = ObjectPropertyUtils.getPropertyValue(model, fieldBindingPath); |
| 206 | |
|
| 207 | 0 | String compareBindingPath = StringUtils.replaceOnce(fieldBindingPath, |
| 208 | |
field.getBindingInfo().getBindingObjectPath(), compareValueObjectBindingPath); |
| 209 | 0 | Object compareValue = ObjectPropertyUtils.getPropertyValue(model, compareBindingPath); |
| 210 | |
|
| 211 | 0 | boolean valueChanged = false; |
| 212 | 0 | if (!((fieldValue == null) && (compareValue == null))) { |
| 213 | |
|
| 214 | 0 | if ((fieldValue == null) || (compareValue == null)) { |
| 215 | 0 | valueChanged = true; |
| 216 | |
} else { |
| 217 | |
|
| 218 | 0 | valueChanged = !fieldValue.equals(compareValue); |
| 219 | |
} |
| 220 | |
} |
| 221 | |
|
| 222 | |
|
| 223 | 0 | if (valueChanged) { |
| 224 | 0 | String onReadyScript = "showChangeIcon('" + field.getId() + "');"; |
| 225 | |
|
| 226 | |
|
| 227 | 0 | Component headerField = group.getHeader(); |
| 228 | 0 | onReadyScript += "showChangeIconOnHeader('" + headerField.getId() + "');"; |
| 229 | |
|
| 230 | 0 | field.setOnDocumentReadyScript(onReadyScript); |
| 231 | |
} |
| 232 | |
|
| 233 | |
|
| 234 | 0 | } |
| 235 | 0 | } |
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
protected String getIdSuffix(ComparableInfo comparable, int index) { |
| 252 | 0 | String idSuffix = comparable.getIdSuffix(); |
| 253 | 0 | if (StringUtils.isBlank(idSuffix)) { |
| 254 | 0 | idSuffix = "_" + index; |
| 255 | |
} |
| 256 | |
|
| 257 | 0 | return idSuffix; |
| 258 | |
} |
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
@Override |
| 264 | |
public Set<Class<? extends Component>> getSupportedComponents() { |
| 265 | 0 | Set<Class<? extends Component>> components = new HashSet<Class<? extends Component>>(); |
| 266 | 0 | components.add(Group.class); |
| 267 | |
|
| 268 | 0 | return components; |
| 269 | |
} |
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
public List<Component> getComponentPrototypes() { |
| 275 | 0 | List<Component> components = new ArrayList<Component>(); |
| 276 | |
|
| 277 | 0 | components.add(headerFieldPrototype); |
| 278 | |
|
| 279 | 0 | return components; |
| 280 | |
} |
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
public int getDefaultOrderSequence() { |
| 290 | 0 | return this.defaultOrderSequence; |
| 291 | |
} |
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
public void setDefaultOrderSequence(int defaultOrderSequence) { |
| 299 | 0 | this.defaultOrderSequence = defaultOrderSequence; |
| 300 | 0 | } |
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
public boolean isGenerateCompareHeaders() { |
| 316 | 0 | return this.generateCompareHeaders; |
| 317 | |
} |
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
public void setGenerateCompareHeaders(boolean generateCompareHeaders) { |
| 325 | 0 | this.generateCompareHeaders = generateCompareHeaders; |
| 326 | 0 | } |
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
public HeaderField getHeaderFieldPrototype() { |
| 335 | 0 | return this.headerFieldPrototype; |
| 336 | |
} |
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
public void setHeaderFieldPrototype(HeaderField headerFieldPrototype) { |
| 344 | 0 | this.headerFieldPrototype = headerFieldPrototype; |
| 345 | 0 | } |
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
public List<ComparableInfo> getComparables() { |
| 359 | 0 | return this.comparables; |
| 360 | |
} |
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
|
| 366 | |
|
| 367 | |
public void setComparables(List<ComparableInfo> comparables) { |
| 368 | 0 | this.comparables = comparables; |
| 369 | 0 | } |
| 370 | |
|
| 371 | |
} |