View Javadoc
1   /**
2    * This file Copyright (c) 2012-2015 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          setNodePath(extractNodePath(parameter));
54          setViewType(extractView(parameter));
55          setQuery(extractQuery(parameter));
56      }
57  
58      public String getNodePath() {
59          return nodePath;
60      }
61  
62      /**
63       * If the node path is empty, assume root path.
64       */
65      private void setNodePath(String nodePath) {
66          this.nodePath = (nodePath == null || nodePath.isEmpty()) ? "/" : nodePath;
67      }
68  
69      public String getViewType() {
70          return viewType;
71      }
72  
73      private void setViewType(String viewType) {
74          this.viewType = viewType;
75      }
76  
77      public String getQuery() {
78          return query;
79      }
80  
81      private void setQuery(String query) {
82          this.query = query;
83      }
84  
85      private String extractNodePath(String parameter) {
86          int i = parameter.indexOf(':');
87          return i != -1 ? parameter.substring(0, i) : parameter;
88      }
89  
90      private String extractView(String parameter) {
91          // first param is path
92          int i = parameter.indexOf(':');
93          if (i != -1) {
94              // isolate view type parameter, there can be more
95              int j = parameter.indexOf(':', i + 1);
96              String view = (j != -1) ? parameter.substring(i + 1, j) : parameter.substring(i + 1);
97              if (StringUtils.isNotBlank(view)) {
98                  return view;
99              }
100         }
101         return null;
102     }
103 
104     public static String extractQuery(String fragment) {
105         // nodePath
106         int i = fragment.indexOf(':');
107         if (i == -1) {
108             return "";
109         }
110         // view
111         int j = fragment.indexOf(':', i + 1);
112         if (j == -1) {
113             return "";
114         }
115         // query
116         return fragment.substring(j + 1);
117     }
118 
119     protected void updateParameter() {
120         StringBuilder sb = new StringBuilder();
121         sb.append(nodePath);
122         sb.append(":");
123         sb.append(viewType);
124         sb.append(":");
125         sb.append(query);
126         super.setParameter(sb.toString());
127     }
128 
129     public static BrowserLocation wrap(Location location) {
130         return new BrowserLocation(location.getAppName(), location.getSubAppId(), location.getParameter());
131     }
132 
133     public void updateNodePath(String newNodePath) {
134         setNodePath(newNodePath);
135         updateParameter();
136     }
137 
138     public void updateViewType(String newViewType) {
139         setViewType(newViewType);
140         updateParameter();
141     }
142 
143     public void updateQuery(String newQuery) {
144         setQuery(newQuery);
145         updateParameter();
146     }
147 }