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.OLEConstants;
6 import org.kuali.ole.deliver.form.OleLoanForm;
7 import org.kuali.ole.olekrad.authorization.form.OLEKRADAuthorizationForm;
8 import org.kuali.rice.krad.UserSession;
9 import org.kuali.rice.krad.uif.UifConstants;
10 import org.kuali.rice.krad.uif.UifParameters;
11 import org.kuali.rice.krad.uif.view.HistoryEntry;
12 import org.kuali.rice.krad.util.GlobalVariables;
13 import org.kuali.rice.krad.util.KRADConstants;
14 import org.kuali.rice.krad.web.controller.UifControllerHelper;
15 import org.kuali.rice.krad.web.controller.UifHandlerExceptionResolver;
16 import org.kuali.rice.krad.web.form.DocumentFormBase;
17 import org.kuali.rice.krad.web.form.LookupForm;
18 import org.kuali.rice.krad.web.form.UifFormBase;
19 import org.springframework.web.servlet.ModelAndView;
20
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23
24
25
26
27
28
29
30
31 public class OLEKRADAuthorizationResolver extends UifHandlerExceptionResolver {
32 private static final Logger LOG = Logger.getLogger(OLEKRADAuthorizationResolver.class);
33 @Override
34 public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
35 Exception ex) {
36
37
38
39
40 if(ex!=null && ex.getMessage()!=null && ex.getMessage().contains("is not authorized")){
41
42 String incidentDocId = request.getParameter(KRADConstants.DOCUMENT_DOCUMENT_NUMBER);
43 String incidentViewId = "";
44
45 UifFormBase form = (UifFormBase)request.getAttribute(UifConstants.REQUEST_FORM);
46 if (form instanceof DocumentFormBase) {
47 if (((DocumentFormBase) form).getDocument() != null) {
48 incidentDocId = ((DocumentFormBase) form).getDocument().getDocumentNumber();
49 }
50 incidentViewId = ((DocumentFormBase) form).getViewId();
51 }
52 GlobalVariables.getUifFormManager().removeSessionForm(form);
53
54 UserSession userSession = (UserSession) request.getSession().getAttribute(KRADConstants.USER_SESSION_KEY);
55
56 OLEKRADAuthorizationForm oleKradAuthorizationForm = new OLEKRADAuthorizationForm();
57 oleKradAuthorizationForm.setDocId(incidentDocId);
58 oleKradAuthorizationForm.setPrincipalId(userSession.getPrincipalId());
59 oleKradAuthorizationForm.setError("Error Message :");
60 if(form instanceof LookupForm){
61 oleKradAuthorizationForm.setInformation("You are not authorized to view \" "+((LookupForm) form).getView().getHeaderText()+" \"");
62
63 }
64 else if(form instanceof UifFormBase){
65 oleKradAuthorizationForm.setInformation("You are not authorized to view \" "+((UifFormBase) form).getView().getHeaderText()+" \"");
66 }
67 else{
68 oleKradAuthorizationForm.setInformation("You are not authorized to view this Document");
69 }
70
71
72
73
74
75 String postUrl = request.getRequestURL().toString();
76 postUrl = postUrl.substring(0, postUrl.lastIndexOf("/")) + "/incidentReport";
77 oleKradAuthorizationForm.setFormPostUrl(postUrl);
78
79 oleKradAuthorizationForm.setViewId("OLEKRADAuthorizationView");
80
81 if (form != null) {
82 oleKradAuthorizationForm.setAjaxRequest(form.isAjaxRequest());
83 } else {
84 String ajaxRequestParm = request.getParameter(UifParameters.AJAX_REQUEST);
85 if (StringUtils.isNotBlank(ajaxRequestParm)) {
86 oleKradAuthorizationForm.setAjaxRequest(Boolean.parseBoolean(ajaxRequestParm));
87 }
88 }
89
90
91 oleKradAuthorizationForm.setView(getViewService().getViewById("OLEKRADAuthorizationView"));
92
93
94
95 HistoryEntry entry = new HistoryEntry("", "", "Incident Report", "", "");
96
97
98
99
100 oleKradAuthorizationForm.setAjaxReturnType(UifConstants.AjaxReturnTypes.UPDATEVIEW.getKey());
101
102 ModelAndView modelAndView = UifControllerHelper.getUIFModelAndView(oleKradAuthorizationForm, "");
103 try {
104 UifControllerHelper.postControllerHandle(request, response, handler, modelAndView);
105 } catch (Exception e) {
106 LOG.error("An error stopped the incident form from loading", e);
107 }
108
109 return modelAndView;
110 }else if(ex!=null && ex.getMessage()!=null &&((UifFormBase)request.getAttribute(UifConstants.REQUEST_FORM)).getClass().getName().equals("org.kuali.ole.deliver.form.OleLoanForm")){
111 UifFormBase form = (UifFormBase)request.getAttribute(UifConstants.REQUEST_FORM);
112 OleLoanForm oleLoanForm = (OleLoanForm) form;
113 if(StringUtils.isNotBlank(oleLoanForm.getInformation()) && (oleLoanForm.getInformation().contains("for key 'ITM_ID_UNIQUE'")|| oleLoanForm.getInformation().contains("The Item is already loaned."))){
114 oleLoanForm.setInformation(OLEConstants.ITEM_ALREADY_LOANED);
115 oleLoanForm.setMessage(null);
116 oleLoanForm.setSuccess(true);
117 oleLoanForm.setNonCirculatingFlag(false);
118 oleLoanForm.setCurrentDate(null);
119 oleLoanForm.setRequestUrl(oleLoanForm.getRequestUrl() + OLEConstants.ITEM_ALREADY_LOANED_REDIRECT_URL + oleLoanForm.getFormKey());
120 oleLoanForm.setFormPostUrl(oleLoanForm.getFormPostUrl() + OLEConstants.ITEM_ALREADY_LOANED_REDIRECT_URL + oleLoanForm.getFormKey());
121 GlobalVariables.getUifFormManager().addSessionForm(oleLoanForm);
122 HistoryEntry entry = new HistoryEntry("", "", "is not a valid date", "", "");
123 ModelAndView modelAndView = UifControllerHelper.getUIFModelAndView(oleLoanForm, "PatronItemViewPage");
124 try {
125 UifControllerHelper.postControllerHandle(request, response, handler, modelAndView);
126 } catch (Exception e) {
127 LOG.error("An error stopped the incident form from loading", e);
128 }
129 return modelAndView;
130 }else{
131 return super.resolveException(request,response,handler,ex);
132 }
133
134 }
135 else {
136 return super.resolveException(request,response,handler,ex);
137
138 }
139
140
141
142
143 }
144 }