001 /*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.kfs.sys.document.validation.event;
017
018 import java.util.ArrayList;
019 import java.util.Arrays;
020 import java.util.Iterator;
021 import java.util.ListIterator;
022
023 import org.apache.commons.lang.StringUtils;
024 import org.kuali.kfs.sys.KFSConstants;
025 import org.kuali.kfs.sys.KFSKeyConstants;
026 import org.kuali.kfs.sys.businessobject.AccountingLine;
027 import org.kuali.kfs.sys.context.SpringContext;
028 import org.kuali.rice.kns.document.Document;
029 import org.kuali.rice.kns.rule.BusinessRule;
030 import org.kuali.rice.kns.service.DataDictionaryService;
031 import org.kuali.rice.kns.util.ErrorMessage;
032 import org.kuali.rice.kns.util.GlobalVariables;
033
034 public class UpdateAccountingLineEvent extends AttributedDocumentEventBase implements AccountingLineEvent {
035 private final AccountingLine accountingLine;
036 private final AccountingLine updatedAccountingLine;
037
038 /**
039 * Constructs an UpdateAccountingLineEvent with the given errorPathPrefix, document, and accountingLine.
040 *
041 * @param errorPathPrefix
042 * @param document
043 * @param accountingLine
044 * @param newAccountingLine
045 */
046 public UpdateAccountingLineEvent(String errorPathPrefix, Document document, AccountingLine originalAccountingLine, AccountingLine updatedAccountingLine) {
047 super("updating accountingLine in document " + getDocumentId(document), errorPathPrefix, document);
048 this.accountingLine = originalAccountingLine;
049 this.updatedAccountingLine = updatedAccountingLine;
050 }
051
052 /**
053 * @see org.kuali.rice.kns.rule.event.AccountingLineEvent#getAccountingLine()
054 */
055 public AccountingLine getOriginalAccountingLine() {
056 return accountingLine;
057 }
058
059 /**
060 * @return updated accountingLine associated with this event
061 */
062 public AccountingLine getUpdatedAccountingLine() {
063 return updatedAccountingLine;
064 }
065
066 /**
067 * @see org.kuali.kfs.sys.document.validation.event.AttributedDocumentEventBase#invokeRuleMethod(org.kuali.rice.kns.rule.BusinessRule)
068 */
069 @Override
070 public boolean invokeRuleMethod(BusinessRule rule) {
071 boolean result = super.invokeRuleMethod(rule);
072 cleanErrorMessages();
073 return result;
074 }
075
076 /**
077 * @return the original accounting line, by a more traditional name
078 */
079 public AccountingLine getAccountingLine() {
080 return accountingLine;
081 }
082
083 /**
084 * Logic to replace generic amount error messages, especially those where extraordinarily large amounts caused format errors
085 */
086 public void cleanErrorMessages() {
087 // create a list of accounting line attribute keys
088 ArrayList linePatterns = new ArrayList();
089 // source patterns: removing wildcards
090 linePatterns.addAll(Arrays.asList(StringUtils.replace(KFSConstants.SOURCE_ACCOUNTING_LINE_ERROR_PATTERN, "*", "").split(",")));
091 // target patterns: removing wildcards
092 linePatterns.addAll(Arrays.asList(StringUtils.replace(KFSConstants.TARGET_ACCOUNTING_LINE_ERROR_PATTERN, "*", "").split(",")));
093
094 // see if any lines have errors
095 for (Iterator i = GlobalVariables.getMessageMap().getPropertiesWithErrors().iterator(); i.hasNext();) {
096 String property = (String) i.next();
097 // only concerned about amount field errors
098 if (property.endsWith("." + KFSConstants.AMOUNT_PROPERTY_NAME)) {
099 // 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(KFSKeyConstants.ERROR_INVALID_FORMAT, error.getErrorKey())) {
111 errorKey = KFSKeyConstants.ERROR_DOCUMENT_ACCOUNTING_LINE_INVALID_FORMAT;
112 params[1] = accountingLine.getAmount().toString();
113 }
114 else {
115 if (StringUtils.equals(KFSKeyConstants.ERROR_MAX_LENGTH, error.getErrorKey())) {
116 errorKey = KFSKeyConstants.ERROR_DOCUMENT_ACCOUNTING_LINE_MAX_LENGTH;
117
118 // String value = ObjectUtils.getPropertyValue(accountingLine,
119 // KFSConstants.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(), KFSConstants.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 }