View Javadoc
1   /**
2    * Copyright 2005-2015 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.rules.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   * @author Kuali Rice Team (rice.collab@kuali.org)
30   */
31  abstract public class DocumentEventBase extends RuleEventBase implements DocumentEvent {
32      private static final Logger LOG = Logger.getLogger(DocumentEventBase.class);
33  
34      protected Document document;
35  
36      /**
37       * As a general rule, business rule classes should not change the original object. This constructor was created so
38       * that PreRulesCheckEvent, a UI level rule checker, can make changes.
39       *
40       * @param description
41       * @param errorPathPrefix
42       */
43      protected DocumentEventBase(String description, String errorPathPrefix) {
44          super( description, errorPathPrefix );
45      }
46  
47      /**
48       * Constructs a KualiEvent with the given description and errorPathPrefix for the given document.
49       *
50       * @param errorPathPrefix
51       * @param document
52       * @param description
53       */
54      public DocumentEventBase(String description, String errorPathPrefix, Document document) {
55          super( description, errorPathPrefix );
56          this.document = document;
57  
58          LOG.debug(description);
59      }
60  
61      /**
62       * @see org.kuali.rice.krad.rules.rule.event.DocumentEvent#getDocument()
63       */
64      public final Document getDocument() {
65          return document;
66      }
67  
68      /**
69       * @see org.kuali.rice.krad.rules.rule.event.DocumentEvent#validate()
70       */
71      @Override
72      public void validate() {
73          if (getDocument() == null) {
74              throw new IllegalArgumentException("invalid (null) event document");
75          }
76      }
77  
78      /**
79       * Provides null-safe access to the documentNumber of the given document.
80       *
81       * @param document
82       * @return String containing the documentNumber of the given document, or some indication of why the documentNumber
83       * isn't
84       * accessible
85       */
86      protected static String getDocumentId(Document document) {
87          String docId = "(null document)";
88  
89          if (document != null) {
90              String documentNumber = document.getDocumentNumber();
91              if (StringUtils.isBlank(documentNumber)) {
92                  docId = "(blank " + KRADPropertyConstants.DOCUMENT_NUMBER + ")";
93              } else {
94                  docId = documentNumber;
95              }
96          }
97  
98          return docId;
99      }
100 }