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