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    
021    import java.util.ArrayList;
022    import java.util.List;
023    
024    import org.apache.commons.lang.StringUtils;
025    import org.kuali.kfs.coa.businessobject.Account;
026    import org.kuali.kfs.module.ld.LaborConstants ;
027    import org.kuali.kfs.module.ld.LaborKeyConstants; 
028    import org.kuali.kfs.module.ld.LaborPropertyConstants;
029    import org.kuali.kfs.module.ld.businessobject.ExpenseTransferAccountingLine;
030    import org.kuali.kfs.module.ld.businessobject.ExpenseTransferSourceAccountingLine;
031    import org.kuali.kfs.module.ld.businessobject.LaborObject;
032    import org.kuali.kfs.module.ld.document.LaborExpenseTransferDocumentBase;
033    import org.kuali.kfs.module.ld.document.SalaryExpenseTransferDocument;
034    import org.kuali.kfs.sys.KFSKeyConstants;
035    import org.kuali.kfs.sys.KFSPropertyConstants;
036    import org.kuali.rice.kns.util.ObjectUtils;
037    import org.kuali.kfs.sys.businessobject.AccountingLine;
038    import org.kuali.kfs.sys.document.AccountingDocument;
039    import org.kuali.kfs.sys.document.validation.GenericValidation;
040    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
041    import org.kuali.rice.kns.util.GlobalVariables;
042    
043    /**
044     * determine whether the given accounting line has already been in the given document
045     * 
046     * @param accountingDocument the given document
047     * @param accountingLine the given accounting line
048     * @return true if the given accounting line has already been in the given document; otherwise, false
049     */
050    public class LaborExpenseTransferExpiredAccountBeUsedValidation 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            // determine if an expired account can be used to accept amount transfer
064            boolean canExpiredAccountBeUsed = canExpiredAccountBeUsed(accountingLine);
065            
066            // not allow the duplicate source accounting line in the document
067            if (!canExpiredAccountBeUsed) {
068                GlobalVariables.getMessageMap().putError(KFSPropertyConstants.ACCOUNT, KFSKeyConstants.ERROR_ACCOUNT_EXPIRED );
069                return false;
070            }
071            
072            return result;
073        }
074    
075        /**
076         * determine whether the expired account in the target accounting line can be used.
077         * 
078         * @param accountingDocument the given accounting line
079         * @return true if the expired account in the target accounting line can be used; otherwise, false
080         */
081        protected boolean canExpiredAccountBeUsed(AccountingLine accountingLine) {
082    
083            Account account = accountingLine.getAccount();
084            if (ObjectUtils.isNotNull(account) && account.isExpired() && !account.isClosed()) {
085                String overrideCode = accountingLine.getOverrideCode();
086                boolean canExpiredAccountUsed = EXPIRED_ACCOUNT.equals(overrideCode);
087                canExpiredAccountUsed = canExpiredAccountUsed || EXPIRED_ACCOUNT_AND_NON_FRINGE_ACCOUNT_USED.equals(overrideCode);
088    
089                if (!canExpiredAccountUsed) {
090                    return false;
091                }
092            }
093            return true;
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    }