001/**
002 * Copyright 2005-2014 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.rice.krad.rules.rule.event;
017
018import org.kuali.rice.krad.document.Document;
019import org.kuali.rice.krad.rules.rule.BusinessRule;
020import org.kuali.rice.krad.rules.rule.SaveDocumentRule;
021import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
022import org.kuali.rice.krad.service.KualiRuleService;
023
024import java.util.ArrayList;
025import java.util.List;
026
027/**
028 * Rule event generated for a save of a document instance.
029 *
030 * <p>This could be triggered when a user presses the save button for a given document or it could
031 * happen when another piece of code calls the save method in the document service.</p>
032 *
033 * @author Kuali Rice Team (rice.collab@kuali.org)
034 */
035public class SaveDocumentEvent extends DocumentEventBase implements SaveEvent {
036
037    /**
038     * Constructs a SaveDocumentEvent with the specified errorPathPrefix and document
039     *
040     * @param document
041     * @param errorPathPrefix
042     */
043    public SaveDocumentEvent(String errorPathPrefix, Document document) {
044        this("creating save event for document " + getDocumentId(document), errorPathPrefix, document);
045    }
046
047    /**
048     * Constructs a SaveDocumentEvent with the given document
049     *
050     * @param document
051     */
052    public SaveDocumentEvent(Document document) {
053        this("", document);
054    }
055
056    /**
057     * @see org.kuali.rice.krad.rules.rule.event.DocumentEventBase#DocumentEventBase(java.lang.String,
058     * java.lang.String, org.kuali.rice.krad.document.Document)
059     */
060    public SaveDocumentEvent(String description, String errorPathPrefix, Document document) {
061        super(description, errorPathPrefix, document);
062    }
063
064    /**
065     * @see org.kuali.rice.krad.rules.rule.event.RuleEvent#getRuleInterfaceClass()
066     */
067    public Class<? extends BusinessRule> getRuleInterfaceClass() {
068        return SaveDocumentRule.class;
069    }
070
071    /**
072     * @see org.kuali.rice.krad.rules.rule.event.RuleEvent#invokeRuleMethod(org.kuali.rice.krad.rules.rule.BusinessRule)
073     */
074    public boolean invokeRuleMethod(BusinessRule rule) {
075        return ((SaveDocumentRule) rule).processSaveDocument(document);
076    }
077
078    /**
079     * @see org.kuali.rice.krad.rules.rule.event.RuleEvent#generateEvents()
080     */
081    @Override
082    public List<RuleEvent> generateEvents() {
083        KualiRuleService ruleService = KRADServiceLocatorWeb.getKualiRuleService();
084
085        List<RuleEvent> events = new ArrayList<RuleEvent>();
086        events.addAll(ruleService.generateAdHocRoutePersonEvents(getDocument()));
087        events.addAll(ruleService.generateAdHocRouteWorkgroupEvents(getDocument()));
088
089        events.addAll(getDocument().generateSaveEvents());
090
091        return events;
092    }
093}