View Javadoc
1   /*
2    * Copyright 2008-2009 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.module.purap.document.validation.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.module.purap.PurapConstants;
20  import org.kuali.ole.module.purap.PurapKeyConstants;
21  import org.kuali.ole.module.purap.PurapPropertyConstants;
22  import org.kuali.ole.module.purap.document.BulkReceivingDocument;
23  import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
24  import org.kuali.ole.module.purap.document.service.BulkReceivingService;
25  import org.kuali.ole.module.purap.document.service.PurchaseOrderService;
26  import org.kuali.ole.sys.OLEPropertyConstants;
27  import org.kuali.ole.sys.context.SpringContext;
28  import org.kuali.ole.sys.document.validation.GenericValidation;
29  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
30  import org.kuali.rice.krad.util.GlobalVariables;
31  import org.kuali.rice.krad.util.ObjectUtils;
32  
33  public class BulkReceivingPurchaseOrderIdValidation extends GenericValidation {
34  
35      private PurchaseOrderService purchaseOrderService;
36  
37      public boolean validate(AttributedDocumentEvent event) {
38  
39          boolean valid = true;
40  
41          BulkReceivingDocument bulkReceivingDocument = (BulkReceivingDocument) event.getDocument();
42  
43          GlobalVariables.getMessageMap().clearErrorPath();
44          GlobalVariables.getMessageMap().addToErrorPath(OLEPropertyConstants.DOCUMENT);
45  
46          return canCreateBulkReceivingDocument(bulkReceivingDocument);
47  
48      }
49  
50      /**
51       * Determines if it is valid to create a bulk receiving document.
52       *
53       * @param bulkReceivingDocument
54       * @return
55       */
56      protected boolean canCreateBulkReceivingDocument(BulkReceivingDocument bulkReceivingDocument) {
57  
58          boolean valid = true;
59  
60          if (bulkReceivingDocument.getPurchaseOrderIdentifier() != null) {
61              PurchaseOrderDocument po = SpringContext.getBean(PurchaseOrderService.class).getCurrentPurchaseOrder(bulkReceivingDocument.getPurchaseOrderIdentifier());
62  
63              if (ObjectUtils.isNull(po)) {
64                  GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_BULK_RECEIVING_DOCUMENT_INVALID_PO, bulkReceivingDocument.getDocumentNumber(), bulkReceivingDocument.getPurchaseOrderIdentifier().toString());
65                  valid = false;
66              } else {
67                  if (!(po.getApplicationDocumentStatus().equals(PurapConstants.PurchaseOrderStatuses.APPDOC_OPEN) ||
68                          po.getApplicationDocumentStatus().equals(PurapConstants.PurchaseOrderStatuses.APPDOC_CLOSED))) {
69                      valid &= false;
70                      GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_BULK_RECEIVING_PO_NOT_OPEN, bulkReceivingDocument.getDocumentNumber(), bulkReceivingDocument.getPurchaseOrderIdentifier().toString());
71                  } else {
72                      String docNumberInProcess = SpringContext.getBean(BulkReceivingService.class).getBulkReceivingDocumentNumberInProcessForPurchaseOrder(po.getPurapDocumentIdentifier(), bulkReceivingDocument.getDocumentNumber());
73                      if (StringUtils.isNotEmpty(docNumberInProcess)) {
74                          valid &= false;
75                          GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_BULK_RECEIVING_DOCUMENT_ACTIVE_FOR_PO, docNumberInProcess, bulkReceivingDocument.getPurchaseOrderIdentifier().toString());
76                      }
77                  }
78              }
79          }
80  
81          return valid;
82      }
83  
84      public PurchaseOrderService getPurchaseOrderService() {
85          return purchaseOrderService;
86      }
87  
88      public void setPurchaseOrderService(PurchaseOrderService purchaseOrderService) {
89          this.purchaseOrderService = purchaseOrderService;
90      }
91  
92  }