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.fp.document.validation.impl;
017    
018    import static org.kuali.kfs.sys.KFSConstants.BALANCE_TYPE_EXTERNAL_ENCUMBRANCE;
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 org.apache.commons.lang.StringUtils;
024    import org.kuali.kfs.fp.businessobject.VoucherSourceAccountingLine;
025    import org.kuali.kfs.sys.KFSKeyConstants;
026    import org.kuali.kfs.sys.businessobject.AccountingLine;
027    import org.kuali.kfs.sys.document.validation.GenericValidation;
028    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
029    import org.kuali.rice.kns.datadictionary.BusinessObjectEntry;
030    import org.kuali.rice.kns.service.DataDictionaryService;
031    import org.kuali.rice.kns.util.GlobalVariables;
032    
033    /**
034     * Validation that if the Journal Voucher is using an external encumbrance balance type, reference fields are included on each accounting line 
035     */
036    public class JournalVoucherAccountingLineExternalEncumbranceReferenceValidation extends GenericValidation {
037        private AccountingLine accountingLineForValidation;
038        private DataDictionaryService dataDictionaryService;
039    
040        /**
041         * This method checks that values exist in the three reference fields (referenceOriginCode, referenceTypeCode, referenceNumber)
042         * that are required if the balance type is set to EXTERNAL ENCUMBRANCE.
043         * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
044         */
045        public boolean validate(AttributedDocumentEvent event) {
046            if (BALANCE_TYPE_EXTERNAL_ENCUMBRANCE.equals(getAccountingLineForValidation().getBalanceTypeCode())) {
047                boolean valid = true;
048    
049                BusinessObjectEntry boe = getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(VoucherSourceAccountingLine.class.getName());
050                if (StringUtils.isEmpty(getAccountingLineForValidation().getReferenceOriginCode())) {
051                    putRequiredPropertyError(boe, REFERENCE_ORIGIN_CODE);
052                    valid = false;
053                }
054                if (StringUtils.isEmpty(getAccountingLineForValidation().getReferenceNumber())) {
055                    putRequiredPropertyError(boe, REFERENCE_NUMBER);
056                    valid = false;
057                }
058                if (StringUtils.isEmpty(getAccountingLineForValidation().getReferenceTypeCode())) {
059                    putRequiredPropertyError(boe, REFERENCE_TYPE_CODE);
060                    valid = false;
061                }
062                return valid;
063            }
064            return true;
065        }
066        
067        /**
068         * Adds a global error for a missing required property. This is used for properties, such as reference origin code, which cannot
069         * be required by the DataDictionary validation because not all documents require them.
070         * 
071         * @param boe
072         * @param propertyName
073         */
074        protected void putRequiredPropertyError(BusinessObjectEntry boe, String propertyName) {
075            String label = boe.getAttributeDefinition(propertyName).getShortLabel();
076            GlobalVariables.getMessageMap().putError(propertyName, KFSKeyConstants.ERROR_REQUIRED, label);
077        }
078    
079        /**
080         * Gets the accountingLineForValidation attribute. 
081         * @return Returns the accountingLineForValidation.
082         */
083        public AccountingLine getAccountingLineForValidation() {
084            return accountingLineForValidation;
085        }
086    
087        /**
088         * Sets the accountingLineForValidation attribute value.
089         * @param accountingLineForValidation The accountingLineForValidation to set.
090         */
091        public void setAccountingLineForValidation(AccountingLine voucherSourceAccountingLine) {
092            this.accountingLineForValidation = voucherSourceAccountingLine;
093        }
094    
095        /**
096         * Gets the dataDictionaryService attribute. 
097         * @return Returns the dataDictionaryService.
098         */
099        public DataDictionaryService getDataDictionaryService() {
100            return dataDictionaryService;
101        }
102    
103        /**
104         * Sets the dataDictionaryService attribute value.
105         * @param dataDictionaryService The dataDictionaryService to set.
106         */
107        public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
108            this.dataDictionaryService = dataDictionaryService;
109        }
110    }