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 org.apache.commons.lang.StringUtils;
019    import org.kuali.kfs.sys.KFSKeyConstants;
020    import org.kuali.kfs.sys.KFSPropertyConstants;
021    import org.kuali.kfs.sys.businessobject.AccountingLine;
022    import org.kuali.kfs.sys.document.validation.GenericValidation;
023    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
024    import org.kuali.rice.kns.datadictionary.BusinessObjectEntry;
025    import org.kuali.rice.kns.service.DataDictionaryService;
026    import org.kuali.rice.kns.util.GlobalVariables;
027    
028    public class PreEncumbranceRequiredReferenceFieldValidation extends GenericValidation {
029        private DataDictionaryService dataDictionaryService;
030        private AccountingLine accountingLineForValidation;
031    
032        public boolean validate(AttributedDocumentEvent event) {
033            boolean valid = true;
034            if (this.getAccountingLineForValidation().isTargetAccountingLine()) {
035                final BusinessObjectEntry boe = getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(getAccountingLineForValidation().getClass().getName());
036                
037                if (StringUtils.isEmpty(getAccountingLineForValidation().getReferenceNumber())) {
038                    putRequiredPropertyError(boe, KFSPropertyConstants.REFERENCE_NUMBER);
039                    valid = false;
040                }
041            }
042            return valid;
043        }
044    
045        /**
046         * Adds a global error for a missing required property. This is used for properties, such as reference origin code, which cannot
047         * be required by the DataDictionary validation because not all documents require them.
048         * 
049         * @param boe
050         * @param propertyName
051         */
052        protected void putRequiredPropertyError(BusinessObjectEntry boe, String propertyName) {
053            final String label = boe.getAttributeDefinition(propertyName).getShortLabel();
054            GlobalVariables.getMessageMap().putError(propertyName, KFSKeyConstants.ERROR_REQUIRED, label);
055        }
056    
057        /**
058         * Gets the dataDictionaryService attribute. 
059         * @return Returns the dataDictionaryService.
060         */
061        public DataDictionaryService getDataDictionaryService() {
062            return dataDictionaryService;
063        }
064    
065        /**
066         * Sets the dataDictionaryService attribute value.
067         * @param dataDictionaryService The dataDictionaryService to set.
068         */
069        public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
070            this.dataDictionaryService = dataDictionaryService;
071        }
072    
073        /**
074         * Gets the accountingLineForValidation attribute. 
075         * @return Returns the accountingLineForValidation.
076         */
077        public AccountingLine getAccountingLineForValidation() {
078            return accountingLineForValidation;
079        }
080    
081        /**
082         * Sets the accountingLineForValidation attribute value.
083         * @param accountingLineForValidation The accountingLineForValidation to set.
084         */
085        public void setAccountingLineForValidation(AccountingLine accoutingLineForValidation) {
086            this.accountingLineForValidation = accoutingLineForValidation;
087        }
088    }