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 UpdateAccountingLineEvent extends AttributedDocumentEventBase implements AccountingLineEvent {
35      private final AccountingLine accountingLine;
36      private final AccountingLine updatedAccountingLine;
37  
38      /**
39       * Constructs an UpdateAccountingLineEvent with the given errorPathPrefix, document, and accountingLine.
40       * 
41       * @param errorPathPrefix
42       * @param document
43       * @param accountingLine
44       * @param newAccountingLine
45       */
46      public UpdateAccountingLineEvent(String errorPathPrefix, Document document, AccountingLine originalAccountingLine, AccountingLine updatedAccountingLine) {
47          super("updating accountingLine in document " + getDocumentId(document), errorPathPrefix, document);
48          this.accountingLine = originalAccountingLine;
49          this.updatedAccountingLine = updatedAccountingLine;
50      }
51      
52      /**
53       * @see org.kuali.rice.krad.rule.event.AccountingLineEvent#getAccountingLine()
54       */
55      public AccountingLine getOriginalAccountingLine() {
56          return accountingLine;
57      }
58  
59      /**
60       * @return updated accountingLine associated with this event
61       */
62      public AccountingLine getUpdatedAccountingLine() {
63          return updatedAccountingLine;
64      }
65      
66      /**
67       * @see org.kuali.ole.sys.document.validation.event.AttributedDocumentEventBase#invokeRuleMethod(org.kuali.rice.krad.rule.BusinessRule)
68       */
69      @Override
70      public boolean invokeRuleMethod(BusinessRule rule) {
71          boolean result = super.invokeRuleMethod(rule);
72          cleanErrorMessages();
73          return result;
74      }
75  
76      /**
77       * @return the original accounting line, by a more traditional name
78       */
79      public AccountingLine getAccountingLine() {
80          return accountingLine;
81      }
82      
83      /**
84       * Logic to replace generic amount error messages, especially those where extraordinarily large amounts caused format errors
85       */
86      public void cleanErrorMessages() {
87          // create a list of accounting line attribute keys
88          ArrayList linePatterns = new ArrayList();
89          // source patterns: removing wildcards
90          linePatterns.addAll(Arrays.asList(StringUtils.replace(OLEConstants.SOURCE_ACCOUNTING_LINE_ERROR_PATTERN, "*", "").split(",")));
91          // target patterns: removing wildcards
92          linePatterns.addAll(Arrays.asList(StringUtils.replace(OLEConstants.TARGET_ACCOUNTING_LINE_ERROR_PATTERN, "*", "").split(",")));
93  
94          // see if any lines have errors
95          for (Iterator i = GlobalVariables.getMessageMap().getPropertiesWithErrors().iterator(); i.hasNext();) {
96              String property = (String) i.next();
97              // only concerned about amount field errors
98              if (property.endsWith("." + OLEConstants.AMOUNT_PROPERTY_NAME)) {
99                  // check if the amount field is associated with an accounting line
100                 boolean isLineProperty = true;
101                 for (Iterator linePatternsIterator = linePatterns.iterator(); i.hasNext() && !isLineProperty;) {
102                     isLineProperty = property.startsWith((String) linePatternsIterator.next());
103                 }
104                 if (isLineProperty) {
105                     // find the specific error messages for the property
106                     for (ListIterator errors = GlobalVariables.getMessageMap().getMessages(property).listIterator(); errors.hasNext();) {
107                         ErrorMessage error = (ErrorMessage) errors.next();
108                         String errorKey = null;
109                         String[] params = new String[2];
110                         if (StringUtils.equals(OLEKeyConstants.ERROR_INVALID_FORMAT, error.getErrorKey())) {
111                             errorKey = OLEKeyConstants.ERROR_DOCUMENT_ACCOUNTING_LINE_INVALID_FORMAT;
112                             params[1] = accountingLine.getAmount().toString();
113                         }
114                         else {
115                             if (StringUtils.equals(OLEKeyConstants.ERROR_MAX_LENGTH, error.getErrorKey())) {
116                                 errorKey = OLEKeyConstants.ERROR_DOCUMENT_ACCOUNTING_LINE_MAX_LENGTH;
117 
118                                 // String value = ObjectUtils.getPropertyValue(accountingLine,
119                                 // OLEConstants.AMOUNT_PROPERTY_NAME)
120 
121                             }
122                         }
123                         if (errorKey != null) {
124                             // now replace error message
125                             error.setErrorKey(errorKey);
126                             // replace parameters
127                             params[0] = SpringContext.getBean(DataDictionaryService.class).getAttributeLabel(accountingLine.getClass(), OLEConstants.AMOUNT_PROPERTY_NAME);
128                             error.setMessageParameters(params);
129                             // put back where it came form
130                             errors.set(error);
131                         }
132                     }
133                 }
134             }
135         }
136     }
137 }