View Javadoc

1   package org.kuali.ole.patron.bill;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.incubator.SolrRequestReponseHandler;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.license.bo.OleEventLogBo;
7   import org.kuali.ole.patron.bo.OlePatronDocument;
8   import org.kuali.ole.patron.bo.OleProxyPatronDocument;
9   import org.kuali.ole.service.impl.OlePatronBillMaintenanceDocumentServiceImpl;
10  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
11  import org.kuali.rice.krad.bo.PersistableAttachment;
12  import org.kuali.rice.krad.bo.PersistableBusinessObject;
13  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
14  import org.kuali.rice.krad.maintenance.MaintenanceUtils;
15  import org.kuali.rice.krad.service.BusinessObjectService;
16  import org.kuali.rice.krad.service.KRADServiceLocator;
17  import org.kuali.rice.krad.service.MaintenanceDocumentService;
18  import org.kuali.rice.krad.uif.UifParameters;
19  import org.kuali.rice.krad.uif.container.CollectionGroup;
20  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
21  import org.kuali.rice.krad.uif.view.View;
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.form.DocumentFormBase;
26  import org.kuali.rice.krad.web.form.MaintenanceForm;
27  import org.kuali.rice.krad.web.form.UifFormBase;
28  import org.springframework.stereotype.Controller;
29  import org.springframework.validation.BindingResult;
30  import org.springframework.web.bind.annotation.ModelAttribute;
31  import org.springframework.web.bind.annotation.RequestMapping;
32  import org.springframework.web.bind.annotation.RequestMethod;
33  import org.springframework.web.servlet.ModelAndView;
34  
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  import java.math.BigDecimal;
38  import java.util.Date;
39  import java.util.HashMap;
40  import java.util.List;
41  import java.util.Map;
42  
43  /**
44   * Created with IntelliJ IDEA.
45   * User: ?
46   * Date: 10/5/12
47   * Time: 2:21 PM
48   * To change this template use File | Settings | File Templates.
49   */
50  @Controller
51  @RequestMapping(value = "/patronBillMaintenance")
52  public class PatronBillMaintenanceDocumentController extends MaintenanceDocumentController {
53  
54      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PatronBillMaintenanceDocumentController.class);
55      private BusinessObjectService businessObjectService;
56  
57      /**
58       * Gets the businessObjectService attribute.
59       *
60       * @return Returns the businessObjectService
61       */
62      public BusinessObjectService getBusinessObjectService() {
63          if (null == businessObjectService) {
64              businessObjectService = KRADServiceLocator.getBusinessObjectService();
65          }
66          return businessObjectService;
67      }
68  
69      /**
70       * Default method for controller that setups a new
71       * <code>MaintenanceView</code> with the default new action
72       */
73      @RequestMapping(params = "methodToCall=" + KRADConstants.Maintenance.METHOD_TO_CALL_NEW)
74      @Override
75      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
76                                HttpServletRequest request, HttpServletResponse response) {
77          Date billDate = new Date();
78          MaintenanceForm maintenanceForm = (MaintenanceForm) form;
79          setupMaintenance(maintenanceForm, request, KRADConstants.MAINTENANCE_NEW_ACTION);
80          MaintenanceDocument document = (MaintenanceDocument) maintenanceForm.getDocument();
81          PatronBillPayment patronBillPayment = (PatronBillPayment) document.getDocumentDataObject();
82          patronBillPayment.setPatronId(request.getParameter(OLEConstants.PTRN_ID));
83          patronBillPayment.setFirstName(request.getParameter(OLEConstants.PTRN_FN));
84          patronBillPayment.setLastName(request.getParameter(OLEConstants.PTRN_LN));
85          Map proxyPatronMap = new HashMap();
86          String firstName = "";
87          String lastName = "";
88          proxyPatronMap.put("olePatronId",request.getParameter(OLEConstants.PTRN_ID));
89          List<OleProxyPatronDocument> oleProxyPatronDocumentList = (List<OleProxyPatronDocument>)KRADServiceLocator.getBusinessObjectService().findMatching(OleProxyPatronDocument.class,proxyPatronMap);
90          String proxyPatronId = "";
91          if(oleProxyPatronDocumentList.size() > 0){
92              proxyPatronId = oleProxyPatronDocumentList.get(0).getProxyPatronId();
93              Map patronMap = new HashMap();
94              patronMap.put("olePatronId",proxyPatronId);
95              List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>)KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class,patronMap);
96              if(olePatronDocumentList.size() > 0){
97                   firstName = olePatronDocumentList.get(0).getEntity().getNames().get(0).getFirstName();
98                   lastName = olePatronDocumentList.get(0).getEntity().getNames().get(0).getLastName();
99                  patronBillPayment.setProxyFirstName(firstName);
100                 patronBillPayment.setProxyLastName(lastName);
101             }
102         }
103         patronBillPayment.setBillDate(billDate);
104         return getUIFModelAndView(maintenanceForm);
105     }
106 
107 
108     /**
109      * This method returns the instance of olePatronBillMaintenanceDocumentService
110      *
111      * @return olePatronMaintenanceDocumentService(MaintenanceDocumentService)
112      */
113     @Override
114     protected MaintenanceDocumentService getMaintenanceDocumentService() {
115         return GlobalResourceLoader.getService("olePatronBillMaintenanceDocumentService");
116     }
117 
118 
119     /**
120      * This method will add the patron bill  document to the existing list and also stores the attachment
121      * to the specified path.
122      *
123      * @param uifForm - MaintenanceForm
124      * @return ModelAndView
125      */
126     @RequestMapping(params = "methodToCall=addToTotalAmount")
127     public ModelAndView addToTotalAmount(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
128                                          HttpServletRequest request, HttpServletResponse response) throws Exception {
129         LOG.debug("Initialized addToTotalAmount method");
130         ModelAndView modelAndView = super.addLine(uifForm, result, request, response);
131         MaintenanceForm form = (MaintenanceForm) uifForm;
132         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
133         PatronBillPayment patronBillPayment = (PatronBillPayment) document.getNewMaintainableObject().getDataObject();
134         BigDecimal amount = new BigDecimal(0.0);
135         if (patronBillPayment.getFeeType() != null) {
136             List<FeeType> feeTypes = patronBillPayment.getFeeType();
137             for (int i = 0; i < feeTypes.size(); i++) {
138                 amount = amount.add(feeTypes.get(i).getFeeAmount());
139             }
140             patronBillPayment.setTotalAmount(amount);
141 
142         }
143         return modelAndView;
144     }
145 
146 
147     /**
148      * Called by the add line action for a new collection line. Method
149      * determines which collection the add action was selected for and invokes
150      * the view helper service to add the line
151      */
152     @Override
153     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=addLine")
154     public ModelAndView addLine(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
155                                 HttpServletRequest request, HttpServletResponse response) {
156         LOG.debug("Initialized addLine method");
157         MaintenanceForm form = (MaintenanceForm) uifForm;
158         String selectedCollectionPath = form.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
159         CollectionGroup collectionGroup = form.getPostedView().getViewIndex().getCollectionGroupByPath(
160                 selectedCollectionPath);
161         String addLinePath = collectionGroup.getAddLineBindingInfo().getBindingPath();
162         SolrRequestReponseHandler solrRequestReponseHandler = new SolrRequestReponseHandler();
163         Object eventObject = ObjectPropertyUtils.getPropertyValue(uifForm, addLinePath);
164         FeeType feeType = (FeeType) eventObject;
165         String itemBarcode = feeType.getItemBarcode();
166         View view = uifForm.getPostedView();
167         if (itemBarcode == "" || !(itemBarcode.isEmpty())) {
168             List<HashMap<String, Object>> documentList = solrRequestReponseHandler.retriveResults(OLEConstants.ITM_BAR_DISP + itemBarcode);
169             if (documentList != null && documentList.size() == 0) {
170                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ITM_BAR_NOT_AVAL);
171                 return updateComponent(uifForm, result, request, response);
172             }
173             GlobalVariables.getMessageMap().removeFromErrorPath(KRADConstants.GLOBAL_ERRORS);
174         }
175         view.getViewHelperService().processCollectionAddLine(view, uifForm, selectedCollectionPath);
176         return updateComponent(uifForm, result, request, response);
177     }
178 
179     /**
180      * This method will add the agreement document to the existing list and also stores the attachment
181      * to the specified path.
182      *
183      * @param uifForm - MaintenanceForm
184      * @return ModelAndView
185      */
186     @RequestMapping(params = "methodToCall=deleteFromTotalAmount")
187     public ModelAndView deleteFromTotalAmount(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
188                                               HttpServletRequest request, HttpServletResponse response) {
189 
190         ModelAndView modelAndView = super.deleteLine(uifForm, result, request, response);
191         MaintenanceForm form = (MaintenanceForm) uifForm;
192         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
193         PatronBillPayment patronBillPayment = (PatronBillPayment) document.getNewMaintainableObject().getDataObject();
194         BigDecimal amount = new BigDecimal(0.0);
195         String billNumber = "";
196         Map feeTypeId = new HashMap();
197         if (patronBillPayment.getFeeType() != null) {
198             List<FeeType> feeTypes = patronBillPayment.getFeeType();
199             for (int i = 0; i < feeTypes.size(); i++) {
200                 amount = amount.add(feeTypes.get(i).getFeeAmount());
201                 billNumber = feeTypes.get(i).getBillNumber();
202                 feeTypeId.put("billNumber", billNumber);
203             }
204             patronBillPayment.setTotalAmount(amount);
205             getBusinessObjectService().deleteMatching(FeeType.class, feeTypeId);
206 
207         }
208         return modelAndView;
209     }
210 
211 
212     /**
213      * This method will add the all fee amount and calculate total
214      *
215      * @param uifForm - MaintenanceForm
216      * @return ModelAndView
217      */
218     @RequestMapping(params = "methodToCall=calculateTotal")
219     public ModelAndView calculateTotal(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
220                                        HttpServletRequest request, HttpServletResponse response) throws Exception {
221 
222         MaintenanceForm form = (MaintenanceForm) uifForm;
223         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
224         PatronBillPayment patronBillPayment = (PatronBillPayment) document.getNewMaintainableObject().getDataObject();
225         BigDecimal amount = new BigDecimal(0.0);
226         if (patronBillPayment.getFeeType() != null) {
227             List<FeeType> feeTypes = patronBillPayment.getFeeType();
228             for (int i = 0; i < feeTypes.size(); i++) {
229                 amount = amount.add(feeTypes.get(i).getFeeAmount());
230             }
231             patronBillPayment.setTotalAmount(amount);
232         }
233         return getUIFModelAndView(form);
234     }
235 
236 
237     @RequestMapping(params = "methodToCall=" + "maintenanceDelete")
238     public ModelAndView maintenanceDelete(@ModelAttribute("KualiForm") MaintenanceForm form, BindingResult result,
239                                           HttpServletRequest request, HttpServletResponse response) throws Exception {
240         setupMaintenanceForDelete(form, request, "Delete");
241         return getUIFModelAndView(form);
242     }
243 
244 
245     @RequestMapping(params = "methodToCall=" + "deleteDocument")
246     public ModelAndView deleteDocument(@ModelAttribute("KualiForm") MaintenanceForm form, BindingResult result,
247                                        HttpServletRequest request, HttpServletResponse response) throws Exception {
248         MaintenanceDocument document = form.getDocument();
249         PatronBillPayment patronBillPayment = new PatronBillPayment();
250         //OleProxyPatronDocument proxyPatronDocument = new OleProxyPatronDocument();
251 
252         if (document.getDocumentDataObject() != null) {
253             patronBillPayment = (PatronBillPayment) document.getDocumentDataObject();
254             if (patronBillPayment != null && patronBillPayment.getPatronId() != null) {
255                 KRADServiceLocator.getBusinessObjectService().delete(patronBillPayment);
256                 GlobalVariables.getMessageMap().putInfoWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS,
257                         OLEConstants.OleLicenseRequest.MSG_DELETE_DOC);
258             } else {
259                 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_MESSAGES, OLEConstants.OleLicenseRequest.ERROR_CHECKLIST_NOT_FOUND);
260                 return getUIFModelAndView(form);
261             }
262         }
263         return close(form, result, request, response);
264     }
265 
266 
267     /**
268      * This method populates confirmation to delete the document.
269      *
270      * @param form
271      * @param request
272      * @param maintenanceAction
273      */
274     protected void setupMaintenanceForDelete(MaintenanceForm form, HttpServletRequest request, String maintenanceAction) {
275         MaintenanceDocument document = form.getDocument();
276 
277         if (document == null) {
278             document = getMaintenanceDocumentService()
279                     .setupNewMaintenanceDocument(form.getDataObjectClassName(), form.getDocTypeName(),
280                             maintenanceAction);
281 
282             form.setDocument(document);
283             form.setDocTypeName(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
284         }
285 
286         form.setMaintenanceAction(maintenanceAction);
287         OlePatronBillMaintenanceDocumentServiceImpl olePatronBillMaintenanceDocumentService = (OlePatronBillMaintenanceDocumentServiceImpl) getMaintenanceDocumentService();
288         olePatronBillMaintenanceDocumentService.setupMaintenanceObjectForDelete(document, maintenanceAction, request.getParameterMap());
289         MaintenanceUtils.checkForLockingDocument(document, false);
290     }
291 
292     /**
293      * Override route to retrieve the calculated total amount
294      * <p/>
295      * (DocumentFormBase, HttpServletRequest, HttpServletResponse)
296      */
297     @Override
298    @RequestMapping(params = "methodToCall=route")
299     public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
300                               HttpServletRequest request, HttpServletResponse response) {
301         ModelAndView modelAndView;
302         MaintenanceForm maintenanceForm = (MaintenanceForm) form;
303 
304        try {
305             calculateTotal(form, result, request, response);
306         } catch (Exception e) {
307             e.printStackTrace();
308         }
309         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
310         PatronBillPayment patronBillPayment = (PatronBillPayment) document.getNewMaintainableObject().getDataObject();
311         if (patronBillPayment.getFeeType() != null) {
312             List<FeeType> feeTypes = patronBillPayment.getFeeType();
313             if(feeTypes.size() ==0){
314                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ADD_FEE_TYPE);
315                 return getUIFModelAndView(maintenanceForm);
316             }
317         }
318         if (document.getNewMaintainableObject().getDataObject() instanceof PersistableAttachment) {
319             PersistableAttachment bo = (PersistableAttachment) getBusinessObjectService()
320                     .retrieve((PersistableBusinessObject) document.getNewMaintainableObject().getDataObject());
321             request.setAttribute("fileName", bo.getFileName());
322         }
323         //modelAndView = getUIFModelAndView(form);
324         modelAndView = super.route(maintenanceForm, result, request, response);
325         return modelAndView;
326     }
327 }