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 UpdateAccountingLineEvent extends AttributedDocumentEventBase implements AccountingLineEvent {
35 private final AccountingLine accountingLine;
36 private final AccountingLine updatedAccountingLine;
37
38
39
40
41
42
43
44
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
54
55 public AccountingLine getOriginalAccountingLine() {
56 return accountingLine;
57 }
58
59
60
61
62 public AccountingLine getUpdatedAccountingLine() {
63 return updatedAccountingLine;
64 }
65
66
67
68
69 @Override
70 public boolean invokeRuleMethod(BusinessRule rule) {
71 boolean result = super.invokeRuleMethod(rule);
72 cleanErrorMessages();
73 return result;
74 }
75
76
77
78
79 public AccountingLine getAccountingLine() {
80 return accountingLine;
81 }
82
83
84
85
86 public void cleanErrorMessages() {
87
88 ArrayList linePatterns = new ArrayList();
89
90 linePatterns.addAll(Arrays.asList(StringUtils.replace(OLEConstants.SOURCE_ACCOUNTING_LINE_ERROR_PATTERN, "*", "").split(",")));
91
92 linePatterns.addAll(Arrays.asList(StringUtils.replace(OLEConstants.TARGET_ACCOUNTING_LINE_ERROR_PATTERN, "*", "").split(",")));
93
94
95 for (Iterator i = GlobalVariables.getMessageMap().getPropertiesWithErrors().iterator(); i.hasNext();) {
96 String property = (String) i.next();
97
98 if (property.endsWith("." + OLEConstants.AMOUNT_PROPERTY_NAME)) {
99
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
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
119
120
121 }
122 }
123 if (errorKey != null) {
124
125 error.setErrorKey(errorKey);
126
127 params[0] = SpringContext.getBean(DataDictionaryService.class).getAttributeLabel(accountingLine.getClass(), OLEConstants.AMOUNT_PROPERTY_NAME);
128 error.setMessageParameters(params);
129
130 errors.set(error);
131 }
132 }
133 }
134 }
135 }
136 }
137 }