1 package org.kuali.ole.olekrad.authorization.controller;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.apache.log4j.Logger;
5 import org.kuali.ole.olekrad.authorization.form.OLEKRADAuthorizationForm;
6 import org.kuali.rice.krad.UserSession;
7 import org.kuali.rice.krad.uif.UifConstants;
8 import org.kuali.rice.krad.uif.UifParameters;
9 import org.kuali.rice.krad.uif.view.HistoryEntry;
10 import org.kuali.rice.krad.util.GlobalVariables;
11 import org.kuali.rice.krad.util.KRADConstants;
12 import org.kuali.rice.krad.web.controller.UifControllerHelper;
13 import org.kuali.rice.krad.web.controller.UifHandlerExceptionResolver;
14 import org.kuali.rice.krad.web.form.DocumentFormBase;
15 import org.kuali.rice.krad.web.form.LookupForm;
16 import org.kuali.rice.krad.web.form.UifFormBase;
17 import org.springframework.web.servlet.ModelAndView;
18
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
21
22
23
24
25
26
27
28
29 public class OLEKRADAuthorizationResolver extends UifHandlerExceptionResolver {
30 private static final Logger LOG = Logger.getLogger(OLEKRADAuthorizationResolver.class);
31 @Override
32 public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
33 Exception ex) {
34
35
36
37
38 if(ex!=null && ex.getMessage()!=null && ex.getMessage().contains("is not authorized")){
39
40 String incidentDocId = request.getParameter(KRADConstants.DOCUMENT_DOCUMENT_NUMBER);
41 String incidentViewId = "";
42
43 UifFormBase form = (UifFormBase)request.getAttribute(UifConstants.REQUEST_FORM);
44 if (form instanceof DocumentFormBase) {
45 if (((DocumentFormBase) form).getDocument() != null) {
46 incidentDocId = ((DocumentFormBase) form).getDocument().getDocumentNumber();
47 }
48 incidentViewId = ((DocumentFormBase) form).getViewId();
49 }
50 GlobalVariables.getUifFormManager().removeSessionForm(form);
51
52 UserSession userSession = (UserSession) request.getSession().getAttribute(KRADConstants.USER_SESSION_KEY);
53
54 OLEKRADAuthorizationForm oleKradAuthorizationForm = new OLEKRADAuthorizationForm();
55 oleKradAuthorizationForm.setDocId(incidentDocId);
56 oleKradAuthorizationForm.setPrincipalId(userSession.getPrincipalId());
57 oleKradAuthorizationForm.setError("Error Message :");
58 if(form instanceof LookupForm){
59 oleKradAuthorizationForm.setInformation("You are not authorized to view \" "+((LookupForm) form).getView().getHeaderText()+" \"");
60
61 }
62 else if(form instanceof UifFormBase){
63 oleKradAuthorizationForm.setInformation("You are not authorized to view \" "+((UifFormBase) form).getView().getHeaderText()+" \"");
64 }
65 else{
66 oleKradAuthorizationForm.setInformation("You are not authorized to view this Document");
67 }
68
69
70
71
72
73 String postUrl = request.getRequestURL().toString();
74 postUrl = postUrl.substring(0, postUrl.lastIndexOf("/")) + "/incidentReport";
75 oleKradAuthorizationForm.setFormPostUrl(postUrl);
76
77 oleKradAuthorizationForm.setViewId("OLEKRADAuthorizationView");
78
79 if (form != null) {
80 oleKradAuthorizationForm.setAjaxRequest(form.isAjaxRequest());
81 } else {
82 String ajaxRequestParm = request.getParameter(UifParameters.AJAX_REQUEST);
83 if (StringUtils.isNotBlank(ajaxRequestParm)) {
84 oleKradAuthorizationForm.setAjaxRequest(Boolean.parseBoolean(ajaxRequestParm));
85 }
86 }
87
88
89 oleKradAuthorizationForm.setView(getViewService().getViewById("OLEKRADAuthorizationView"));
90
91
92
93 HistoryEntry entry = new HistoryEntry("", "", "Incident Report", "", "");
94
95
96
97
98 oleKradAuthorizationForm.setAjaxReturnType(UifConstants.AjaxReturnTypes.UPDATEVIEW.getKey());
99
100 ModelAndView modelAndView = UifControllerHelper.getUIFModelAndView(oleKradAuthorizationForm, "");
101 try {
102 UifControllerHelper.postControllerHandle(request, response, handler, modelAndView);
103 } catch (Exception e) {
104 LOG.error("An error stopped the incident form from loading", e);
105 }
106
107 return modelAndView;
108 }
109 else {
110 return super.resolveException(request,response,handler,ex);
111
112 }
113
114
115
116
117 }
118 }