001package org.kuali.ole.deliver.controller;
002
003import org.apache.log4j.Logger;
004import org.kuali.ole.deliver.api.OleDeliverRequestDefinition;
005import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
006import org.kuali.ole.deliver.form.OleMyAccountRequestForm;
007import org.kuali.ole.deliver.service.OleMyAccountProcess;
008import org.kuali.ole.service.OlePatronHelperService;
009import org.kuali.ole.service.OlePatronHelperServiceImpl;
010import org.kuali.rice.krad.service.BusinessObjectService;
011import org.kuali.rice.krad.web.controller.UifControllerBase;
012import org.kuali.rice.krad.web.form.UifFormBase;
013import org.springframework.stereotype.Controller;
014import org.springframework.validation.BindingResult;
015import org.springframework.web.bind.annotation.ModelAttribute;
016import org.springframework.web.bind.annotation.RequestMapping;
017import org.springframework.web.servlet.ModelAndView;
018
019import javax.servlet.http.HttpServletRequest;
020import javax.servlet.http.HttpServletResponse;
021import java.util.ArrayList;
022import java.util.List;
023
024/**
025 * The OleMyAccountController is the controller class for processing all the actions that corresponds to the renewal by patron  functionality in OLE.
026 * The request mapping tag takes care of mapping the individual action to the corresponding functions.
027 */
028@Controller
029@RequestMapping(value = "/myaccountrequestcontroller")
030public class OleMyAccountRequestController extends UifControllerBase {
031
032    private static final Logger LOG = Logger.getLogger(OleMyAccountRequestController.class);
033
034    private BusinessObjectService boService;
035    private OleMyAccountProcess oleMyAccountProcess;
036    private OlePatronHelperService olePatronHelperService = new OlePatronHelperServiceImpl();
037
038    /**
039     * This method creates new OleMyAccountRequestForm form
040     *
041     * @param request
042     * @return OleMyAccountRequestForm
043     */
044    @Override
045    protected OleMyAccountRequestForm createInitialForm(HttpServletRequest request) {
046        return new OleMyAccountRequestForm();
047    }
048
049    /**
050     * This method converts UifFormBase to OleMyAccountRequestForm
051     *
052     * @param form
053     * @param result
054     * @param request
055     * @param response
056     * @return ModelAndView
057     */
058    @Override
059    @RequestMapping(params = "methodToCall=start")
060    public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
061                              HttpServletRequest request, HttpServletResponse response) {
062        OleMyAccountRequestForm oleMyAccountRequestForm = (OleMyAccountRequestForm) form;
063        return super.start(oleMyAccountRequestForm, result, request, response);
064    }
065
066    @RequestMapping(params = "methodToCall=requestRecord")
067    public ModelAndView requestRecord(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
068                                      HttpServletRequest request, HttpServletResponse response) throws Exception {
069        LOG.debug("Inside requestRecord method");
070        String olePatronId = request.getParameter("olePatronId");
071        OleMyAccountRequestForm oleMyAccountRequestForm = (OleMyAccountRequestForm) form;
072        try {
073            List<OleDeliverRequestDefinition> oleDeliverRequestDefinitions = getOleMyAccountProcess().getPatronRequestItems(olePatronId);
074            List<OleDeliverRequestBo> oleDeliverRequestBos = new ArrayList<OleDeliverRequestBo>();
075            for (OleDeliverRequestDefinition oleDeliverRequestDefinition : oleDeliverRequestDefinitions) {
076                OleDeliverRequestBo oleDeliverRequestBo = OleDeliverRequestBo.from(oleDeliverRequestDefinition);
077                oleDeliverRequestBos.add(oleDeliverRequestBo);
078            }
079            oleMyAccountRequestForm.setOleDeliverRequestBos(oleDeliverRequestBos);
080        } catch (Exception e) {
081            LOG.error("request record from Myaccount------->" + e.getMessage(), e);
082        }
083        return getUIFModelAndView(oleMyAccountRequestForm, "OlePatronRequestRecordPage");
084    }
085
086    @RequestMapping(params = "methodToCall=cancelRequest")
087    public ModelAndView cancelRequest(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
088                                      HttpServletRequest request, HttpServletResponse response) throws Exception {
089        LOG.debug("Inside cancelRequest method");
090        OleMyAccountRequestForm oleMyAccountRequestForm = (OleMyAccountRequestForm) form;
091        String olePatronId = request.getParameter("olePatronId");
092        List<OleDeliverRequestBo> oleDeliverRequestBo = oleMyAccountRequestForm.getOleDeliverRequestBos();
093        List<OleDeliverRequestDefinition> oleDeliverRequestDefinitions = new ArrayList<OleDeliverRequestDefinition>();
094        List<OleDeliverRequestDefinition> oleDeliverRequestDefinitionNew = new ArrayList<OleDeliverRequestDefinition>();
095        try {
096            for (OleDeliverRequestBo oleDeliverRequest : oleDeliverRequestBo) {
097                if (oleDeliverRequest.isRequestFlag()) {
098                    OleDeliverRequestDefinition oleDeliverRequestDefinition = OleDeliverRequestBo.to(oleDeliverRequest);
099                    oleDeliverRequestDefinitions.add(oleDeliverRequestDefinition);
100                } else {
101                    OleDeliverRequestDefinition oleDeliverRequestDefinition = OleDeliverRequestBo.to(oleDeliverRequest);
102                    oleDeliverRequestDefinitionNew.add(oleDeliverRequestDefinition);
103                }
104            }
105            getOleMyAccountProcess().cancelRequest(oleDeliverRequestDefinitions);
106            List<OleDeliverRequestBo> oleDeliverRequestBos = new ArrayList<OleDeliverRequestBo>();
107            OleDeliverRequestBo oleDeliverRequestBoNew = new OleDeliverRequestBo();
108            for (OleDeliverRequestDefinition oleDeliverRequestDefinition : oleDeliverRequestDefinitionNew) {
109                oleDeliverRequestBoNew = OleDeliverRequestBo.from(oleDeliverRequestDefinition);
110                oleDeliverRequestBos.add(oleDeliverRequestBoNew);
111            }
112            oleMyAccountRequestForm.setOleDeliverRequestBos(oleDeliverRequestBos);
113        } catch (Exception e) {
114            LOG.error("cancel request from Myaccount------->" + e.getMessage(), e);
115        }
116        return getUIFModelAndView(oleMyAccountRequestForm, "OlePatronRequestRecordPage");
117    }
118
119    public OleMyAccountProcess getOleMyAccountProcess() {
120
121        if (oleMyAccountProcess == null)
122            oleMyAccountProcess = new OleMyAccountProcess();
123        return oleMyAccountProcess;
124    }
125
126}