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 AddAccountingLineEvent extends AttributedDocumentEventBase implements AccountingLineEvent {
035        private final AccountingLine accountingLine;
036        
037        /**
038         * Constructs an AddAccountingLineEvent with the given errorPathPrefix, document, and accountingLine.
039         * 
040         * @param errorPathPrefix
041         * @param document
042         * @param accountingLine
043         */
044        public AddAccountingLineEvent(String errorPathPrefix, Document document, AccountingLine accountingLine) {
045            super("adding accountingLine to document " + getDocumentId(document), errorPathPrefix, document);
046            this.accountingLine = accountingLine;
047        }
048    
049        /**
050         * @see org.kuali.rice.kns.rule.event.AccountingLineEvent#getAccountingLine()
051         */
052        public AccountingLine getAccountingLine() {
053            return accountingLine;
054        }
055    
056        /**
057         * Overridden to call parent and then clean up the error messages.
058         * @see org.kuali.kfs.sys.document.validation.event.AttributedDocumentEventBase#invokeRuleMethod(org.kuali.rice.kns.rule.BusinessRule)
059         */
060        @Override
061        public boolean invokeRuleMethod(BusinessRule rule) {
062            boolean result = super.invokeRuleMethod(rule);
063            cleanErrorMessages();
064            return result;
065        }
066        
067        /**
068         * Logic to replace generic amount error messages, especially those where extraordinarily large amounts caused format errors
069         */
070        public void cleanErrorMessages() {
071            // create a list of accounting line attribute keys
072            ArrayList linePatterns = new ArrayList();
073            // source patterns: removing wildcards
074            linePatterns.addAll(Arrays.asList(StringUtils.replace(KFSConstants.SOURCE_ACCOUNTING_LINE_ERROR_PATTERN, "*", "").split(",")));
075            // target patterns: removing wildcards
076            linePatterns.addAll(Arrays.asList(StringUtils.replace(KFSConstants.TARGET_ACCOUNTING_LINE_ERROR_PATTERN, "*", "").split(",")));
077    
078            // see if any lines have errors
079            for (Iterator i = GlobalVariables.getMessageMap().getPropertiesWithErrors().iterator(); i.hasNext();) {
080                String property = (String) i.next();
081                // only concerned about amount field errors
082                if (property.endsWith("." + KFSConstants.AMOUNT_PROPERTY_NAME)) {
083                    // check if the amount field is associated with an accounting line
084                    boolean isLineProperty = true;
085                    for (Iterator linePatternsIterator = linePatterns.iterator(); i.hasNext() && !isLineProperty;) {
086                        isLineProperty = property.startsWith((String) linePatternsIterator.next());
087                    }
088                    if (isLineProperty) {
089                        // find the specific error messages for the property
090                        for (ListIterator errors = GlobalVariables.getMessageMap().getMessages(property).listIterator(); errors.hasNext();) {
091                            ErrorMessage error = (ErrorMessage) errors.next();
092                            String errorKey = null;
093                            String[] params = new String[2];
094                            if (StringUtils.equals(KFSKeyConstants.ERROR_INVALID_FORMAT, error.getErrorKey())) {
095                                errorKey = KFSKeyConstants.ERROR_DOCUMENT_ACCOUNTING_LINE_INVALID_FORMAT;
096                                params[1] = accountingLine.getAmount().toString();
097                            }
098                            else {
099                                if (StringUtils.equals(KFSKeyConstants.ERROR_MAX_LENGTH, error.getErrorKey())) {
100                                    errorKey = KFSKeyConstants.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(), KFSConstants.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    }