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.module.external.kc.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.KFSConstants;
28  import org.kuali.kfs.sys.KFSKeyConstants;
29  import org.kuali.kfs.sys.context.SpringContext;
30  import org.kuali.rice.core.api.config.property.ConfigurationService;
31  import org.kuali.rice.krad.util.ErrorMessage;
32  import org.kuali.rice.krad.util.GlobalVariables;
33  import org.kuali.rice.krad.util.MessageMap;
34  
35  /**
36   * This class will help extract the error messages from GlobalVariables object and creates a list of string.
37   */
38  public class GlobalVariablesExtractHelper {
39      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(GlobalVariablesExtractHelper.class);
40  
41      /**
42       * Extracts errors for error report writing.
43       *
44       * @return a list of error messages
45       */
46      public static void insertError(String message, String param) {
47          MessageMap errorMap = GlobalVariables.getMessageMap();
48          errorMap.putError(KFSConstants.GLOBAL_ERRORS, KFSKeyConstants.ERROR_CUSTOM, message + param);
49      }
50  
51      public static List<String> extractGlobalVariableErrors() {
52          List<String> result = new ArrayList<String>();
53  
54          MessageMap errorMap = GlobalVariables.getMessageMap();
55  
56          // Set<String> errorKeys = errorMap.keySet(); // deprecated
57          Set<String> errorKeys = errorMap.getAllPropertiesWithErrors();
58          List<ErrorMessage> errorMessages = null;
59          Object[] messageParams;
60          String errorKeyString;
61          String errorString;
62  
63          for (String errorProperty : errorKeys) {
64              // errorMessages = (List<ErrorMessage>) errorMap.get(errorProperty); // deprecated
65              errorMessages = errorMap.getErrorMessagesForProperty(errorProperty);
66              LOG.debug("error Messages :::: " + errorMessages.toString());
67              for (ErrorMessage errorMessage : errorMessages) {
68                  errorKeyString = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(errorMessage.getErrorKey());
69                  messageParams = errorMessage.getMessageParameters();
70                  LOG.debug("message parameters:::  " + messageParams);
71                  LOG.debug("errorKeyString :::: " + errorKeyString);
72                  // MessageFormat.format only seems to replace one
73                  // per pass, so I just keep beating on it until all are gone.
74                  if (StringUtils.isBlank(errorKeyString)) {
75                      errorString = errorMessage.getErrorKey();
76                  }
77                  else {
78                      errorString = errorKeyString;
79                  }
80                  LOG.debug(errorString);
81                  if (errorString.matches("^.*\\{\\d\\}.*$")) {
82                      errorString = MessageFormat.format(errorString, messageParams);
83                  }
84                  result.add(errorString);
85              }
86          }
87  
88          // clear the stuff out of global vars, as we need to reformat it and put it back
89          GlobalVariables.clear();
90          return result;
91      }
92  
93      public static String replaceTokens(String line, String ... replacements) {
94          int i = 0;
95          for (String err : replacements) {
96              String repl = "{" + String.valueOf(i++) + "}";
97              line = StringUtils.replaceOnce(line, repl, err);
98          }
99          return line;
100     }
101 
102 }