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   * @deprecated since 6.2 - use {@link info.magnolia.ui.contentapp.ContentBrowserSubApp.BrowserLocation} instead.
45   *
46   * @see <a href="https://documentation.magnolia-cms.com/display/DOCS62/Upgrading+to+Magnolia+6.2.x">Upgrading to Magnolia 6.2.x</a>
47   */
48  @Deprecated
49  public class BrowserLocation extends DefaultLocation {
50  
51      private String nodePath;
52      private String viewType;
53      private String query;
54  
55      public BrowserLocation(String appName, String subAppId, String parameter) {
56          super(LOCATION_TYPE_APP, appName, subAppId, parameter);
57  
58          parameter = StringUtils.defaultString(parameter);
59          setNodePath(extractNodePath(parameter));
60          setViewType(extractView(parameter));
61          setQuery(extractQuery(parameter));
62      }
63  
64      public String getNodePath() {
65          return unescapeSpecialCharacters(nodePath);
66      }
67  
68      /**
69       * If the node path is empty, assume root path.
70       */
71      private void setNodePath(String nodePath) {
72          this.nodePath = (nodePath == null || nodePath.isEmpty()) ? "/" : escapeSpecialCharacters(nodePath);
73      }
74  
75      public String getViewType() {
76          return unescapeSpecialCharacters(viewType);
77      }
78  
79      private void setViewType(String viewType) {
80          this.viewType = escapeSpecialCharacters(viewType);
81      }
82  
83      public String getQuery() {
84          return unescapeSpecialCharacters(query);
85      }
86  
87      private void setQuery(String query) {
88          this.query = escapeSpecialCharacters(query);
89      }
90  
91      private String extractNodePath(String parameter) {
92          int i = parameter.indexOf(':');
93          return i != -1 ? parameter.substring(0, i) : parameter;
94      }
95  
96      private String extractView(String parameter) {
97          // first param is path
98          int i = parameter.indexOf(':');
99          if (i != -1) {
100             // isolate view type parameter, there can be more
101             int j = parameter.indexOf(':', i + 1);
102             String view = (j != -1) ? parameter.substring(i + 1, j) : parameter.substring(i + 1);
103             if (StringUtils.isNotBlank(view)) {
104                 return view;
105             }
106         }
107         return null;
108     }
109 
110     public static String extractQuery(String fragment) {
111         // nodePath
112         int i = fragment.indexOf(':');
113         if (i == -1) {
114             return "";
115         }
116         // view
117         int j = fragment.indexOf(':', i + 1);
118         if (j == -1) {
119             return "";
120         }
121         // query
122         return fragment.substring(j + 1);
123     }
124 
125     protected void updateParameter() {
126         StringBuilder sb = new StringBuilder();
127         sb.append(nodePath);
128         sb.append(":");
129         sb.append(viewType);
130         sb.append(":");
131         sb.append(query);
132         super.setParameter(sb.toString());
133     }
134 
135     public static BrowserLocation wrap(Location location) {
136         return new BrowserLocation(location.getAppName(), location.getSubAppId(), location.getParameter());
137     }
138 
139     public void updateNodePath(String newNodePath) {
140         setNodePath(newNodePath);
141         updateParameter();
142     }
143 
144     public void updateViewType(String newViewType) {
145         setViewType(newViewType);
146         updateParameter();
147     }
148 
149     public void updateQuery(String newQuery) {
150         setQuery(newQuery);
151         updateParameter();
152     }
153 
154 }