View Javadoc
1   /*
2    * Copyright 2008 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.ole.sys.document.validation.event;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.Iterator;
21  import java.util.ListIterator;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.ole.sys.OLEConstants;
25  import org.kuali.ole.sys.OLEKeyConstants;
26  import org.kuali.ole.sys.businessobject.AccountingLine;
27  import org.kuali.ole.sys.context.SpringContext;
28  import org.kuali.rice.kns.service.DataDictionaryService;
29  import org.kuali.rice.krad.document.Document;
30  import org.kuali.rice.krad.rules.rule.BusinessRule;
31  import org.kuali.rice.krad.util.ErrorMessage;
32  import org.kuali.rice.krad.util.GlobalVariables;
33  
34  public class AddAccountingLineEvent extends AttributedDocumentEventBase implements AccountingLineEvent {
35      private final AccountingLine accountingLine;
36      
37      /**
38       * Constructs an AddAccountingLineEvent with the given errorPathPrefix, document, and accountingLine.
39       * 
40       * @param errorPathPrefix
41       * @param document
42       * @param accountingLine
43       */
44      public AddAccountingLineEvent(String errorPathPrefix, Document document, AccountingLine accountingLine) {
45          super("adding accountingLine to document " + getDocumentId(document), errorPathPrefix, document);
46          this.accountingLine = accountingLine;
47      }
48  
49      /**
50       * @see org.kuali.rice.krad.rule.event.AccountingLineEvent#getAccountingLine()
51       */
52      public AccountingLine getAccountingLine() {
53          return accountingLine;
54      }
55  
56      /**
57       * Overridden to call parent and then clean up the error messages.
58       * @see org.kuali.ole.sys.document.validation.event.AttributedDocumentEventBase#invokeRuleMethod(org.kuali.rice.krad.rule.BusinessRule)
59       */
60      @Override
61      public boolean invokeRuleMethod(BusinessRule rule) {
62          boolean result = super.invokeRuleMethod(rule);
63          cleanErrorMessages();
64          return result;
65      }
66      
67      /**
68       * Logic to replace generic amount error messages, especially those where extraordinarily large amounts caused format errors
69       */
70      public void cleanErrorMessages() {
71          // create a list of accounting line attribute keys
72          ArrayList linePatterns = new ArrayList();
73          // source patterns: removing wildcards
74          linePatterns.addAll(Arrays.asList(StringUtils.replace(OLEConstants.SOURCE_ACCOUNTING_LINE_ERROR_PATTERN, "*", "").split(",")));
75          // target patterns: removing wildcards
76          linePatterns.addAll(Arrays.asList(StringUtils.replace(OLEConstants.TARGET_ACCOUNTING_LINE_ERROR_PATTERN, "*", "").split(",")));
77  
78          // see if any lines have errors
79          for (Iterator i = GlobalVariables.getMessageMap().getPropertiesWithErrors().iterator(); i.hasNext();) {
80              String property = (String) i.next();
81              // only concerned about amount field errors
82              if (property.endsWith("." + OLEConstants.AMOUNT_PROPERTY_NAME)) {
83                  // check if the amount field is associated with an accounting line
84                  boolean isLineProperty = true;
85                  for (Iterator linePatternsIterator = linePatterns.iterator(); i.hasNext() && !isLineProperty;) {
86                      isLineProperty = property.startsWith((String) linePatternsIterator.next());
87                  }
88                  if (isLineProperty) {
89                      // find the specific error messages for the property
90                      for (ListIterator errors = GlobalVariables.getMessageMap().getMessages(property).listIterator(); errors.hasNext();) {
91                          ErrorMessage error = (ErrorMessage) errors.next();
92                          String errorKey = null;
93                          String[] params = new String[2];
94                          if (StringUtils.equals(OLEKeyConstants.ERROR_INVALID_FORMAT, error.getErrorKey())) {
95                              errorKey = OLEKeyConstants.ERROR_DOCUMENT_ACCOUNTING_LINE_INVALID_FORMAT;
96                              params[1] = accountingLine.getAmount().toString();
97                          }
98                          else {
99                              if (StringUtils.equals(OLEKeyConstants.ERROR_MAX_LENGTH, error.getErrorKey())) {
100                                 errorKey = OLEKeyConstants.ERROR_DOCUMENT_ACCOUNTING_LINE_MAX_LENGTH;
101                             }
102                         }
103                         if (errorKey != null) {
104                             // now replace error message
105                             error.setErrorKey(errorKey);
106                             // replace parameters
107                             params[0] = SpringContext.getBean(DataDictionaryService.class).getAttributeLabel(accountingLine.getClass(), OLEConstants.AMOUNT_PROPERTY_NAME);
108                             error.setMessageParameters(params);
109                             // put back where it came form
110                             errors.set(error);
111                         }
112                     }
113                 }
114             }
115         }
116     }
117 }