View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.select.document.web;
17  
18  import org.apache.struts.action.ActionForm;
19  import org.apache.struts.action.ActionForward;
20  import org.apache.struts.action.ActionMapping;
21  import org.kuali.ole.batch.service.OLEClaimNoticeService;
22  import org.kuali.ole.select.OleSelectConstant;
23  import org.kuali.ole.select.businessobject.OleLineItemReceivingItem;
24  import org.kuali.ole.select.businessobject.OlePurchaseOrderItem;
25  import org.kuali.ole.select.businessobject.OleReceiptStatus;
26  import org.kuali.ole.select.document.OleLineItemReceivingDocument;
27  import org.kuali.ole.select.document.OleReceivingQueueSearchDocument;
28  import org.kuali.ole.select.document.service.OleLineItemReceivingService;
29  import org.kuali.ole.sys.OLEConstants;
30  import org.kuali.ole.sys.OLEKeyConstants;
31  import org.kuali.ole.sys.context.SpringContext;
32  import org.kuali.rice.core.api.config.property.ConfigurationService;
33  import org.kuali.rice.core.api.datetime.DateTimeService;
34  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
35  import org.kuali.rice.core.api.util.RiceConstants;
36  import org.kuali.rice.kew.actionlist.service.ActionListService;
37  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
38  import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
39  import org.kuali.rice.kew.api.KewApiConstants;
40  import org.kuali.rice.kew.api.exception.WorkflowException;
41  import org.kuali.rice.kew.service.KEWServiceLocator;
42  import org.kuali.rice.kns.web.struts.action.KualiTransactionalDocumentActionBase;
43  import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
44  import org.kuali.rice.krad.document.Document;
45  import org.kuali.rice.krad.service.BusinessObjectService;
46  import org.kuali.rice.krad.service.KRADServiceLocator;
47  import org.kuali.rice.krad.util.GlobalVariables;
48  import org.kuali.rice.krad.util.KRADConstants;
49  import org.kuali.rice.krad.util.ObjectUtils;
50  
51  import javax.servlet.http.HttpServletRequest;
52  import javax.servlet.http.HttpServletResponse;
53  import java.sql.Timestamp;
54  import java.text.ParseException;
55  import java.text.SimpleDateFormat;
56  import java.util.*;
57  import java.util.regex.Matcher;
58  import java.util.regex.Pattern;
59  
60  public class OleReceivingQueueSearchAction extends KualiTransactionalDocumentActionBase {
61  
62      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleReceivingQueueSearchAction.class);
63      protected Document document;
64      private static transient OleLineItemReceivingService oleLineItemReceivingService;
65  
66      /**
67       * Constructs a OleReceivingQueueSearchAction.java.
68       */
69      public OleReceivingQueueSearchAction() {
70          super();
71      }
72  
73      /**
74       * This method Searches and retrieves the purchase order result
75       *
76       * @param mapping
77       * @param form
78       * @param request
79       * @param response
80       * @return
81       * @throws Exception
82       */
83      public ActionForward valueSearch(ActionMapping mapping, ActionForm form,
84                                       HttpServletRequest request, HttpServletResponse response) throws Exception {
85          OleReceivingQueueSearchForm oleReceivingQueueSearchForm = (OleReceivingQueueSearchForm) form;
86          OleReceivingQueueSearchDocument oleReceivingQueueSearchDocument = (OleReceivingQueueSearchDocument) oleReceivingQueueSearchForm.getDocument();
87          oleReceivingQueueSearchDocument.setReceivingDocumentsList(new ArrayList<String>());
88          String purchaseOrderString = oleReceivingQueueSearchDocument.getPurchaseOrderNumber();
89          String purchaseOrderStatus=oleReceivingQueueSearchDocument.getPurchaseOrderStatusDescription()!=null?oleReceivingQueueSearchDocument.getPurchaseOrderStatusDescription():"";
90          String datePattern = "\\d{2}/\\d{2}/\\d{4}";
91          boolean isValid = true;
92          if ((oleReceivingQueueSearchDocument.getEndDate() != null && !oleReceivingQueueSearchDocument.getEndDate().equalsIgnoreCase(""))) {
93              if (!oleReceivingQueueSearchDocument.getEndDate().matches(datePattern)) {
94                  oleReceivingQueueSearchDocument.getPurchaseOrderItems().clear();
95                  GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ERROR_SELECT_INVALID_DATE, new String[]{"PO To Date"});
96                  isValid = false;
97              }
98              try {
99                  SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
100                 sdf.setLenient(false);
101                 Date date = sdf.parse(oleReceivingQueueSearchDocument.getEndDate());
102 
103             } catch (ParseException e) {
104                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ERROR_SELECT_INVALID_DATE, new String[]{"PO To Date"});
105                 isValid = false;
106             }
107         }
108         if ((oleReceivingQueueSearchDocument.getBeginDate() != null && !oleReceivingQueueSearchDocument.getBeginDate().equalsIgnoreCase(""))) {
109             if (!oleReceivingQueueSearchDocument.getBeginDate().matches(datePattern)) {
110                 oleReceivingQueueSearchDocument.getPurchaseOrderItems().clear();
111                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ERROR_SELECT_INVALID_DATE, new String[]{"PO From Date"});
112                 isValid = false;
113             }
114             try {
115                 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
116                 sdf.setLenient(false);
117                 Date date = sdf.parse(oleReceivingQueueSearchDocument.getBeginDate());
118             } catch (ParseException e) {
119                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ERROR_SELECT_INVALID_DATE, new String[]{"PO From Date"});
120                 isValid = false;
121             }
122         }
123         if (!isValid) {
124             return mapping.findForward(RiceConstants.MAPPING_BASIC);
125         }
126         if (purchaseOrderString != null) {
127             boolean isValidSting = true;
128             String[] purchaseOrderNumbers = purchaseOrderString.split(",");
129             for (String purchaseOrderNumber : purchaseOrderNumbers) {
130                 if (!validate(purchaseOrderNumber)) {
131                     isValidSting = false;
132                     break;
133                 }
134             }
135             if (!isValidSting) {
136                 oleReceivingQueueSearchDocument.getPurchaseOrderItems().clear();
137                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ERROR_SELECT_PO_ITM_FORMAT);
138                 return mapping.findForward(RiceConstants.MAPPING_BASIC);
139             }
140         }
141         if(purchaseOrderStatus.equalsIgnoreCase(OleSelectConstant.CLOSED)){
142             oleReceivingQueueSearchDocument.getPurchaseOrderItems().clear();
143             GlobalVariables.getMessageMap().putInfo(OleSelectConstant.RECEIVING_QUEUE_SEARCH,
144                     OLEKeyConstants.ERROR_NO_PURCHASEORDERS_FOUND_FOR_CLOSED);
145             //return mapping.findForward(RiceConstants.MAPPING_BASIC);
146 
147         }else{
148             //oleReceivingQueueSearchDocument.receiveingQueueRecordSearch();
149             oleReceivingQueueSearchDocument.receiveingQueueRecordSearchs();
150         }
151         //request.setAttribute(OLEConstants.OrderQueue.CONSTANTS, (KewApiConstants.class));
152         return mapping.findForward(RiceConstants.MAPPING_BASIC);
153     }
154 
155     public boolean validate(final String hex) {
156         Pattern pattern = Pattern.compile("[0-9]+");
157         Matcher matcher = pattern.matcher(hex);
158         return matcher.matches();
159 
160     }
161 
162     @Override
163     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
164         ActionForward actionForward = null;
165         if(((OleReceivingQueueSearchForm) form).getMethodToCall() != null) {
166             if(((OleReceivingQueueSearchForm) form).getMethodToCall().equalsIgnoreCase("docHandler")){
167                 ((OleReceivingQueueSearchForm) form).setDocId(null);
168             }
169         }
170         return super.execute(mapping, form, request, response);
171     }
172 
173     /**
174      * This method clears the tabs in the UI
175      *
176      * @param mapping
177      * @param form
178      * @param request
179      * @param response
180      * @return
181      * @throws Exception
182      */
183     public ActionForward valueClear(ActionMapping mapping, ActionForm form,
184                                     HttpServletRequest request, HttpServletResponse response) throws Exception {
185         OleReceivingQueueSearchForm oleReceivingQueueSearchForm = (OleReceivingQueueSearchForm) form;
186         OleReceivingQueueSearchDocument oleReceivingQueueSearchDocument = (OleReceivingQueueSearchDocument) oleReceivingQueueSearchForm.getDocument();
187         oleReceivingQueueSearchDocument.setPurchaseOrderItems(null);
188         oleReceivingQueueSearchDocument.setPurchaseOrderNumber(null);
189         oleReceivingQueueSearchDocument.setVendorName(null);
190         //oleReceivingQueueSearchDocument.setVendor(false);
191         //oleReceivingQueueSearchDocument.setPurchaseOrderDate(false);
192         oleReceivingQueueSearchDocument.setPurchaseOrderStatusDescription(null);
193         oleReceivingQueueSearchDocument.setMonograph(false);
194         oleReceivingQueueSearchDocument.setPurchaseOrderType(null);
195         oleReceivingQueueSearchDocument.setBeginDate(null);
196         oleReceivingQueueSearchDocument.setEndDate(null);
197         oleReceivingQueueSearchDocument.setStandardNumber(null);
198         oleReceivingQueueSearchDocument.setTitle(null);
199         oleReceivingQueueSearchDocument.setClaimFilter(false);
200         oleReceivingQueueSearchDocument.setReceivingDocumentsList(new ArrayList<String>());
201         //oleReceivingQueueSearchDocument.setJournal(null);
202         //request.setAttribute(OLEConstants.OrderQueue.CONSTANTS, KewApiConstants.class.newInstance());
203         return mapping.findForward(RiceConstants.MAPPING_BASIC);
204     }
205 
206 
207     /**
208      * Gets the document attribute.
209      *
210      * @return Returns the document.
211      */
212     public Document getDocument() {
213         return document;
214     }
215 
216     /**
217      * Sets the document attribute value.
218      *
219      * @param document The document to set.
220      */
221     public void setDocument(Document document) {
222         this.document = document;
223     }
224 
225     /**
226      * This method selects all Purchase Order Items in Receiving Queue search page
227      *
228      * @param mapping
229      * @param form
230      * @param request
231      * @param response
232      * @return
233      * @throws Exception
234      */
235     public ActionForward selectAll(ActionMapping mapping, ActionForm form,
236                                    HttpServletRequest request, HttpServletResponse response) throws Exception {
237         OleReceivingQueueSearchForm oleReceivingQueueSearchForm = (OleReceivingQueueSearchForm) form;
238         List<OlePurchaseOrderItem> items = ((OleReceivingQueueSearchDocument) oleReceivingQueueSearchForm.getDocument()).getPurchaseOrderItems();
239         List<OlePurchaseOrderItem> refreshItems = new ArrayList<OlePurchaseOrderItem>(0);
240 
241         for (OlePurchaseOrderItem item : items) {
242             if(item.isClaimFilter()){
243                 item.setClaimPoAdded(true);
244             }
245             item.setPoAdded(true);
246             refreshItems.add(item);
247         }
248 
249         ((OleReceivingQueueSearchDocument) oleReceivingQueueSearchForm.getDocument()).setPurchaseOrderItems(refreshItems);
250         //request.setAttribute(OLEConstants.OrderQueue.CONSTANTS, KewApiConstants.class.newInstance());
251         return mapping.findForward(RiceConstants.MAPPING_BASIC);
252     }
253 
254     /**
255      * This method deselects all Purchase Order Items in Receiving Queue search page
256      *
257      * @param mapping
258      * @param form
259      * @param request
260      * @param response
261      * @return
262      * @throws Exception
263      */
264     public ActionForward unselectAll(ActionMapping mapping, ActionForm form,
265                                      HttpServletRequest request, HttpServletResponse response) throws Exception {
266         OleReceivingQueueSearchForm oleReceivingQueueSearchForm = (OleReceivingQueueSearchForm) form;
267         List<OlePurchaseOrderItem> items = ((OleReceivingQueueSearchDocument) oleReceivingQueueSearchForm.getDocument()).getPurchaseOrderItems();
268         List<OlePurchaseOrderItem> refreshItems = new ArrayList<OlePurchaseOrderItem>(0);
269 
270         for (OlePurchaseOrderItem item : items) {
271             if(item.isClaimFilter()){
272                 item.setClaimPoAdded(false);
273             }
274             item.setPoAdded(false);
275             refreshItems.add(item);
276         }
277 
278         ((OleReceivingQueueSearchDocument) oleReceivingQueueSearchForm.getDocument()).setPurchaseOrderItems(refreshItems);
279         //request.setAttribute(OLEConstants.OrderQueue.CONSTANTS, KewApiConstants.class.newInstance());
280         return mapping.findForward(RiceConstants.MAPPING_BASIC);
281     }
282 
283     /**
284      * This method returns a new OleLineItemReceivingDocument
285      *
286      * @return
287      * @throws WorkflowException
288      */
289     protected OleLineItemReceivingDocument createLineItemReceivingDocument() throws WorkflowException {
290         Document doc = getDocumentService().getNewDocument("OLE_RCVL");
291         return (OleLineItemReceivingDocument) doc;
292     }
293 
294     /**
295      * This method validates and performs complete receiving of PO IDs selected.
296      *
297      * @param mapping
298      * @param form
299      * @param request
300      * @param response
301      * @return
302      * @throws Exception
303      */
304     public ActionForward completeReceiving(ActionMapping mapping, ActionForm form,
305                                            HttpServletRequest request, HttpServletResponse response) throws Exception {
306         LOG.debug("Inside completeReceiving of OleReceivingQueueSearchDocument");
307         OleReceivingQueueSearchForm oleReceivingQueueSearchForm = (OleReceivingQueueSearchForm) form;
308         OleReceivingQueueSearchDocument oleReceivingQueueSearchDocument = (OleReceivingQueueSearchDocument) oleReceivingQueueSearchForm.getDocument();
309         OleLineItemReceivingDocument rlDoc = null;
310         // Elimiate duplicate POs from the list
311         List<OlePurchaseOrderItem> olePurchaseOrderItems = new ArrayList<OlePurchaseOrderItem>();
312         HashMap<Integer, OlePurchaseOrderItem> selectedPOs = getSelectedPurchaseOrders(oleReceivingQueueSearchDocument.getPurchaseOrderItems());
313         if (selectedPOs.size() == 0) {
314             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ERROR_SELECT_PO_ITM);
315             return mapping.findForward(RiceConstants.MAPPING_BASIC);
316         }
317         boolean isReceivingExist = false;
318         List<String> errorCreatingReceivingForPoIdList = new ArrayList<String>();
319         for (OlePurchaseOrderItem olePurchaseOrderItem : oleReceivingQueueSearchDocument.getPurchaseOrderItems()) {
320             if (olePurchaseOrderItem.isPoAdded()) {
321                 /*boolean isReceivingExisted=false;*/
322                 if (validateReceivingForProcess(olePurchaseOrderItem)) {
323                     isReceivingExist = true;
324                     olePurchaseOrderItem.setPoAdded(false);
325                     if (!errorCreatingReceivingForPoIdList.contains(olePurchaseOrderItem.getPurchaseOrder().getPurapDocumentIdentifier().toString())) {
326                         errorCreatingReceivingForPoIdList.add(olePurchaseOrderItem.getPurchaseOrder().getPurapDocumentIdentifier().toString());
327                     }
328                 }
329                 /*if (!isReceivingExisted) {
330                 if (olePurchaseOrderItem.getItemQuantity() != null) {
331                     KualiDecimal kualiDecimal = olePurchaseOrderItem.getItemQuantity();
332                     int itemQty = kualiDecimal.intValue();
333                     Map<String, String> map = new HashMap<String, String>();
334                     map.put("itemIdentifier", olePurchaseOrderItem.getItemIdentifier().toString());
335                     List<OlePurchaseOrderItem> orderItems = (List<OlePurchaseOrderItem>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePurchaseOrderItem.class, map);
336                     if (orderItems.size() > 0) {
337                         OlePurchaseOrderItem purchaseOrderItem = orderItems.get(0);
338                         if (!(purchaseOrderItem.getNoOfCopiesReceived() != null && purchaseOrderItem.getNoOfCopiesReceived().equalsIgnoreCase(""))) {
339                             if (itemQty == Integer.parseInt(purchaseOrderItem.getNoOfCopiesReceived())) {
340                                 olePurchaseOrderItem.setPoAdded(false);
341 
342                             }
343                         }
344                         if (!(purchaseOrderItem.getNoOfPartsReceived() != null && purchaseOrderItem.getNoOfPartsReceived().equalsIgnoreCase(""))) {
345                             if (purchaseOrderItem.getNoOfPartsReceived().equals(purchaseOrderItem.getItemNoOfParts())) {
346                                 olePurchaseOrderItem.setPoAdded(false);
347 
348                             }
349                         }
350                     }
351                 }
352 
353                 }*/
354             }
355         }
356         selectedPOs = getSelectedPurchaseOrders(oleReceivingQueueSearchDocument.getPurchaseOrderItems());
357         // Map containing PO ID and receive PO status
358         HashMap<Integer, Boolean> receivePOStatus = new HashMap<Integer, Boolean>();
359         oleReceivingQueueSearchDocument.getReceivingDocumentsList().clear();
360         boolean isInfoMsg = false;
361         for (Map.Entry<Integer, OlePurchaseOrderItem> entry : selectedPOs.entrySet()) {
362             if (LOG.isDebugEnabled()) {
363                 LOG.debug("Creating Line Item Receiving Document for PO ID - " + entry.getKey());
364             }
365             rlDoc = createLineItemReceivingDocument();
366             boolean receivePOSuccess = oleReceivingQueueSearchDocument.receivePO(rlDoc, entry.getKey(), false);
367             if (receivePOSuccess) {
368                 oleReceivingQueueSearchDocument.getReceivingDocumentsList().add(rlDoc.getDocumentNumber());
369                 // assignActionRequests(rlDoc.getDocumentHeader().getWorkflowDocument().getDocumentId().toString());
370                 isInfoMsg = true;
371             }
372             receivePOStatus.put(entry.getKey(), receivePOSuccess);
373             if (LOG.isDebugEnabled()) {
374                 LOG.debug("Creating Line Item Receiving Document for PO ID - " + entry.getKey() + " completed successfully");
375             }
376         }
377         List<OlePurchaseOrderItem> refreshedPOList = new ArrayList<OlePurchaseOrderItem>();
378         for (OlePurchaseOrderItem poItem : oleReceivingQueueSearchDocument.getPurchaseOrderItems()) {
379             Integer poId = poItem.getOlePurchaseOrderDocument().getPurapDocumentIdentifier();
380             refreshedPOList.add(poItem);
381             /*if(ObjectUtils.isNull(receivePOStatus.get(poId))){
382                 refreshedPOList.add(poItem);
383             }else{
384                 if(receivePOStatus.get(poId) == false) {
385                     refreshedPOList.add(poItem);
386                 }
387             }*/
388         }
389 
390         ((OleReceivingQueueSearchDocument) oleReceivingQueueSearchForm.getDocument()).setPurchaseOrderItems(refreshedPOList);
391 
392         LOG.debug("Leaving completeReceiving of OleReceivingQueueSearchDocument");
393         String errorCreatingReceivingForPoIds = "";
394         for (String string : errorCreatingReceivingForPoIdList) {
395             errorCreatingReceivingForPoIds = errorCreatingReceivingForPoIds + string + ",";
396         }
397         if (isReceivingExist) {
398             errorCreatingReceivingForPoIds = errorCreatingReceivingForPoIds.substring(0, errorCreatingReceivingForPoIds.length() - 1);
399             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ERROR_RECEIVING_EXIST, errorCreatingReceivingForPoIds);
400         }
401 
402         return unselectAll(mapping, oleReceivingQueueSearchForm, request, response);
403     }
404 
405 
406     public ActionForward claiming(ActionMapping mapping, ActionForm form,
407                                   HttpServletRequest request, HttpServletResponse response) throws Exception {
408         LOG.debug("Inside completeReceiving of OleReceivingQueueSearchDocument");
409         OleReceivingQueueSearchForm oleReceivingQueueSearchForm = (OleReceivingQueueSearchForm) form;
410         OleReceivingQueueSearchDocument oleReceivingQueueSearchDocument = (OleReceivingQueueSearchDocument) oleReceivingQueueSearchForm.getDocument();
411         // Elimiate duplicate POs from the list
412         List<OlePurchaseOrderItem> olePurchaseOrderItems = new ArrayList<OlePurchaseOrderItem>();
413         for (OlePurchaseOrderItem poItem : oleReceivingQueueSearchDocument.getPurchaseOrderItems()) {
414             if (poItem.isPoAdded()) {
415                 olePurchaseOrderItems.add(poItem);
416             }
417         }
418         if (olePurchaseOrderItems.size() == 0) {
419             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ERROR_SELECT_PO_ITM);
420             return mapping.findForward(RiceConstants.MAPPING_BASIC);
421         }
422         OLEClaimNoticeService oleClaimNoticeService  = GlobalResourceLoader.getService("oleClaimNoticeService");
423         oleClaimNoticeService.generateClaimReportFromPO(olePurchaseOrderItems,false);
424         return unselectAll(mapping, oleReceivingQueueSearchForm, request, response);
425     }
426 
427 
428     /**
429      * This method validates and creates receiving document from the selected PO Items.
430      *
431      * @param mapping
432      * @param form
433      * @param request
434      * @param response
435      * @return
436      * @throws Exception
437      */
438     public ActionForward createReceiving(ActionMapping mapping, ActionForm form, HttpServletRequest request,
439                                          HttpServletResponse response) throws Exception {
440         LOG.debug("Inside completereceiving of OleReceivingQueueSearchDocument");
441         OleReceivingQueueSearchForm oleReceivingQueueSearchForm = (OleReceivingQueueSearchForm) form;
442         OleReceivingQueueSearchDocument oleReceivingQueueSearchDocument = (OleReceivingQueueSearchDocument) oleReceivingQueueSearchForm
443                 .getDocument();
444         OleLineItemReceivingDocument rlDoc = null;
445         HashMap<Integer, OlePurchaseOrderItem> selectedPOs = getSelectedPurchaseOrders(oleReceivingQueueSearchDocument
446                 .getPurchaseOrderItems());
447         List<OlePurchaseOrderItem> olePurchaseOrderItems = new ArrayList<OlePurchaseOrderItem>();
448         if (selectedPOs.size() == 0) {
449             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ERROR_SELECT_PO_ITM);
450             return mapping.findForward(RiceConstants.MAPPING_BASIC);
451         }
452         boolean isReceivingExist = false;
453         List<String> errorCreatingReceivingForPoIdList = new ArrayList<String>();
454         for (OlePurchaseOrderItem olePurchaseOrderItem : oleReceivingQueueSearchDocument.getPurchaseOrderItems()) {
455             if (olePurchaseOrderItem.isPoAdded()) {
456                 if (validateReceivingForProcess(olePurchaseOrderItem)) {
457                     isReceivingExist = true;
458                     olePurchaseOrderItem.setPoAdded(false);
459                     if (!errorCreatingReceivingForPoIdList.contains(olePurchaseOrderItem.getOlePurchaseOrderDocument().getPurapDocumentIdentifier().toString())) {
460                         errorCreatingReceivingForPoIdList.add(olePurchaseOrderItem.getOlePurchaseOrderDocument().getPurapDocumentIdentifier().toString());
461                     }
462                 }
463                 /*if (olePurchaseOrderItem.getItemQuantity() != null) {
464                     KualiDecimal kualiDecimal = olePurchaseOrderItem.getItemQuantity();
465                     int itemQty = kualiDecimal.intValue();
466                     Map<String, String> map = new HashMap<String, String>();
467                     map.put("itemIdentifier", olePurchaseOrderItem.getItemIdentifier().toString());
468                     List<OlePurchaseOrderItem> orderItems = (List<OlePurchaseOrderItem>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePurchaseOrderItem.class, map);
469                     if (orderItems.size() > 0) {
470                         OlePurchaseOrderItem purchaseOrderItem = orderItems.get(0);
471                         if (!(purchaseOrderItem.getNoOfCopiesReceived() != null && purchaseOrderItem.getNoOfCopiesReceived().equalsIgnoreCase(""))) {
472                             if (itemQty == Integer.parseInt(purchaseOrderItem.getNoOfCopiesReceived())) {
473                                 olePurchaseOrderItem.setPoAdded(false);
474 
475                             }
476                         }
477                         if (!(purchaseOrderItem.getNoOfPartsReceived() != null && purchaseOrderItem.getNoOfPartsReceived().equalsIgnoreCase(""))) {
478                             if (purchaseOrderItem.getNoOfPartsReceived().equals(purchaseOrderItem.getItemNoOfParts())) {
479                                 olePurchaseOrderItem.setPoAdded(false);
480 
481                             }
482                         }
483                     }
484                 }*/
485             }
486         }
487         selectedPOs = getSelectedPurchaseOrders(oleReceivingQueueSearchDocument.getPurchaseOrderItems());
488         // Map containing PO ID and receive PO status
489         HashMap<Integer, Boolean> receivePOStatus = new HashMap<Integer, Boolean>();
490         boolean isInfoMsg = false;
491         for (Map.Entry<Integer, OlePurchaseOrderItem> entry : selectedPOs.entrySet()) {
492             if (LOG.isDebugEnabled()) {
493                 LOG.debug("Creating Line Item Receiving Document for PO ID - " + entry.getKey());
494             }
495             rlDoc = createLineItemReceivingDocument();
496             getOleLineItemReceivingService().getInitialCollapseSections(rlDoc);
497             boolean receivePOSuccess = oleReceivingQueueSearchDocument.receivePO(rlDoc, entry.getKey(), true);
498             if (receivePOSuccess) {
499                 oleReceivingQueueSearchDocument.getReceivingDocumentsList().add(rlDoc.getDocumentNumber());
500                 isInfoMsg = true;
501             }
502             receivePOStatus.put(entry.getKey(), receivePOSuccess);
503             if (LOG.isDebugEnabled()) {
504                 LOG.debug("Creating Line Item Receiving Document for PO ID - " + entry.getKey()
505                         + " completed successfully");
506             }
507         }
508         List<OlePurchaseOrderItem> refreshedPOList = new ArrayList<OlePurchaseOrderItem>();
509         for (OlePurchaseOrderItem poItem : oleReceivingQueueSearchDocument.getPurchaseOrderItems()) {
510             Integer poId = poItem.getOlePurchaseOrderDocument().getPurapDocumentIdentifier();
511             refreshedPOList.add(poItem);
512             /*if (ObjectUtils.isNull(receivePOStatus.get(poId))) {
513                 refreshedPOList.add(poItem);
514             }
515             else {
516                 if (receivePOStatus.get(poId) == false) {
517                     refreshedPOList.add(poItem);
518                 }
519             }*/
520         }
521         if (oleReceivingQueueSearchDocument.getReceivingDocumentsList().size() == 1) {
522             ActionForward dest = returnToReceiving(oleReceivingQueueSearchDocument.getReceivingDocumentsList().get(0));
523             return dest;
524         }
525         ((OleReceivingQueueSearchDocument) oleReceivingQueueSearchForm.getDocument())
526                 .setPurchaseOrderItems(refreshedPOList);
527 
528         LOG.debug("Leaving completeReceiving of OleReceivingQueueSearchDocument");
529         String errorCreatingReceivingForPoIds = "";
530         for (String string : errorCreatingReceivingForPoIdList) {
531             errorCreatingReceivingForPoIds = errorCreatingReceivingForPoIds + string + ",";
532         }
533         if (isReceivingExist) {
534             errorCreatingReceivingForPoIds = errorCreatingReceivingForPoIds.substring(0, errorCreatingReceivingForPoIds.length() - 1);
535             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.ERROR_RECEIVING_EXIST, errorCreatingReceivingForPoIds);
536         }
537 
538         return mapping.findForward(RiceConstants.MAPPING_BASIC);
539     }
540 
541     /**
542      * This method populates a HashMap with POID and Purchase Order Item. The display is at line item level, and users can select
543      * multiple line items under same PO and click on Receive. This method removes duplicate PO selections in such cases.
544      *
545      * @param purchaseOrders Selected PurchaseOrders from UI
546      * @return
547      */
548     private HashMap<Integer, OlePurchaseOrderItem> getSelectedPurchaseOrders(List<OlePurchaseOrderItem> purchaseOrders) {
549         LOG.debug("Inside getSelectedPurchaseOrders of OleReceivingQueueSearchDocument");
550         HashMap<Integer, OlePurchaseOrderItem> poItemMap = new HashMap<Integer, OlePurchaseOrderItem>();
551         for (OlePurchaseOrderItem poItem : purchaseOrders) {
552             if (poItem.isPoAdded()) {
553                 if (ObjectUtils.isNull(poItemMap.get(poItem.getOlePurchaseOrderDocument().getPurapDocumentIdentifier()))) {
554                     poItemMap.put(poItem.getOlePurchaseOrderDocument().getPurapDocumentIdentifier(), poItem);
555                 }
556             }
557         }
558         if (LOG.isDebugEnabled()) {
559             LOG.debug("Number of actual PO selected in OleReceivingQueueSearchDocument.getSelectedPurchaseOrders - " + poItemMap.size());
560             LOG.debug("Leaving getSelectedPurchaseOrders of OleReceivingQueueSearchDocument");
561         }
562 
563         return poItemMap;
564     }
565 
566     /**
567      * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#cancel(org.apache.struts.action.ActionMapping,
568      *      org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
569      */
570     @Override
571     public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
572         KualiDocumentFormBase kualiDocumentFormBase = (KualiDocumentFormBase) form;
573         return returnToSender(request, mapping, kualiDocumentFormBase);
574     }
575 
576     /**
577      * This method redirects the receiving queue to Line Item Receiving Document.
578      *
579      * @param rcvDocId
580      * @return ActionForward which will redirect the user to the docSearchDisplay for the Receiving Document with the given
581      *         documentId
582      */
583     private ActionForward returnToReceiving(String rcvDocId) {
584         String receivingUrl = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(
585                 OLEConstants.WORKFLOW_URL_KEY)
586                 + "/DocHandler.do?docId=" + rcvDocId + "&command=displayDocSearchView";
587         return new ActionForward(receivingUrl, true);
588     }
589 
590 
591     /**
592      * This method redirects the receiving queue to Payment Request Document.
593      *
594      * @param purchaseOrderId
595      * @return ActionForward which will redirect the user to the docSearchDisplay for the Receiving Document with the given
596      *         documentId
597      */
598     private ActionForward returnToPayment(String purchaseOrderId) {
599         String paymentUrl = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(
600                 OLEConstants.WORKFLOW_URL_KEY)
601                 + "/selectOlePaymentRequest.do?purchaseOrderIdentifier=" + purchaseOrderId
602                 + "&methodToCall=docHandler&command=initiate&docTypeName=OLE_PREQ";
603         return new ActionForward(paymentUrl, true);
604     }
605 
606     /**
607      * This method validates and creates receiving document and the Payment Request Document from the selected PO Items.
608      *
609      * @param mapping
610      * @param form
611      * @param request
612      * @param response
613      * @return
614      * @throws Exception
615      */
616     public ActionForward createPaymentRequestDocument(ActionMapping mapping, ActionForm form,
617                                                       HttpServletRequest request, HttpServletResponse response) throws Exception {
618 
619         return mapping.findForward(RiceConstants.MAPPING_BASIC);
620     }
621 
622     /**
623      * This method validates and creates receiving document and the Payment Request Document from the selected PO Items.
624      *
625      * @param mapping
626      * @param form
627      * @param request
628      * @param response
629      * @return
630      * @throws Exception
631      */
632     public ActionForward receiveAndPay(ActionMapping mapping, ActionForm form, HttpServletRequest request,
633                                        HttpServletResponse response) throws Exception {
634         LOG.debug("Inside receiveAndPay of OleReceivingQueueSearchDocument");
635         OleReceivingQueueSearchForm oleReceivingQueueSearchForm = (OleReceivingQueueSearchForm) form;
636         OleReceivingQueueSearchDocument oleReceivingQueueSearchDocument = (OleReceivingQueueSearchDocument) oleReceivingQueueSearchForm.getDocument();
637         OleLineItemReceivingDocument rlDoc = null;
638         // Elimiate duplicate POs from the list
639         HashMap<Integer, OlePurchaseOrderItem> selectedPOs = getSelectedPurchaseOrders(oleReceivingQueueSearchDocument.getPurchaseOrderItems());
640         // Map containing PO ID and receive PO status
641         HashMap<Integer, Boolean> receivePOStatus = new HashMap<Integer, Boolean>();
642         boolean isInfoMsg = false;
643         for (Map.Entry<Integer, OlePurchaseOrderItem> entry : selectedPOs.entrySet()) {
644             if (LOG.isDebugEnabled()) {
645                 LOG.debug("Creating Line Item Receiving Document for PO ID - " + entry.getKey());
646             }
647             rlDoc = createLineItemReceivingDocument();
648             boolean receivePOSuccess = oleReceivingQueueSearchDocument.receivePO(rlDoc, entry.getKey(), false);
649             if (receivePOSuccess) {
650                 oleReceivingQueueSearchDocument.getReceivingDocumentsList().add(rlDoc.getDocumentNumber());
651                 // assignActionRequests(rlDoc.getDocumentHeader().getWorkflowDocument().getDocumentId().toString());
652                 isInfoMsg = true;
653             }
654             receivePOStatus.put(entry.getKey(), receivePOSuccess);
655             if (LOG.isDebugEnabled()) {
656                 LOG.debug("Creating Line Item Receiving Document for PO ID - " + entry.getKey() + " completed successfully");
657             }
658         }
659         List<OlePurchaseOrderItem> refreshedPOList = new ArrayList<OlePurchaseOrderItem>();
660         for (OlePurchaseOrderItem poItem : oleReceivingQueueSearchDocument.getPurchaseOrderItems()) {
661             Integer poId = poItem.getOlePurchaseOrderDocument().getPurapDocumentIdentifier();
662             if (ObjectUtils.isNull(receivePOStatus.get(poId))) {
663                 refreshedPOList.add(poItem);
664             } else {
665                 if (receivePOStatus.get(poId) == false) {
666                     refreshedPOList.add(poItem);
667                 }
668             }
669         }
670 
671         ((OleReceivingQueueSearchDocument) oleReceivingQueueSearchForm.getDocument()).setPurchaseOrderItems(refreshedPOList);
672 
673         LOG.debug("Leaving receiveAndPay of OleReceivingQueueSearchDocument");
674 
675         return mapping.findForward(RiceConstants.MAPPING_BASIC);
676     }
677 
678     @Override
679     protected void loadDocument(KualiDocumentFormBase kualiDocumentFormBase) throws WorkflowException {
680         super.loadDocument(kualiDocumentFormBase);
681         OleReceivingQueueSearchForm oleReceivingQueueSearchForm = (OleReceivingQueueSearchForm) kualiDocumentFormBase;
682         OleReceivingQueueSearchDocument oleReceivingQueueSearchDocument = (OleReceivingQueueSearchDocument) oleReceivingQueueSearchForm
683                 .getDocument();
684         oleReceivingQueueSearchDocument.setReceivingDocumentsList(new ArrayList<String>());
685     }
686 
687     /**
688      * This method assigns specified document to the selector.
689      */
690     private void assignActionRequests(String routeHeaderId) {
691         LOG.debug("Inside assignActionRequests of OleOrderQueueDocument");
692         Timestamp currentTime = SpringContext.getBean(DateTimeService.class).getCurrentTimestamp();
693         ActionListService actionListSrv = KEWServiceLocator.getActionListService();
694         ActionRequestService actionReqSrv = KEWServiceLocator.getActionRequestService();
695         ActionRequestValue actionRequest = new ActionRequestValue();
696         actionRequest.setActionRequested(KewApiConstants.ACTION_REQUEST_FYI_REQ);
697         actionRequest.setPrincipalId("OLE10003");
698         actionRequest.setCreateDate(currentTime);
699         actionRequest.setForceAction(true);
700         actionReqSrv.saveActionRequest(actionRequest);
701         /*
702          * List<ActionRequestValue> actionReqValues = actionReqSrv.findAllPendingRequests(routeHeaderId); for (ActionRequestValue
703          * actionRequest : actionReqValues) { List<ActionItem> actionItems = actionRequest.getActionItems(); for (ActionItem
704          * actionItem : actionItems) { actionItem.setPrincipalId(GlobalVariables.getUserSession().getPrincipalId());
705          * actionItem.setDateAssigned(currentTime); actionListSrv.saveActionItem(actionItem); }
706          * actionRequest.setActionRequested(KewApiConstants.ACTION_REQUEST_FYI_REQ);
707          * actionRequest.setPrincipalId(GlobalVariables.getUserSession().getPrincipalId());
708          * actionRequest.setCreateDate(currentTime); actionReqSrv.saveActionRequest(actionRequest); }
709          */
710         LOG.debug("Leaving assignActionRequests of OleOrderQueueDocument");
711     }
712 
713     public boolean validateReceivingForProcess(OlePurchaseOrderItem purchaseOrderItem) {
714         boolean isValid = false;
715         Map<String, String> map = new HashMap<String, String>();
716         map.put("purchaseOrderIdentifier", purchaseOrderItem.getItemIdentifier().toString());
717         List<OleLineItemReceivingItem> oleLineItemReceivingItems = (List<OleLineItemReceivingItem>) KRADServiceLocator.getBusinessObjectService().findMatching(OleLineItemReceivingItem.class, map);
718 
719         if (oleLineItemReceivingItems.size() > 0) {
720             OleLineItemReceivingItem oleLineItemReceivingItem = oleLineItemReceivingItems.get(0);
721             if (oleLineItemReceivingItem.getReceiptStatusId() != null) {
722                 if (oleLineItemReceivingItem.getReceiptStatusId().toString().equalsIgnoreCase(getReceiptStatusDetails(OLEConstants.PO_RECEIPT_STATUS_FULLY_RECEIVED) + "")) {
723                     isValid = true;
724                 }
725             }
726         }
727         return isValid;
728     }
729 
730     public int getReceiptStatusDetails(String receiptStatusCd) {
731         int receiptStatusId = 0;
732         Map<String, String> receiptStatusCdMap = new HashMap<String, String>();
733         receiptStatusCdMap.put(OLEConstants.RCPT_STATUS_CD, receiptStatusCd);
734         List<OleReceiptStatus> oleReceiptStatusList = (List) SpringContext.getBean(BusinessObjectService.class)
735                 .findMatching(OleReceiptStatus.class, receiptStatusCdMap);
736         for (OleReceiptStatus oleReceiptStatus : oleReceiptStatusList) {
737             receiptStatusId = oleReceiptStatus.getReceiptStatusId().intValue();
738         }
739         return receiptStatusId;
740     }
741 
742     public static OleLineItemReceivingService getOleLineItemReceivingService() {
743         if (oleLineItemReceivingService == null) {
744             oleLineItemReceivingService = SpringContext.getBean(OleLineItemReceivingService.class);
745         }
746         return oleLineItemReceivingService;
747     }
748 
749 }