View Javadoc
1   /*
2    * Copyright 2008 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.module.purap.document.validation.ContinuePurapRule;
27  import org.kuali.ole.sys.OLEKeyConstants;
28  import org.kuali.ole.sys.OLEPropertyConstants;
29  import org.kuali.ole.sys.context.SpringContext;
30  import org.kuali.rice.kns.rules.DocumentRuleBase;
31  import org.kuali.rice.krad.document.Document;
32  import org.kuali.rice.krad.document.TransactionalDocument;
33  import org.kuali.rice.krad.util.GlobalVariables;
34  import org.kuali.rice.krad.util.ObjectUtils;
35  
36  public class BulkReceivingDocumentRule extends DocumentRuleBase implements ContinuePurapRule {
37  
38      @Override
39      protected boolean processCustomRouteDocumentBusinessRules(Document document) {
40  
41          boolean valid = true;
42  
43          BulkReceivingDocument bulkReceivingDocument = (BulkReceivingDocument) document;
44  
45          GlobalVariables.getMessageMap().clearErrorPath();
46          GlobalVariables.getMessageMap().addToErrorPath(OLEPropertyConstants.DOCUMENT);
47  
48          valid &= super.processCustomRouteDocumentBusinessRules(document);
49          valid &= canCreateBulkReceivingDocument(bulkReceivingDocument);
50  
51          return valid;
52      }
53  
54      public boolean processContinuePurapBusinessRules(TransactionalDocument document) {
55  
56          boolean valid = true;
57          BulkReceivingDocument bulkReceivingDocument = (BulkReceivingDocument) document;
58  
59          GlobalVariables.getMessageMap().clearErrorPath();
60          GlobalVariables.getMessageMap().addToErrorPath(OLEPropertyConstants.DOCUMENT);
61  
62          valid = hasRequiredFieldsForContinue(bulkReceivingDocument) &&
63                  canCreateBulkReceivingDocument(bulkReceivingDocument);
64  
65          return valid;
66      }
67  
68      /**
69       * Make sure the required fields on the init screen are filled in.
70       *
71       * @param bulkReceivingDocument
72       * @return
73       */
74      protected boolean hasRequiredFieldsForContinue(BulkReceivingDocument bulkReceivingDocument) {
75  
76          boolean valid = true;
77  
78          if (ObjectUtils.isNull(bulkReceivingDocument.getShipmentReceivedDate())) {
79              GlobalVariables.getMessageMap().putError(PurapPropertyConstants.SHIPMENT_RECEIVED_DATE, OLEKeyConstants.ERROR_REQUIRED, PurapConstants.BulkReceivingDocumentStrings.VENDOR_DATE);
80              valid = false;
81          }
82  
83          return valid;
84      }
85  
86      /**
87       * Determines if it is valid to create a bulk receiving document.
88       *
89       * @param bulkReceivingDocument
90       * @return
91       */
92      protected boolean canCreateBulkReceivingDocument(BulkReceivingDocument bulkReceivingDocument) {
93  
94          boolean valid = true;
95  
96          if (bulkReceivingDocument.getPurchaseOrderIdentifier() != null) {
97              PurchaseOrderDocument po = SpringContext.getBean(PurchaseOrderService.class).getCurrentPurchaseOrder(bulkReceivingDocument.getPurchaseOrderIdentifier());
98  
99              if (ObjectUtils.isNull(po)) {
100                 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_BULK_RECEIVING_DOCUMENT_INVALID_PO, bulkReceivingDocument.getDocumentNumber(), bulkReceivingDocument.getPurchaseOrderIdentifier().toString());
101                 valid = false;
102             } else {
103                 if (!(po.getApplicationDocumentStatus().equals(PurapConstants.PurchaseOrderStatuses.APPDOC_OPEN) ||
104                         po.getApplicationDocumentStatus().equals(PurapConstants.PurchaseOrderStatuses.APPDOC_CLOSED))) {
105                     valid &= false;
106                     GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_BULK_RECEIVING_PO_NOT_OPEN, bulkReceivingDocument.getDocumentNumber(), bulkReceivingDocument.getPurchaseOrderIdentifier().toString());
107                 } else {
108                     String docNumberInProcess = SpringContext.getBean(BulkReceivingService.class).getBulkReceivingDocumentNumberInProcessForPurchaseOrder(po.getPurapDocumentIdentifier(), bulkReceivingDocument.getDocumentNumber());
109                     if (StringUtils.isNotEmpty(docNumberInProcess)) {
110                         valid &= false;
111                         GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_BULK_RECEIVING_DOCUMENT_ACTIVE_FOR_PO, docNumberInProcess, bulkReceivingDocument.getPurchaseOrderIdentifier().toString());
112                     }
113                 }
114             }
115         }
116 
117         return valid;
118     }
119 
120 }