001 /*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.kfs.module.purap.document.validation.impl;
017
018 import org.apache.commons.lang.StringUtils;
019 import org.kuali.kfs.module.purap.PurapConstants;
020 import org.kuali.kfs.module.purap.PurapKeyConstants;
021 import org.kuali.kfs.module.purap.PurapPropertyConstants;
022 import org.kuali.kfs.module.purap.document.BulkReceivingDocument;
023 import org.kuali.kfs.module.purap.document.PurchaseOrderDocument;
024 import org.kuali.kfs.module.purap.document.service.BulkReceivingService;
025 import org.kuali.kfs.module.purap.document.service.PurchaseOrderService;
026 import org.kuali.kfs.module.purap.document.validation.ContinuePurapRule;
027 import org.kuali.kfs.sys.KFSKeyConstants;
028 import org.kuali.kfs.sys.KFSPropertyConstants;
029 import org.kuali.kfs.sys.context.SpringContext;
030 import org.kuali.rice.kns.document.Document;
031 import org.kuali.rice.kns.document.TransactionalDocument;
032 import org.kuali.rice.kns.rules.DocumentRuleBase;
033 import org.kuali.rice.kns.util.GlobalVariables;
034 import org.kuali.rice.kns.util.ObjectUtils;
035
036 public class BulkReceivingDocumentRule extends DocumentRuleBase implements ContinuePurapRule {
037
038 @Override
039 protected boolean processCustomRouteDocumentBusinessRules(Document document) {
040
041 boolean valid = true;
042
043 BulkReceivingDocument bulkReceivingDocument = (BulkReceivingDocument)document;
044
045 GlobalVariables.getMessageMap().clearErrorPath();
046 GlobalVariables.getMessageMap().addToErrorPath(KFSPropertyConstants.DOCUMENT);
047
048 valid &= super.processCustomRouteDocumentBusinessRules(document);
049 valid &= canCreateBulkReceivingDocument(bulkReceivingDocument);
050
051 return valid;
052 }
053
054 public boolean processContinuePurapBusinessRules(TransactionalDocument document) {
055
056 boolean valid = true;
057 BulkReceivingDocument bulkReceivingDocument = (BulkReceivingDocument)document;
058
059 GlobalVariables.getMessageMap().clearErrorPath();
060 GlobalVariables.getMessageMap().addToErrorPath(KFSPropertyConstants.DOCUMENT);
061
062 valid = hasRequiredFieldsForContinue(bulkReceivingDocument) &&
063 canCreateBulkReceivingDocument(bulkReceivingDocument);
064
065 return valid;
066 }
067
068 /**
069 * Make sure the required fields on the init screen are filled in.
070 *
071 * @param bulkReceivingDocument
072 * @return
073 */
074 protected boolean hasRequiredFieldsForContinue(BulkReceivingDocument bulkReceivingDocument){
075
076 boolean valid = true;
077
078 if (ObjectUtils.isNull(bulkReceivingDocument.getShipmentReceivedDate())) {
079 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.SHIPMENT_RECEIVED_DATE, KFSKeyConstants.ERROR_REQUIRED, PurapConstants.BulkReceivingDocumentStrings.VENDOR_DATE);
080 valid = false;
081 }
082
083 return valid;
084 }
085
086 /**
087 * Determines if it is valid to create a bulk receiving document.
088 *
089 * @param bulkReceivingDocument
090 * @return
091 */
092 protected boolean canCreateBulkReceivingDocument(BulkReceivingDocument bulkReceivingDocument){
093
094 boolean valid = true;
095
096 if (bulkReceivingDocument.getPurchaseOrderIdentifier() != null){
097 PurchaseOrderDocument po = SpringContext.getBean(PurchaseOrderService.class).getCurrentPurchaseOrder(bulkReceivingDocument.getPurchaseOrderIdentifier());
098
099 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.getStatusCode().equals(PurapConstants.PurchaseOrderStatuses.OPEN) ||
104 po.getStatusCode().equals(PurapConstants.PurchaseOrderStatuses.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 }