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 static org.kuali.kfs.sys.businessobject.AccountingLineOverride.CODE.EXPIRED_ACCOUNT;
019    import static org.kuali.kfs.sys.businessobject.AccountingLineOverride.CODE.EXPIRED_ACCOUNT_AND_NON_FRINGE_ACCOUNT_USED;
020    import static org.kuali.kfs.sys.businessobject.AccountingLineOverride.CODE.NON_FRINGE_ACCOUNT_USED;
021    
022    import java.util.ArrayList;
023    import java.util.List;
024    
025    import org.apache.commons.lang.StringUtils;
026    import org.kuali.kfs.coa.businessobject.Account;
027    import org.kuali.kfs.module.ld.LaborConstants ;
028    import org.kuali.kfs.module.ld.LaborKeyConstants; 
029    import org.kuali.kfs.module.ld.LaborPropertyConstants;
030    import org.kuali.kfs.module.ld.businessobject.ExpenseTransferAccountingLine;
031    import org.kuali.kfs.module.ld.businessobject.ExpenseTransferSourceAccountingLine;
032    import org.kuali.kfs.module.ld.businessobject.LaborObject;
033    import org.kuali.kfs.module.ld.document.LaborExpenseTransferDocumentBase;
034    import org.kuali.kfs.module.ld.document.SalaryExpenseTransferDocument;
035    import org.kuali.kfs.sys.KFSKeyConstants;
036    import org.kuali.kfs.sys.KFSPropertyConstants;
037    import org.kuali.rice.kns.util.ObjectUtils;
038    import org.kuali.kfs.sys.businessobject.AccountingLine;
039    import org.kuali.kfs.sys.document.AccountingDocument;
040    import org.kuali.kfs.sys.document.validation.GenericValidation;
041    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
042    import org.kuali.rice.kns.util.GlobalVariables;
043    
044    /**
045     * verify if the accounts in target accounting lines accept fringe benefits
046     * 
047     * @param accountingLine the given accounting line
048     * @return true if the accounts in the target accounting lines accept fringe benefits; otherwise, false
049     */
050    public class LaborExpenseTransferAccountAcceptFringeBenefitValidation extends GenericValidation {
051        private AccountingLine accountingLineForValidation;
052        
053        /**
054         * Validates that an accounting line whether the expired account in the target accounting line 
055         * can be used.
056         * <strong>Expects an accounting line as the first a parameter</strong>
057         * @see org.kuali.kfs.validation.Validation#validate(java.lang.Object[])
058         */
059        public boolean validate(AttributedDocumentEvent event) {
060            boolean result = true;
061            AccountingLine accountingLine = getAccountingLineForValidation();
062              
063            // verify if the accounts in target accounting lines accept fringe benefits
064            if (!isAccountAcceptFringeBenefit(accountingLine)) {
065                GlobalVariables.getMessageMap().putError(KFSPropertyConstants.TARGET_ACCOUNTING_LINES, LaborKeyConstants.ERROR_ACCOUNT_NOT_ACCEPT_FRINGES, accountingLine.getAccount().getReportsToChartOfAccountsCode(), accountingLine.getAccount().getReportsToAccountNumber());
066                return false;
067            }
068                            
069            return result;
070        }
071    
072        /**
073         * Determines whether the account in the target line accepts fringe benefits.
074         * 
075         * @param accountingLine the line to check
076         * @return true if the accounts in the target accounting lines accept fringe benefits; otherwise, false
077         */
078        protected boolean isAccountAcceptFringeBenefit(AccountingLine accountingLine) {
079            boolean acceptsFringeBenefits = true;
080    
081     //       accountingLine.refreshReferenceObject(KFSPropertyConstants.ACCOUNT);
082            Account account = accountingLine.getAccount();
083            if (ObjectUtils.isNotNull(account) && !account.isAccountsFringesBnftIndicator()) {
084                String overrideCode = accountingLine.getOverrideCode();
085                boolean canNonFringeAccountUsed = NON_FRINGE_ACCOUNT_USED.equals(overrideCode);
086                canNonFringeAccountUsed = canNonFringeAccountUsed || EXPIRED_ACCOUNT_AND_NON_FRINGE_ACCOUNT_USED.equals(overrideCode);
087    
088                if (!canNonFringeAccountUsed) {
089                    acceptsFringeBenefits = false;
090                }
091            }
092    
093            return acceptsFringeBenefits;
094        }
095            
096        /**
097         * Gets the accountingLineForValidation attribute. 
098         * @return Returns the accountingLineForValidation.
099         */
100        public AccountingLine getAccountingLineForValidation() {
101            return accountingLineForValidation;
102        }
103    
104        /**
105         * Sets the accountingLineForValidation attribute value.
106         * @param accountingLineForValidation The accountingLineForValidation to set.
107         */
108        public void setAccountingLineForValidation(AccountingLine accountingLineForValidation) {
109            this.accountingLineForValidation = accountingLineForValidation;
110        }
111    }