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    import java.util.Set;
020    import java.util.Map.Entry;
021    
022    import org.kuali.kfs.module.ld.LaborKeyConstants;
023    import org.kuali.kfs.module.ld.document.SalaryExpenseTransferDocument;
024    import org.kuali.kfs.sys.KFSPropertyConstants;
025    import org.kuali.kfs.sys.document.AccountingDocument;
026    import org.kuali.kfs.sys.document.validation.GenericValidation;
027    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
028    import org.kuali.rice.kns.document.Document;
029    import org.kuali.rice.kns.util.GlobalVariables;
030    import org.kuali.rice.kns.util.KualiDecimal;
031    
032    /**
033     * Validates that an accounting document's balances by object codes are unchanged
034     */
035    public class SalaryExpenseTransferObjectCodeBalancesUnchangedValidation extends GenericValidation {
036        private AccountingDocument accountingDocumentForValidation;
037    
038        /**
039         * Validates that an accounting document have balances unchanged for the object codes <strong>Expects an accounting document as
040         * the first a parameter</strong>
041         * 
042         * @see org.kuali.kfs.validation.Validation#validate(java.lang.Object[])
043         */
044        public boolean validate(AttributedDocumentEvent event) {
045            boolean result = true;
046    
047            AccountingDocument accountingDocumentForValidation = getAccountingDocumentForValidation();        
048            SalaryExpenseTransferDocument salaryExpenseTransferDocument = (SalaryExpenseTransferDocument) accountingDocumentForValidation;
049            
050            Map<String, KualiDecimal> approvalObjectCodeBalances = salaryExpenseTransferDocument.getApprovalObjectCodeBalances();
051            boolean unBalanced = approvalObjectCodeBalances != null && approvalObjectCodeBalances.isEmpty();
052            
053            Map<String, KualiDecimal> unbalancedObjectCodes = salaryExpenseTransferDocument.getUnbalancedObjectCodes();
054            unBalanced &= (unbalancedObjectCodes ==null || !unbalancedObjectCodes.isEmpty());
055            
056            if (unBalanced || !isObjectCodeBalancesUnchanged(salaryExpenseTransferDocument)) {
057                GlobalVariables.getMessageMap().putError(KFSPropertyConstants.TARGET_ACCOUNTING_LINES, LaborKeyConstants.ERROR_TRANSFER_AMOUNT_BY_OBJECT_APPROVAL_CHANGE);
058                result = false;
059            }
060    
061            return result;
062        }
063    
064        /**
065         * Checks whether amounts by object codes are unchanged
066         * 
067         * @param accountingDocumentForValidation The accounting document from which the amounts by objects codes are checked
068         * @return True if the given accounting documents amounts by object code are unchanged, false otherwise.
069         */
070        protected boolean isObjectCodeBalancesUnchanged(SalaryExpenseTransferDocument salaryExpenseTransferDocument) {
071            boolean isUnchanged = true;
072    
073            Map<String, KualiDecimal> initiatedObjectCodeBalances = salaryExpenseTransferDocument.getApprovalObjectCodeBalances();
074            Map<String, KualiDecimal> currentObjectCodeBalances = salaryExpenseTransferDocument.getUnbalancedObjectCodes();
075    
076            Set<Entry<String, KualiDecimal>> initiatedObjectCodes = initiatedObjectCodeBalances.entrySet();
077            Set<Entry<String, KualiDecimal>> currentObjectCodes = currentObjectCodeBalances.entrySet();
078    
079            if (!initiatedObjectCodes.equals(currentObjectCodes))
080                isUnchanged = false;
081    
082            return isUnchanged;
083    
084        }
085    
086        /**
087         * Gets the accountingDocumentForValidation attribute. 
088         * @return Returns the accountingDocumentForValidation.
089         */
090        public AccountingDocument getAccountingDocumentForValidation() {
091            return accountingDocumentForValidation;
092        }
093    
094        /**
095         * Sets the accountingDocumentForValidation attribute value.
096         * 
097         * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
098         */
099        public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
100            this.accountingDocumentForValidation = accountingDocumentForValidation;
101        }
102    }