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.ENCUMBRANCE_UPDATE_CODE;
19  import static org.kuali.ole.sys.OLEPropertyConstants.REFERENCE_NUMBER;
20  import static org.kuali.ole.sys.OLEPropertyConstants.REFERENCE_ORIGIN_CODE;
21  import static org.kuali.ole.sys.OLEPropertyConstants.REFERENCE_TYPE_CODE;
22  
23  import java.util.List;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.kuali.ole.coa.businessobject.AccountingPeriod;
27  import org.kuali.ole.coa.service.BalanceTypeService;
28  import org.kuali.ole.fp.businessobject.VoucherSourceAccountingLine;
29  import org.kuali.ole.fp.document.JournalVoucherDocument;
30  import org.kuali.ole.sys.OLEConstants;
31  import org.kuali.ole.sys.OLEKeyConstants;
32  import org.kuali.ole.sys.OLEPropertyConstants;
33  import org.kuali.ole.sys.businessobject.AccountingLine;
34  import org.kuali.ole.sys.context.SpringContext;
35  import org.kuali.ole.sys.document.validation.GenericValidation;
36  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
37  import org.kuali.rice.kns.datadictionary.BusinessObjectEntry;
38  import org.kuali.rice.kns.service.DataDictionaryService;
39  import org.kuali.rice.krad.util.GlobalVariables;
40  
41  /**
42   * Validation that if the Journal Voucher is using an encumbrance balance type, reference fields are included on each accounting line 
43   */
44  public class JournalVoucherAccountingLineEncumbranceReferenceValidation extends GenericValidation {
45      private JournalVoucherDocument journalVoucherForValidation;
46      private AccountingLine accountingLineForValidation;
47      
48      private DataDictionaryService dataDictionaryService;
49  
50      /**
51       * This method checks that values exist in the three reference fields (referenceOriginCode, referenceTypeCode, referenceNumber)
52       * that are required if 
53       * 1) the balance type is of type Encumbrance 
54       * 2) encumbrance update code is R
55       * 
56       * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
57       */
58      public boolean validate(AttributedDocumentEvent event) {        
59          boolean valid = true;
60          if (isEncumbranceBalanceType(getAccountingLineForValidation().getBalanceTypeCode())) {            
61              if (StringUtils.isBlank(getAccountingLineForValidation().getEncumbranceUpdateCode())) {
62                  BusinessObjectEntry boe = (BusinessObjectEntry) getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(VoucherSourceAccountingLine.class.getName());
63                  putRequiredPropertyError(boe, ENCUMBRANCE_UPDATE_CODE);
64                  valid = false;
65              } else if (OLEConstants.ENCUMB_UPDT_REFERENCE_DOCUMENT_CD.equals(getAccountingLineForValidation().getEncumbranceUpdateCode())){
66                  //check the required reference fields
67                  if (StringUtils.isBlank(getAccountingLineForValidation().getReferenceOriginCode())) {
68                      BusinessObjectEntry boe = (BusinessObjectEntry) getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(VoucherSourceAccountingLine.class.getName());
69                      putRequiredPropertyError(boe, REFERENCE_ORIGIN_CODE);
70                      valid = false;
71                  }
72                  if (StringUtils.isBlank(getAccountingLineForValidation().getReferenceNumber())) {
73                      BusinessObjectEntry boe = (BusinessObjectEntry) getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(VoucherSourceAccountingLine.class.getName());
74                      putRequiredPropertyError(boe, REFERENCE_NUMBER);
75                      valid = false;
76                  }
77                  if (StringUtils.isBlank(getAccountingLineForValidation().getReferenceTypeCode())) {
78                      BusinessObjectEntry boe = (BusinessObjectEntry) getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(VoucherSourceAccountingLine.class.getName());
79                      putRequiredPropertyError(boe, REFERENCE_TYPE_CODE);
80                      valid = false;
81                  }
82              }
83          }
84          return valid;
85      }
86      
87      /**
88       * Using the document accounting period to determine university fiscal year and look up all the encumbrance
89       * balance type - check if the selected balance type is for encumbrance
90       * 
91       * @return true/false  - true if it is an encumbrance balance type
92       */
93      private boolean isEncumbranceBalanceType(String balanceTypeCode){
94          getJournalVoucherForValidation().refreshReferenceObject(OLEPropertyConstants.ACCOUNTING_PERIOD);
95          AccountingPeriod accountingPeriod = getJournalVoucherForValidation().getAccountingPeriod();
96          
97          //get encumbrance balance type list
98          BalanceTypeService balanceTypeService = SpringContext.getBean(BalanceTypeService.class);
99          List<String> encumbranceBalanceTypes = balanceTypeService.getEncumbranceBalanceTypes(accountingPeriod.getUniversityFiscalYear());
100         
101         return encumbranceBalanceTypes.contains(balanceTypeCode);
102     }
103     
104     /**
105      * Adds a global error for a missing required property. This is used for properties, such as reference origin code, which cannot
106      * be required by the DataDictionary validation because not all documents require them.
107      * 
108      * @param boe
109      * @param propertyName
110      */
111     protected void putRequiredPropertyError(BusinessObjectEntry boe, String propertyName) {
112         String label = boe.getAttributeDefinition(propertyName).getShortLabel();
113         GlobalVariables.getMessageMap().putError(propertyName, OLEKeyConstants.ERROR_REQUIRED, label);
114     }
115 
116     /**
117      * Gets the journalVoucherForValidation attribute. 
118      * @return Returns the journalVoucherForValidation.
119      */
120     public JournalVoucherDocument getJournalVoucherForValidation() {
121         return journalVoucherForValidation;
122     }
123 
124     /**
125      * Sets the journalVoucherForValidation attribute value.
126      * @param journalVoucherForValidation The journalVoucherForValidation to set.
127      */
128     public void setJournalVoucherForValidation(JournalVoucherDocument journalVoucherForValidation) {
129         this.journalVoucherForValidation = journalVoucherForValidation;
130     }
131     
132     /**
133      * Gets the accountingLineForValidation attribute. 
134      * @return Returns the accountingLineForValidation.
135      */
136     public AccountingLine getAccountingLineForValidation() {
137         return accountingLineForValidation;
138     }
139 
140     /**
141      * Sets the accountingLineForValidation attribute value.
142      * @param accountingLineForValidation The accountingLineForValidation to set.
143      */
144     public void setAccountingLineForValidation(AccountingLine voucherSourceAccountingLine) {
145         this.accountingLineForValidation = voucherSourceAccountingLine;
146     }
147 
148     /**
149      * Gets the dataDictionaryService attribute. 
150      * @return Returns the dataDictionaryService.
151      */
152     public DataDictionaryService getDataDictionaryService() {
153         return dataDictionaryService;
154     }
155 
156     /**
157      * Sets the dataDictionaryService attribute value.
158      * @param dataDictionaryService The dataDictionaryService to set.
159      */
160     public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
161         this.dataDictionaryService = dataDictionaryService;
162     }
163 }