1 package org.kuali.ole.deliver.controller;
2
3 import org.apache.log4j.Logger;
4 import org.kuali.ole.OLEConstants;
5 import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
6 import org.kuali.ole.deliver.form.OleDeliverRequestReOrderForm;
7 import org.kuali.ole.deliver.processor.LoanProcessor;
8 import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl;
9 import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
10
11 import org.kuali.ole.docstore.common.document.ItemOleml;
12 import org.kuali.ole.docstore.common.search.SearchResponse;
13 import org.kuali.ole.docstore.common.search.SearchResult;
14 import org.kuali.ole.docstore.common.search.SearchResultField;
15
16 import org.kuali.ole.sys.context.SpringContext;
17 import org.kuali.rice.krad.service.KRADServiceLocator;
18 import org.kuali.rice.krad.web.controller.UifControllerBase;
19 import org.kuali.rice.krad.web.form.UifFormBase;
20 import org.springframework.stereotype.Controller;
21 import org.springframework.validation.BindingResult;
22 import org.springframework.web.bind.annotation.ModelAttribute;
23 import org.springframework.web.bind.annotation.RequestMapping;
24 import org.springframework.web.servlet.ModelAndView;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31
32
33
34
35
36
37
38
39 @Controller
40 @RequestMapping(value = "/deliverRequestController")
41 public class OleDeliverRequestReOrderController extends UifControllerBase {
42 private OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService = new OleDeliverRequestDocumentHelperServiceImpl();
43 private LoanProcessor loanProcessor = new LoanProcessor();
44 private DocstoreClientLocator docstoreClientLocator;
45
46 public DocstoreClientLocator getDocstoreClientLocator() {
47
48 if (docstoreClientLocator == null) {
49 docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
50
51 }
52 return docstoreClientLocator;
53 }
54
55
56 public OleDeliverRequestDocumentHelperServiceImpl getOleDeliverRequestDocumentHelperService() {
57 return oleDeliverRequestDocumentHelperService;
58 }
59
60 public void setOleDeliverRequestDocumentHelperService(OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService) {
61 this.oleDeliverRequestDocumentHelperService = oleDeliverRequestDocumentHelperService;
62 }
63
64 public LoanProcessor getLoanProcessor() {
65 return loanProcessor;
66 }
67
68 public void setLoanProcessor(LoanProcessor loanProcessor) {
69 this.loanProcessor = loanProcessor;
70 }
71
72 private static final Logger LOG = Logger.getLogger(OleDeliverRequestReOrderController.class);
73
74
75
76
77
78
79
80 @Override
81 protected OleDeliverRequestReOrderForm createInitialForm(HttpServletRequest request) {
82 return new OleDeliverRequestReOrderForm();
83 }
84
85 @Override
86 @RequestMapping(params = "methodToCall=start")
87 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
88 HttpServletRequest request, HttpServletResponse response) {
89 LOG.debug("Inside start method");
90 OleDeliverRequestReOrderForm oleDeliverRequestForm = (OleDeliverRequestReOrderForm) form;
91 return super.start(oleDeliverRequestForm, result, request, response);
92 }
93
94
95 @RequestMapping(params = "methodToCall=search")
96 public ModelAndView search(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
97 HttpServletRequest request, HttpServletResponse response) {
98 LOG.debug("Inside search method");
99 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
130
131 } catch (Exception e) {
132 LOG.error("Exception", e);
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
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 }