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.KFSConstants.GENERIC_CODE_PROPERTY_NAME;
019    import static org.kuali.kfs.sys.KFSPropertyConstants.REFERENCE_NUMBER;
020    import static org.kuali.kfs.sys.KFSPropertyConstants.REFERENCE_ORIGIN_CODE;
021    import static org.kuali.kfs.sys.KFSPropertyConstants.REFERENCE_TYPE_CODE;
022    
023    import java.util.Collection;
024    import java.util.HashMap;
025    import java.util.Map;
026    
027    import org.apache.commons.lang.StringUtils;
028    import org.kuali.kfs.coa.businessobject.BalanceType;
029    import org.kuali.kfs.module.ld.LaborConstants;
030    import org.kuali.kfs.module.ld.LaborKeyConstants;
031    import org.kuali.kfs.sys.document.service.AccountingDocumentRuleHelperService;
032    import org.kuali.kfs.sys.KFSConstants;
033    import org.kuali.kfs.sys.KFSKeyConstants;
034    import org.kuali.kfs.sys.KFSPropertyConstants;
035    import org.kuali.kfs.sys.ObjectUtil;
036    import org.kuali.kfs.sys.businessobject.AccountingLine;
037    import org.kuali.kfs.sys.context.SpringContext;
038    import org.kuali.rice.kns.datadictionary.BusinessObjectEntry;
039    import org.kuali.rice.kns.service.BusinessObjectService;
040    import org.kuali.rice.kns.service.DataDictionaryService;
041    import org.kuali.rice.kim.service.PersonService;
042    import org.kuali.rice.kns.util.GlobalVariables;
043    import org.kuali.kfs.sys.document.validation.GenericValidation;
044    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
045    
046    /**
047     * Validates that a labor journal voucher document's accounting lines have valid encumbrance code 
048     */
049    public class LaborJournalVoucherExternalEncumbranceValidation extends GenericValidation {
050        private AccountingLine accountingLineForValidation;
051        
052        /**
053         * Validates that the accounting line in the labor journal voucher document for valid encumbrance code 
054         * @see org.kuali.kfs.validation.Validation#validate(java.lang.Object[])
055         */
056        public boolean validate(AttributedDocumentEvent event) {
057            boolean result = true;
058            
059            AccountingLine accountingLineForValidation = getAccountingLineForValidation() ;
060            if (!externalEncumbranceSpecificBusinessRulesValid(accountingLineForValidation)) {
061                result = false ;
062            }
063            return result ;    
064        }
065    
066        /**
067         * Checks whether employee id exists
068         * 
069         * @param accountingLineForValidation laborJournalVoucherDetail line will be checked for valid encumbrance code
070         * @return True if accountingLineForValidation has the valid encumbrance code, false otherwise.
071         */ 
072        protected boolean externalEncumbranceSpecificBusinessRulesValid(AccountingLine accountingLineForValidation) {
073            boolean externalEncumbranceValid  = true ;
074            
075            accountingLineForValidation.refreshReferenceObject(KFSPropertyConstants.BALANCE_TYP);
076            BalanceType balanceTyp = accountingLineForValidation.getBalanceTyp();
077            AccountingDocumentRuleHelperService journalVoucherRuleUtil = SpringContext.getBean(AccountingDocumentRuleHelperService.class) ;
078            if (!journalVoucherRuleUtil.isValidBalanceType(balanceTyp, GENERIC_CODE_PROPERTY_NAME)) {
079                externalEncumbranceValid = false ;
080            }
081            else if (balanceTyp.isFinBalanceTypeEncumIndicator() && KFSConstants.ENCUMB_UPDT_DOCUMENT_CD.equals(accountingLineForValidation.getEncumbranceUpdateCode())) {
082                externalEncumbranceValid = this.isRequiredReferenceFieldsValid(accountingLineForValidation);
083            }
084                
085            return externalEncumbranceValid ;
086        }
087    
088        /**
089         * This method checks that values exist in the three reference fields that are required if the balance type is set to EXTERNAL
090         * ENCUMBRANCE.
091         * 
092         * @param accountingLine The accounting line being validated.
093         * @return True if all of the required external encumbrance reference fields are valid, false otherwise.
094         */
095        protected boolean isRequiredReferenceFieldsValid(AccountingLine accountingLine) {
096            boolean valid = true;
097    
098            if (StringUtils.isEmpty(accountingLine.getReferenceOriginCode())) {
099                valid = false;
100            }
101            if (StringUtils.isEmpty(accountingLine.getReferenceNumber())) {
102                valid = false;
103            }
104            if (StringUtils.isEmpty(accountingLine.getReferenceTypeCode())) {
105                valid = false;
106            }
107            return valid;
108        }    
109        
110        /**
111         * Gets the accountingLineForValidation attribute. 
112         * @return Returns the accountingLineForValidation.
113         */
114        public AccountingLine getAccountingLineForValidation() {
115            return accountingLineForValidation;
116        }
117    
118        /**
119         * Sets the accountingLineForValidation attribute value.
120         * @param accountingLineForValidation The accountingLineForValidation to set.
121         */
122        public void setAccountingLineForValidation(AccountingLine accountingLineForValidation) {
123            this.accountingLineForValidation = accountingLineForValidation;
124        }
125    }