View Javadoc
1   /*
2    * Copyright 2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.fp.document.validation.impl;
17  
18  import static org.kuali.ole.sys.OLEPropertyConstants.SELECTED_ACCOUNTING_PERIOD;
19  import static org.kuali.ole.sys.document.validation.impl.AccountingDocumentRuleBaseConstants.ERROR_PATH.DOCUMENT_ERROR_PREFIX;
20  
21  import org.kuali.ole.coa.businessobject.AccountingPeriod;
22  import org.kuali.ole.fp.document.JournalVoucherDocument;
23  import org.kuali.ole.sys.OLEKeyConstants;
24  import org.kuali.ole.sys.OLEPropertyConstants;
25  import org.kuali.ole.sys.document.validation.GenericValidation;
26  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
27  import org.kuali.rice.kns.service.DataDictionaryService;
28  import org.kuali.rice.krad.datadictionary.AttributeDefinition;
29  import org.kuali.rice.krad.datadictionary.DataDictionaryEntry;
30  import org.kuali.rice.krad.util.GlobalVariables;
31  import org.kuali.rice.krad.util.ObjectUtils;
32  
33  /**
34   * Validation of the accounting period on a Journal Voucher document.
35   */
36  public class JournalVoucherAccountingPeriodValidation extends GenericValidation {
37      private JournalVoucherDocument journalVoucherForValidation;
38      private DataDictionaryService dataDictionaryService;
39  
40      /**
41       * Checks that the accounting period for a journal voucher is present in the persistence store and currently open
42       * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
43       */
44      public boolean validate(AttributedDocumentEvent event) {
45          // retrieve from system to make sure it exists
46          String label = getLabelFromDataDictionary(JournalVoucherDocument.class, OLEPropertyConstants.ACCOUNTING_PERIOD);
47          getJournalVoucherForValidation().refreshReferenceObject(OLEPropertyConstants.ACCOUNTING_PERIOD);
48          AccountingPeriod accountingPeriod = getJournalVoucherForValidation().getAccountingPeriod();
49          if (ObjectUtils.isNull(accountingPeriod)) {
50              GlobalVariables.getMessageMap().putError(DOCUMENT_ERROR_PREFIX + SELECTED_ACCOUNTING_PERIOD, OLEKeyConstants.ERROR_EXISTENCE, label);
51              return false;
52          }
53  
54          // make sure it's open for use
55          if (!accountingPeriod.isActive()) {
56              GlobalVariables.getMessageMap().putError(DOCUMENT_ERROR_PREFIX + SELECTED_ACCOUNTING_PERIOD, OLEKeyConstants.ERROR_DOCUMENT_ACCOUNTING_PERIOD_CLOSED);
57              return false;
58          }
59  
60          return true;
61      }
62      
63      /**
64       * Looks up a label from the data dictionary
65       * @param entryClass the class of the attribute to lookup the label for
66       * @param attributeName the attribute to look up the label for
67       * @return the label
68       */
69      protected String getLabelFromDataDictionary(Class entryClass, String attributeName) {
70          DataDictionaryEntry entry = getDataDictionaryService().getDataDictionary().getDictionaryObjectEntry(entryClass.getName());
71          if (entry == null) {
72              throw new IllegalArgumentException("Cannot find DataDictionary entry for " + entryClass);
73          }
74          AttributeDefinition attributeDefinition = entry.getAttributeDefinition(attributeName);
75          if (attributeDefinition == null) {
76              throw new IllegalArgumentException("Cannot find " + entryClass + " attribute with name " + attributeName);
77          }
78          return attributeDefinition.getLabel();
79      }
80  
81      /**
82       * Gets the journalVoucherForValidation attribute. 
83       * @return Returns the journalVoucherForValidation.
84       */
85      public JournalVoucherDocument getJournalVoucherForValidation() {
86          return journalVoucherForValidation;
87      }
88  
89      /**
90       * Sets the journalVoucherForValidation attribute value.
91       * @param journalVoucherForValidation The journalVoucherForValidation to set.
92       */
93      public void setJournalVoucherForValidation(JournalVoucherDocument journalVoucherForValidation) {
94          this.journalVoucherForValidation = journalVoucherForValidation;
95      }
96  
97      /**
98       * Gets the dataDictionaryService attribute. 
99       * @return Returns the dataDictionaryService.
100      */
101     public DataDictionaryService getDataDictionaryService() {
102         return dataDictionaryService;
103     }
104 
105     /**
106      * Sets the dataDictionaryService attribute value.
107      * @param dataDictionaryService The dataDictionaryService to set.
108      */
109     public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
110         this.dataDictionaryService = dataDictionaryService;
111     }
112 }