001/**
002 * Copyright 2005-2016 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 */
029abstract public class KualiDocumentEventBase implements KualiDocumentEvent {
030    private static final Logger LOG = Logger.getLogger(KualiDocumentEventBase.class);
031
032    private final String description;
033    private final String errorPathPrefix;
034    protected Document document;
035
036    /**
037     * 
038     * As a general rule, business rule classes should not change the original object. This constructor was created so that
039     * PreRulesCheckEvent, a UI level rule checker, can make changes.
040     * 
041     * @param description
042     * @param errorPathPrefix
043     */
044    protected KualiDocumentEventBase(String description, String errorPathPrefix) {
045        this.description = description;
046        this.errorPathPrefix = errorPathPrefix;
047    }
048
049    /**
050     * Constructs a KualiEvent with the given description and errorPathPrefix for the given document.
051     * 
052     * @param errorPathPrefix
053     * @param document
054     * @param description
055     */
056    public KualiDocumentEventBase(String description, String errorPathPrefix, Document document) {
057        this.description = description;
058        this.errorPathPrefix = errorPathPrefix;
059        this.document = document;
060
061        LOG.debug(description);
062    }
063
064
065    /**
066     * @see org.kuali.rice.krad.rules.rule.event.KualiDocumentEvent#getDocument()
067     */
068    public final Document getDocument() {
069        return document;
070    }
071
072    /**
073     * @see org.kuali.rice.krad.rules.rule.event.KualiDocumentEvent#getName()
074     */
075    public final String getName() {
076        return this.getClass().getName();
077    }
078
079    /**
080     * @return a description of this event
081     */
082    public final String getDescription() {
083        return description;
084    }
085
086    /**
087     * @see org.kuali.rice.krad.rules.rule.event.KualiDocumentEvent#getErrorPathPrefix()
088     */
089    public String getErrorPathPrefix() {
090        return errorPathPrefix;
091    }
092
093
094    /**
095     * @see java.lang.Object#toString()
096     */
097    @Override
098    public String toString() {
099        return getName();
100    }
101
102    /**
103     * @see org.kuali.rice.krad.rules.rule.event.KualiDocumentEvent#validate()
104     */
105    public void validate() {
106        if (getDocument() == null) {
107            throw new IllegalArgumentException("invalid (null) event document");
108        }
109    }
110
111    /**
112     * @see org.kuali.rice.krad.rules.rule.event.KualiDocumentEvent#generateEvents()
113     */
114    public List<KualiDocumentEvent> generateEvents() {
115        return new ArrayList<KualiDocumentEvent>();
116    }
117
118    /**
119     * Provides null-safe access to the documentNumber of the given document.
120     * 
121     * @param document
122     * @return String containing the documentNumber of the given document, or some indication of why the documentNumber isn't
123     *         accessible
124     */
125    protected static String getDocumentId(Document document) {
126        String docId = "(null document)";
127
128        if (document != null) {
129            String documentNumber = document.getDocumentNumber();
130            if (StringUtils.isBlank(documentNumber)) {
131                docId = "(blank " + KRADPropertyConstants.DOCUMENT_NUMBER + ")";
132            }
133            else {
134                docId = documentNumber;
135            }
136        }
137
138        return docId;
139    }
140}