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.SELECTED_ACCOUNTING_PERIOD; 019 import static org.kuali.kfs.sys.document.validation.impl.AccountingDocumentRuleBaseConstants.ERROR_PATH.DOCUMENT_ERROR_PREFIX; 020 021 import org.kuali.kfs.coa.businessobject.AccountingPeriod; 022 import org.kuali.kfs.fp.document.JournalVoucherDocument; 023 import org.kuali.kfs.sys.KFSConstants; 024 import org.kuali.kfs.sys.KFSKeyConstants; 025 import org.kuali.kfs.sys.KFSPropertyConstants; 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.AttributeDefinition; 029 import org.kuali.rice.kns.datadictionary.DataDictionaryEntry; 030 import org.kuali.rice.kns.service.DataDictionaryService; 031 import org.kuali.rice.kns.util.GlobalVariables; 032 import org.kuali.rice.kns.util.ObjectUtils; 033 034 /** 035 * Validation of the accounting period on a Journal Voucher document. 036 */ 037 public class JournalVoucherAccountingPeriodValidation extends GenericValidation { 038 private JournalVoucherDocument journalVoucherForValidation; 039 private DataDictionaryService dataDictionaryService; 040 041 /** 042 * Checks that the accounting period for a journal voucher is present in the persistence store and currently open 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 // retrieve from system to make sure it exists 047 String label = getLabelFromDataDictionary(JournalVoucherDocument.class, KFSPropertyConstants.ACCOUNTING_PERIOD); 048 getJournalVoucherForValidation().refreshReferenceObject(KFSPropertyConstants.ACCOUNTING_PERIOD); 049 AccountingPeriod accountingPeriod = getJournalVoucherForValidation().getAccountingPeriod(); 050 if (ObjectUtils.isNull(accountingPeriod)) { 051 GlobalVariables.getMessageMap().putError(DOCUMENT_ERROR_PREFIX + SELECTED_ACCOUNTING_PERIOD, KFSKeyConstants.ERROR_EXISTENCE, label); 052 return false; 053 } 054 055 // make sure it's open for use 056 if (!accountingPeriod.isActive()) { 057 GlobalVariables.getMessageMap().putError(DOCUMENT_ERROR_PREFIX + SELECTED_ACCOUNTING_PERIOD, KFSKeyConstants.ERROR_DOCUMENT_ACCOUNTING_PERIOD_CLOSED); 058 return false; 059 } 060 061 return true; 062 } 063 064 /** 065 * Looks up a label from the data dictionary 066 * @param entryClass the class of the attribute to lookup the label for 067 * @param attributeName the attribute to look up the label for 068 * @return the label 069 */ 070 protected String getLabelFromDataDictionary(Class entryClass, String attributeName) { 071 DataDictionaryEntry entry = getDataDictionaryService().getDataDictionary().getDictionaryObjectEntry(entryClass.getName()); 072 if (entry == null) { 073 throw new IllegalArgumentException("Cannot find DataDictionary entry for " + entryClass); 074 } 075 AttributeDefinition attributeDefinition = entry.getAttributeDefinition(attributeName); 076 if (attributeDefinition == null) { 077 throw new IllegalArgumentException("Cannot find " + entryClass + " attribute with name " + attributeName); 078 } 079 return attributeDefinition.getLabel(); 080 } 081 082 /** 083 * Gets the journalVoucherForValidation attribute. 084 * @return Returns the journalVoucherForValidation. 085 */ 086 public JournalVoucherDocument getJournalVoucherForValidation() { 087 return journalVoucherForValidation; 088 } 089 090 /** 091 * Sets the journalVoucherForValidation attribute value. 092 * @param journalVoucherForValidation The journalVoucherForValidation to set. 093 */ 094 public void setJournalVoucherForValidation(JournalVoucherDocument journalVoucherForValidation) { 095 this.journalVoucherForValidation = journalVoucherForValidation; 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 }