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 org.apache.commons.lang.StringUtils;
019    import org.kuali.kfs.module.ld.LaborConstants;
020    import org.kuali.kfs.module.ld.LaborKeyConstants;
021    import org.kuali.kfs.module.ld.businessobject.ExpenseTransferAccountingLine;
022    import org.kuali.kfs.module.ld.businessobject.LaborObject;
023    import org.kuali.kfs.module.ld.document.BenefitExpenseTransferDocument;
024    import org.kuali.kfs.sys.KFSPropertyConstants;
025    import org.kuali.kfs.sys.businessobject.AccountingLine;
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.util.GlobalVariables;
029    import org.kuali.rice.kns.util.ObjectUtils;
030    
031    /**
032     * Validates that an accounting line has fringe benefit object code 
033     */
034    public class BenefitExpenseTransferFringeBenefitObjectCodeValidation extends GenericValidation {
035        private AccountingLine accountingLineForValidation;
036    
037        /**
038         * only fringe benefit labor object codes are allowed on the benefit expense transfer document
039         * <strong>Expects an accounting line as the first a parameter</strong>
040         * @see org.kuali.kfs.validation.Validation#validate(java.lang.Object[])
041         */
042        public boolean validate(AttributedDocumentEvent event) {
043            boolean result = true;
044            
045            AccountingLine accountingLine = getAccountingLineForValidation();
046            if (!isFringeBenefitObjectCode(accountingLine)) {
047                GlobalVariables.getMessageMap().putError(KFSPropertyConstants.FINANCIAL_OBJECT_CODE, LaborKeyConstants.INVALID_FRINGE_OBJECT_CODE_ERROR );
048                result = false;
049            }
050            return result;
051        }
052    
053        /**
054         * Checks whether the given AccountingLine's Object Code is a fringe benefit object code.
055         * 
056         * @param accountingLine The accounting line the fringe benefit object code will be retrieved from.
057         * @return True if the given accounting line's object code is a fringe benefit object code, false otherwise.
058         */ 
059        protected boolean isFringeBenefitObjectCode(AccountingLine accountingLine) {
060            boolean fringeObjectCode = true ;
061            
062            ExpenseTransferAccountingLine expenseTransferAccountingLine = (ExpenseTransferAccountingLine) accountingLine;
063    
064            expenseTransferAccountingLine.refreshReferenceObject(KFSPropertyConstants.LABOR_OBJECT); 
065            LaborObject laborObject = expenseTransferAccountingLine.getLaborObject();
066            if (ObjectUtils.isNull(laborObject)) {
067                return false;
068            }
069            boolean isItFringeObjectCode = LaborConstants.BenefitExpenseTransfer.LABOR_LEDGER_BENEFIT_CODE.equals(laborObject.getFinancialObjectFringeOrSalaryCode());
070            if (!isItFringeObjectCode) {
071                fringeObjectCode = false ;
072            }
073            
074            return fringeObjectCode ;
075        }
076    
077        /**
078         * Gets the accountingLineForValidation attribute. 
079         * @return Returns the accountingLineForValidation.
080         */
081        public AccountingLine getAccountingLineForValidation() {
082            return accountingLineForValidation;
083        }
084    
085        /**
086         * Sets the accountingLineForValidation attribute value.
087         * @param accountingLineForValidation The accountingLineForValidation to set.
088         */
089        public void setAccountingLineForValidation(AccountingLine accountingLineForValidation) {
090            this.accountingLineForValidation = accountingLineForValidation;
091        }
092    }