1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.web.controller;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.kuali.rice.krad.service.KRADServiceLocator;
21 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
22 import org.kuali.rice.krad.uif.UifConstants;
23 import org.kuali.rice.krad.uif.UifParameters;
24 import org.kuali.rice.krad.uif.util.ComponentFactory;
25 import org.kuali.rice.krad.uif.view.History;
26 import org.kuali.rice.krad.uif.view.HistoryEntry;
27 import org.kuali.rice.krad.uif.view.View;
28 import org.kuali.rice.krad.uif.component.Component;
29 import org.kuali.rice.krad.uif.service.ViewService;
30 import org.kuali.rice.krad.util.KRADConstants;
31 import org.kuali.rice.krad.util.KRADUtils;
32 import org.kuali.rice.krad.web.form.UifFormBase;
33 import org.kuali.rice.krad.web.form.UifFormManager;
34 import org.springframework.web.servlet.ModelAndView;
35
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Map;
42
43
44
45
46
47
48
49
50
51
52 public class UifControllerHelper {
53 private static final Logger LOG = Logger.getLogger(UifControllerHelper.class);
54
55
56
57
58
59
60
61
62
63
64 public static ModelAndView getUIFModelAndView(UifFormBase form, String pageId) {
65 if (StringUtils.isNotBlank(pageId)) {
66 form.setPageId(pageId);
67 }
68
69
70 ModelAndView modelAndView = new ModelAndView();
71 modelAndView.addObject(UifConstants.DEFAULT_MODEL_NAME, form);
72 modelAndView.setViewName(UifConstants.SPRING_VIEW_ID);
73
74 return modelAndView;
75 }
76
77
78
79
80
81 public static void postControllerHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
82 ModelAndView modelAndView) throws Exception {
83 if (!(handler instanceof UifControllerBase) || (modelAndView == null)) {
84 return;
85 }
86 UifControllerBase controller = (UifControllerBase) handler;
87
88 Object model = modelAndView.getModelMap().get(UifConstants.DEFAULT_MODEL_NAME);
89 if (!(model instanceof UifFormBase)) {
90 return;
91 }
92 UifFormBase form = (UifFormBase) model;
93
94
95 if (!form.isRequestRedirected()) {
96
97 prepareViewForRendering(request, form);
98
99
100 Component component = null;
101 if (form.isUpdateComponentRequest() || form.isUpdateDialogRequest()) {
102 component = form.getPostedView().getViewIndex().getComponentById(form.getUpdateComponentId());
103 } else if (form.isUpdatePageRequest()) {
104 component = form.getView().getCurrentPage();
105 }
106
107 if (component != null) {
108 modelAndView.addObject(UifConstants.COMPONENT_MODEL_NAME, component);
109 }
110
111
112 prepareHistory(request, form);
113 }
114
115
116 modelAndView.addObject(UifParameters.REQUEST, request);
117 modelAndView.addObject(KRADConstants.USER_SESSION_KEY, request.getSession().getAttribute(
118 KRADConstants.USER_SESSION_KEY));
119
120 Map<String, String> properties = KRADServiceLocator.getKualiConfigurationService().getAllProperties();
121 modelAndView.addObject(UifParameters.CONFIG_PROPERTIES, properties);
122 }
123
124
125
126
127
128
129
130
131 public static void prepareHistory(HttpServletRequest request, UifFormBase form) {
132 View view = form.getView();
133
134
135 History history = form.getFormHistory();
136 if (history == null || request.getMethod().equals("GET")) {
137 history = new History();
138
139 processReturnLocationOverride(form);
140
141 history.setHomewardPath(view.getBreadcrumbs().getHomewardPathList());
142 history.setAppendHomewardPath(view.getBreadcrumbs().isDisplayHomewardPath());
143
144
145 if (StringUtils.isNotBlank(request.getParameter(UifConstants.UrlParams.SHOW_HOME))) {
146 history.setAppendHomewardPath(Boolean.parseBoolean(request.getParameter(
147 UifConstants.UrlParams.SHOW_HOME)));
148 }
149
150
151 if (form.getDialogManager().getDialogs().size() > 0) {
152 history.setAppendPassedHistory(false);
153 } else if (StringUtils.isNotBlank(request.getParameter(UifConstants.UrlParams.SHOW_HISTORY))) {
154 history.setAppendPassedHistory(Boolean.parseBoolean(request.getParameter(
155 UifConstants.UrlParams.SHOW_HISTORY)));
156 } else {
157 history.setAppendPassedHistory(view.getBreadcrumbs().isDisplayPassedHistory());
158 }
159
160 history.buildCurrentEntryFromRequest(form, request);
161 history.buildHistoryFromParameterString(request.getParameter(UifConstants.UrlParams.HISTORY));
162
163 form.setFormHistory(history);
164 }
165 }
166
167
168
169
170
171
172 protected static void processReturnLocationOverride(UifFormBase form) {
173 View view = form.getView();
174
175 if (form.getReturnLocation() == null) {
176 return;
177 }
178
179 List<HistoryEntry> homewardPathList = new ArrayList<HistoryEntry>();
180 if ((view != null) && (view.getBreadcrumbs() != null) &&
181 (view.getBreadcrumbs().getHomewardPathList() != null)) {
182 homewardPathList = view.getBreadcrumbs().getHomewardPathList();
183 }
184
185 HistoryEntry historyEntry = new HistoryEntry("", "", "Home", form.getReturnLocation(), "");
186 if (homewardPathList.isEmpty()) {
187 homewardPathList.add(historyEntry);
188 } else if (StringUtils.equals(homewardPathList.get(0).getTitle(), "Home")) {
189 homewardPathList.set(0, historyEntry);
190 } else {
191 homewardPathList.add(0, historyEntry);
192 }
193 }
194
195
196
197
198
199
200
201 public static void prepareViewForRendering(HttpServletRequest request, UifFormBase form) {
202
203 if (form.isUpdateComponentRequest() || form.isUpdateDialogRequest()) {
204 String refreshComponentId = form.getUpdateComponentId();
205
206
207 Component comp = ComponentFactory.getNewInstanceForRefresh(form.getPostedView(), refreshComponentId);
208
209 View postedView = form.getPostedView();
210
211
212 postedView.getViewHelperService().performComponentLifecycle(postedView, form, comp, refreshComponentId);
213
214
215 postedView.getCurrentPage().getValidationMessages().generateMessages(false, postedView, form,
216 postedView.getCurrentPage());
217 } else {
218
219 View view = form.getView();
220
221
222 if (StringUtils.isNotBlank(form.getPageId())) {
223 view.setCurrentPageId(form.getPageId());
224 }
225
226 Map<String, String> parameterMap = KRADUtils.translateRequestParameterMap(request.getParameterMap());
227 parameterMap.putAll(form.getViewRequestParameters());
228
229
230 getViewService().buildView(view, form, parameterMap);
231 }
232 }
233
234
235
236
237
238
239
240
241
242
243
244
245
246 public static void removeUnusedBreadcrumbs(UifFormManager uifFormManager, String formKey, String lastFormKey) {
247 if (StringUtils.isBlank(formKey) || StringUtils.isBlank(lastFormKey) || StringUtils.equals(formKey,
248 lastFormKey)) {
249 return;
250 }
251
252 UifFormBase previousForm = uifFormManager.getSessionForm(lastFormKey);
253 if (previousForm == null) {
254 return;
255 }
256
257 boolean cleanUpRemainingForms = false;
258 for (HistoryEntry historyEntry : previousForm.getFormHistory().getHistoryEntries()) {
259 if (cleanUpRemainingForms) {
260 uifFormManager.removeSessionFormByKey(historyEntry.getFormKey());
261 } else {
262 if (StringUtils.equals(formKey, historyEntry.getFormKey())) {
263 cleanUpRemainingForms = true;
264 }
265 }
266 }
267
268 uifFormManager.removeSessionFormByKey(lastFormKey);
269 }
270
271 protected static ViewService getViewService() {
272 return KRADServiceLocatorWeb.getViewService();
273 }
274 }