001/*
002 * Copyright 2008-2009 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.sys.OLEPropertyConstants;
027import org.kuali.ole.sys.context.SpringContext;
028import org.kuali.ole.sys.document.validation.GenericValidation;
029import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
030import org.kuali.rice.krad.util.GlobalVariables;
031import org.kuali.rice.krad.util.ObjectUtils;
032
033public class BulkReceivingPurchaseOrderIdValidation extends GenericValidation {
034
035    private PurchaseOrderService purchaseOrderService;
036
037    public boolean validate(AttributedDocumentEvent event) {
038
039        boolean valid = true;
040
041        BulkReceivingDocument bulkReceivingDocument = (BulkReceivingDocument) event.getDocument();
042
043        GlobalVariables.getMessageMap().clearErrorPath();
044        GlobalVariables.getMessageMap().addToErrorPath(OLEPropertyConstants.DOCUMENT);
045
046        return canCreateBulkReceivingDocument(bulkReceivingDocument);
047
048    }
049
050    /**
051     * Determines if it is valid to create a bulk receiving document.
052     *
053     * @param bulkReceivingDocument
054     * @return
055     */
056    protected boolean canCreateBulkReceivingDocument(BulkReceivingDocument bulkReceivingDocument) {
057
058        boolean valid = true;
059
060        if (bulkReceivingDocument.getPurchaseOrderIdentifier() != null) {
061            PurchaseOrderDocument po = SpringContext.getBean(PurchaseOrderService.class).getCurrentPurchaseOrder(bulkReceivingDocument.getPurchaseOrderIdentifier());
062
063            if (ObjectUtils.isNull(po)) {
064                GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_BULK_RECEIVING_DOCUMENT_INVALID_PO, bulkReceivingDocument.getDocumentNumber(), bulkReceivingDocument.getPurchaseOrderIdentifier().toString());
065                valid = false;
066            } else {
067                if (!(po.getApplicationDocumentStatus().equals(PurapConstants.PurchaseOrderStatuses.APPDOC_OPEN) ||
068                        po.getApplicationDocumentStatus().equals(PurapConstants.PurchaseOrderStatuses.APPDOC_CLOSED))) {
069                    valid &= false;
070                    GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_BULK_RECEIVING_PO_NOT_OPEN, bulkReceivingDocument.getDocumentNumber(), bulkReceivingDocument.getPurchaseOrderIdentifier().toString());
071                } else {
072                    String docNumberInProcess = SpringContext.getBean(BulkReceivingService.class).getBulkReceivingDocumentNumberInProcessForPurchaseOrder(po.getPurapDocumentIdentifier(), bulkReceivingDocument.getDocumentNumber());
073                    if (StringUtils.isNotEmpty(docNumberInProcess)) {
074                        valid &= false;
075                        GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, PurapKeyConstants.ERROR_BULK_RECEIVING_DOCUMENT_ACTIVE_FOR_PO, docNumberInProcess, bulkReceivingDocument.getPurchaseOrderIdentifier().toString());
076                    }
077                }
078            }
079        }
080
081        return valid;
082    }
083
084    public PurchaseOrderService getPurchaseOrderService() {
085        return purchaseOrderService;
086    }
087
088    public void setPurchaseOrderService(PurchaseOrderService purchaseOrderService) {
089        this.purchaseOrderService = purchaseOrderService;
090    }
091
092}