View Javadoc

1   package org.kuali.ole.patron.bill;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.service.impl.OlePatronBillMaintenanceDocumentServiceImpl;
5   import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
6   import org.kuali.rice.krad.bo.PersistableAttachment;
7   import org.kuali.rice.krad.bo.PersistableBusinessObject;
8   import org.kuali.rice.krad.maintenance.MaintenanceDocument;
9   import org.kuali.rice.krad.maintenance.MaintenanceUtils;
10  import org.kuali.rice.krad.service.BusinessObjectService;
11  import org.kuali.rice.krad.service.KRADServiceLocator;
12  import org.kuali.rice.krad.service.MaintenanceDocumentService;
13  import org.kuali.rice.krad.util.GlobalVariables;
14  import org.kuali.rice.krad.util.KRADConstants;
15  import org.kuali.rice.krad.web.controller.MaintenanceDocumentController;
16  import org.kuali.rice.krad.web.form.DocumentFormBase;
17  import org.kuali.rice.krad.web.form.MaintenanceForm;
18  import org.kuali.rice.krad.web.form.UifFormBase;
19  import org.springframework.stereotype.Controller;
20  import org.springframework.validation.BindingResult;
21  import org.springframework.web.bind.annotation.ModelAttribute;
22  import org.springframework.web.bind.annotation.RequestMapping;
23  import org.springframework.web.servlet.ModelAndView;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  import java.math.BigDecimal;
28  import java.util.Date;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  /**
34   * Created with IntelliJ IDEA.
35   * User: ?
36   * Date: 10/5/12
37   * Time: 2:21 PM
38   * To change this template use File | Settings | File Templates.
39   */
40  @Controller
41  @RequestMapping(value = "/patronBillMaintenance")
42  public class PatronBillMaintenanceDocumentController extends MaintenanceDocumentController {
43  
44      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PatronBillMaintenanceDocumentController.class);
45      private BusinessObjectService businessObjectService;
46  
47      /**
48       *  Gets the businessObjectService attribute.
49       * @return  Returns the businessObjectService
50       */
51      public BusinessObjectService getBusinessObjectService() {
52          if (null == businessObjectService) {
53              businessObjectService = KRADServiceLocator.getBusinessObjectService();
54          }
55          return businessObjectService;
56      }
57  
58      @RequestMapping(params = "methodToCall=" + KRADConstants.Maintenance.METHOD_TO_CALL_NEW)
59      @Override
60      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
61                                HttpServletRequest request, HttpServletResponse response) {
62          Date billDate = new Date();
63          MaintenanceForm maintenanceForm = (MaintenanceForm) form;
64          setupMaintenance(maintenanceForm, request, KRADConstants.MAINTENANCE_NEW_ACTION);
65          MaintenanceDocument document = (MaintenanceDocument) maintenanceForm.getDocument();
66          PatronBillPayment patronBillPayment=(PatronBillPayment)   document.getDocumentDataObject();
67          patronBillPayment.setPatronId(request.getParameter("patronId"));
68          patronBillPayment.setFirstName(request.getParameter("firstName"));
69          patronBillPayment.setLastName(request.getParameter("lastName"));
70  
71          patronBillPayment.setBillDate(billDate);
72          return getUIFModelAndView(maintenanceForm);
73      }
74  
75  
76  
77      /**
78       * This method returns the instance of olePatronBillMaintenanceDocumentService
79       * @return olePatronMaintenanceDocumentService(MaintenanceDocumentService)
80       */
81      @Override
82      protected MaintenanceDocumentService getMaintenanceDocumentService() {
83          return GlobalResourceLoader.getService("olePatronBillMaintenanceDocumentService");
84      }
85  
86  
87      /**
88       * This method will add the patron bill  document to the existing list and also stores the attachment
89       *   to the specified path.
90       *
91       * @param uifForm - MaintenanceForm
92       * @return ModelAndView
93       */
94      @RequestMapping(params = "methodToCall=addToTotalAmount")
95      public ModelAndView addToTotalAmount(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
96                                                  HttpServletRequest request, HttpServletResponse response) throws Exception{
97          ModelAndView modelAndView=super.addLine(uifForm, result, request, response);
98          MaintenanceForm form = (MaintenanceForm) uifForm;
99          MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
100         PatronBillPayment patronBillPayment = (PatronBillPayment)document.getNewMaintainableObject().getDataObject();
101         BigDecimal amount = new BigDecimal(0.0);
102         if(patronBillPayment.getFeeType() !=null ) {
103             List<FeeType> feeTypes = patronBillPayment.getFeeType();
104             for (int i=0;i<feeTypes.size();i++) {
105                 amount = amount.add(feeTypes.get(i).getFeeAmount());
106             }
107             patronBillPayment.setTotalAmount(amount);
108 
109         }
110         return modelAndView;
111     }
112 
113 
114     /**
115      * This method will add the agreement document to the existing list and also stores the attachment
116      *   to the specified path.
117      *
118      * @param uifForm - MaintenanceForm
119      * @return ModelAndView
120      */
121     @RequestMapping(params = "methodToCall=deleteFromTotalAmount")
122     public ModelAndView deleteFromTotalAmount(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
123                                          HttpServletRequest request, HttpServletResponse response) {
124 
125         ModelAndView modelAndView=super.deleteLine(uifForm, result, request, response);
126         MaintenanceForm form = (MaintenanceForm) uifForm;
127         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
128         PatronBillPayment patronBillPayment = (PatronBillPayment)document.getNewMaintainableObject().getDataObject();
129         BigDecimal amount = new BigDecimal(0.0);
130         String billNumber="";
131         Map feeTypeId = new HashMap();
132         if(patronBillPayment.getFeeType() !=null ) {
133             List<FeeType> feeTypes = patronBillPayment.getFeeType();
134             for (int i=0;i<feeTypes.size();i++) {
135                 amount = amount.add(feeTypes.get(i).getFeeAmount());
136                 billNumber = feeTypes.get(i).getBillNumber();
137                 feeTypeId.put("billNumber",billNumber);
138             }
139             patronBillPayment.setTotalAmount(amount);
140             getBusinessObjectService().deleteMatching(FeeType.class,feeTypeId);
141 
142         }
143         return modelAndView;
144     }
145 
146 
147 
148 
149     @RequestMapping(params = "methodToCall=calculateTotal")
150     public ModelAndView calculateTotal(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
151                                          HttpServletRequest request, HttpServletResponse response) throws Exception{
152 
153         MaintenanceForm form = (MaintenanceForm) uifForm;
154         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
155         PatronBillPayment patronBillPayment = (PatronBillPayment)document.getNewMaintainableObject().getDataObject();
156         BigDecimal amount = new BigDecimal(0.0);
157         if(patronBillPayment.getFeeType() !=null ) {
158             List<FeeType> feeTypes = patronBillPayment.getFeeType();
159             for (int i=0;i<feeTypes.size();i++) {
160                 amount = amount.add(feeTypes.get(i).getFeeAmount());
161             }
162             patronBillPayment.setTotalAmount(amount);
163         }
164         return getUIFModelAndView(form);
165     }
166 
167 
168     @RequestMapping(params = "methodToCall=" +"maintenanceDelete")
169     public ModelAndView maintenanceDelete(@ModelAttribute("KualiForm") MaintenanceForm form, BindingResult result,
170                                           HttpServletRequest request, HttpServletResponse response) throws Exception {
171         setupMaintenanceForDelete(form, request,"Delete");
172         return getUIFModelAndView(form);
173     }
174 
175 
176     @RequestMapping(params = "methodToCall=" +"deleteDocument")
177     public ModelAndView deleteDocument(@ModelAttribute("KualiForm") MaintenanceForm form, BindingResult result,
178                                        HttpServletRequest request, HttpServletResponse response) throws Exception {
179         MaintenanceDocument document = form.getDocument();
180         PatronBillPayment patronBillPayment = new PatronBillPayment();
181         //OleProxyPatronDocument proxyPatronDocument = new OleProxyPatronDocument();
182 
183         if(document.getDocumentDataObject() != null) {
184             patronBillPayment = (PatronBillPayment)document.getDocumentDataObject();
185             if(patronBillPayment != null && patronBillPayment.getPatronId() != null) {
186                 KRADServiceLocator.getBusinessObjectService().delete(patronBillPayment);
187                 GlobalVariables.getMessageMap().putInfoWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS,
188                         OLEConstants.OleLicenseRequest.MSG_DELETE_DOC);
189             }
190             else {
191                 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_MESSAGES, OLEConstants.OleLicenseRequest.ERROR_CHECKLIST_NOT_FOUND);
192                 return getUIFModelAndView(form);
193             }
194         }
195         return close(form, result, request, response);
196     }
197 
198 
199 
200     /**
201      * This method populates confirmation to delete the document.
202      * @param form
203      * @param request
204      * @param maintenanceAction
205      */
206     protected void setupMaintenanceForDelete(MaintenanceForm form, HttpServletRequest request, String maintenanceAction) {
207         MaintenanceDocument document = form.getDocument();
208 
209         if (document == null) {
210             document = getMaintenanceDocumentService()
211                     .setupNewMaintenanceDocument(form.getDataObjectClassName(), form.getDocTypeName(),
212                             maintenanceAction);
213 
214             form.setDocument(document);
215             form.setDocTypeName(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
216         }
217 
218         form.setMaintenanceAction(maintenanceAction);
219         OlePatronBillMaintenanceDocumentServiceImpl olePatronBillMaintenanceDocumentService = (OlePatronBillMaintenanceDocumentServiceImpl)getMaintenanceDocumentService();
220         olePatronBillMaintenanceDocumentService.setupMaintenanceObjectForDelete(document, maintenanceAction, request.getParameterMap());
221         MaintenanceUtils.checkForLockingDocument(document, false);
222     }
223 
224     /**
225      * Override route to retrieve the maintenance object if it is an attachment
226      *
227      *      (DocumentFormBase, HttpServletRequest, HttpServletResponse)
228      */
229     @Override
230     @RequestMapping(params = "methodToCall=route")
231     public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
232                               HttpServletRequest request, HttpServletResponse response) {
233 
234         ModelAndView modelAndView;
235 
236         modelAndView = super.route(form, result, request, response);
237         try{
238             calculateTotal(form, result, request, response);
239         }catch(Exception e){
240 
241         }
242 
243         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
244         if (document.getNewMaintainableObject().getDataObject() instanceof PersistableAttachment) {
245             PersistableAttachment bo = (PersistableAttachment) getBusinessObjectService()
246                     .retrieve((PersistableBusinessObject) document.getNewMaintainableObject().getDataObject());
247             request.setAttribute("fileName", bo.getFileName());
248         }
249 
250         modelAndView = getUIFModelAndView(form);
251 
252         return modelAndView;
253     }
254 
255 
256 
257 
258 
259 
260 
261 }