001package org.kuali.ole.deliver.controller;
002
003import org.apache.log4j.Logger;
004import org.kuali.ole.OLEConstants;
005import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
006import org.kuali.ole.deliver.form.OleDeliverRequestReOrderForm;
007import org.kuali.ole.deliver.processor.LoanProcessor;
008import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl;
009import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
010
011import org.kuali.ole.docstore.common.document.ItemOleml;
012import org.kuali.ole.docstore.common.search.SearchResponse;
013import org.kuali.ole.docstore.common.search.SearchResult;
014import org.kuali.ole.docstore.common.search.SearchResultField;
015
016import org.kuali.ole.sys.context.SpringContext;
017import org.kuali.rice.krad.service.KRADServiceLocator;
018import org.kuali.rice.krad.web.controller.UifControllerBase;
019import org.kuali.rice.krad.web.form.UifFormBase;
020import org.springframework.stereotype.Controller;
021import org.springframework.validation.BindingResult;
022import org.springframework.web.bind.annotation.ModelAttribute;
023import org.springframework.web.bind.annotation.RequestMapping;
024import org.springframework.web.servlet.ModelAndView;
025
026import javax.servlet.http.HttpServletRequest;
027import javax.servlet.http.HttpServletResponse;
028import java.util.HashMap;
029import java.util.List;
030import java.util.Map;
031
032/**
033 * Created with IntelliJ IDEA.
034 * User: ?
035 * Date: 10/15/12
036 * Time: 10:31 AM
037 * To change this template use File | Settings | File Templates.
038 */
039@Controller
040@RequestMapping(value = "/deliverRequestController")
041public class OleDeliverRequestReOrderController extends UifControllerBase {
042    private OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService = new OleDeliverRequestDocumentHelperServiceImpl();
043    private LoanProcessor loanProcessor = new LoanProcessor();
044    private DocstoreClientLocator docstoreClientLocator;
045
046    public DocstoreClientLocator getDocstoreClientLocator() {
047
048        if (docstoreClientLocator == null) {
049            docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
050
051        }
052        return docstoreClientLocator;
053    }
054
055
056    public OleDeliverRequestDocumentHelperServiceImpl getOleDeliverRequestDocumentHelperService() {
057        return oleDeliverRequestDocumentHelperService;
058    }
059
060    public void setOleDeliverRequestDocumentHelperService(OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService) {
061        this.oleDeliverRequestDocumentHelperService = oleDeliverRequestDocumentHelperService;
062    }
063
064    public LoanProcessor getLoanProcessor() {
065        return loanProcessor;
066    }
067
068    public void setLoanProcessor(LoanProcessor loanProcessor) {
069        this.loanProcessor = loanProcessor;
070    }
071
072    private static final Logger LOG = Logger.getLogger(OleDeliverRequestReOrderController.class);
073
074    /**
075     * This method creates new OleDeliverRequestReOrderForm form
076     *
077     * @param request
078     * @return OleDeliverRequestReOrderForm
079     */
080    @Override
081    protected OleDeliverRequestReOrderForm createInitialForm(HttpServletRequest request) {
082        return new OleDeliverRequestReOrderForm();
083    }
084
085    @Override
086    @RequestMapping(params = "methodToCall=start")
087    public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
088                              HttpServletRequest request, HttpServletResponse response) {
089        LOG.debug("Inside start method");
090        OleDeliverRequestReOrderForm oleDeliverRequestForm = (OleDeliverRequestReOrderForm) form;
091        return super.start(oleDeliverRequestForm, result, request, response);
092    }
093
094
095    @RequestMapping(params = "methodToCall=search")
096    public ModelAndView search(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
097                               HttpServletRequest request, HttpServletResponse response) {
098        LOG.debug("Inside search method");
099        OleDeliverRequestReOrderForm oleDeliverRequestForm = (OleDeliverRequestReOrderForm) form;
100        oleDeliverRequestForm.setMessage(null);
101        String itemId = oleDeliverRequestForm.getItemId();
102        String itemUuid = oleDeliverRequestForm.getItemUuid();
103        if (itemUuid == null || (itemUuid != null && itemUuid.isEmpty())) {
104            try {
105
106
107                org.kuali.ole.docstore.common.document.Item item = new ItemOleml();
108                org.kuali.ole.docstore.common.search.SearchParams search_Params = new org.kuali.ole.docstore.common.search.SearchParams();
109                SearchResponse searchResponse = null;
110                search_Params.getSearchConditions().add(search_Params.buildSearchCondition("phrase", search_Params.buildSearchField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), item.ITEM_BARCODE, itemId), ""));
111
112
113                search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.ITEM.getCode(), "id"));
114
115
116                searchResponse = getDocstoreClientLocator().getDocstoreClient().search(search_Params);
117                for (SearchResult searchResult : searchResponse.getSearchResults()) {
118                    for (SearchResultField searchResultField : searchResult.getSearchResultFields()) {
119                        String fieldName = searchResultField.getFieldName();
120                        String fieldValue = searchResultField.getFieldValue() != null ? searchResultField.getFieldValue() : "";
121
122                        if (fieldName.equalsIgnoreCase("id") && !fieldValue.isEmpty() && searchResultField.getDocType().equalsIgnoreCase("item")) {
123                            itemUuid = fieldValue;
124                        }
125                    }
126                }
127
128
129                // itemUuid = itemUUIDMap.get(OLEConstants.ITEM_UUID);
130
131            } catch (Exception e) {
132                LOG.error("Exception", e);  //To change body of catch statement use File | Settings | File Templates.
133            }
134        }
135
136        Map<String, String> itemMap = new HashMap<String, String>();
137        itemMap.put(OLEConstants.ITEM_UUID, itemUuid);
138        List<OleDeliverRequestBo> itemList = (List<OleDeliverRequestBo>) KRADServiceLocator.getBusinessObjectService().findMatchingOrderBy(OleDeliverRequestBo.class, itemMap, OLEConstants.OleDeliverRequest.QUEUE_POSITION, true);
139        if (itemList.size() > 0) {
140            for (int i = 0; i < itemList.size(); i++) {
141                oleDeliverRequestDocumentHelperService.processItem(itemList.get(i));
142               /* itemList.get(i).setOleItemSearch(oleDeliverRequestDocumentHelperService.getItemDetails(itemList.get(i).getItemUuid()));*/
143            }
144        }
145        if (itemList.size() == 0) {
146            oleDeliverRequestForm.setMessage(OLEConstants.OleDeliverRequest.NO_PENDING_REQUEST);
147        }
148        oleDeliverRequestForm.setDeliverRequestBos(itemList);
149        return getUIFModelAndView(oleDeliverRequestForm, "DeliverRequestSearchPage");
150    }
151
152    @RequestMapping(params = "methodToCall=reOrder")
153    public ModelAndView reOrder(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
154                                HttpServletRequest request, HttpServletResponse response) {
155        LOG.debug("Inside reOrder method");
156        OleDeliverRequestReOrderForm oleDeliverRequestForm = (OleDeliverRequestReOrderForm) form;
157        List<OleDeliverRequestBo> itemList = (List<OleDeliverRequestBo>) oleDeliverRequestForm.getDeliverRequestBos();
158
159        OleDeliverRequestDocumentHelperServiceImpl service = new OleDeliverRequestDocumentHelperServiceImpl();
160        if (itemList.size() > 0) {
161            String message = service.validateQueuePosition(itemList);
162            if (!message.equals(OLEConstants.OleDeliverRequest.REORDER_SUCCESS)) {
163                oleDeliverRequestForm.setMessage(message);
164            } else {
165                KRADServiceLocator.getBusinessObjectService().save(itemList);
166                oleDeliverRequestForm.setDeliverRequestBos(null);
167                oleDeliverRequestForm.setMessage(message);
168            }
169        }
170        return getUIFModelAndView(oleDeliverRequestForm, "DeliverRequestSearchPage");
171    }
172
173    @RequestMapping(params = "methodToCall=refresh")
174    public ModelAndView refresh(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
175                                HttpServletRequest request, HttpServletResponse response) throws Exception {
176        OleDeliverRequestReOrderForm oleDeliverRequestForm = (OleDeliverRequestReOrderForm) form;
177        ((OleDeliverRequestReOrderForm) form).setMessage(null);
178        super.refresh(form, result, request, response);
179        return search(form, result, request, response);
180    }
181
182}