1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.lifecycle;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.uif.UifConstants;
20 import org.kuali.rice.krad.uif.component.Component;
21 import org.kuali.rice.krad.uif.container.CollectionGroup;
22 import org.kuali.rice.krad.uif.util.ComponentUtils;
23 import org.kuali.rice.krad.uif.util.LifecycleElement;
24 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
25 import org.kuali.rice.krad.uif.view.ViewIndex;
26
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33
34
35
36
37
38
39
40 public class LifecycleRefreshPathBuilder {
41
42
43
44
45
46 public static void processLifecycleElements() {
47 ViewIndex viewIndex = ViewLifecycle.getView().getViewIndex();
48
49 Map<String, LifecycleElement> lifecycleElements = viewIndex.getLifecycleElementsByPath();
50 for (LifecycleElement lifecycleElement : lifecycleElements.values()) {
51 processLifecycleElement(lifecycleElement);
52 }
53 }
54
55
56
57
58
59
60
61 protected static void processLifecycleElement(LifecycleElement element) {
62 if ((element == null) || !(element instanceof Component)) {
63 return;
64 }
65
66 Component component = (Component) element;
67 if (ComponentUtils.canBeRefreshed(component) || (component instanceof CollectionGroup) ||
68 component.isForceSessionPersistence()) {
69 ViewPostMetadata viewPostMetadata = ViewLifecycle.getViewPostMetadata();
70
71 ComponentPostMetadata componentPostMetadata = viewPostMetadata.getComponentPostMetadata(component.getId());
72 if (componentPostMetadata == null) {
73 componentPostMetadata = viewPostMetadata.initializeComponentPostMetadata(component);
74 }
75
76 componentPostMetadata.setPath(component.getViewPath());
77
78 buildRefreshPathMappings(component, componentPostMetadata);
79
80 storePhasePathMapping(component, componentPostMetadata);
81 }
82 }
83
84
85
86
87
88
89
90
91
92
93 protected static void buildRefreshPathMappings(LifecycleElement lifecycleElement,
94 ComponentPostMetadata componentPostMetadata) {
95 List<String> initializePaths = new ArrayList<String>();
96 List<String> applyModelPaths = new ArrayList<String>();
97 List<String> finalizePaths = new ArrayList<String>();
98
99 String refreshElementPath = lifecycleElement.getViewPath();
100 processElementPath(refreshElementPath, initializePaths, applyModelPaths, finalizePaths, new HashSet<String>());
101
102 Map<String, List<String>> refreshPathMappings = new HashMap<String, List<String>>();
103
104 refreshPathMappings.put(UifConstants.ViewPhases.INITIALIZE, initializePaths);
105 refreshPathMappings.put(UifConstants.ViewPhases.APPLY_MODEL, applyModelPaths);
106 refreshPathMappings.put(UifConstants.ViewPhases.FINALIZE, finalizePaths);
107
108 componentPostMetadata.setRefreshPathMappings(refreshPathMappings);
109 }
110
111
112
113
114
115
116
117 protected static void storePhasePathMapping(LifecycleElement lifecycleElement,
118 ComponentPostMetadata componentPostMetadata) {
119 Map<String, String> storedPhasePathMapping = new HashMap<String, String>();
120
121 String refreshElementPath = lifecycleElement.getViewPath();
122
123 Map<String, String> phasePathMapping = lifecycleElement.getPhasePathMapping();
124 for (Map.Entry<String, String> phasePathEntry : phasePathMapping.entrySet()) {
125 if (!StringUtils.equals(phasePathEntry.getValue(), refreshElementPath)) {
126 storedPhasePathMapping.put(phasePathEntry.getKey(), phasePathEntry.getValue());
127 }
128 }
129
130 if (!storedPhasePathMapping.isEmpty()) {
131 componentPostMetadata.setPhasePathMapping(storedPhasePathMapping);
132 }
133 }
134
135
136
137
138
139
140
141
142
143
144
145 protected static void processElementPath(String path, List<String> initializePaths, List<String> applyModelPaths,
146 List<String> finalizePaths, Set<String> visitedPaths) {
147 String[] parentProperties = ObjectPropertyUtils.splitPropertyPath(path);
148
149 String previousPath = null;
150 for (int i = 0; i < parentProperties.length; i++) {
151 String parentPath = parentProperties[i];
152 if (previousPath != null) {
153 parentPath = previousPath + "." + parentPath;
154 }
155
156 if (!visitedPaths.contains(parentPath)) {
157 visitedPaths.add(parentPath);
158
159 addElementRefreshPaths(parentPath, initializePaths, applyModelPaths, finalizePaths, visitedPaths);
160 }
161
162 previousPath = parentPath;
163 }
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179 protected static void addElementRefreshPaths(String elementPath, List<String> initializePaths,
180 List<String> applyModelPaths, List<String> finalizePaths, Set<String> visitedPaths) {
181 LifecycleElement lifecycleElement = ViewLifecycle.getView().getViewIndex().getLifecycleElementByPath(
182 elementPath);
183 if (lifecycleElement == null) {
184 return;
185 }
186
187 Map<String, String> phasePathMapping = lifecycleElement.getPhasePathMapping();
188 for (Map.Entry<String, String> phasePathEntry : phasePathMapping.entrySet()) {
189 String refreshPath = phasePathEntry.getValue();
190
191 addElementRefreshPath(refreshPath, phasePathEntry.getKey(), initializePaths, applyModelPaths,
192 finalizePaths);
193
194
195 if (StringUtils.equals(elementPath, refreshPath)) {
196 processElementPath(refreshPath, initializePaths, applyModelPaths, finalizePaths, visitedPaths);
197 }
198 }
199 }
200
201
202
203
204
205
206
207
208
209
210 protected static void addElementRefreshPath(String path, String phase, List<String> initializePaths,
211 List<String> applyModelPaths, List<String> finalizePaths) {
212 if (UifConstants.ViewPhases.INITIALIZE.equals(phase)) {
213 initializePaths.add(path);
214 } else if (UifConstants.ViewPhases.APPLY_MODEL.equals(phase)) {
215 applyModelPaths.add(path);
216 } else if (UifConstants.ViewPhases.FINALIZE.equals(phase)) {
217 finalizePaths.add(path);
218 }
219 }
220
221 }