View Javadoc
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   * Created with IntelliJ IDEA.
26   * User: sheiksalahudeenm
27   * Date: 8/22/13
28   * Time: 9:56 PM
29   * To change this template use File | Settings | File Templates.
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          //LOG.error("The following error was caught by the UifHandlerExceptionResolver : ", ex);
37  
38          // log exception
39          //LOG.error(ex.getMessage(), ex);
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              // Set the post url map to the incident report controller and not
74              // the one the exception occurred on
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              // Set the view object
91              oleKradAuthorizationForm.setView(getViewService().getViewById("OLEKRADAuthorizationView"));
92  
93              // Add a new History entry to avoid errors in the postHandle
94  
95              HistoryEntry entry = new HistoryEntry("", "", "Incident Report", "", "");
96  
97              // oleKradAuthorizationForm.setFormHistory(history);
98  
99              // Set the ajax return type
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             //OJB operation; SQL []; Duplicate entry '100' for key 'ITM_ID_UNIQUE'; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '100' for key 'ITM_ID_UNIQUE'
135 
136         }
137         else {
138             return super.resolveException(request,response,handler,ex);
139 
140         }
141 
142 
143 
144 
145     }
146 }