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.endow.util;
017
018 import java.text.MessageFormat;
019 import java.util.ArrayList;
020 import java.util.List;
021 import java.util.Set;
022
023 import org.apache.commons.lang.StringUtils;
024 import org.kuali.kfs.sys.context.SpringContext;
025 import org.kuali.rice.kns.service.KualiConfigurationService;
026 import org.kuali.rice.kns.util.ErrorMessage;
027 import org.kuali.rice.kns.util.GlobalVariables;
028 import org.kuali.rice.kns.util.MessageMap;
029
030 /**
031 * This class will help extract the error messages from GlobalVariables object
032 * and creates a list of string.
033 */
034 public class GloabalVariablesExtractHelper {
035
036 /**
037 * Extracts errors for error report writing.
038 *
039 * @return a list of error messages
040 */
041 public static List<String> extractGlobalVariableErrors() {
042 List<String> result = new ArrayList<String>();
043
044 MessageMap errorMap = GlobalVariables.getMessageMap();
045
046 //Set<String> errorKeys = errorMap.keySet(); // deprecated
047 Set<String> errorKeys = errorMap.getAllPropertiesWithErrors();
048 List<ErrorMessage> errorMessages = null;
049 Object[] messageParams;
050 String errorKeyString;
051 String errorString;
052
053 for (String errorProperty : errorKeys) {
054 //errorMessages = (List<ErrorMessage>) errorMap.get(errorProperty); // deprecated
055 errorMessages = (List<ErrorMessage>) errorMap.getErrorMessagesForProperty(errorProperty);
056
057 for (ErrorMessage errorMessage : errorMessages) {
058 errorKeyString = SpringContext.getBean(KualiConfigurationService.class).getPropertyString(errorMessage.getErrorKey());
059 messageParams = errorMessage.getMessageParameters();
060
061 // MessageFormat.format only seems to replace one
062 // per pass, so I just keep beating on it until all are gone.
063 if (StringUtils.isBlank(errorKeyString)) {
064 errorString = errorMessage.getErrorKey();
065 }
066 else {
067 errorString = errorKeyString;
068 }
069 System.out.println(errorString);
070 while (errorString.matches("^.*\\{\\d\\}.*$")) {
071 errorString = MessageFormat.format(errorString, messageParams);
072 }
073 result.add(errorString);
074 }
075 }
076
077 // clear the stuff out of global vars, as we need to reformat it and put it back
078 GlobalVariables.clear();
079 return result;
080 }
081 }