001package org.kuali.ole.deliver.controller;
002
003import org.apache.commons.collections.CollectionUtils;
004import org.apache.commons.lang3.StringUtils;
005import org.kuali.ole.OLEConstants;
006import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
007import org.kuali.ole.deliver.form.OLEItemRequestedRecordsForm;
008import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl;
009import org.kuali.rice.krad.service.KRADServiceLocator;
010import org.kuali.rice.krad.web.controller.UifControllerBase;
011import org.kuali.rice.krad.web.form.UifFormBase;
012import org.springframework.stereotype.Controller;
013import org.springframework.validation.BindingResult;
014import org.springframework.web.bind.annotation.ModelAttribute;
015import org.springframework.web.bind.annotation.RequestMapping;
016import org.springframework.web.servlet.ModelAndView;
017
018import javax.servlet.http.HttpServletRequest;
019import javax.servlet.http.HttpServletResponse;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024/**
025 * Created by chenchulakshmig on 1/23/15.
026 */
027@Controller
028@RequestMapping(value = "/itemRequestedRecord")
029public class OLEItemRequestedRecordsController extends UifControllerBase {
030
031    private OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService;
032
033    public OleDeliverRequestDocumentHelperServiceImpl getOleDeliverRequestDocumentHelperService() {
034        if (oleDeliverRequestDocumentHelperService == null) {
035            oleDeliverRequestDocumentHelperService = new OleDeliverRequestDocumentHelperServiceImpl();
036        }
037        return oleDeliverRequestDocumentHelperService;
038    }
039
040    @Override
041    protected UifFormBase createInitialForm(HttpServletRequest request) {
042        return new OLEItemRequestedRecordsForm();
043    }
044
045    @RequestMapping(params = "methodToCall=viewRequestedRecords")
046    public ModelAndView viewRequestedRecords(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
047                                             HttpServletRequest request, HttpServletResponse response) throws Exception {
048        OLEItemRequestedRecordsForm oleItemRequestedRecordsForm = (OLEItemRequestedRecordsForm) form;
049        String itemBarcode = request.getParameter(OLEConstants.OleDeliverRequest.ITEM_BARCODE);
050        if (StringUtils.isNotBlank(itemBarcode)) {
051            Map itemMap = new HashMap();
052            itemMap.put(OLEConstants.OleDeliverRequest.ITEM_ID, itemBarcode);
053            List<OleDeliverRequestBo> deliverRequestBos = (List<OleDeliverRequestBo>) KRADServiceLocator.getBusinessObjectService().findMatching(OleDeliverRequestBo.class, itemMap);
054            if (CollectionUtils.isNotEmpty(deliverRequestBos)) {
055                for (int i = 0; i < deliverRequestBos.size(); i++) {
056                    getOleDeliverRequestDocumentHelperService().processItem(deliverRequestBos.get(i));
057                }
058                oleItemRequestedRecordsForm.setRequestBos(deliverRequestBos);
059            }
060        }
061        return getUIFModelAndView(oleItemRequestedRecordsForm, "OLEItemRequestedRecordPage");
062    }
063}