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