|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
   package org.kuali.rice.kns.uif.history;  | 
  |  12 |     | 
     | 
  |  13 |     | 
   import java.io.Serializable;  | 
  |  14 |     | 
   import java.io.UnsupportedEncodingException;  | 
  |  15 |     | 
   import java.net.URLDecoder;  | 
  |  16 |     | 
   import java.net.URLEncoder;  | 
  |  17 |     | 
   import java.util.ArrayList;  | 
  |  18 |     | 
   import java.util.Enumeration;  | 
  |  19 |     | 
   import java.util.List;  | 
  |  20 |     | 
     | 
  |  21 |     | 
   import javax.servlet.http.HttpServletRequest;  | 
  |  22 |     | 
     | 
  |  23 |     | 
   import org.apache.commons.lang.StringUtils;  | 
  |  24 |     | 
   import org.apache.log4j.Logger;  | 
  |  25 |     | 
   import org.kuali.rice.kns.uif.UifConstants;  | 
  |  26 |     | 
   import org.kuali.rice.kns.uif.container.View;  | 
  |  27 |     | 
   import org.kuali.rice.kns.uif.util.ViewModelUtils;  | 
  |  28 |     | 
   import org.kuali.rice.kns.web.spring.form.UifFormBase;  | 
  |  29 |     | 
     | 
  |  30 |     | 
     | 
  |  31 |     | 
     | 
  |  32 |     | 
     | 
  |  33 |     | 
     | 
  |  34 |     | 
     | 
  |  35 |     | 
     | 
  |  36 |     | 
   public class History implements Serializable{     | 
  |  37 |     | 
             | 
  |  38 |     | 
           private static final long serialVersionUID = -8279297694371557335L;  | 
  |  39 |     | 
     | 
  |  40 |    0 |            public History() { | 
  |  41 |    0 |            historyEntries = new ArrayList<HistoryEntry>();  | 
  |  42 |    0 |        }  | 
  |  43 |     | 
     | 
  |  44 |    0 |        private static final Logger LOG = Logger.getLogger(History.class);  | 
  |  45 |     | 
       public static final String ENTRY_TOKEN = "$";  | 
  |  46 |     | 
       public static final String VAR_TOKEN = ",";  | 
  |  47 |     | 
       private List<HistoryEntry> homewardPath;  | 
  |  48 |     | 
       private List<HistoryEntry> historyEntries;  | 
  |  49 |     | 
       private boolean appendHomewardPath;  | 
  |  50 |     | 
       private boolean appendPassedHistory;  | 
  |  51 |     | 
       private HistoryEntry current;  | 
  |  52 |     | 
     | 
  |  53 |     | 
         | 
  |  54 |     | 
     | 
  |  55 |     | 
     | 
  |  56 |     | 
     | 
  |  57 |     | 
     | 
  |  58 |     | 
       public List<HistoryEntry> getHomewardPath() { | 
  |  59 |    0 |            return this.homewardPath;  | 
  |  60 |     | 
       }  | 
  |  61 |     | 
     | 
  |  62 |     | 
         | 
  |  63 |     | 
     | 
  |  64 |     | 
     | 
  |  65 |     | 
     | 
  |  66 |     | 
       public void setHomewardPath(List<HistoryEntry> homewardPath) { | 
  |  67 |    0 |            this.homewardPath = homewardPath;  | 
  |  68 |    0 |        }  | 
  |  69 |     | 
     | 
  |  70 |     | 
         | 
  |  71 |     | 
     | 
  |  72 |     | 
     | 
  |  73 |     | 
     | 
  |  74 |     | 
     | 
  |  75 |     | 
     | 
  |  76 |     | 
     | 
  |  77 |     | 
     | 
  |  78 |     | 
       public List<HistoryEntry> getHistoryEntries() { | 
  |  79 |    0 |            return this.historyEntries;  | 
  |  80 |     | 
       }  | 
  |  81 |     | 
     | 
  |  82 |     | 
         | 
  |  83 |     | 
     | 
  |  84 |     | 
     | 
  |  85 |     | 
     | 
  |  86 |     | 
     | 
  |  87 |     | 
       public void setHistoryEntries(List<HistoryEntry> history) { | 
  |  88 |    0 |            this.historyEntries = history;  | 
  |  89 |    0 |        }  | 
  |  90 |     | 
     | 
  |  91 |     | 
         | 
  |  92 |     | 
     | 
  |  93 |     | 
     | 
  |  94 |     | 
     | 
  |  95 |     | 
     | 
  |  96 |     | 
     | 
  |  97 |     | 
     | 
  |  98 |     | 
     | 
  |  99 |     | 
       public HistoryEntry getCurrent() { | 
  |  100 |    0 |            return this.current;  | 
  |  101 |     | 
       }  | 
  |  102 |     | 
     | 
  |  103 |     | 
         | 
  |  104 |     | 
     | 
  |  105 |     | 
     | 
  |  106 |     | 
     | 
  |  107 |     | 
       private void setCurrent(String viewId, String pageId, String title, String url, String formKey) { | 
  |  108 |    0 |            HistoryEntry entry = new HistoryEntry(viewId, pageId, title, url, formKey);  | 
  |  109 |    0 |            current = entry;  | 
  |  110 |    0 |        }  | 
  |  111 |     | 
     | 
  |  112 |     | 
         | 
  |  113 |     | 
     | 
  |  114 |     | 
     | 
  |  115 |     | 
     | 
  |  116 |     | 
       public void setCurrent(HistoryEntry current) { | 
  |  117 |    0 |            this.current = current;  | 
  |  118 |    0 |        }  | 
  |  119 |     | 
     | 
  |  120 |     | 
         | 
  |  121 |     | 
     | 
  |  122 |     | 
     | 
  |  123 |     | 
     | 
  |  124 |     | 
     | 
  |  125 |     | 
     | 
  |  126 |     | 
     | 
  |  127 |     | 
     | 
  |  128 |     | 
       public void buildHistoryFromParameterString(String parameterString) { | 
  |  129 |    0 |            if (StringUtils.isNotEmpty(parameterString)) { | 
  |  130 |     | 
               try { | 
  |  131 |    0 |                    parameterString = URLDecoder.decode(parameterString, "UTF-8");  | 
  |  132 |    0 |                } catch (UnsupportedEncodingException e) { | 
  |  133 |    0 |                    LOG.error("Error decoding history param", e); | 
  |  134 |    0 |                }  | 
  |  135 |    0 |                historyEntries = new ArrayList<HistoryEntry>();  | 
  |  136 |    0 |                if (appendPassedHistory) { | 
  |  137 |    0 |                    String[] historyTokens = parameterString.split("\\" + ENTRY_TOKEN); | 
  |  138 |    0 |                    for (String token : historyTokens) { | 
  |  139 |    0 |                        String[] params = token.split(VAR_TOKEN);  | 
  |  140 |    0 |                        pushToHistory(params[0], params[1], params[2], params[3], params[4]);  | 
  |  141 |     | 
                   }  | 
  |  142 |     | 
               }  | 
  |  143 |     | 
           }  | 
  |  144 |     | 
     | 
  |  145 |    0 |            if (appendHomewardPath) { | 
  |  146 |    0 |                historyEntries.addAll(homewardPath);  | 
  |  147 |     | 
           }  | 
  |  148 |    0 |        }  | 
  |  149 |     | 
     | 
  |  150 |     | 
         | 
  |  151 |     | 
     | 
  |  152 |     | 
     | 
  |  153 |     | 
     | 
  |  154 |     | 
     | 
  |  155 |     | 
     | 
  |  156 |     | 
     | 
  |  157 |     | 
       public String getHistoryParameterString() { | 
  |  158 |    0 |            String historyString = "";  | 
  |  159 |    0 |                for (HistoryEntry e : historyEntries) { | 
  |  160 |    0 |                    if (historyEntries.indexOf(e) == 0) { | 
  |  161 |    0 |                        historyString = historyString + e.toParam();  | 
  |  162 |     | 
                   } else { | 
  |  163 |    0 |                        historyString = historyString + ENTRY_TOKEN + e.toParam();  | 
  |  164 |     | 
                   }  | 
  |  165 |     | 
               }  | 
  |  166 |     | 
             | 
  |  167 |    0 |            if (historyString.equals("")) { | 
  |  168 |    0 |                historyString = historyString + current.toParam();  | 
  |  169 |     | 
           } else { | 
  |  170 |    0 |                historyString = historyString + ENTRY_TOKEN + current.toParam();  | 
  |  171 |     | 
           }  | 
  |  172 |     | 
           try { | 
  |  173 |    0 |                historyString = URLEncoder.encode(historyString, "UTF-8");  | 
  |  174 |    0 |            } catch (Exception e) { | 
  |  175 |    0 |                LOG.error("Error encoding history param", e); | 
  |  176 |    0 |            }  | 
  |  177 |    0 |            return historyString;  | 
  |  178 |     | 
       }  | 
  |  179 |     | 
         | 
  |  180 |     | 
         | 
  |  181 |     | 
     | 
  |  182 |     | 
     | 
  |  183 |     | 
     | 
  |  184 |     | 
     | 
  |  185 |     | 
     | 
  |  186 |     | 
     | 
  |  187 |     | 
     | 
  |  188 |     | 
     | 
  |  189 |     | 
       public List<HistoryEntry> getGeneratedBreadcrumbs(){ | 
  |  190 |    0 |            List<HistoryEntry> breadcrumbs = new ArrayList<HistoryEntry>();  | 
  |  191 |    0 |            for(int i=0; i < historyEntries.size(); i++){ | 
  |  192 |    0 |                if(i == 0){ | 
  |  193 |    0 |                    breadcrumbs.add(copyEntry(historyEntries.get(i)));  | 
  |  194 |     | 
               }  | 
  |  195 |     | 
               else{ | 
  |  196 |    0 |                    HistoryEntry breadcrumb = copyEntry(historyEntries.get(i));  | 
  |  197 |    0 |                    String historyParam = "";  | 
  |  198 |    0 |                    for(int j=0; j < i; j++){ | 
  |  199 |    0 |                       historyParam = historyParam + ENTRY_TOKEN + historyEntries.get(j).toParam();  | 
  |  200 |     | 
                   }  | 
  |  201 |    0 |                    historyParam = historyParam.replaceFirst("\\" + ENTRY_TOKEN, ""); | 
  |  202 |     | 
                   try { | 
  |  203 |    0 |                        historyParam = URLEncoder.encode(historyParam, "UTF-8");  | 
  |  204 |    0 |                    } catch (Exception e) { | 
  |  205 |    0 |                        LOG.error("Error encoding history param", e); | 
  |  206 |    0 |                    }  | 
  |  207 |     | 
                     | 
  |  208 |    0 |                    String url = "";  | 
  |  209 |    0 |                    if(breadcrumb.getUrl().contains("?")){ | 
  |  210 |    0 |                        url = breadcrumb.getUrl() + "&" + UifConstants.UrlParams.HISTORY + "=" + historyParam;  | 
  |  211 |     | 
                   }  | 
  |  212 |     | 
                   else{ | 
  |  213 |    0 |                        url = breadcrumb.getUrl() + "?" + UifConstants.UrlParams.HISTORY + "=" + historyParam;  | 
  |  214 |     | 
                   }  | 
  |  215 |    0 |                    breadcrumb.setUrl(url);  | 
  |  216 |    0 |                    breadcrumbs.add(breadcrumb);  | 
  |  217 |     | 
               }  | 
  |  218 |     | 
           }  | 
  |  219 |     | 
             | 
  |  220 |    0 |            return breadcrumbs;  | 
  |  221 |     | 
       }  | 
  |  222 |     | 
         | 
  |  223 |     | 
         | 
  |  224 |     | 
     | 
  |  225 |     | 
     | 
  |  226 |     | 
     | 
  |  227 |     | 
     | 
  |  228 |     | 
       public HistoryEntry getGeneratedCurrentBreadcrumb(){ | 
  |  229 |    0 |            HistoryEntry breadcrumb = copyEntry(current);  | 
  |  230 |    0 |            String historyParam = "";  | 
  |  231 |    0 |            for(int j=0; j < historyEntries.size(); j++){ | 
  |  232 |    0 |               historyParam = historyParam + ENTRY_TOKEN + historyEntries.get(j).toParam();  | 
  |  233 |     | 
           }  | 
  |  234 |    0 |            historyParam = historyParam.replaceFirst("\\" + ENTRY_TOKEN, ""); | 
  |  235 |     | 
             | 
  |  236 |     | 
           try { | 
  |  237 |    0 |                historyParam = URLEncoder.encode(historyParam, "UTF-8");  | 
  |  238 |    0 |            } catch (Exception e) { | 
  |  239 |    0 |                LOG.error("Error encoding history param", e); | 
  |  240 |    0 |            }  | 
  |  241 |     | 
             | 
  |  242 |    0 |            String url = "";  | 
  |  243 |    0 |            if(breadcrumb.getUrl().contains("?")){ | 
  |  244 |    0 |                url = breadcrumb.getUrl() + "&" + UifConstants.UrlParams.HISTORY + "=" + historyParam;  | 
  |  245 |     | 
           }  | 
  |  246 |     | 
           else{ | 
  |  247 |    0 |                url = breadcrumb.getUrl() + "?" + UifConstants.UrlParams.HISTORY + "=" + historyParam;  | 
  |  248 |     | 
           }  | 
  |  249 |    0 |            breadcrumb.setUrl(url);  | 
  |  250 |    0 |            return breadcrumb;  | 
  |  251 |     | 
       }  | 
  |  252 |     | 
         | 
  |  253 |     | 
         | 
  |  254 |     | 
     | 
  |  255 |     | 
     | 
  |  256 |     | 
     | 
  |  257 |     | 
     | 
  |  258 |     | 
     | 
  |  259 |     | 
       private HistoryEntry copyEntry(HistoryEntry e){ | 
  |  260 |    0 |            return new HistoryEntry(e.getViewId(), e.getPageId(), e.getTitle(), e.getUrl(), e.getFormKey());  | 
  |  261 |     | 
       }  | 
  |  262 |     | 
     | 
  |  263 |     | 
         | 
  |  264 |     | 
     | 
  |  265 |     | 
     | 
  |  266 |     | 
     | 
  |  267 |     | 
     | 
  |  268 |     | 
     | 
  |  269 |     | 
     | 
  |  270 |     | 
     | 
  |  271 |     | 
     | 
  |  272 |     | 
     | 
  |  273 |     | 
       public void pushToHistory(String viewId, String pageId, String title, String url, String formKey) { | 
  |  274 |    0 |            HistoryEntry entry = new HistoryEntry(viewId, pageId, title, url, formKey);  | 
  |  275 |    0 |            historyEntries.add(entry);  | 
  |  276 |    0 |        }  | 
  |  277 |     | 
     | 
  |  278 |     | 
         | 
  |  279 |     | 
     | 
  |  280 |     | 
     | 
  |  281 |     | 
     | 
  |  282 |     | 
     | 
  |  283 |     | 
     | 
  |  284 |     | 
     | 
  |  285 |     | 
       public void setAppendHomewardPath(boolean appendHomewardPath) { | 
  |  286 |    0 |            this.appendHomewardPath = appendHomewardPath;  | 
  |  287 |    0 |        }  | 
  |  288 |     | 
     | 
  |  289 |     | 
         | 
  |  290 |     | 
     | 
  |  291 |     | 
     | 
  |  292 |     | 
       public boolean isAppendHomewardPath() { | 
  |  293 |    0 |            return appendHomewardPath;  | 
  |  294 |     | 
       }  | 
  |  295 |     | 
     | 
  |  296 |     | 
         | 
  |  297 |     | 
     | 
  |  298 |     | 
     | 
  |  299 |     | 
     | 
  |  300 |     | 
     | 
  |  301 |     | 
     | 
  |  302 |     | 
     | 
  |  303 |     | 
       public void setAppendPassedHistory(boolean appendPassedHistory) { | 
  |  304 |    0 |            this.appendPassedHistory = appendPassedHistory;  | 
  |  305 |    0 |        }  | 
  |  306 |     | 
     | 
  |  307 |     | 
         | 
  |  308 |     | 
     | 
  |  309 |     | 
     | 
  |  310 |     | 
       public boolean isAppendPassedHistory() { | 
  |  311 |    0 |            return appendPassedHistory;  | 
  |  312 |     | 
       }  | 
  |  313 |     | 
     | 
  |  314 |     | 
         | 
  |  315 |     | 
     | 
  |  316 |     | 
     | 
  |  317 |     | 
     | 
  |  318 |     | 
     | 
  |  319 |     | 
     | 
  |  320 |     | 
     | 
  |  321 |     | 
     | 
  |  322 |     | 
       @SuppressWarnings("unchecked") | 
  |  323 |     | 
       public void setCurrent(UifFormBase form, HttpServletRequest request) { | 
  |  324 |    0 |           if(!request.getMethod().equals("POST")){ | 
  |  325 |    0 |               boolean showHomeValue = false;  | 
  |  326 |    0 |               boolean pageIdValue = false;  | 
  |  327 |    0 |               boolean formKeyValue = false;  | 
  |  328 |    0 |               String queryString = "";  | 
  |  329 |    0 |               String url = request.getRequestURL().toString();  | 
  |  330 |     | 
                | 
  |  331 |    0 |               Enumeration<String> params = request.getParameterNames();  | 
  |  332 |    0 |               while(params.hasMoreElements()){ | 
  |  333 |    0 |                   String key = params.nextElement();  | 
  |  334 |    0 |                   if(!key.equals(UifConstants.UrlParams.HISTORY)){ | 
  |  335 |    0 |                       for(String value: request.getParameterValues(key)){ | 
  |  336 |    0 |                           queryString = queryString + "&" + key + "=" + value;    | 
  |  337 |     | 
                      }  | 
  |  338 |     | 
                  }  | 
  |  339 |    0 |                   else if(key.equals(UifConstants.UrlParams.PAGE_ID)){ | 
  |  340 |    0 |                       pageIdValue = true;  | 
  |  341 |     | 
                  }  | 
  |  342 |    0 |                   else if(key.equals(UifConstants.UrlParams.SHOW_HOME)){ | 
  |  343 |    0 |                       showHomeValue = true;  | 
  |  344 |     | 
                  }  | 
  |  345 |    0 |                   else if(key.equals(UifConstants.UrlParams.FORM_KEY)){ | 
  |  346 |    0 |                       formKeyValue = true;  | 
  |  347 |     | 
                  }  | 
  |  348 |    0 |               }  | 
  |  349 |     | 
                | 
  |  350 |     | 
                | 
  |  351 |    0 |               if(StringUtils.isNotBlank(form.getFormKey()) && !formKeyValue){ | 
  |  352 |    0 |                   queryString = queryString + "&" + UifConstants.UrlParams.FORM_KEY + "=" + form.getFormKey();  | 
  |  353 |     | 
              }  | 
  |  354 |    0 |               if(StringUtils.isNotBlank(form.getPageId()) && !pageIdValue){ | 
  |  355 |    0 |                   queryString = queryString + "&" + UifConstants.UrlParams.PAGE_ID + "=" + form.getPageId();  | 
  |  356 |     | 
              }  | 
  |  357 |    0 |               if(!showHomeValue){ | 
  |  358 |    0 |                   queryString = queryString + "&" + UifConstants.UrlParams.SHOW_HOME + "=false";  | 
  |  359 |     | 
              }  | 
  |  360 |     | 
                | 
  |  361 |    0 |               queryString = queryString.replaceFirst("&", ""); | 
  |  362 |     | 
                | 
  |  363 |    0 |               if(StringUtils.isNotEmpty(queryString)){ | 
  |  364 |    0 |                   url = url + "?" + queryString;  | 
  |  365 |     | 
              }  | 
  |  366 |     | 
                | 
  |  367 |    0 |               View view = form.getView();  | 
  |  368 |    0 |               String title = view.getTitle();  | 
  |  369 |     | 
                | 
  |  370 |     | 
                | 
  |  371 |    0 |               Object titleValue = ViewModelUtils.getValue(view, form, view.getViewLabelFieldPropertyName(), view.getViewLabelFieldBindingInfo());  | 
  |  372 |    0 |               String titleAppend = "";  | 
  |  373 |    0 |               if(titleValue != null){ | 
  |  374 |    0 |                   titleAppend = titleValue.toString();  | 
  |  375 |     | 
              }  | 
  |  376 |    0 |               if(StringUtils.isNotBlank(titleAppend) && view.getAppendOption() != null){ | 
  |  377 |    0 |                   if(view.getAppendOption().equalsIgnoreCase(UifConstants.TitleAppendTypes.DASH)){ | 
  |  378 |    0 |                       title = title + " - " + titleAppend;  | 
  |  379 |     | 
                  }  | 
  |  380 |    0 |                   else if(view.getAppendOption().equalsIgnoreCase(UifConstants.TitleAppendTypes.PARENTHESIS)){ | 
  |  381 |    0 |                       title = title + "(" + titleAppend +")"; | 
  |  382 |     | 
                  }  | 
  |  383 |    0 |                   else if(view.getAppendOption().equalsIgnoreCase(UifConstants.TitleAppendTypes.REPLACE)){ | 
  |  384 |    0 |                       title = titleAppend;  | 
  |  385 |     | 
                  }  | 
  |  386 |     | 
                    | 
  |  387 |     | 
              }  | 
  |  388 |    0 |               this.setCurrent(form.getViewId(), form.getPageId(), title, url, form.getFormKey());  | 
  |  389 |     | 
          }  | 
  |  390 |    0 |        }  | 
  |  391 |     | 
   }  |