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.kuali.ole.module.purap.PurapConstants;
019import org.kuali.ole.module.purap.PurapConstants.PODocumentsStrings;
020import org.kuali.ole.module.purap.PurapKeyConstants;
021import org.kuali.ole.module.purap.businessobject.PurchaseOrderItem;
022import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
023import org.kuali.ole.module.purap.document.service.PurchaseOrderService;
024import org.kuali.ole.sys.document.validation.GenericValidation;
025import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
026import org.kuali.rice.krad.util.GlobalVariables;
027
028import java.util.HashMap;
029import java.util.List;
030
031/**
032 * A validation that checks whether the given accounting line is accessible to the given user or not
033 */
034public class PurchaseOrderSplitValidation extends GenericValidation {
035
036    private PurchaseOrderService purchaseOrderService;
037    private PurchaseOrderDocument accountingDocumentForValidation;
038
039    /**
040     * Applies rules for validation of the Split of PO and PO child documents
041     *
042     * @param document A PurchaseOrderDocument (or one of its children)
043     * @return True if all relevant validation rules are passed.
044     */
045    public boolean validate(AttributedDocumentEvent event) {
046        boolean valid = true;
047        PurchaseOrderDocument po = (PurchaseOrderDocument) event.getDocument();
048        HashMap<String, List<PurchaseOrderItem>> categorizedItems = purchaseOrderService.categorizeItemsForSplit((List<PurchaseOrderItem>) po.getItems());
049        List<PurchaseOrderItem> movingPOItems = categorizedItems.get(PODocumentsStrings.ITEMS_MOVING_TO_SPLIT);
050        List<PurchaseOrderItem> remainingPOLineItems = categorizedItems.get(PODocumentsStrings.LINE_ITEMS_REMAINING);
051        if (movingPOItems.isEmpty()) {
052            GlobalVariables.getMessageMap().putError(PurapConstants.SPLIT_PURCHASE_ORDER_TAB_ERRORS, PurapKeyConstants.ERROR_PURCHASE_ORDER_SPLIT_ONE_ITEM_MUST_MOVE);
053            valid &= false;
054        } else if (remainingPOLineItems.isEmpty()) {
055            GlobalVariables.getMessageMap().putError(PurapConstants.SPLIT_PURCHASE_ORDER_TAB_ERRORS, PurapKeyConstants.ERROR_PURCHASE_ORDER_SPLIT_ONE_ITEM_MUST_REMAIN);
056            valid &= false;
057        }
058        return valid;
059    }
060
061    public PurchaseOrderService getPurchaseOrderService() {
062        return purchaseOrderService;
063    }
064
065    public void setPurchaseOrderService(PurchaseOrderService purchaseOrderService) {
066        this.purchaseOrderService = purchaseOrderService;
067    }
068
069    public PurchaseOrderDocument getAccountingDocumentForValidation() {
070        return accountingDocumentForValidation;
071    }
072
073    public void setAccountingDocumentForValidation(PurchaseOrderDocument accountingDocumentForValidation) {
074        this.accountingDocumentForValidation = accountingDocumentForValidation;
075    }
076
077}
078