View Javadoc
1   /**
2    * Copyright 2012 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   *
15   */
16  package org.kuali.student.enrollment.class1.hold.controller;
17  
18  import org.kuali.rice.core.api.util.RiceKeyConstants;
19  import org.kuali.rice.krad.uif.UifConstants;
20  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
21  import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleRefreshBuild;
22  import org.kuali.rice.krad.util.GlobalVariables;
23  import org.kuali.rice.krad.util.KRADConstants;
24  import org.kuali.rice.krad.web.controller.MaintenanceDocumentController;
25  import org.kuali.rice.krad.web.controller.MethodAccessible;
26  import org.kuali.rice.krad.web.form.DocumentFormBase;
27  import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
28  import org.kuali.rice.krad.web.form.UifFormBase;
29  import org.kuali.student.common.object.KSObjectUtils;
30  import org.kuali.student.common.uif.util.KSControllerHelper;
31  import org.kuali.student.enrollment.class1.hold.dto.AppliedHoldMaintenanceWrapper;
32  import org.kuali.student.enrollment.class1.hold.service.HoldsViewHelperService;
33  import org.kuali.student.enrollment.class1.hold.util.HoldsConstants;
34  import org.kuali.student.enrollment.class1.hold.util.HoldsUtil;
35  import org.kuali.student.r2.core.constants.HoldServiceConstants;
36  import org.springframework.stereotype.Controller;
37  import org.springframework.validation.BindingResult;
38  import org.springframework.web.bind.annotation.ModelAttribute;
39  import org.springframework.web.bind.annotation.RequestMapping;
40  import org.springframework.web.servlet.ModelAndView;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  import java.util.Date;
45  
46  /**
47   * This class provides a controller for HoldIssue objects
48   *
49   * @author Kuali Student Blue Team (SA)
50   */
51  @Controller
52  @RequestMapping(value = "/appliedHoldMaintenance")
53  public class AppliedHoldMaintenanceController extends MaintenanceDocumentController {
54  
55      /**
56       * Setups a new <code>MaintenanceDocumentView</code> with the edit maintenance
57       * action
58       */
59      @MethodAccessible
60      @RequestMapping(params = "methodToCall=" + KRADConstants.Maintenance.METHOD_TO_CALL_EDIT)
61      public ModelAndView maintenanceEdit(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
62                                          HttpServletRequest request, HttpServletResponse response) throws Exception {
63  
64          setupMaintenance(form, request, KRADConstants.MAINTENANCE_EDIT_ACTION);
65          AppliedHoldMaintenanceWrapper holdWrapper = this.getAppliedHoldWrapper(form);
66          if (HoldsConstants.APPLIED_HOLDS_ACTION_APPLY.equals(holdWrapper.getAction())) {
67              return getUIFModelAndView(form, HoldsConstants.APPLIED_HOLD_APPLY_PAGE_ID);
68          } else if (HoldsConstants.APPLIED_HOLDS_ACTION_EDIT.equals(holdWrapper.getAction())) {
69              return getUIFModelAndView(form, HoldsConstants.APPLIED_HOLD_EDIT_PAGE_ID);
70          } else if (HoldsConstants.APPLIED_HOLDS_ACTION_EXPIRE.equals(holdWrapper.getAction())) {
71              return getUIFModelAndView(form, HoldsConstants.APPLIED_HOLD_EXPIRE_PAGE_ID);
72          } else if (HoldsConstants.APPLIED_HOLDS_ACTION_DELETE.equals(holdWrapper.getAction())) {
73              return getUIFModelAndView(form, HoldsConstants.APPLIED_HOLD_DELETE_PAGE_ID);
74          }
75          return getUIFModelAndView(form);
76      }
77  
78      @RequestMapping(params = "methodToCall=apply")
79      public ModelAndView apply(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
80                                HttpServletRequest request, HttpServletResponse response) {
81  
82          AppliedHoldMaintenanceWrapper holdWrapper = this.getAppliedHoldWrapper(form);
83          if (holdWrapper.getHoldIssue() != null) {
84              if (!this.getViewHelper(form).isAuthorized(holdWrapper.getHoldIssue().getId(), HoldsConstants.APPLIED_HOLD_ACTION_EVENT_APPLY_HOLD)) {
85                  GlobalVariables.getMessageMap().putError(HoldsConstants.HOLD_ISSUE_HOLD_CODE, HoldsConstants.APPLIED_HOLDS_MSG_ERROR_UNAUTHORIZED_APPLY);
86              } else {
87                  try {
88                      holdWrapper.getAppliedHold().setStateKey(HoldServiceConstants.APPLIED_HOLD_ACTIVE_STATE_KEY);
89                      super.route(form, result, request, response);
90                  } catch (Exception e) {
91                      GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM, KSObjectUtils.unwrapException(20, e).getMessage());
92                  }
93              }
94          } else
95              GlobalVariables.getMessageMap().putError(HoldsConstants.APPLIED_HOLDS_PROP_NAME_CODE, HoldsConstants.APPLIED_HOLDS_MSG_ERROR_HOLD_CODE_INVALID);
96  
97          if (GlobalVariables.getMessageMap().hasErrors()) {
98              return getUIFModelAndView(form);
99          }
100 
101         HoldsUtil.showMessage(HoldsConstants.APPLIED_HOLDS_MSG_SUCCESS_HOLD_APPLIED, holdWrapper.getMaintenanceHold().getHoldCode());
102         return back(form, result, request, response);
103     }
104 
105     @RequestMapping(params = "methodToCall=edit")
106     public ModelAndView edit(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
107                              HttpServletRequest request, HttpServletResponse response) {
108 
109         AppliedHoldMaintenanceWrapper holdWrapper = this.getAppliedHoldWrapper(form);
110         if (!this.getViewHelper(form).isAuthorized(holdWrapper.getHoldIssue().getId(), HoldsConstants.APPLIED_HOLD_ACTION_EVENT_APPLY_HOLD)) {
111             GlobalVariables.getMessageMap().putError(HoldsConstants.HOLD_ISSUE_HOLD_CODE, HoldsConstants.APPLIED_HOLDS_MSG_ERROR_UNAUTHORIZED_APPLY);
112         } else {
113             try {
114                 holdWrapper.getAppliedHold().setStateKey(HoldServiceConstants.APPLIED_HOLD_ACTIVE_STATE_KEY);
115                 super.route(form, result, request, response);
116             } catch (Exception e) {
117                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM, KSObjectUtils.unwrapException(20, e).getMessage());
118             }
119         }
120         if (GlobalVariables.getMessageMap().hasErrors()) {
121             return getUIFModelAndView(form);
122         }
123         return back(form, result, request, response);
124 
125     }
126 
127     @RequestMapping(params = "methodToCall=expire")
128     public ModelAndView expire(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
129                                HttpServletRequest request, HttpServletResponse response) {
130 
131         AppliedHoldMaintenanceWrapper holdWrapper = this.getAppliedHoldWrapper(form);
132         if (!this.getViewHelper(form).isAuthorized(holdWrapper.getMaintenanceHold().getAppliedHold().getHoldIssueId(), HoldsConstants.APPLIED_HOLD_ACTION_EVENT_EXPIRE_HOLD)) {
133             GlobalVariables.getMessageMap().putError(HoldsConstants.HOLD_ISSUE_HOLD_CODE, HoldsConstants.APPLIED_HOLDS_MSG_ERROR_UNAUTHORIZED_EXPIRE,holdWrapper.getMaintenanceHold().getHoldCode());
134         } else {
135             try {
136                 holdWrapper.getAppliedHold().setStateKey(HoldServiceConstants.APPLIED_HOLD_EXPIRED_STATE_KEY);
137                 super.route(form, result, request, response);
138             } catch (Exception e) {
139                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM, KSObjectUtils.unwrapException(20, e).getMessage());
140             }
141         }
142         if (GlobalVariables.getMessageMap().hasErrors()) {
143             return getUIFModelAndView(form);
144         }
145 
146         HoldsUtil.showMessage(HoldsConstants.APPLIED_HOLDS_MSG_SUCCESS_HOLD_EXPIRED, holdWrapper.getMaintenanceHold().getHoldCode());
147         return back(form, result, request, response);
148 
149     }
150 
151     @RequestMapping(params = "methodToCall=delete")
152     public ModelAndView delete(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
153                                HttpServletRequest request, HttpServletResponse response) {
154 
155         AppliedHoldMaintenanceWrapper holdWrapper = this.getAppliedHoldWrapper(form);
156         if (!this.getViewHelper(form).isAuthorized(holdWrapper.getMaintenanceHold().getAppliedHold().getHoldIssueId(), HoldsConstants.APPLIED_HOLD_ACTION_EVENT_EXPIRE_HOLD)) {
157             GlobalVariables.getMessageMap().putError(HoldsConstants.HOLD_ISSUE_HOLD_CODE, HoldsConstants.APPLIED_HOLDS_MSG_ERROR_UNAUTHORIZED_DELETE, holdWrapper.getMaintenanceHold().getHoldCode());
158         } else {
159             try {
160                 holdWrapper.getAppliedHold().setStateKey(HoldServiceConstants.APPLIED_HOLD_DELETED_STATE_KEY);
161                 holdWrapper.getAppliedHold().setExpirationDate(new Date());
162                 super.route(form, result, request, response);
163             } catch (Exception e) {
164                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM, KSObjectUtils.unwrapException(20, e).getMessage());
165             }
166         }
167         if (GlobalVariables.getMessageMap().hasErrors()) {
168             return getUIFModelAndView(form);
169         }
170 
171         HoldsUtil.showMessage(HoldsConstants.APPLIED_HOLDS_MSG_SUCCESS_HOLD_DELETED, holdWrapper.getMaintenanceHold().getHoldCode());
172         return back(form, result, request, response);
173 
174     }
175 
176     @RequestMapping(params = "methodToCall=cancel")
177     @Override
178     public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
179                                HttpServletRequest request, HttpServletResponse response) {
180 
181         DocumentFormBase documentForm = (DocumentFormBase) form;
182         performWorkflowAction(documentForm, UifConstants.WorkflowAction.CANCEL, false);
183 
184         return back(form, result, request, response);
185     }
186 
187     @MethodAccessible
188     @RequestMapping(params = "methodToCall=searchHoldIssueByCode")
189     public ModelAndView searchHoldIssueByCode(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
190                                               HttpServletRequest request, HttpServletResponse response) throws Exception {
191 
192         AppliedHoldMaintenanceWrapper holdWrapper = this.getAppliedHoldWrapper(form);
193         holdWrapper.setHoldIssue(this.getViewHelper(form).searchHoldIssueByCode(holdWrapper.getHoldCode()));
194 
195         if (GlobalVariables.getMessageMap().hasErrors()) {
196             holdWrapper.setHoldIssue(null);
197         }
198         return getUIFModelAndView(form);
199     }
200 
201     @MethodAccessible
202     @RequestMapping(params = "methodToCall=refresh")
203     public ModelAndView refresh(@ModelAttribute("KualiForm") final UifFormBase form, BindingResult result,
204                                 final HttpServletRequest request, HttpServletResponse response) throws Exception {
205 
206         ViewLifecycle.encapsulateLifecycle(form.getView(), form, form.getViewPostMetadata(), null, request, response,
207                 new ViewLifecycleRefreshBuild());
208 
209         AppliedHoldMaintenanceWrapper holdWrapper = this.getAppliedHoldWrapper(form);
210         holdWrapper.setHoldIssue(this.getViewHelper(form).searchHoldIssueByCode(holdWrapper.getHoldCode()));
211 
212         return getUIFModelAndView(form);
213     }
214 
215     /**
216      * @param form
217      * @return
218      */
219     protected HoldsViewHelperService getViewHelper(UifFormBase form) {
220         return (HoldsViewHelperService) KSControllerHelper.getViewHelperService(form);
221     }
222 
223     protected AppliedHoldMaintenanceWrapper getAppliedHoldWrapper(UifFormBase form) {
224         MaintenanceDocumentForm document = this.getMaintenanceDocumentForm(form);
225         return (AppliedHoldMaintenanceWrapper) document.getDocument().getDocumentDataObject();
226     }
227 
228     //Method for checking if the form is an instance of maintenanceDocumentForm, then returning the casted form
229     protected MaintenanceDocumentForm getMaintenanceDocumentForm(UifFormBase form) {
230         if (form instanceof MaintenanceDocumentForm) {
231             return (MaintenanceDocumentForm) form;
232         }
233         throw new RuntimeException("Error retrieving Maintenance document form from UifFormBase");
234     }
235 }