1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.lifecycle.model;
17
18 import java.lang.annotation.Annotation;
19 import java.util.Map;
20 import java.util.Map.Entry;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.kuali.rice.krad.uif.component.ClientSideState;
24 import org.kuali.rice.krad.uif.component.Component;
25 import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
26 import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleTaskBase;
27 import org.kuali.rice.krad.uif.util.CopyUtils;
28 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
29 import org.kuali.rice.krad.uif.view.View;
30 import org.kuali.rice.krad.uif.view.ViewModel;
31
32
33
34
35
36
37 public class SyncClientSideStateTask extends ViewLifecycleTaskBase<Component> {
38
39
40
41
42 public SyncClientSideStateTask() {
43 super(Component.class);
44 }
45
46
47
48
49
50
51
52 @Override
53 protected void performLifecycleTask() {
54 Component component = (Component) getElementState().getElement();
55 ViewModel model = (ViewModel) ViewLifecycle.getModel();
56
57
58 Map<String, Object> clientSideState = model.getClientStateForSyncing();
59 if (!(component instanceof View) && clientSideState.containsKey(component.getId())) {
60 @SuppressWarnings("unchecked")
61 Map<String, Object> componentState =
62 (Map<String, Object>) clientSideState.get(component.getId());
63 clientSideState = componentState;
64 }
65
66
67 if ((clientSideState != null) && (!clientSideState.isEmpty())) {
68 Map<String, Annotation> annotatedFields = CopyUtils.getFieldsWithAnnotation(component.getClass(),
69 ClientSideState.class);
70
71 for (Entry<String, Annotation> annotatedField : annotatedFields.entrySet()) {
72 ClientSideState clientSideStateAnnot = (ClientSideState) annotatedField.getValue();
73
74 String variableName = clientSideStateAnnot.variableName();
75 if (StringUtils.isBlank(variableName)) {
76 variableName = annotatedField.getKey();
77 }
78
79 if (clientSideState.containsKey(variableName)) {
80 Object value = clientSideState.get(variableName);
81 ObjectPropertyUtils.setPropertyValue(component, annotatedField.getKey(), value);
82 }
83 }
84 }
85 }
86
87 }