View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.sys.util;
20  
21  import java.text.MessageFormat;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Set;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.kuali.kfs.sys.context.SpringContext;
28  import org.kuali.rice.core.api.config.property.ConfigurationService;
29  import org.kuali.rice.krad.util.ErrorMessage;
30  import org.kuali.rice.krad.util.GlobalVariables;
31  import org.kuali.rice.krad.util.MessageMap;
32  
33  public class GlobalVariablesUtils {
34     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(GlobalVariablesUtils.class);
35  
36     public static List<String> extractGlobalVariableErrors() {
37         List<String> result = new ArrayList<String>();
38  
39         MessageMap errorMap = GlobalVariables.getMessageMap();
40  
41         // Set<String> errorKeys = errorMap.keySet(); // deprecated
42         Set<String> errorKeys = errorMap.getAllPropertiesWithErrors();
43         List<ErrorMessage> errorMessages = null;
44         Object[] messageParams;
45         String errorKeyString;
46         String errorString;
47  
48         for (String errorProperty : errorKeys) {
49             // errorMessages = (List<ErrorMessage>) errorMap.get(errorProperty); // deprecated
50             errorMessages = errorMap.getErrorMessagesForProperty(errorProperty);
51             LOG.debug("error Messages :::: " + errorMessages.toString());
52             for (ErrorMessage errorMessage : errorMessages) {
53                 errorKeyString = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(errorMessage.getErrorKey());
54                 messageParams = errorMessage.getMessageParameters();
55                 LOG.debug("message parameters:::  " + messageParams);
56                 LOG.debug("errorKeyString :::: " + errorKeyString);
57                 // MessageFormat.format only seems to replace one
58                 // per pass, so I just keep beating on it until all are gone.
59                 if (StringUtils.isBlank(errorKeyString)) {
60                     errorString = errorMessage.getErrorKey();
61                 }
62                 else {
63                     errorString = errorKeyString;
64                 }
65                 LOG.debug(errorString);
66                 if (errorString.matches("^.*\\{\\d\\}.*$")) {
67                     errorString = MessageFormat.format(errorString, messageParams);
68                 }
69                 result.add(errorString);
70             }
71         }
72  
73         // clear the stuff out of global vars, as we need to reformat it and put it back
74         GlobalVariables.clear();
75         return result;
76     }
77  }