View Javadoc

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