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.sys.document.validation.impl;
17  
18  import java.util.Map;
19  
20  import org.kuali.ole.sys.OLEPropertyConstants;
21  import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
22  import org.kuali.ole.sys.context.SpringContext;
23  import org.kuali.ole.sys.document.GeneralLedgerPostingDocumentBase;
24  import org.kuali.ole.sys.document.datadictionary.FinancialSystemTransactionalDocumentEntry;
25  import org.kuali.ole.sys.document.validation.AccountingRuleEngineRule;
26  import org.kuali.ole.sys.document.validation.Validation;
27  import org.kuali.ole.sys.document.validation.event.AttributedAddAdHocRoutePersonEvent;
28  import org.kuali.ole.sys.document.validation.event.AttributedAddAdHocRouteWorkgroupEvent;
29  import org.kuali.ole.sys.document.validation.event.AttributedAddNoteEvent;
30  import org.kuali.ole.sys.document.validation.event.AttributedApproveDocumentEvent;
31  import org.kuali.ole.sys.document.validation.event.AttributedBlanketApproveDocumentEvent;
32  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
33  import org.kuali.ole.sys.document.validation.event.AttributedRouteDocumentEvent;
34  import org.kuali.ole.sys.document.validation.event.AttributedSaveDocumentEvent;
35  import org.kuali.rice.kns.service.DataDictionaryService;
36  import org.kuali.rice.krad.bo.AdHocRoutePerson;
37  import org.kuali.rice.krad.bo.AdHocRouteWorkgroup;
38  import org.kuali.rice.krad.bo.Note;
39  import org.kuali.rice.krad.document.Document;
40  import org.kuali.rice.krad.document.TransactionalDocument;
41  import org.kuali.rice.kns.rules.DocumentRuleBase;
42  import org.kuali.rice.krad.rules.rule.event.ApproveDocumentEvent;
43  import org.kuali.rice.krad.rules.rule.event.BlanketApproveDocumentEvent;
44  
45  /**
46   * A rule that uses the accounting rule engine to perform rule validations.
47   */
48  public class AccountingRuleEngineRuleBase extends DocumentRuleBase implements AccountingRuleEngineRule {
49      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AccountingRuleEngineRuleBase.class);
50      
51      /**
52       * Constructs a AccountingRuleEngineRuleBase.java.
53       */
54      public AccountingRuleEngineRuleBase() {
55          super();
56      }
57  
58      /**
59       * @see org.kuali.ole.sys.document.validation.AccountingRuleEngineRule#validateForEvent(org.kuali.rice.krad.rule.event.KualiDocumentEvent)
60       */
61      public boolean validateForEvent(AttributedDocumentEvent event) {
62          final FinancialSystemTransactionalDocumentEntry documentEntry = getDataDictionaryEntryForDocument((TransactionalDocument)event.getDocument());
63          final Map<Class, String> validationMap = documentEntry.getValidationMap();
64          
65          if (validationMap == null || !validationMap.containsKey(event.getClass())) {
66              return true; // no validation?  just return true
67          } else {
68              final String beanName = validationMap.get(event.getClass());
69              Validation validationBean = SpringContext.getBean(Validation.class, beanName);
70              
71              final boolean valid = validationBean.stageValidation(event);            
72              return valid;
73          }
74      }
75      
76      /**
77       * Returns the validation from the data dictionary for the document in the event
78       * @param document the document to look up a data dictionary entry for
79       * @return a document entry
80       */
81      protected FinancialSystemTransactionalDocumentEntry getDataDictionaryEntryForDocument(TransactionalDocument document) {
82          return (FinancialSystemTransactionalDocumentEntry)SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getDictionaryObjectEntry(document.getClass().getName());
83      }
84  
85      /**
86       * @see org.kuali.rice.krad.rules.DocumentRuleBase#processCustomAddAdHocRoutePersonBusinessRules(org.kuali.rice.krad.document.Document, org.kuali.rice.krad.bo.AdHocRoutePerson)
87       */
88      @Override
89      protected boolean processCustomAddAdHocRoutePersonBusinessRules(Document document, AdHocRoutePerson person) {
90          boolean result = super.processCustomAddAdHocRoutePersonBusinessRules(document, person);
91          
92          result &= validateForEvent(new AttributedAddAdHocRoutePersonEvent(document, person));
93          
94          return result;
95      }
96  
97      /**
98       * @see org.kuali.rice.krad.rules.DocumentRuleBase#processCustomAddAdHocRouteWorkgroupBusinessRules(org.kuali.rice.krad.document.Document, org.kuali.rice.krad.bo.AdHocRouteWorkgroup)
99       */
100     @Override
101     protected boolean processCustomAddAdHocRouteWorkgroupBusinessRules(Document document, AdHocRouteWorkgroup workgroup) {
102         boolean result = super.processCustomAddAdHocRouteWorkgroupBusinessRules(document, workgroup);
103         
104         result &= validateForEvent(new AttributedAddAdHocRouteWorkgroupEvent(document, workgroup));
105         
106         return result;
107     }
108 
109     /**
110      * @see org.kuali.rice.krad.rules.DocumentRuleBase#processCustomAddNoteBusinessRules(org.kuali.rice.krad.document.Document, org.kuali.rice.krad.bo.Note)
111      */
112     @Override
113     protected boolean processCustomAddNoteBusinessRules(Document document, Note note) {
114         boolean result = super.processCustomAddNoteBusinessRules(document, note);
115         
116         result &= validateForEvent(new AttributedAddNoteEvent(document, note));
117         
118         return result;
119     }
120 
121     /**
122      * @see org.kuali.rice.krad.rules.DocumentRuleBase#processCustomApproveDocumentBusinessRules(org.kuali.rice.krad.rule.event.ApproveDocumentEvent)
123      */
124     @Override
125     protected boolean processCustomApproveDocumentBusinessRules(ApproveDocumentEvent approveEvent) {
126         boolean result = super.processCustomApproveDocumentBusinessRules(approveEvent);
127         
128         if (approveEvent instanceof BlanketApproveDocumentEvent) {
129             result &= validateForEvent(new AttributedBlanketApproveDocumentEvent(approveEvent.getErrorPathPrefix(), approveEvent.getDocument()));
130         } else {
131             result &= validateForEvent(new AttributedApproveDocumentEvent(approveEvent.getErrorPathPrefix(), approveEvent.getDocument()));
132         }
133         
134         return result;
135     }
136 
137     /**
138      * @see org.kuali.rice.krad.rules.DocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.krad.document.Document)
139      */
140     @Override
141     protected boolean processCustomRouteDocumentBusinessRules(Document document) {
142         boolean result = super.processCustomRouteDocumentBusinessRules(document);
143 
144         AttributedRouteDocumentEvent event = new AttributedRouteDocumentEvent(document);        
145         result &= validateForEvent(event);
146         
147         return result;
148     }
149 
150     /**
151      * @see org.kuali.rice.krad.rules.DocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.krad.document.Document)
152      */
153     @Override
154     protected boolean processCustomSaveDocumentBusinessRules(Document document) {
155         boolean result = super.processCustomSaveDocumentBusinessRules(document);
156         
157         result &= validateForEvent(new AttributedSaveDocumentEvent(document));
158         
159         return result;
160     }
161     
162     /**
163      * @see org.kuali.rice.krad.rules.DocumentRuleBase#isDocumentAttributesValid(org.kuali.rice.krad.document.Document, boolean)
164      */
165     @Override
166     public boolean isDocumentAttributesValid(Document document, boolean validateRequired) {
167         FinancialSystemTransactionalDocumentEntry documentEntry = getDataDictionaryEntryForDocument((TransactionalDocument)document);
168         Integer maxDictionaryValidationDepth = documentEntry.getMaxDictionaryValidationDepth();
169         
170         if(maxDictionaryValidationDepth != null) {
171             this.setMaxDictionaryValidationDepth(maxDictionaryValidationDepth);
172         }
173         
174         //refresh the document's reference objects..
175         document.refreshNonUpdateableReferences();
176         
177         //refresh GLPE nonupdateable business object references....
178         if (document instanceof GeneralLedgerPostingDocumentBase) {
179             GeneralLedgerPostingDocumentBase glpeDocument = (GeneralLedgerPostingDocumentBase) document;
180             for (GeneralLedgerPendingEntry glpe : glpeDocument.getGeneralLedgerPendingEntries()) {
181                 glpe.refreshReferenceObject(OLEPropertyConstants.FINANCIAL_OBJECT);
182             }
183         }
184         
185         return super.isDocumentAttributesValid(document, validateRequired);
186     }
187 }