1
2
3
4
5
6
7
8
9
10
11 package org.kuali.rice.krad.uif.util;
12
13 import org.apache.commons.lang.StringUtils;
14 import org.apache.log4j.Logger;
15 import org.kuali.rice.krad.UserSession;
16 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
17 import org.kuali.rice.krad.service.SessionDocumentService;
18 import org.kuali.rice.krad.uif.UifConstants;
19 import org.kuali.rice.krad.uif.UifParameters;
20 import org.kuali.rice.krad.uif.container.View;
21 import org.kuali.rice.krad.uif.core.Component;
22 import org.kuali.rice.krad.uif.history.History;
23 import org.kuali.rice.krad.uif.service.ViewService;
24 import org.kuali.rice.krad.util.KRADConstants;
25 import org.kuali.rice.krad.web.controller.UifControllerBase;
26 import org.kuali.rice.krad.web.form.DocumentFormBase;
27 import org.kuali.rice.krad.web.form.UifFormBase;
28 import org.springframework.web.servlet.ModelAndView;
29
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32
33
34
35
36
37
38
39
40
41
42 public class UifWebUtils {
43
44 private static final Logger LOG = Logger.getLogger(UifWebUtils.class);
45
46
47
48
49
50
51
52
53
54
55
56
57 public static UifFormBase getFormFromRequest(HttpServletRequest request) {
58 UifFormBase form = null;
59
60 String formKeyParam = request.getParameter(UifParameters.FORM_KEY);
61 String docId = request.getParameter(KRADConstants.DOCUMENT_DOCUMENT_NUMBER);
62 if (StringUtils.isNotBlank(formKeyParam)) {
63 form = (UifFormBase) request.getSession().getAttribute(formKeyParam);
64
65 if (form == null) {
66 UserSession userSession =
67 (UserSession) request.getSession().getAttribute(KRADConstants.USER_SESSION_KEY);
68 form = KRADServiceLocatorWeb.getSessionDocumentService()
69 .getDocumentForm(docId, formKeyParam, userSession, request.getRemoteAddr());
70 }
71 }
72
73 return form;
74 }
75
76
77
78
79
80
81
82
83
84
85
86 public static ModelAndView getUIFModelAndView(UifFormBase form, String viewId, String pageId) {
87
88 form.setViewId(viewId);
89 if (StringUtils.isNotBlank(pageId)) {
90 form.setPageId(pageId);
91 }
92
93
94 ModelAndView modelAndView = new ModelAndView();
95 modelAndView.addObject(UifConstants.DEFAULT_MODEL_NAME, form);
96 modelAndView.setViewName(UifConstants.SPRING_VIEW_ID);
97
98 return modelAndView;
99 }
100
101 public static ModelAndView getComponentModelAndView(Component component, Object model) {
102 ModelAndView modelAndView = new ModelAndView();
103 modelAndView.addObject(UifConstants.DEFAULT_MODEL_NAME, model);
104 modelAndView.addObject("Component", component);
105 modelAndView.setViewName("ComponentUpdate");
106
107 return modelAndView;
108 }
109
110
111
112
113
114 public static void postControllerHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
115 ModelAndView modelAndView) throws Exception {
116 if (handler instanceof UifControllerBase && (modelAndView != null)) {
117 UifControllerBase controller = (UifControllerBase) handler;
118 UifFormBase form = null;
119
120
121 if (modelAndView.getViewName().equals(UifConstants.SPRING_VIEW_ID)) {
122 Object model = modelAndView.getModelMap().get(UifConstants.DEFAULT_MODEL_NAME);
123 if (model instanceof UifFormBase) {
124 form = (UifFormBase) model;
125
126 form.setPreviousView(null);
127
128
129 prepareHistory(request, form);
130
131
132 request.getSession().setAttribute(form.getFormKey(), model);
133 if (form instanceof DocumentFormBase) {
134 UserSession userSession =
135 (UserSession) request.getSession().getAttribute(KRADConstants.USER_SESSION_KEY);
136 getSessionDocumentService()
137 .setDocumentForm((DocumentFormBase) form, userSession, request.getRemoteAddr());
138 }
139
140
141 checkMethodToCallAuthorization(request, controller, form);
142
143
144 prepareViewForRendering(form);
145 }
146 }
147 }
148 }
149
150
151
152
153
154
155
156
157
158
159
160 public static void checkMethodToCallAuthorization(HttpServletRequest request, UifControllerBase controller,
161 UifFormBase form) {
162
163
164 String methodToCall = request.getParameter(KRADConstants.DISPATCH_REQUEST_PARAMETER);
165
166 if (!controller.getMethodToCallsToNotCheckAuthorization().contains(methodToCall)) {
167 if (LOG.isDebugEnabled()) {
168 LOG.debug("'" + methodToCall + "' not in set of excempt methods: " +
169 controller.getMethodToCallsToNotCheckAuthorization());
170 }
171
172 controller.checkAuthorization(form, methodToCall);
173 } else {
174 if (LOG.isDebugEnabled()) {
175 LOG.debug("'" + methodToCall + "' is exempt from auth checks.");
176 }
177 }
178 }
179
180
181
182
183
184
185
186
187 public static void prepareHistory(HttpServletRequest request, UifFormBase form) {
188 View view = form.getView();
189
190
191 History history = form.getFormHistory();
192 if (history == null || request.getMethod().equals("GET")) {
193 history = new History();
194 history.setHomewardPath(view.getBreadcrumbs().getHomewardPathList());
195 history.setAppendHomewardPath(view.getBreadcrumbs().isDisplayHomewardPath());
196 history.setAppendPassedHistory(view.getBreadcrumbs().isDisplayPassedHistory());
197
198
199 if (StringUtils.isNotBlank(request.getParameter(UifConstants.UrlParams.SHOW_HOME))) {
200 history.setAppendHomewardPath(
201 Boolean.parseBoolean(request.getParameter(UifConstants.UrlParams.SHOW_HOME)));
202 }
203
204 if (StringUtils.isNotBlank(request.getParameter(UifConstants.UrlParams.SHOW_HISTORY))) {
205 history.setAppendPassedHistory(
206 Boolean.parseBoolean(request.getParameter(UifConstants.UrlParams.SHOW_HISTORY)));
207 }
208
209 history.setCurrent(form, request);
210 history.buildHistoryFromParameterString(request.getParameter(UifConstants.UrlParams.HISTORY));
211 form.setFormHistory(history);
212 }
213 }
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231 public static void prepareViewForRendering(UifFormBase form) {
232
233
234 View view = form.getView();
235 String viewId = form.getViewId();
236 if ((view == null) || !StringUtils.equals(viewId, view.getId())) {
237 if (LOG.isDebugEnabled()) {
238 LOG.debug("Getting new view instance for view id: " + viewId);
239 }
240
241 view = getViewService().getView(viewId, form.getViewRequestParameters());
242
243
244 form.setRenderFullView(true);
245 }
246
247
248 if (StringUtils.equals(UifConstants.ViewStatus.FINAL, view.getViewStatus())) {
249 if (LOG.isDebugEnabled()) {
250 LOG.debug("Rebuilding view due to final status, view id: " + viewId);
251 }
252
253 view = getViewService().rebuildView(viewId, form, form.getViewRequestParameters());
254 } else {
255
256 getViewService().buildView(view, form);
257 }
258
259
260 form.setValidateDirty(view.isValidateDirty());
261
262
263 if (StringUtils.isNotBlank(form.getPageId())) {
264 view.setCurrentPageId(form.getPageId());
265 }
266 }
267
268 protected static SessionDocumentService getSessionDocumentService() {
269 return KRADServiceLocatorWeb.getSessionDocumentService();
270 }
271
272 protected static ViewService getViewService() {
273 return KRADServiceLocatorWeb.getViewService();
274 }
275 }