001/*
002 * Copyright 2008 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 */
016package org.kuali.ole.module.purap.document.validation.impl;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.ole.module.purap.PurapConstants;
020import org.kuali.ole.module.purap.PurapKeyConstants;
021import org.kuali.ole.module.purap.PurapPropertyConstants;
022import org.kuali.ole.module.purap.document.BulkReceivingDocument;
023import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
024import org.kuali.ole.module.purap.document.service.BulkReceivingService;
025import org.kuali.ole.module.purap.document.service.PurchaseOrderService;
026import org.kuali.ole.module.purap.document.validation.ContinuePurapRule;
027import org.kuali.ole.sys.OLEKeyConstants;
028import org.kuali.ole.sys.OLEPropertyConstants;
029import org.kuali.ole.sys.context.SpringContext;
030import org.kuali.rice.kns.rules.DocumentRuleBase;
031import org.kuali.rice.krad.document.Document;
032import org.kuali.rice.krad.document.TransactionalDocument;
033import org.kuali.rice.krad.util.GlobalVariables;
034import org.kuali.rice.krad.util.ObjectUtils;
035
036public 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(OLEPropertyConstants.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(OLEPropertyConstants.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, OLEKeyConstants.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.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}