001/*
002 * Copyright 2008 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 */
016package org.kuali.ole.sys.document.validation.impl;
017
018import java.util.Map;
019
020import org.kuali.ole.sys.OLEPropertyConstants;
021import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
022import org.kuali.ole.sys.context.SpringContext;
023import org.kuali.ole.sys.document.GeneralLedgerPostingDocumentBase;
024import org.kuali.ole.sys.document.datadictionary.FinancialSystemTransactionalDocumentEntry;
025import org.kuali.ole.sys.document.validation.AccountingRuleEngineRule;
026import org.kuali.ole.sys.document.validation.Validation;
027import org.kuali.ole.sys.document.validation.event.AttributedAddAdHocRoutePersonEvent;
028import org.kuali.ole.sys.document.validation.event.AttributedAddAdHocRouteWorkgroupEvent;
029import org.kuali.ole.sys.document.validation.event.AttributedAddNoteEvent;
030import org.kuali.ole.sys.document.validation.event.AttributedApproveDocumentEvent;
031import org.kuali.ole.sys.document.validation.event.AttributedBlanketApproveDocumentEvent;
032import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
033import org.kuali.ole.sys.document.validation.event.AttributedRouteDocumentEvent;
034import org.kuali.ole.sys.document.validation.event.AttributedSaveDocumentEvent;
035import org.kuali.rice.kns.service.DataDictionaryService;
036import org.kuali.rice.krad.bo.AdHocRoutePerson;
037import org.kuali.rice.krad.bo.AdHocRouteWorkgroup;
038import org.kuali.rice.krad.bo.Note;
039import org.kuali.rice.krad.document.Document;
040import org.kuali.rice.krad.document.TransactionalDocument;
041import org.kuali.rice.kns.rules.DocumentRuleBase;
042import org.kuali.rice.krad.rules.rule.event.ApproveDocumentEvent;
043import org.kuali.rice.krad.rules.rule.event.BlanketApproveDocumentEvent;
044
045/**
046 * A rule that uses the accounting rule engine to perform rule validations.
047 */
048public class AccountingRuleEngineRuleBase extends DocumentRuleBase implements AccountingRuleEngineRule {
049    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AccountingRuleEngineRuleBase.class);
050    
051    /**
052     * Constructs a AccountingRuleEngineRuleBase.java.
053     */
054    public AccountingRuleEngineRuleBase() {
055        super();
056    }
057
058    /**
059     * @see org.kuali.ole.sys.document.validation.AccountingRuleEngineRule#validateForEvent(org.kuali.rice.krad.rule.event.KualiDocumentEvent)
060     */
061    public boolean validateForEvent(AttributedDocumentEvent event) {
062        final FinancialSystemTransactionalDocumentEntry documentEntry = getDataDictionaryEntryForDocument((TransactionalDocument)event.getDocument());
063        final Map<Class, String> validationMap = documentEntry.getValidationMap();
064        
065        if (validationMap == null || !validationMap.containsKey(event.getClass())) {
066            return true; // no validation?  just return true
067        } else {
068            final String beanName = validationMap.get(event.getClass());
069            Validation validationBean = SpringContext.getBean(Validation.class, beanName);
070            
071            final boolean valid = validationBean.stageValidation(event);            
072            return valid;
073        }
074    }
075    
076    /**
077     * Returns the validation from the data dictionary for the document in the event
078     * @param document the document to look up a data dictionary entry for
079     * @return a document entry
080     */
081    protected FinancialSystemTransactionalDocumentEntry getDataDictionaryEntryForDocument(TransactionalDocument document) {
082        return (FinancialSystemTransactionalDocumentEntry)SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getDictionaryObjectEntry(document.getClass().getName());
083    }
084
085    /**
086     * @see org.kuali.rice.krad.rules.DocumentRuleBase#processCustomAddAdHocRoutePersonBusinessRules(org.kuali.rice.krad.document.Document, org.kuali.rice.krad.bo.AdHocRoutePerson)
087     */
088    @Override
089    protected boolean processCustomAddAdHocRoutePersonBusinessRules(Document document, AdHocRoutePerson person) {
090        boolean result = super.processCustomAddAdHocRoutePersonBusinessRules(document, person);
091        
092        result &= validateForEvent(new AttributedAddAdHocRoutePersonEvent(document, person));
093        
094        return result;
095    }
096
097    /**
098     * @see org.kuali.rice.krad.rules.DocumentRuleBase#processCustomAddAdHocRouteWorkgroupBusinessRules(org.kuali.rice.krad.document.Document, org.kuali.rice.krad.bo.AdHocRouteWorkgroup)
099     */
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}