Coverage Report - org.kuali.rice.krad.uif.history.History
 
Classes in this File Line Coverage Branch Coverage Complexity
History
0%
0/131
0%
0/64
2.895
 
 1  
 /*
 2  
  * Copyright 2011 The Kuali Foundation Licensed under the Educational Community
 3  
  * License, Version 1.0 (the "License"); you may not use this file except in
 4  
  * compliance with the License. You may obtain a copy of the License at
 5  
  * http://www.opensource.org/licenses/ecl1.php Unless required by applicable law
 6  
  * or agreed to in writing, software distributed under the License is
 7  
  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 8  
  * KIND, either express or implied. See the License for the specific language
 9  
  * governing permissions and limitations under the License.
 10  
  */
 11  
 package org.kuali.rice.krad.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.krad.uif.UifConstants;
 26  
 import org.kuali.rice.krad.uif.container.View;
 27  
 import org.kuali.rice.krad.uif.util.ViewModelUtils;
 28  
 import org.kuali.rice.krad.web.spring.form.UifFormBase;
 29  
 
 30  
 /**
 31  
  * History class used to keep track of views visited so they can be displayed in the ui
 32  
  * as breadcrumbs - both as homeward path and history path interpretations.
 33  
  * 
 34  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 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  
      * Gets the predetermined homeward path for this view's history.
 55  
      * This is set by the same property in the view's Breadcrumbs configuration.
 56  
      * @return the homewardPath
 57  
      */
 58  
     public List<HistoryEntry> getHomewardPath() {
 59  0
         return this.homewardPath;
 60  
     }
 61  
 
 62  
     /**
 63  
      * @param homewardPath
 64  
      *            the homewardPath to set
 65  
      */
 66  
     public void setHomewardPath(List<HistoryEntry> homewardPath) {
 67  0
         this.homewardPath = homewardPath;
 68  0
     }
 69  
 
 70  
     /**
 71  
      * Gets a list of the current HistoryEntries not including the current entry.
 72  
      * This list does not include the "&history=" query parameter on each HistoryEntry's
 73  
      * url variable.  For HistoryEntries that include history information to be passed to the
 74  
      * view they are retrieving, getGeneratedBreadcrumbs is used.
 75  
      * 
 76  
      * @return the history
 77  
      */
 78  
     public List<HistoryEntry> getHistoryEntries() {
 79  0
         return this.historyEntries;
 80  
     }
 81  
 
 82  
     /**
 83  
      * 
 84  
      * @param history
 85  
      *            the history to set
 86  
      */
 87  
     public void setHistoryEntries(List<HistoryEntry> history) {
 88  0
         this.historyEntries = history;
 89  0
     }
 90  
 
 91  
     /**
 92  
      * Gets the current view's HistoryEntry.
 93  
      * This does not include the "&history=" query parameter on its
 94  
      * url variable.  For the HistoryEntry that includes history information to be passed
 95  
      * on the url it is retrieving, getGeneratedCurrentBreadcrumb is used.
 96  
      * 
 97  
      * @return the current
 98  
      */
 99  
     public HistoryEntry getCurrent() {
 100  0
         return this.current;
 101  
     }
 102  
 
 103  
     /**
 104  
      * @param current
 105  
      *            the current to set
 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  
      * @param current
 114  
      *            the current to set
 115  
      */
 116  
     public void setCurrent(HistoryEntry current) {
 117  0
         this.current = current;
 118  0
     }
 119  
 
 120  
     /**
 121  
      * This method takes in the encoded history query parameter string passed on the url and parses it to create
 122  
      * the list of historyEntries.  It will also append any homeward path if appendHomewardPath is true.  This
 123  
      * append will happen after the passedHistory entries are appended so it will not make sense to use both settings in
 124  
      * most cases.
 125  
      * 
 126  
      * @param parameterString
 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  
      * This method gets the encoded and tokenized history parameter string that is representative of the HistoryEntries
 152  
      * currently in History and includes the current view's HistoryEntry.  This parameter should be appended on any
 153  
      * appropriate links which perform view swapping.
 154  
      * 
 155  
      * @return
 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  
         // add current
 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  
      * This method generates a list of HistoryEntries that can be used as breadcrumbs by the breadcrumb widget.  This
 182  
      * method appends the appropriate history information on the HistoryEntry url variables so when a view is requested
 183  
      * its history can be regenerated for use in its breadcrumbs.  It also sets the the passed showHome variable to false
 184  
      * to prevent showing the homeward path more than once (as it is passed through the history variable backwards).
 185  
      * This does not include the current HistoryEntry as a breadcrumb.
 186  
      * 
 187  
      * @return
 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  
      * This method gets the current HistoryEntry in the breadcrumb format described in getGeneratedBreadcrumbs.
 225  
      * 
 226  
      * @return
 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  
      * Copies a HistoryEntry, for use during breadcrumb generation.
 255  
      * 
 256  
      * @param e
 257  
      * @return
 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  
      * This method pushes the information passed in to history.
 265  
      * Note: currently only used internally in the class - be cautious about its external use.
 266  
      * 
 267  
      * @param viewId
 268  
      * @param pageId
 269  
      * @param title
 270  
      * @param url
 271  
      * @param formKey
 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  
      * When this is set to true, the homeward path will be appended.
 280  
      * Note:  For most cases this should only be on during the first view load.
 281  
      * This setting is set automatically in most cases.
 282  
      * @param appendHomewardPath
 283  
      *            the appendHomewardPath to set
 284  
      */
 285  
     public void setAppendHomewardPath(boolean appendHomewardPath) {
 286  0
         this.appendHomewardPath = appendHomewardPath;
 287  0
     }
 288  
 
 289  
     /**
 290  
      * @return the appendHomewardPath
 291  
      */
 292  
     public boolean isAppendHomewardPath() {
 293  0
         return appendHomewardPath;
 294  
     }
 295  
 
 296  
     /**
 297  
      * Appends the passed history as each different view is shown.  This setting should be used when displaying
 298  
      * passed history is relevant to the user (ie inquiry/lookup chains).  This setting is set automatically in
 299  
      * most cases.
 300  
      * @param appendPassedHistory
 301  
      *            the appendPassedHistory to set
 302  
      */
 303  
     public void setAppendPassedHistory(boolean appendPassedHistory) {
 304  0
         this.appendPassedHistory = appendPassedHistory;
 305  0
     }
 306  
 
 307  
     /**
 308  
      * @return the appendPassedHistory
 309  
      */
 310  
     public boolean isAppendPassedHistory() {
 311  0
         return appendPassedHistory;
 312  
     }
 313  
 
 314  
     /**
 315  
      * Sets the current HistoryEntry using information from the form and the request.  This history parameter is extracted
 316  
      * out of the url inorder for a "clean" url to be used in history parameter and breadcrumb generation, as passing history
 317  
      * history through the nested urls is unnecessary.
 318  
      * 
 319  
      * @param form
 320  
      * @param request
 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  
            //remove history attribute
 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  
            //add formKey and pageId to url
 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  
            //may move this into view logic instead in the future if it is required for the view's title (not just breadcrumb)
 370  
            //if so remove this and just use getTitle - this logic would be in performFinalize instead
 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  
                //else it is none or blank so no title modification will be used
 387  
            }
 388  0
            this.setCurrent(form.getViewId(), form.getPageId(), title, url, form.getFormKey());
 389  
        }
 390  0
     }
 391  
 }