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.module.external.kc.util;
017
018 import java.text.MessageFormat;
019 import java.util.ArrayList;
020 import java.util.List;
021 import java.util.Map;
022 import java.util.Set;
023 import java.util.regex.Matcher;
024 import java.util.regex.Pattern;
025
026 import org.apache.commons.lang.StringUtils;
027 import org.kuali.kfs.sys.KFSConstants;
028 import org.kuali.kfs.sys.KFSKeyConstants;
029 import org.kuali.kfs.sys.context.SpringContext;
030 import org.kuali.rice.kns.service.KualiConfigurationService;
031 import org.kuali.rice.kns.util.ErrorMessage;
032 import org.kuali.rice.kns.util.GlobalVariables;
033 import org.kuali.rice.kns.util.MessageMap;
034
035 /**
036 * This class will help extract the error messages from GlobalVariables object and creates a list of string.
037 */
038 public class GlobalVariablesExtractHelper {
039 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(GlobalVariablesExtractHelper.class);
040
041 /**
042 * Extracts errors for error report writing.
043 *
044 * @return a list of error messages
045 */
046 public static void insertError(String message, String param) {
047 MessageMap errorMap = GlobalVariables.getMessageMap();
048 errorMap.putError(KFSConstants.GLOBAL_ERRORS, KFSKeyConstants.ERROR_CUSTOM, message + param);
049 }
050
051 public static List<String> extractGlobalVariableErrors() {
052 List<String> result = new ArrayList<String>();
053
054 MessageMap errorMap = GlobalVariables.getMessageMap();
055
056 // Set<String> errorKeys = errorMap.keySet(); // deprecated
057 Set<String> errorKeys = errorMap.getAllPropertiesWithErrors();
058 List<ErrorMessage> errorMessages = null;
059 Object[] messageParams;
060 String errorKeyString;
061 String errorString;
062
063 for (String errorProperty : errorKeys) {
064 // errorMessages = (List<ErrorMessage>) errorMap.get(errorProperty); // deprecated
065 errorMessages = (List<ErrorMessage>) errorMap.getErrorMessagesForProperty(errorProperty);
066
067 for (ErrorMessage errorMessage : errorMessages) {
068 errorKeyString = SpringContext.getBean(KualiConfigurationService.class).getPropertyString(errorMessage.getErrorKey());
069 messageParams = errorMessage.getMessageParameters();
070
071 // MessageFormat.format only seems to replace one
072 // per pass, so I just keep beating on it until all are gone.
073 if (StringUtils.isBlank(errorKeyString)) {
074 errorString = errorMessage.getErrorKey();
075 }
076 else {
077 errorString = errorKeyString;
078 }
079 LOG.debug(errorString);
080 while (errorString.matches("^.*\\{\\d\\}.*$")) {
081 errorString = MessageFormat.format(errorString, messageParams);
082 }
083 result.add(errorString);
084 }
085 }
086
087 // clear the stuff out of global vars, as we need to reformat it and put it back
088 GlobalVariables.clear();
089 return result;
090 }
091
092 public static String replaceTokens(String line, String ... replacements) {
093 int i = 0;
094 for (String err : replacements) {
095 String repl = "{" + String.valueOf(i++) + "}";
096 line = StringUtils.replaceOnce(line, repl, err);
097 }
098 return line;
099 }
100
101 }