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.ld.document.validation.impl;
017    
018    import java.util.Map;
019    
020    import org.kuali.kfs.module.ld.LaborKeyConstants;
021    import org.kuali.kfs.module.ld.document.LaborExpenseTransferDocumentBase;
022    import org.kuali.kfs.sys.KFSPropertyConstants;
023    import org.kuali.kfs.sys.document.AccountingDocument;
024    import org.kuali.kfs.sys.document.validation.GenericValidation;
025    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
026    import org.kuali.rice.kns.document.Document;
027    import org.kuali.rice.kns.util.GlobalVariables;
028    import org.kuali.rice.kns.util.KualiDecimal;
029    
030    /**
031     * target accounting lines must have the same amounts as source accounting lines for each object code in the document
032     * 
033     * @param document the given document
034     * @return true if target accounting lines have the same amounts as source accounting lines for each object code; otherwise, false
035     */
036    public class LaborExpenseTransferValidAmountTransferredByObjectCodeValidation extends GenericValidation {    
037        private Document documentForValidation;  
038        
039        /**
040         * Validates before the document routes 
041         * @see org.kuali.kfs.validation.Validation#validate(java.lang.Object[])
042         */
043        public boolean validate(AttributedDocumentEvent event) {
044            boolean result = true;
045    
046            Document documentForValidation = getDocumentForValidation();
047            
048            LaborExpenseTransferDocumentBase expenseTransferDocument = (LaborExpenseTransferDocumentBase) documentForValidation;
049    
050            // check to ensure totals of accounting lines in source and target sections match
051            if (!isValidAmountTransferredByObjectCode(expenseTransferDocument)) {
052                GlobalVariables.getMessageMap().putError(KFSPropertyConstants.TARGET_ACCOUNTING_LINES, LaborKeyConstants.ERROR_TRANSFER_AMOUNT_NOT_BALANCED_BY_OBJECT);
053                return false;
054            }
055    
056            return result;       
057        }
058    
059        /**
060         * Determine whether target accounting lines have the same amounts as source accounting lines for each object code
061         * 
062         * @param accountingDocument the given accounting document
063         * @return true if target accounting lines have the same amounts as source accounting lines for each object code; otherwise,
064         *         false
065         */
066        protected boolean isValidAmountTransferredByObjectCode(AccountingDocument accountingDocument) {
067            LaborExpenseTransferDocumentBase expenseTransferDocument = (LaborExpenseTransferDocumentBase) accountingDocument;
068    
069            boolean isValid = true;
070    
071            Map<String, KualiDecimal> unbalancedObjectCodes = expenseTransferDocument.getUnbalancedObjectCodes();
072            if (!unbalancedObjectCodes.isEmpty()) {
073                isValid = false;
074            }
075    
076            return isValid;
077        }
078    
079        /**
080         * Gets the documentForValidation attribute. 
081         * @return Returns the documentForValidation.
082         */
083        public Document getDocumentForValidation() {
084            return documentForValidation;
085        }
086    
087        /**
088         * Sets the accountingDocumentForValidation attribute value.
089         * @param documentForValidation The documentForValidation to set.
090         */
091        public void setDocumentForValidation(Document documentForValidation) {
092            this.documentForValidation = documentForValidation;
093        }    
094    }