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.KFSPropertyConstants.REFERENCE_NUMBER;
019    
020    import org.apache.commons.lang.StringUtils;
021    import org.kuali.kfs.fp.document.GeneralErrorCorrectionDocument;
022    import org.kuali.kfs.fp.document.NonCheckDisbursementDocument;
023    import org.kuali.kfs.sys.KFSKeyConstants;
024    import org.kuali.kfs.sys.businessobject.AccountingLine;
025    import org.kuali.kfs.sys.context.SpringContext;
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.datadictionary.BusinessObjectEntry;
029    import org.kuali.rice.kns.service.DataDictionaryService;
030    import org.kuali.rice.kns.service.ParameterService;
031    import org.kuali.rice.kns.util.GlobalVariables;
032    
033    /**
034     * Validates that an accounting line has a reference number 
035     */
036    public class NonCheckDisbursementRequiredReferenceFieldValidation extends GenericValidation {
037        private DataDictionaryService dataDictionaryService;
038        private AccountingLine accountingLineForValidation;
039    
040        /**
041         * determines if a reference number has been added to the Accounting Line
042         * <strong>Expects an accounting line as the first a parameter</strong>
043         * @see org.kuali.kfs.sys.document.validation.Validation#validate(java.lang.Object[])
044         */
045        
046        
047        public boolean validate(AttributedDocumentEvent event) {
048            
049            NonCheckDisbursementDocument document = (NonCheckDisbursementDocument)event.getDocument();
050            
051            boolean valid = true;
052            Class alclass = null;
053            BusinessObjectEntry boe;
054    
055            if (accountingLineForValidation.isSourceAccountingLine()) {
056                alclass = document.getSourceAccountingLineClass();
057            }
058            else if (accountingLineForValidation.isTargetAccountingLine()) {
059                alclass = document.getTargetAccountingLineClass();
060            }
061    
062            boe = getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(alclass.getName());
063           
064            if (StringUtils.isEmpty(accountingLineForValidation.getReferenceNumber())) {
065                putRequiredPropertyError(boe, REFERENCE_NUMBER);
066                valid = false;
067            }
068            return valid;
069        }
070    
071        /**
072         * Adds a global error for a missing required property. This is used for properties, such as reference origin code, which cannot
073         * be required by the DataDictionary validation because not all documents require them.
074         * 
075         * @param boe
076         * @param propertyName
077         */
078        protected void putRequiredPropertyError(BusinessObjectEntry boe, String propertyName) {
079    
080            String label = boe.getAttributeDefinition(propertyName).getShortLabel();
081            GlobalVariables.getMessageMap().putError(propertyName, KFSKeyConstants.ERROR_REQUIRED, label);
082    
083        }
084    
085        /**
086         * Gets the accountingLineForValidation attribute. 
087         * @return Returns the accountingLineForValidation.
088         */
089        public AccountingLine getAccountingLineForValidation() {
090            return accountingLineForValidation;
091        }
092    
093        /**
094         * Sets the accountingLineForValidation attribute value.
095         * @param accountingLineForValidation The accountingLineForValidation to set.
096         */
097        public void setAccountingLineForValidation(AccountingLine accountingLineForValidation) {
098            this.accountingLineForValidation = accountingLineForValidation;
099        }
100    
101        /**
102         * Gets the dataDictionaryService attribute. 
103         * @return Returns the dataDictionaryService.
104         */
105        public DataDictionaryService getDataDictionaryService() {
106            return dataDictionaryService;
107        }
108    
109        /**
110         * Sets the dataDictionaryService attribute value.
111         * @param dataDictionaryService The dataDictionaryService to set.
112         */
113        public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
114            this.dataDictionaryService = dataDictionaryService;
115        }
116    }