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 | 0 | public class UifWebUtils { |
52 | 0 | 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 | 0 | UifFormBase form = null; |
67 | |
|
68 | 0 | String formKeyParam = request.getParameter(UifParameters.FORM_KEY); |
69 | 0 | if (StringUtils.isBlank(formKeyParam)) { |
70 | 0 | formKeyParam = (String) request.getAttribute(UifParameters.FORM_KEY); |
71 | |
} |
72 | |
|
73 | 0 | String docId = request.getParameter(KRADConstants.DOCUMENT_DOCUMENT_NUMBER); |
74 | 0 | if (StringUtils.isNotBlank(formKeyParam)) { |
75 | 0 | form = (UifFormBase) request.getSession().getAttribute(formKeyParam); |
76 | |
|
77 | 0 | if (form == null) { |
78 | 0 | UserSession userSession = (UserSession) request.getSession().getAttribute( |
79 | |
KRADConstants.USER_SESSION_KEY); |
80 | 0 | form = KRADServiceLocatorWeb.getSessionDocumentService().getDocumentForm(docId, formKeyParam, |
81 | |
userSession, request.getRemoteAddr()); |
82 | |
} |
83 | |
} |
84 | |
|
85 | 0 | return form; |
86 | |
} |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public static ModelAndView getUIFModelAndView(UifFormBase form, String pageId) { |
98 | 0 | if (StringUtils.isNotBlank(pageId)) { |
99 | 0 | form.setPageId(pageId); |
100 | |
} |
101 | |
|
102 | |
|
103 | 0 | ModelAndView modelAndView = new ModelAndView(); |
104 | 0 | modelAndView.addObject(UifConstants.DEFAULT_MODEL_NAME, form); |
105 | 0 | modelAndView.setViewName(UifConstants.SPRING_VIEW_ID); |
106 | |
|
107 | 0 | return modelAndView; |
108 | |
} |
109 | |
|
110 | |
public static ModelAndView getComponentModelAndView(Component component, Object model) { |
111 | 0 | ModelAndView modelAndView = new ModelAndView(); |
112 | 0 | modelAndView.addObject(UifConstants.DEFAULT_MODEL_NAME, model); |
113 | 0 | modelAndView.addObject("Component", component); |
114 | 0 | modelAndView.setViewName("ComponentUpdate"); |
115 | |
|
116 | 0 | 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 | 0 | if (handler instanceof UifControllerBase && (modelAndView != null)) { |
126 | 0 | UifControllerBase controller = (UifControllerBase) handler; |
127 | 0 | UifFormBase form = null; |
128 | |
|
129 | |
|
130 | 0 | if (modelAndView.getViewName().equals(UifConstants.SPRING_VIEW_ID)) { |
131 | 0 | Object model = modelAndView.getModelMap().get(UifConstants.DEFAULT_MODEL_NAME); |
132 | 0 | if (model instanceof UifFormBase) { |
133 | 0 | form = (UifFormBase) model; |
134 | |
|
135 | 0 | form.setPreviousView(null); |
136 | |
|
137 | |
|
138 | 0 | if (form instanceof DocumentFormBase) { |
139 | 0 | UserSession userSession = (UserSession) request.getSession().getAttribute( |
140 | |
KRADConstants.USER_SESSION_KEY); |
141 | 0 | getSessionDocumentService().setDocumentForm((DocumentFormBase) form, userSession, |
142 | |
request.getRemoteAddr()); |
143 | |
} |
144 | |
|
145 | |
|
146 | 0 | checkMethodToCallAuthorization(request, controller, form); |
147 | |
|
148 | |
|
149 | 0 | prepareViewForRendering(request, form); |
150 | |
|
151 | |
|
152 | 0 | prepareHistory(request, form); |
153 | |
} |
154 | |
} |
155 | |
} |
156 | 0 | } |
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 | 0 | String methodToCall = request.getParameter(KRADConstants.DISPATCH_REQUEST_PARAMETER); |
173 | |
|
174 | 0 | if (!controller.getMethodToCallsToNotCheckAuthorization().contains(methodToCall)) { |
175 | 0 | if (LOG.isDebugEnabled()) { |
176 | 0 | LOG.debug("'" + methodToCall + "' not in set of excempt methods: " + controller |
177 | |
.getMethodToCallsToNotCheckAuthorization()); |
178 | |
} |
179 | |
|
180 | 0 | controller.checkAuthorization(form, methodToCall); |
181 | |
} else { |
182 | 0 | if (LOG.isDebugEnabled()) { |
183 | 0 | LOG.debug("'" + methodToCall + "' is exempt from auth checks."); |
184 | |
} |
185 | |
} |
186 | 0 | } |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
public static void prepareHistory(HttpServletRequest request, UifFormBase form) { |
196 | 0 | View view = form.getView(); |
197 | |
|
198 | |
|
199 | 0 | History history = form.getFormHistory(); |
200 | 0 | if (history == null || request.getMethod().equals("GET")) { |
201 | 0 | history = new History(); |
202 | 0 | history.setHomewardPath(view.getBreadcrumbs().getHomewardPathList()); |
203 | 0 | history.setAppendHomewardPath(view.getBreadcrumbs().isDisplayHomewardPath()); |
204 | 0 | history.setAppendPassedHistory(view.getBreadcrumbs().isDisplayPassedHistory()); |
205 | |
|
206 | |
|
207 | 0 | if (StringUtils.isNotBlank(request.getParameter(UifConstants.UrlParams.SHOW_HOME))) { |
208 | 0 | history.setAppendHomewardPath(Boolean.parseBoolean(request.getParameter( |
209 | |
UifConstants.UrlParams.SHOW_HOME))); |
210 | |
} |
211 | |
|
212 | 0 | if (StringUtils.isNotBlank(request.getParameter(UifConstants.UrlParams.SHOW_HISTORY))) { |
213 | 0 | history.setAppendPassedHistory(Boolean.parseBoolean(request.getParameter( |
214 | |
UifConstants.UrlParams.SHOW_HISTORY))); |
215 | |
} |
216 | |
|
217 | 0 | history.setCurrent(form, request); |
218 | 0 | history.buildHistoryFromParameterString(request.getParameter(UifConstants.UrlParams.HISTORY)); |
219 | 0 | form.setFormHistory(history); |
220 | |
} |
221 | 0 | } |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
public static void prepareViewForRendering(HttpServletRequest request, UifFormBase form) { |
234 | 0 | View view = form.getView(); |
235 | |
|
236 | |
|
237 | 0 | if (StringUtils.isNotBlank(form.getPageId())) { |
238 | 0 | view.setCurrentPageId(form.getPageId()); |
239 | |
} |
240 | |
|
241 | 0 | Map<String, String> parameterMap = KRADUtils.translateRequestParameterMap(request.getParameterMap()); |
242 | 0 | parameterMap.putAll(form.getViewRequestParameters()); |
243 | |
|
244 | |
|
245 | 0 | getViewService().buildView(view, form, parameterMap); |
246 | |
|
247 | |
|
248 | 0 | form.setValidateDirty(view.isValidateDirty()); |
249 | 0 | } |
250 | |
|
251 | |
protected static SessionDocumentService getSessionDocumentService() { |
252 | 0 | return KRADServiceLocatorWeb.getSessionDocumentService(); |
253 | |
} |
254 | |
|
255 | |
protected static ViewService getViewService() { |
256 | 0 | return KRADServiceLocatorWeb.getViewService(); |
257 | |
} |
258 | |
} |