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