001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.core.rule.event;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import org.apache.commons.lang.StringUtils;
022: import org.apache.log4j.Logger;
023: import org.kuali.RicePropertyConstants;
024: import org.kuali.core.document.Document;
025: import org.kuali.core.util.ObjectUtils;
026:
027: /**
028: * Abstract superclass for document-related events.
029: */
030: abstract public class KualiDocumentEventBase implements
031: KualiDocumentEvent {
032: private static final Logger LOG = Logger
033: .getLogger(KualiDocumentEventBase.class);
034:
035: private final String description;
036: private final String errorPathPrefix;
037: protected Document document;
038:
039: /**
040: *
041: * As a general rule, business rule classes should not change the original object. This constructor was created so that
042: * PreRulesCheckEvent, a UI level rule checker, can make changes.
043: *
044: * @param description
045: * @param errorPathPrefix
046: */
047: protected KualiDocumentEventBase(String description,
048: String errorPathPrefix) {
049:
050: if (!(this instanceof PreRulesCheckEvent)) {
051: throw new Error(
052: "THIS CONSTRUCTOR SHOULD ONLY BE USED AT THE UI LAYER");
053: }
054:
055: this .description = description;
056: this .errorPathPrefix = errorPathPrefix;
057: }
058:
059: /**
060: * Constructs a KualiEvent with the given description and errorPathPrefix for the given document.
061: *
062: * @param errorPathPrefix
063: * @param document
064: * @param description
065: */
066: public KualiDocumentEventBase(String description,
067: String errorPathPrefix, Document document) {
068: this .description = description;
069: this .errorPathPrefix = errorPathPrefix;
070:
071: setDocument(document);
072:
073: LOG.debug(description);
074: }
075:
076: private final void setDocument(Document document) {
077: try {
078: // by doing a deep copy, we are ensuring that the business rule class can't update
079: // the original object by reference
080: this .document = (Document) ObjectUtils.deepCopy(document);
081: // have to manually set the FlexDoc b/c it is transient and the deepCopy won't actually copy that object over
082: // for a serialization based copy
083: this .document.getDocumentHeader().setWorkflowDocument(
084: document.getDocumentHeader().getWorkflowDocument());
085: } catch (Exception e) {
086: LOG.warn("Unable to perform deep copy on document", e);
087: // just set to the passed in document
088: this .document = document;
089: // throw new RuntimeException("Failed to invoke deep copy of document.", e);
090: }
091: }
092:
093: /**
094: * @see org.kuali.core.rule.event.KualiDocumentEvent#getDocument()
095: */
096: public final Document getDocument() {
097: return document;
098: }
099:
100: /**
101: * @see org.kuali.core.rule.event.KualiDocumentEvent#getName()
102: */
103: public final String getName() {
104: return this .getClass().getName();
105: }
106:
107: /**
108: * @return a description of this event
109: */
110: public final String getDescription() {
111: return description;
112: }
113:
114: /**
115: * @see org.kuali.core.rule.event.KualiDocumentEvent#getErrorPathPrefix()
116: */
117: public String getErrorPathPrefix() {
118: return errorPathPrefix;
119: }
120:
121: /**
122: * @see java.lang.Object#toString()
123: */
124: public String toString() {
125: return getName();
126: }
127:
128: /**
129: * @see org.kuali.core.rule.event.KualiDocumentEvent#validate()
130: */
131: public void validate() {
132: if (getDocument() == null) {
133: throw new IllegalArgumentException(
134: "invalid (null) event document");
135: }
136: }
137:
138: /**
139: * @see org.kuali.core.rule.event.KualiDocumentEvent#generateEvents()
140: */
141: public List generateEvents() {
142: return new ArrayList();
143: }
144:
145: /**
146: * Provides null-safe access to the documentNumber of the given document.
147: *
148: * @param document
149: * @return String containing the documentNumber of the given document, or some indication of why the documentNumber isn't
150: * accessible
151: */
152: protected static String getDocumentId(Document document) {
153: String docId = "(null document)";
154:
155: if (document != null) {
156: String documentNumber = document.getDocumentNumber();
157: if (StringUtils.isBlank(documentNumber)) {
158: docId = "(blank "
159: + RicePropertyConstants.DOCUMENT_NUMBER + ")";
160: } else {
161: docId = documentNumber;
162: }
163: }
164:
165: return docId;
166: }
167: }
|