001/**
002 * Copyright 2005-2018 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.apache.commons.lang.StringUtils;
019import org.apache.log4j.Logger;
020import org.kuali.rice.krad.document.Document;
021import org.kuali.rice.krad.util.KRADPropertyConstants;
022
023import java.util.ArrayList;
024import java.util.List;
025
026/**
027 * Abstract superclass for document-related events.
028 *
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031abstract public class DocumentEventBase extends RuleEventBase implements DocumentEvent {
032    private static final Logger LOG = Logger.getLogger(DocumentEventBase.class);
033
034    protected Document document;
035
036    /**
037     * As a general rule, business rule classes should not change the original object. This constructor was created so
038     * that PreRulesCheckEvent, a UI level rule checker, can make changes.
039     *
040     * @param description
041     * @param errorPathPrefix
042     */
043    protected DocumentEventBase(String description, String errorPathPrefix) {
044        super( description, errorPathPrefix );
045    }
046
047    /**
048     * Constructs a KualiEvent with the given description and errorPathPrefix for the given document.
049     *
050     * @param errorPathPrefix
051     * @param document
052     * @param description
053     */
054    public DocumentEventBase(String description, String errorPathPrefix, Document document) {
055        super( description, errorPathPrefix );
056        this.document = document;
057
058        LOG.debug(description);
059    }
060
061    /**
062     * @see org.kuali.rice.krad.rules.rule.event.DocumentEvent#getDocument()
063     */
064    public final Document getDocument() {
065        return document;
066    }
067
068    /**
069     * @see org.kuali.rice.krad.rules.rule.event.DocumentEvent#validate()
070     */
071    @Override
072    public void validate() {
073        if (getDocument() == null) {
074            throw new IllegalArgumentException("invalid (null) event document");
075        }
076    }
077
078    /**
079     * Provides null-safe access to the documentNumber of the given document.
080     *
081     * @param document
082     * @return String containing the documentNumber of the given document, or some indication of why the documentNumber
083     * isn't
084     * accessible
085     */
086    protected static String getDocumentId(Document document) {
087        String docId = "(null document)";
088
089        if (document != null) {
090            String documentNumber = document.getDocumentNumber();
091            if (StringUtils.isBlank(documentNumber)) {
092                docId = "(blank " + KRADPropertyConstants.DOCUMENT_NUMBER + ")";
093            } else {
094                docId = documentNumber;
095            }
096        }
097
098        return docId;
099    }
100}