1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
39
40
41
42
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
51
52 public AccountingLine getAccountingLine() {
53 return accountingLine;
54 }
55
56
57
58
59
60 @Override
61 public boolean invokeRuleMethod(BusinessRule rule) {
62 boolean result = super.invokeRuleMethod(rule);
63 cleanErrorMessages();
64 return result;
65 }
66
67
68
69
70 public void cleanErrorMessages() {
71
72 ArrayList linePatterns = new ArrayList();
73
74 linePatterns.addAll(Arrays.asList(StringUtils.replace(OLEConstants.SOURCE_ACCOUNTING_LINE_ERROR_PATTERN, "*", "").split(",")));
75
76 linePatterns.addAll(Arrays.asList(StringUtils.replace(OLEConstants.TARGET_ACCOUNTING_LINE_ERROR_PATTERN, "*", "").split(",")));
77
78
79 for (Iterator i = GlobalVariables.getMessageMap().getPropertiesWithErrors().iterator(); i.hasNext();) {
80 String property = (String) i.next();
81
82 if (property.endsWith("." + OLEConstants.AMOUNT_PROPERTY_NAME)) {
83
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
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
105 error.setErrorKey(errorKey);
106
107 params[0] = SpringContext.getBean(DataDictionaryService.class).getAttributeLabel(accountingLine.getClass(), OLEConstants.AMOUNT_PROPERTY_NAME);
108 error.setMessageParameters(params);
109
110 errors.set(error);
111 }
112 }
113 }
114 }
115 }
116 }
117 }