View Javadoc
1   /**
2    * This file Copyright (c) 2012-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.contentapp.browser;
35  
36  import info.magnolia.ui.api.location.DefaultLocation;
37  import info.magnolia.ui.api.location.Location;
38  
39  import org.apache.commons.lang3.StringUtils;
40  
41  /**
42   * ContentLocation used in ContentSubApps. Extends the Default Location by adding fields for the nodePath, viewType and query.
43   */
44  public class BrowserLocation extends DefaultLocation {
45  
46      private String nodePath;
47      private String viewType;
48      private String query;
49  
50      public BrowserLocation(String appName, String subAppId, String parameter) {
51          super(LOCATION_TYPE_APP, appName, subAppId, parameter);
52  
53          parameter = StringUtils.defaultString(parameter);
54          setNodePath(extractNodePath(parameter));
55          setViewType(extractView(parameter));
56          setQuery(extractQuery(parameter));
57      }
58  
59      public String getNodePath() {
60          return unescapeSpecialCharacters(nodePath);
61      }
62  
63      /**
64       * If the node path is empty, assume root path.
65       */
66      private void setNodePath(String nodePath) {
67          this.nodePath = (nodePath == null || nodePath.isEmpty()) ? "/" : escapeSpecialCharacters(nodePath);
68      }
69  
70      public String getViewType() {
71          return unescapeSpecialCharacters(viewType);
72      }
73  
74      private void setViewType(String viewType) {
75          this.viewType = escapeSpecialCharacters(viewType);
76      }
77  
78      public String getQuery() {
79          return unescapeSpecialCharacters(query);
80      }
81  
82      private void setQuery(String query) {
83          this.query = escapeSpecialCharacters(query);
84      }
85  
86      private String extractNodePath(String parameter) {
87          int i = parameter.indexOf(':');
88          return i != -1 ? parameter.substring(0, i) : parameter;
89      }
90  
91      private String extractView(String parameter) {
92          // first param is path
93          int i = parameter.indexOf(':');
94          if (i != -1) {
95              // isolate view type parameter, there can be more
96              int j = parameter.indexOf(':', i + 1);
97              String view = (j != -1) ? parameter.substring(i + 1, j) : parameter.substring(i + 1);
98              if (StringUtils.isNotBlank(view)) {
99                  return view;
100             }
101         }
102         return null;
103     }
104 
105     public static String extractQuery(String fragment) {
106         // nodePath
107         int i = fragment.indexOf(':');
108         if (i == -1) {
109             return "";
110         }
111         // view
112         int j = fragment.indexOf(':', i + 1);
113         if (j == -1) {
114             return "";
115         }
116         // query
117         return fragment.substring(j + 1);
118     }
119 
120     protected void updateParameter() {
121         StringBuilder sb = new StringBuilder();
122         sb.append(nodePath);
123         sb.append(":");
124         sb.append(viewType);
125         sb.append(":");
126         sb.append(query);
127         super.setParameter(sb.toString());
128     }
129 
130     public static BrowserLocation wrap(Location location) {
131         return new BrowserLocation(location.getAppName(), location.getSubAppId(), location.getParameter());
132     }
133 
134     public void updateNodePath(String newNodePath) {
135         setNodePath(newNodePath);
136         updateParameter();
137     }
138 
139     public void updateViewType(String newViewType) {
140         setViewType(newViewType);
141         updateParameter();
142     }
143 
144     public void updateQuery(String newQuery) {
145         setQuery(newQuery);
146         updateParameter();
147     }
148 
149 }