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 org.apache.commons.lang.StringUtils;
 14  
 import org.apache.log4j.Logger;
 15  
 import org.kuali.rice.krad.uif.UifConstants;
 16  
 import org.kuali.rice.krad.uif.container.View;
 17  
 import org.kuali.rice.krad.uif.util.ViewModelUtils;
 18  
 import org.kuali.rice.krad.web.form.UifFormBase;
 19  
 
 20  
 import javax.servlet.http.HttpServletRequest;
 21  
 import java.io.Serializable;
 22  
 import java.io.UnsupportedEncodingException;
 23  
 import java.net.URLDecoder;
 24  
 import java.net.URLEncoder;
 25  
 import java.util.ArrayList;
 26  
 import java.util.Enumeration;
 27  
 import java.util.List;
 28  
 
 29  
 /**
 30  
  * History class used to keep track of views visited so they can be displayed in the ui
 31  
  * as breadcrumbs - both as homeward path and history path interpretations.
 32  
  * 
 33  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 34  
  */
 35  
 public class History implements Serializable{    
 36  
         
 37  
         private static final long serialVersionUID = -8279297694371557335L;
 38  
 
 39  0
         public History() {
 40  0
         historyEntries = new ArrayList<HistoryEntry>();
 41  0
     }
 42  
 
 43  0
     private static final Logger LOG = Logger.getLogger(History.class);
 44  
     public static final String ENTRY_TOKEN = "$";
 45  
     public static final String VAR_TOKEN = ",";
 46  
     private List<HistoryEntry> homewardPath;
 47  
     private List<HistoryEntry> historyEntries;
 48  
     private boolean appendHomewardPath;
 49  
     private boolean appendPassedHistory;
 50  
     private HistoryEntry current;
 51  
 
 52  
     /**
 53  
      * Gets the predetermined homeward path for this view's history.
 54  
      * This is set by the same property in the view's Breadcrumbs configuration.
 55  
      * @return the homewardPath
 56  
      */
 57  
     public List<HistoryEntry> getHomewardPath() {
 58  0
         return this.homewardPath;
 59  
     }
 60  
 
 61  
     /**
 62  
      * @param homewardPath
 63  
      *            the homewardPath to set
 64  
      */
 65  
     public void setHomewardPath(List<HistoryEntry> homewardPath) {
 66  0
         this.homewardPath = homewardPath;
 67  0
     }
 68  
 
 69  
     /**
 70  
      * Gets a list of the current HistoryEntries not including the current entry.
 71  
      * This list does not include the "&history=" query parameter on each HistoryEntry's
 72  
      * url variable.  For HistoryEntries that include history information to be passed to the
 73  
      * view they are retrieving, getGeneratedBreadcrumbs is used.
 74  
      * 
 75  
      * @return the history
 76  
      */
 77  
     public List<HistoryEntry> getHistoryEntries() {
 78  0
         return this.historyEntries;
 79  
     }
 80  
 
 81  
     /**
 82  
      * 
 83  
      * @param history
 84  
      *            the history to set
 85  
      */
 86  
     public void setHistoryEntries(List<HistoryEntry> history) {
 87  0
         this.historyEntries = history;
 88  0
     }
 89  
 
 90  
     /**
 91  
      * Gets the current view's HistoryEntry.
 92  
      * This does not include the "&history=" query parameter on its
 93  
      * url variable.  For the HistoryEntry that includes history information to be passed
 94  
      * on the url it is retrieving, getGeneratedCurrentBreadcrumb is used.
 95  
      * 
 96  
      * @return the current
 97  
      */
 98  
     public HistoryEntry getCurrent() {
 99  0
         return this.current;
 100  
     }
 101  
 
 102  
     /**
 103  
      * @param current
 104  
      *            the current to set
 105  
      */
 106  
     private void setCurrent(String viewId, String pageId, String title, String url, String formKey) {
 107  0
         HistoryEntry entry = new HistoryEntry(viewId, pageId, title, url, formKey);
 108  0
         current = entry;
 109  0
     }
 110  
 
 111  
     /**
 112  
      * @param current
 113  
      *            the current to set
 114  
      */
 115  
     public void setCurrent(HistoryEntry current) {
 116  0
         this.current = current;
 117  0
     }
 118  
 
 119  
     /**
 120  
      * This method takes in the encoded history query parameter string passed on the url and parses it to create
 121  
      * the list of historyEntries.  It will also append any homeward path if appendHomewardPath is true.  This
 122  
      * append will happen after the passedHistory entries are appended so it will not make sense to use both settings in
 123  
      * most cases.
 124  
      * 
 125  
      * @param parameterString
 126  
      */
 127  
     public void buildHistoryFromParameterString(String parameterString) {
 128  0
         if (StringUtils.isNotEmpty(parameterString)) {
 129  
             try {
 130  0
                 parameterString = URLDecoder.decode(parameterString, "UTF-8");
 131  0
             } catch (UnsupportedEncodingException e) {
 132  0
                 LOG.error("Error decoding history param", e);
 133  0
             }
 134  0
             historyEntries = new ArrayList<HistoryEntry>();
 135  0
             if (appendPassedHistory) {
 136  0
                 String[] historyTokens = parameterString.split("\\" + ENTRY_TOKEN);
 137  0
                 for (String token : historyTokens) {
 138  0
                     String[] params = token.split(VAR_TOKEN);
 139  0
                     pushToHistory(params[0], params[1], params[2], params[3], params[4]);
 140  
                 }
 141  
             }
 142  
         }
 143  
 
 144  0
         if (appendHomewardPath) {
 145  0
             historyEntries.addAll(homewardPath);
 146  
         }
 147  0
     }
 148  
 
 149  
     /**
 150  
      * This method gets the encoded and tokenized history parameter string that is representative of the HistoryEntries
 151  
      * currently in History and includes the current view's HistoryEntry.  This parameter should be appended on any
 152  
      * appropriate links which perform view swapping.
 153  
      * 
 154  
      * @return
 155  
      */
 156  
     public String getHistoryParameterString() {
 157  0
         String historyString = "";
 158  0
             for (HistoryEntry e : historyEntries) {
 159  0
                 if (historyEntries.indexOf(e) == 0) {
 160  0
                     historyString = historyString + e.toParam();
 161  
                 } else {
 162  0
                     historyString = historyString + ENTRY_TOKEN + e.toParam();
 163  
                 }
 164  
             }
 165  
         // add current
 166  0
         if (historyString.equals("")) {
 167  0
             historyString = historyString + current.toParam();
 168  
         } else {
 169  0
             historyString = historyString + ENTRY_TOKEN + current.toParam();
 170  
         }
 171  
         try {
 172  0
             historyString = URLEncoder.encode(historyString, "UTF-8");
 173  0
         } catch (Exception e) {
 174  0
             LOG.error("Error encoding history param", e);
 175  0
         }
 176  0
         return historyString;
 177  
     }
 178  
     
 179  
     /**
 180  
      * This method generates a list of HistoryEntries that can be used as breadcrumbs by the breadcrumb widget.  This
 181  
      * method appends the appropriate history information on the HistoryEntry url variables so when a view is requested
 182  
      * its history can be regenerated for use in its breadcrumbs.  It also sets the the passed showHome variable to false
 183  
      * to prevent showing the homeward path more than once (as it is passed through the history variable backwards).
 184  
      * This does not include the current HistoryEntry as a breadcrumb.
 185  
      * 
 186  
      * @return
 187  
      */
 188  
     public List<HistoryEntry> getGeneratedBreadcrumbs(){
 189  0
         List<HistoryEntry> breadcrumbs = new ArrayList<HistoryEntry>();
 190  0
         for(int i=0; i < historyEntries.size(); i++){
 191  0
             if(i == 0){
 192  0
                 breadcrumbs.add(copyEntry(historyEntries.get(i)));
 193  
             }
 194  
             else{
 195  0
                 HistoryEntry breadcrumb = copyEntry(historyEntries.get(i));
 196  0
                 String historyParam = "";
 197  0
                 for(int j=0; j < i; j++){
 198  0
                    historyParam = historyParam + ENTRY_TOKEN + historyEntries.get(j).toParam();
 199  
                 }
 200  0
                 historyParam = historyParam.replaceFirst("\\" + ENTRY_TOKEN, "");
 201  
                 try {
 202  0
                     historyParam = URLEncoder.encode(historyParam, "UTF-8");
 203  0
                 } catch (Exception e) {
 204  0
                     LOG.error("Error encoding history param", e);
 205  0
                 }
 206  
                 
 207  0
                 String url = "";
 208  0
                 if(breadcrumb.getUrl().contains("?")){
 209  0
                     url = breadcrumb.getUrl() + "&" + UifConstants.UrlParams.HISTORY + "=" + historyParam;
 210  
                 }
 211  
                 else{
 212  0
                     url = breadcrumb.getUrl() + "?" + UifConstants.UrlParams.HISTORY + "=" + historyParam;
 213  
                 }
 214  0
                 breadcrumb.setUrl(url);
 215  0
                 breadcrumbs.add(breadcrumb);
 216  
             }
 217  
         }
 218  
         
 219  0
         return breadcrumbs;
 220  
     }
 221  
     
 222  
     /**
 223  
      * This method gets the current HistoryEntry in the breadcrumb format described in getGeneratedBreadcrumbs.
 224  
      * 
 225  
      * @return
 226  
      */
 227  
     public HistoryEntry getGeneratedCurrentBreadcrumb(){
 228  0
         HistoryEntry breadcrumb = copyEntry(current);
 229  0
         String historyParam = "";
 230  0
         for(int j=0; j < historyEntries.size(); j++){
 231  0
            historyParam = historyParam + ENTRY_TOKEN + historyEntries.get(j).toParam();
 232  
         }
 233  0
         historyParam = historyParam.replaceFirst("\\" + ENTRY_TOKEN, "");
 234  
         
 235  
         try {
 236  0
             historyParam = URLEncoder.encode(historyParam, "UTF-8");
 237  0
         } catch (Exception e) {
 238  0
             LOG.error("Error encoding history param", e);
 239  0
         }
 240  
         
 241  0
         String url = "";
 242  0
         if(breadcrumb.getUrl().contains("?")){
 243  0
             url = breadcrumb.getUrl() + "&" + UifConstants.UrlParams.HISTORY + "=" + historyParam;
 244  
         }
 245  
         else{
 246  0
             url = breadcrumb.getUrl() + "?" + UifConstants.UrlParams.HISTORY + "=" + historyParam;
 247  
         }
 248  0
         breadcrumb.setUrl(url);
 249  0
         return breadcrumb;
 250  
     }
 251  
     
 252  
     /**
 253  
      * Copies a HistoryEntry, for use during breadcrumb generation.
 254  
      * 
 255  
      * @param e
 256  
      * @return
 257  
      */
 258  
     private HistoryEntry copyEntry(HistoryEntry e){
 259  0
         return new HistoryEntry(e.getViewId(), e.getPageId(), e.getTitle(), e.getUrl(), e.getFormKey());
 260  
     }
 261  
 
 262  
     /**
 263  
      * This method pushes the information passed in to history.
 264  
      * Note: currently only used internally in the class - be cautious about its external use.
 265  
      * 
 266  
      * @param viewId
 267  
      * @param pageId
 268  
      * @param title
 269  
      * @param url
 270  
      * @param formKey
 271  
      */
 272  
     public void pushToHistory(String viewId, String pageId, String title, String url, String formKey) {
 273  0
         HistoryEntry entry = new HistoryEntry(viewId, pageId, title, url, formKey);
 274  0
         historyEntries.add(entry);
 275  0
     }
 276  
 
 277  
     /**
 278  
      * When this is set to true, the homeward path will be appended.
 279  
      * Note:  For most cases this should only be on during the first view load.
 280  
      * This setting is set automatically in most cases.
 281  
      * @param appendHomewardPath
 282  
      *            the appendHomewardPath to set
 283  
      */
 284  
     public void setAppendHomewardPath(boolean appendHomewardPath) {
 285  0
         this.appendHomewardPath = appendHomewardPath;
 286  0
     }
 287  
 
 288  
     /**
 289  
      * @return the appendHomewardPath
 290  
      */
 291  
     public boolean isAppendHomewardPath() {
 292  0
         return appendHomewardPath;
 293  
     }
 294  
 
 295  
     /**
 296  
      * Appends the passed history as each different view is shown.  This setting should be used when displaying
 297  
      * passed history is relevant to the user (ie inquiry/lookup chains).  This setting is set automatically in
 298  
      * most cases.
 299  
      * @param appendPassedHistory
 300  
      *            the appendPassedHistory to set
 301  
      */
 302  
     public void setAppendPassedHistory(boolean appendPassedHistory) {
 303  0
         this.appendPassedHistory = appendPassedHistory;
 304  0
     }
 305  
 
 306  
     /**
 307  
      * @return the appendPassedHistory
 308  
      */
 309  
     public boolean isAppendPassedHistory() {
 310  0
         return appendPassedHistory;
 311  
     }
 312  
 
 313  
     /**
 314  
      * Sets the current HistoryEntry using information from the form and the request.  This history parameter is extracted
 315  
      * out of the url inorder for a "clean" url to be used in history parameter and breadcrumb generation, as passing history
 316  
      * history through the nested urls is unnecessary.
 317  
      * 
 318  
      * @param form
 319  
      * @param request
 320  
      */
 321  
     @SuppressWarnings("unchecked")
 322  
     public void setCurrent(UifFormBase form, HttpServletRequest request) {
 323  0
        if(!request.getMethod().equals("POST")){
 324  0
            boolean showHomeValue = false;
 325  0
            boolean pageIdValue = false;
 326  0
            boolean formKeyValue = false;
 327  0
            String queryString = "";
 328  0
            String url = request.getRequestURL().toString();
 329  
            //remove history attribute
 330  0
            Enumeration<String> params = request.getParameterNames();
 331  0
            while(params.hasMoreElements()){
 332  0
                String key = params.nextElement();
 333  0
                if(!key.equals(UifConstants.UrlParams.HISTORY)){
 334  0
                    for(String value: request.getParameterValues(key)){
 335  0
                        queryString = queryString + "&" + key + "=" + value;  
 336  
                    }
 337  
                }
 338  0
                else if(key.equals(UifConstants.UrlParams.PAGE_ID)){
 339  0
                    pageIdValue = true;
 340  
                }
 341  0
                else if(key.equals(UifConstants.UrlParams.SHOW_HOME)){
 342  0
                    showHomeValue = true;
 343  
                }
 344  0
                else if(key.equals(UifConstants.UrlParams.FORM_KEY)){
 345  0
                    formKeyValue = true;
 346  
                }
 347  0
            }
 348  
            
 349  
            //add formKey and pageId to url
 350  0
            if(StringUtils.isNotBlank(form.getFormKey()) && !formKeyValue){
 351  0
                queryString = queryString + "&" + UifConstants.UrlParams.FORM_KEY + "=" + form.getFormKey();
 352  
            }
 353  0
            if(StringUtils.isNotBlank(form.getPageId()) && !pageIdValue){
 354  0
                queryString = queryString + "&" + UifConstants.UrlParams.PAGE_ID + "=" + form.getPageId();
 355  
            }
 356  0
            if(!showHomeValue){
 357  0
                queryString = queryString + "&" + UifConstants.UrlParams.SHOW_HOME + "=false";
 358  
            }
 359  
            
 360  0
            queryString = queryString.replaceFirst("&", "");
 361  
            
 362  0
            if(StringUtils.isNotEmpty(queryString)){
 363  0
                url = url + "?" + queryString;
 364  
            }
 365  
            
 366  0
            View view = form.getView();
 367  0
            String title = view.getTitle();
 368  
            //may move this into view logic instead in the future if it is required for the view's title (not just breadcrumb)
 369  
            //if so remove this and just use getTitle - this logic would be in performFinalize instead
 370  0
            Object titleValue = ViewModelUtils.getValue(view, form, view.getViewLabelFieldPropertyName(), view.getViewLabelFieldBindingInfo());
 371  0
            String titleAppend = "";
 372  0
            if(titleValue != null){
 373  0
                titleAppend = titleValue.toString();
 374  
            }
 375  0
            if(StringUtils.isNotBlank(titleAppend) && view.getAppendOption() != null){
 376  0
                if(view.getAppendOption().equalsIgnoreCase(UifConstants.TitleAppendTypes.DASH)){
 377  0
                    title = title + " - " + titleAppend;
 378  
                }
 379  0
                else if(view.getAppendOption().equalsIgnoreCase(UifConstants.TitleAppendTypes.PARENTHESIS)){
 380  0
                    title = title + "(" + titleAppend +")";
 381  
                }
 382  0
                else if(view.getAppendOption().equalsIgnoreCase(UifConstants.TitleAppendTypes.REPLACE)){
 383  0
                    title = titleAppend;
 384  
                }
 385  
                //else it is none or blank so no title modification will be used
 386  
            }
 387  0
            this.setCurrent(form.getViewId(), form.getPageId(), title, url, form.getFormKey());
 388  
        }
 389  0
     }
 390  
 }