View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.rule.event;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.rice.krad.document.Document;
21  import org.kuali.rice.krad.util.KRADPropertyConstants;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  /**
27   * Abstract superclass for document-related events.
28   */
29  abstract public class KualiDocumentEventBase implements KualiDocumentEvent {
30      private static final Logger LOG = Logger.getLogger(KualiDocumentEventBase.class);
31  
32      private final String description;
33      private final String errorPathPrefix;
34      protected Document document;
35  
36      /**
37       * 
38       * As a general rule, business rule classes should not change the original object. This constructor was created so that
39       * PreRulesCheckEvent, a UI level rule checker, can make changes.
40       * 
41       * @param description
42       * @param errorPathPrefix
43       */
44      protected KualiDocumentEventBase(String description, String errorPathPrefix) {
45          this.description = description;
46          this.errorPathPrefix = errorPathPrefix;
47      }
48  
49      /**
50       * Constructs a KualiEvent with the given description and errorPathPrefix for the given document.
51       * 
52       * @param errorPathPrefix
53       * @param document
54       * @param description
55       */
56      public KualiDocumentEventBase(String description, String errorPathPrefix, Document document) {
57          this.description = description;
58          this.errorPathPrefix = errorPathPrefix;
59          this.document = document;
60  
61          LOG.debug(description);
62      }
63  
64  
65      /**
66       * @see org.kuali.rice.krad.rule.event.KualiDocumentEvent#getDocument()
67       */
68      public final Document getDocument() {
69          return document;
70      }
71  
72      /**
73       * @see org.kuali.rice.krad.rule.event.KualiDocumentEvent#getName()
74       */
75      public final String getName() {
76          return this.getClass().getName();
77      }
78  
79      /**
80       * @return a description of this event
81       */
82      public final String getDescription() {
83          return description;
84      }
85  
86      /**
87       * @see org.kuali.rice.krad.rule.event.KualiDocumentEvent#getErrorPathPrefix()
88       */
89      public String getErrorPathPrefix() {
90          return errorPathPrefix;
91      }
92  
93  
94      /**
95       * @see java.lang.Object#toString()
96       */
97      @Override
98      public String toString() {
99          return getName();
100     }
101 
102     /**
103      * @see org.kuali.rice.krad.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.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 }