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.sys.document.validation.impl;
017    
018    import java.util.Map;
019    
020    import org.kuali.kfs.fp.businessobject.Check;
021    import org.kuali.kfs.fp.document.validation.event.AddCheckEvent;
022    import org.kuali.kfs.fp.document.validation.event.DeleteCheckEvent;
023    import org.kuali.kfs.fp.document.validation.event.UpdateCheckEvent;
024    import org.kuali.kfs.sys.context.SpringContext;
025    import org.kuali.kfs.sys.document.AccountingDocument;
026    import org.kuali.kfs.sys.document.datadictionary.FinancialSystemTransactionalDocumentEntry;
027    import org.kuali.kfs.sys.document.validation.AccountingRuleEngineRule;
028    import org.kuali.kfs.sys.document.validation.Validation;
029    import org.kuali.kfs.sys.document.validation.event.AttributedAddAdHocRoutePersonEvent;
030    import org.kuali.kfs.sys.document.validation.event.AttributedAddAdHocRouteWorkgroupEvent;
031    import org.kuali.kfs.sys.document.validation.event.AttributedAddNoteEvent;
032    import org.kuali.kfs.sys.document.validation.event.AttributedApproveDocumentEvent;
033    import org.kuali.kfs.sys.document.validation.event.AttributedBlanketApproveDocumentEvent;
034    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
035    import org.kuali.kfs.sys.document.validation.event.AttributedRouteDocumentEvent;
036    import org.kuali.kfs.sys.document.validation.event.AttributedSaveDocumentEvent;
037    import org.kuali.rice.kns.bo.AdHocRoutePerson;
038    import org.kuali.rice.kns.bo.AdHocRouteWorkgroup;
039    import org.kuali.rice.kns.bo.Note;
040    import org.kuali.rice.kns.document.Document;
041    import org.kuali.rice.kns.document.TransactionalDocument;
042    import org.kuali.rice.kns.rule.event.ApproveDocumentEvent;
043    import org.kuali.rice.kns.rule.event.BlanketApproveDocumentEvent;
044    import org.kuali.rice.kns.rules.DocumentRuleBase;
045    import org.kuali.rice.kns.service.DataDictionaryService;
046    
047    /**
048     * A rule that uses the accounting rule engine to perform rule validations.
049     */
050    public class AccountingRuleEngineRuleBase extends DocumentRuleBase implements AccountingRuleEngineRule {
051        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AccountingRuleEngineRuleBase.class);
052        
053        /**
054         * Constructs a AccountingRuleEngineRuleBase.java.
055         */
056        public AccountingRuleEngineRuleBase() {
057            super();
058        }
059    
060        /**
061         * @see org.kuali.kfs.sys.document.validation.AccountingRuleEngineRule#validateForEvent(org.kuali.rice.kns.rule.event.KualiDocumentEvent)
062         */
063        public boolean validateForEvent(AttributedDocumentEvent event) {
064            final FinancialSystemTransactionalDocumentEntry documentEntry = getDataDictionaryEntryForDocument((TransactionalDocument)event.getDocument());
065            final Map<Class, String> validationMap = documentEntry.getValidationMap();
066            
067            if (validationMap == null || !validationMap.containsKey(event.getClass())) {
068                return true; // no validation?  just return true
069            } else {
070                final String beanName = validationMap.get(event.getClass());
071                Validation validationBean = SpringContext.getBean(Validation.class, beanName);
072                
073                final boolean valid = validationBean.stageValidation(event);            
074                return valid;
075            }
076        }
077        
078        /**
079         * Returns the validation from the data dictionary for the document in the event
080         * @param document the document to look up a data dictionary entry for
081         * @return a document entry
082         */
083        protected FinancialSystemTransactionalDocumentEntry getDataDictionaryEntryForDocument(TransactionalDocument document) {
084            return (FinancialSystemTransactionalDocumentEntry)SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getDictionaryObjectEntry(document.getClass().getName());
085        }
086    
087        /**
088         * @see org.kuali.rice.kns.rules.DocumentRuleBase#processCustomAddAdHocRoutePersonBusinessRules(org.kuali.rice.kns.document.Document, org.kuali.rice.kns.bo.AdHocRoutePerson)
089         */
090        @Override
091        protected boolean processCustomAddAdHocRoutePersonBusinessRules(Document document, AdHocRoutePerson person) {
092            boolean result = super.processCustomAddAdHocRoutePersonBusinessRules(document, person);
093            
094            result &= validateForEvent(new AttributedAddAdHocRoutePersonEvent(document, person));
095            
096            return result;
097        }
098    
099        /**
100         * @see org.kuali.rice.kns.rules.DocumentRuleBase#processCustomAddAdHocRouteWorkgroupBusinessRules(org.kuali.rice.kns.document.Document, org.kuali.rice.kns.bo.AdHocRouteWorkgroup)
101         */
102        @Override
103        protected boolean processCustomAddAdHocRouteWorkgroupBusinessRules(Document document, AdHocRouteWorkgroup workgroup) {
104            boolean result = super.processCustomAddAdHocRouteWorkgroupBusinessRules(document, workgroup);
105            
106            result &= validateForEvent(new AttributedAddAdHocRouteWorkgroupEvent(document, workgroup));
107            
108            return result;
109        }
110    
111        /**
112         * @see org.kuali.rice.kns.rules.DocumentRuleBase#processCustomAddNoteBusinessRules(org.kuali.rice.kns.document.Document, org.kuali.rice.kns.bo.Note)
113         */
114        @Override
115        protected boolean processCustomAddNoteBusinessRules(Document document, Note note) {
116            boolean result = super.processCustomAddNoteBusinessRules(document, note);
117            
118            result &= validateForEvent(new AttributedAddNoteEvent(document, note));
119            
120            return result;
121        }
122    
123        /**
124         * @see org.kuali.rice.kns.rules.DocumentRuleBase#processCustomApproveDocumentBusinessRules(org.kuali.rice.kns.rule.event.ApproveDocumentEvent)
125         */
126        @Override
127        protected boolean processCustomApproveDocumentBusinessRules(ApproveDocumentEvent approveEvent) {
128            boolean result = super.processCustomApproveDocumentBusinessRules(approveEvent);
129            
130            if (approveEvent instanceof BlanketApproveDocumentEvent) {
131                result &= validateForEvent(new AttributedBlanketApproveDocumentEvent(approveEvent.getErrorPathPrefix(), approveEvent.getDocument()));
132            } else {
133                result &= validateForEvent(new AttributedApproveDocumentEvent(approveEvent.getErrorPathPrefix(), approveEvent.getDocument()));
134            }
135            
136            return result;
137        }
138    
139        /**
140         * @see org.kuali.rice.kns.rules.DocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.Document)
141         */
142        @Override
143        protected boolean processCustomRouteDocumentBusinessRules(Document document) {
144            boolean result = super.processCustomRouteDocumentBusinessRules(document);
145    
146            AttributedRouteDocumentEvent event = new AttributedRouteDocumentEvent(document);        
147            result &= validateForEvent(event);
148            
149            return result;
150        }
151    
152        /**
153         * @see org.kuali.rice.kns.rules.DocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.kns.document.Document)
154         */
155        @Override
156        protected boolean processCustomSaveDocumentBusinessRules(Document document) {
157            boolean result = super.processCustomSaveDocumentBusinessRules(document);
158            
159            result &= validateForEvent(new AttributedSaveDocumentEvent(document));
160            
161            return result;
162        }
163        
164        /**
165         * @see org.kuali.rice.kns.rules.DocumentRuleBase#isDocumentAttributesValid(org.kuali.rice.kns.document.Document, boolean)
166         */
167        @Override
168        public boolean isDocumentAttributesValid(Document document, boolean validateRequired) {
169            FinancialSystemTransactionalDocumentEntry documentEntry = getDataDictionaryEntryForDocument((TransactionalDocument)document);
170            Integer maxDictionaryValidationDepth = documentEntry.getMaxDictionaryValidationDepth();
171            
172            if(maxDictionaryValidationDepth != null) {
173                this.setMaxDictionaryValidationDepth(maxDictionaryValidationDepth);
174            }
175            
176            return super.isDocumentAttributesValid(document, validateRequired);
177        }
178    }