001package org.kuali.ole.deliver.controller;
002
003import org.apache.log4j.Logger;
004import org.kuali.ole.OLEConstants;
005import org.kuali.ole.deliver.bo.PatronBillPayment;
006import org.kuali.ole.deliver.bo.PatronBillReview;
007import org.kuali.ole.deliver.form.PatronBillReviewForm;
008import org.kuali.rice.krad.service.BusinessObjectService;
009import org.kuali.rice.krad.service.KRADServiceLocator;
010import org.kuali.rice.krad.util.GlobalVariables;
011import org.kuali.rice.krad.util.KRADConstants;
012import org.kuali.rice.krad.web.controller.UifControllerBase;
013import org.kuali.rice.krad.web.form.UifFormBase;
014import org.springframework.stereotype.Controller;
015import org.springframework.validation.BindingResult;
016import org.springframework.web.bind.annotation.ModelAttribute;
017import org.springframework.web.bind.annotation.RequestMapping;
018import org.springframework.web.servlet.ModelAndView;
019
020import javax.servlet.http.HttpServletRequest;
021import javax.servlet.http.HttpServletResponse;
022import java.util.HashMap;
023import java.util.Iterator;
024import java.util.List;
025import java.util.Map;
026
027/**
028 * Created with IntelliJ IDEA.
029 * User: ?
030 * Date: 12/6/12
031 * Time: 2:53 PM
032 * To change this template use File | Settings | File Templates.
033 */
034@Controller
035@RequestMapping(value = "/patronBillReview")
036public class PatronBillReviewController extends UifControllerBase {
037
038    private static final Logger LOG = Logger.getLogger(org.kuali.ole.deliver.controller.PatronBillReviewController.class);
039
040
041    private BusinessObjectService businessObjectService;
042
043    /**
044     * Gets the businessObjectService attribute.
045     *
046     * @return Returns the businessObjectService
047     */
048    private BusinessObjectService getBusinessObjectService() {
049        if (null == businessObjectService) {
050            businessObjectService = KRADServiceLocator.getBusinessObjectService();
051        }
052        return businessObjectService;
053    }
054
055    /**
056     * This method creates new PatronBillReview form
057     *
058     * @param request
059     * @return PatronBillReviewForm
060     */
061    @Override
062    protected PatronBillReviewForm createInitialForm(HttpServletRequest request) {
063        return new PatronBillReviewForm();
064    }
065
066    /**
067     * This method takes the initial request when click(Patron Bill) on Patron Screen.
068     *
069     * @param form
070     * @param result
071     * @param request
072     * @param response
073     * @return ModelAndView
074     */
075    @Override
076    @RequestMapping(params = "methodToCall=start")
077    public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
078                              HttpServletRequest request, HttpServletResponse response) {
079        PatronBillReviewForm patronBillReviewForm = (PatronBillReviewForm) form;
080        PatronBillReview patronBillReview = new PatronBillReview();
081        List<PatronBillReviewForm> patronBillReviewFormList = patronBillReview.getPatronBill();
082        if (patronBillReviewFormList.size() == 0) {
083            GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.NO_BILLS);
084        }
085        patronBillReviewForm.setPatronBillReviewFormList(patronBillReviewFormList);
086
087        List<PatronBillReviewForm> patronBillReviewedFormList = patronBillReview.getPatronReviewedBill();
088        patronBillReviewForm.setPatronBillReviewedFormList(patronBillReviewedFormList);
089        return super.start(patronBillReviewForm, result, request, response);
090    }
091
092
093    @RequestMapping(params = "methodToCall=review")
094    public ModelAndView billReviewed(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
095                                     HttpServletRequest request, HttpServletResponse response) {
096        PatronBillReviewForm patronBillReviewForm = (PatronBillReviewForm) form;
097        List<PatronBillReviewForm> patronBillReviewFormList = patronBillReviewForm.getPatronBillReviewFormList();
098        Iterator itr = patronBillReviewFormList.iterator();
099        PatronBillReviewForm patronBillReviewForms;
100        int count = 0;
101        while (itr.hasNext()) {
102            patronBillReviewForms = (PatronBillReviewForm) itr.next();
103            boolean billReview = patronBillReviewForms.isSelect();
104            if (billReview) {
105                count += 1;
106                String billNumber = patronBillReviewForms.getBillNumber();
107                Map billNumberMap = new HashMap();
108                billNumberMap.put("billNumber", billNumber);
109                PatronBillPayment patronBillPayment = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(PatronBillPayment.class, billNumberMap);
110                patronBillPayment.setReviewed(true);
111                getBusinessObjectService().save(patronBillPayment);
112            }
113        }
114        if (count == 0) {
115            GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.SELECT);
116        }
117
118        return start(patronBillReviewForm, result, request, response);
119    }
120}
121
122
123
124