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 | 0 | public class UifWebUtils { |
43 | |
|
44 | 0 | 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 | 0 | UifFormBase form = null; |
59 | |
|
60 | 0 | String formKeyParam = request.getParameter(UifParameters.FORM_KEY); |
61 | 0 | String docId = request.getParameter(KRADConstants.DOCUMENT_DOCUMENT_NUMBER); |
62 | 0 | if (StringUtils.isNotBlank(formKeyParam)) { |
63 | 0 | form = (UifFormBase) request.getSession().getAttribute(formKeyParam); |
64 | |
|
65 | 0 | if (form == null) { |
66 | 0 | UserSession userSession = |
67 | |
(UserSession) request.getSession().getAttribute(KRADConstants.USER_SESSION_KEY); |
68 | 0 | form = KRADServiceLocatorWeb.getSessionDocumentService() |
69 | |
.getDocumentForm(docId, formKeyParam, userSession, request.getRemoteAddr()); |
70 | |
} |
71 | |
} |
72 | |
|
73 | 0 | 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 | 0 | form.setViewId(viewId); |
89 | 0 | if (StringUtils.isNotBlank(pageId)) { |
90 | 0 | form.setPageId(pageId); |
91 | |
} |
92 | |
|
93 | |
|
94 | 0 | ModelAndView modelAndView = new ModelAndView(); |
95 | 0 | modelAndView.addObject(UifConstants.DEFAULT_MODEL_NAME, form); |
96 | 0 | modelAndView.setViewName(UifConstants.SPRING_VIEW_ID); |
97 | |
|
98 | 0 | return modelAndView; |
99 | |
} |
100 | |
|
101 | |
public static ModelAndView getComponentModelAndView(Component component, Object model) { |
102 | 0 | ModelAndView modelAndView = new ModelAndView(); |
103 | 0 | modelAndView.addObject(UifConstants.DEFAULT_MODEL_NAME, model); |
104 | 0 | modelAndView.addObject("Component", component); |
105 | 0 | modelAndView.setViewName("ComponentUpdate"); |
106 | |
|
107 | 0 | 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 | 0 | if (handler instanceof UifControllerBase && (modelAndView != null)) { |
117 | 0 | UifControllerBase controller = (UifControllerBase) handler; |
118 | 0 | UifFormBase form = null; |
119 | |
|
120 | |
|
121 | 0 | if (modelAndView.getViewName().equals(UifConstants.SPRING_VIEW_ID)) { |
122 | 0 | Object model = modelAndView.getModelMap().get(UifConstants.DEFAULT_MODEL_NAME); |
123 | 0 | if (model instanceof UifFormBase) { |
124 | 0 | form = (UifFormBase) model; |
125 | |
|
126 | 0 | form.setPreviousView(null); |
127 | |
|
128 | |
|
129 | 0 | prepareHistory(request, form); |
130 | |
|
131 | |
|
132 | 0 | request.getSession().setAttribute(form.getFormKey(), model); |
133 | 0 | if (form instanceof DocumentFormBase) { |
134 | 0 | UserSession userSession = |
135 | |
(UserSession) request.getSession().getAttribute(KRADConstants.USER_SESSION_KEY); |
136 | 0 | getSessionDocumentService() |
137 | |
.setDocumentForm((DocumentFormBase) form, userSession, request.getRemoteAddr()); |
138 | |
} |
139 | |
|
140 | |
|
141 | 0 | checkMethodToCallAuthorization(request, controller, form); |
142 | |
|
143 | |
|
144 | 0 | prepareViewForRendering(form); |
145 | |
} |
146 | |
} |
147 | |
} |
148 | 0 | } |
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 | 0 | String methodToCall = request.getParameter(KRADConstants.DISPATCH_REQUEST_PARAMETER); |
165 | |
|
166 | 0 | if (!controller.getMethodToCallsToNotCheckAuthorization().contains(methodToCall)) { |
167 | 0 | if (LOG.isDebugEnabled()) { |
168 | 0 | LOG.debug("'" + methodToCall + "' not in set of excempt methods: " + |
169 | |
controller.getMethodToCallsToNotCheckAuthorization()); |
170 | |
} |
171 | |
|
172 | 0 | controller.checkAuthorization(form, methodToCall); |
173 | |
} else { |
174 | 0 | if (LOG.isDebugEnabled()) { |
175 | 0 | LOG.debug("'" + methodToCall + "' is exempt from auth checks."); |
176 | |
} |
177 | |
} |
178 | 0 | } |
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
public static void prepareHistory(HttpServletRequest request, UifFormBase form) { |
188 | 0 | View view = form.getView(); |
189 | |
|
190 | |
|
191 | 0 | History history = form.getFormHistory(); |
192 | 0 | if (history == null || request.getMethod().equals("GET")) { |
193 | 0 | history = new History(); |
194 | 0 | history.setHomewardPath(view.getBreadcrumbs().getHomewardPathList()); |
195 | 0 | history.setAppendHomewardPath(view.getBreadcrumbs().isDisplayHomewardPath()); |
196 | 0 | history.setAppendPassedHistory(view.getBreadcrumbs().isDisplayPassedHistory()); |
197 | |
|
198 | |
|
199 | 0 | if (StringUtils.isNotBlank(request.getParameter(UifConstants.UrlParams.SHOW_HOME))) { |
200 | 0 | history.setAppendHomewardPath( |
201 | |
Boolean.parseBoolean(request.getParameter(UifConstants.UrlParams.SHOW_HOME))); |
202 | |
} |
203 | |
|
204 | 0 | if (StringUtils.isNotBlank(request.getParameter(UifConstants.UrlParams.SHOW_HISTORY))) { |
205 | 0 | history.setAppendPassedHistory( |
206 | |
Boolean.parseBoolean(request.getParameter(UifConstants.UrlParams.SHOW_HISTORY))); |
207 | |
} |
208 | |
|
209 | 0 | history.setCurrent(form, request); |
210 | 0 | history.buildHistoryFromParameterString(request.getParameter(UifConstants.UrlParams.HISTORY)); |
211 | 0 | form.setFormHistory(history); |
212 | |
} |
213 | 0 | } |
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 | 0 | View view = form.getView(); |
235 | 0 | String viewId = form.getViewId(); |
236 | 0 | if ((view == null) || !StringUtils.equals(viewId, view.getId())) { |
237 | 0 | if (LOG.isDebugEnabled()) { |
238 | 0 | LOG.debug("Getting new view instance for view id: " + viewId); |
239 | |
} |
240 | |
|
241 | 0 | view = getViewService().getView(viewId, form.getViewRequestParameters()); |
242 | |
|
243 | |
|
244 | 0 | form.setRenderFullView(true); |
245 | |
} |
246 | |
|
247 | |
|
248 | 0 | if (StringUtils.equals(UifConstants.ViewStatus.FINAL, view.getViewStatus())) { |
249 | 0 | if (LOG.isDebugEnabled()) { |
250 | 0 | LOG.debug("Rebuilding view due to final status, view id: " + viewId); |
251 | |
} |
252 | |
|
253 | 0 | view = getViewService().rebuildView(viewId, form, form.getViewRequestParameters()); |
254 | |
} else { |
255 | |
|
256 | 0 | getViewService().buildView(view, form); |
257 | |
} |
258 | |
|
259 | |
|
260 | 0 | form.setValidateDirty(view.isValidateDirty()); |
261 | |
|
262 | |
|
263 | 0 | if (StringUtils.isNotBlank(form.getPageId())) { |
264 | 0 | view.setCurrentPageId(form.getPageId()); |
265 | |
} |
266 | 0 | } |
267 | |
|
268 | |
protected static SessionDocumentService getSessionDocumentService() { |
269 | 0 | return KRADServiceLocatorWeb.getSessionDocumentService(); |
270 | |
} |
271 | |
|
272 | |
protected static ViewService getViewService() { |
273 | 0 | return KRADServiceLocatorWeb.getViewService(); |
274 | |
} |
275 | |
} |