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 java.util.List;
19 import java.util.Map;
20
21 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
22 import org.kuali.rice.krad.uif.UifConstants;
23 import org.kuali.rice.krad.uif.container.PageGroup;
24 import org.kuali.rice.krad.uif.service.ViewHelperService;
25 import org.kuali.rice.krad.uif.util.ProcessLogger;
26 import org.kuali.rice.krad.uif.view.View;
27 import org.kuali.rice.krad.uif.view.ViewModel;
28 import org.kuali.rice.krad.web.form.UifFormBase;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33
34
35
36
37 public class ViewLifecycleBuild implements Runnable {
38 private static final Logger LOG = LoggerFactory.getLogger(ViewLifecycleBuild.class);
39
40 private final Map<String, String> parameters;
41 private final Map<String, List<String>> refreshPathMappings;
42
43
44
45
46
47
48
49
50
51 public ViewLifecycleBuild(Map<String, String> parameters, Map<String, List<String>> refreshPathMappings) {
52 this.parameters = parameters;
53 this.refreshPathMappings = refreshPathMappings;
54 }
55
56
57
58
59 @Override
60 public void run() {
61 View view = ViewLifecycle.getView();
62
63 ProcessLogger.trace("begin-view-lifecycle:" + view.getId());
64
65 populateViewRequestParameters();
66
67 runInitializePhase();
68
69 runApplyModelPhase();
70
71 runFinalizePhase();
72
73
74 ((ViewModel) ViewLifecycle.getModel()).setApplyDefaultValues(false);
75
76
77 String growlScript = ViewLifecycle.getHelper().buildGrowlScript();
78 ((ViewModel) ViewLifecycle.getModel()).setGrowlScript(growlScript);
79
80
81 if (ViewLifecycle.isRefreshLifecycle()) {
82 PageGroup page = view.getCurrentPage();
83 page.getValidationMessages().generateMessages(view, ViewLifecycle.getModel(), page);
84 }
85
86 LifecycleRefreshPathBuilder.processLifecycleElements();
87
88 ViewLifecycle.getViewPostMetadata().cleanAfterLifecycle();
89
90 ProcessLogger.trace("finalize:" + view.getId());
91 }
92
93
94
95
96
97 protected void populateViewRequestParameters() {
98 View view = ViewLifecycle.getView();
99 ViewHelperService helper = ViewLifecycle.getHelper();
100 UifFormBase model = (UifFormBase) ViewLifecycle.getModel();
101
102
103
104 Map<String, String> parametersToPopulate = parameters;
105 if (ViewLifecycle.isRefreshLifecycle()) {
106 parametersToPopulate = model.getViewRequestParameters();
107 }
108
109 helper.populateViewFromRequestParameters(parametersToPopulate);
110
111
112 model.setViewRequestParameters(view.getViewRequestParameters());
113 }
114
115
116
117
118
119
120
121 protected void runInitializePhase() {
122 ViewLifecycleProcessor processor = ViewLifecycle.getProcessor();
123
124 List<String> refreshPaths = null;
125 if (refreshPathMappings != null) {
126 refreshPaths = refreshPathMappings.get(UifConstants.ViewPhases.INITIALIZE);
127 }
128
129 ViewLifecyclePhase phase = KRADServiceLocatorWeb.getViewLifecyclePhaseBuilder().buildPhase(
130 ViewLifecycle.getView(), UifConstants.ViewPhases.INITIALIZE, refreshPaths);
131
132 View view = ViewLifecycle.getView();
133 ViewHelperService helper = ViewLifecycle.getHelper();
134 UifFormBase model = (UifFormBase) ViewLifecycle.getModel();
135
136 ViewLifecycle.getExpressionEvaluator().initializeEvaluationContext(model);
137
138 if (LOG.isInfoEnabled()) {
139 LOG.info("performing initialize phase for view: " + view.getId());
140 }
141
142 helper.performCustomViewInitialization(model);
143
144 processor.performPhase(phase);
145
146 ProcessLogger.trace("initialize:" + view.getId());
147 }
148
149
150
151
152
153
154
155 protected void runApplyModelPhase() {
156 ViewLifecycleProcessor processor = ViewLifecycle.getProcessor();
157
158 List<String> refreshPaths = null;
159 if (refreshPathMappings != null) {
160 refreshPaths = refreshPathMappings.get(UifConstants.ViewPhases.APPLY_MODEL);
161 }
162
163 ViewLifecyclePhase phase = KRADServiceLocatorWeb.getViewLifecyclePhaseBuilder().buildPhase(
164 ViewLifecycle.getView(), UifConstants.ViewPhases.APPLY_MODEL, refreshPaths);
165
166 View view = ViewLifecycle.getView();
167 ViewHelperService helper = ViewLifecycle.getHelper();
168 UifFormBase model = (UifFormBase) ViewLifecycle.getModel();
169
170 if (LOG.isInfoEnabled()) {
171 LOG.info("performing apply model phase for view: " + view.getId());
172 }
173
174
175
176 if(model.isApplyDefaultValues()) {
177 helper.applyDefaultValues(view);
178
179
180 model.setApplyDefaultValues(false);
181 }
182
183
184 helper.retrieveEditModesAndActionFlags();
185
186
187 helper.setViewContext();
188
189 processor.performPhase(phase);
190
191 ProcessLogger.trace("apply-model:" + view.getId());
192 }
193
194
195
196
197
198
199 protected void runFinalizePhase() {
200 ViewLifecycleProcessor processor = ViewLifecycle.getProcessor();
201
202 List<String> refreshPaths = null;
203 if (refreshPathMappings != null) {
204 refreshPaths = refreshPathMappings.get(UifConstants.ViewPhases.FINALIZE);
205 }
206
207 ViewLifecyclePhase phase = KRADServiceLocatorWeb.getViewLifecyclePhaseBuilder().buildPhase(
208 ViewLifecycle.getView(), UifConstants.ViewPhases.FINALIZE, refreshPaths);
209
210 View view = ViewLifecycle.getView();
211 if (LOG.isInfoEnabled()) {
212 LOG.info("performing finalize phase for view: " + view.getId());
213 }
214
215 processor.performPhase(phase);
216 }
217
218 }