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 java.util.HashMap;
019    import java.util.List;
020    
021    import org.kuali.kfs.module.purap.PurapConstants;
022    import org.kuali.kfs.module.purap.PurapConstants.PODocumentsStrings;
023    import org.kuali.kfs.module.purap.PurapKeyConstants;
024    import org.kuali.kfs.module.purap.businessobject.PurchaseOrderItem;
025    import org.kuali.kfs.module.purap.document.PurchaseOrderDocument;
026    import org.kuali.kfs.module.purap.document.service.PurchaseOrderService;
027    import org.kuali.kfs.sys.document.validation.GenericValidation;
028    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
029    import org.kuali.rice.kns.util.GlobalVariables;
030    
031    /**
032     * A validation that checks whether the given accounting line is accessible to the given user or not
033     */
034    public 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            }
055            else if (remainingPOLineItems.isEmpty()) {
056                GlobalVariables.getMessageMap().putError(PurapConstants.SPLIT_PURCHASE_ORDER_TAB_ERRORS, PurapKeyConstants.ERROR_PURCHASE_ORDER_SPLIT_ONE_ITEM_MUST_REMAIN);
057                valid &= false;
058            }
059            return valid;
060        }
061    
062        public PurchaseOrderService getPurchaseOrderService() {
063            return purchaseOrderService;
064        }
065    
066        public void setPurchaseOrderService(PurchaseOrderService purchaseOrderService) {
067            this.purchaseOrderService = purchaseOrderService;
068        }
069    
070        public PurchaseOrderDocument getAccountingDocumentForValidation() {
071            return accountingDocumentForValidation;
072        }
073    
074        public void setAccountingDocumentForValidation(PurchaseOrderDocument accountingDocumentForValidation) {
075            this.accountingDocumentForValidation = accountingDocumentForValidation;
076        }
077    
078    }
079