View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.detail;
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   * ItemLocation used in implementers of {@link info.magnolia.ui.contentapp.detail.DetailSubApp}.
43   * Extends the Default Location by adding fields for :
44   * <ul>
45   * <li>the nodePath (some/node/path)</li>
46   * <li>the {@link DetailView.ViewType} (viewType)</li>
47   * <li>the node version (version)</li>
48   * </ul>
49   * <p>
50   * {@code appType:appName:subAppId;some/node/path:viewType:version}
51   *
52   * @deprecated since 6.2 - use {@link info.magnolia.ui.contentapp.detail.ContentDetailSubApp.DetailLocation} instead.
53   *
54   * @see <a href="https://documentation.magnolia-cms.com/display/DOCS62/Upgrading+to+Magnolia+6.2.x">Upgrading to Magnolia 6.2.x</a>
55   */
56  @Deprecated
57  public class DetailLocation extends DefaultLocation {
58  
59      private DetailView.ViewType viewType;
60      private String nodePath;
61      private String version;
62      // Position of the parameter based on the ':' used as separator.
63      private final static int NODE_PATH_PARAM_POSITION = 0;
64      private final static int VIEW_TYPE_PARAM_POSITION = 1;
65      private final static int VERSION_PARAM_POSITION = 2;
66  
67      public DetailLocation(String appName, String subAppId, String parameter) {
68          super(LOCATION_TYPE_APP, appName, subAppId, parameter);
69  
70          setNodePath(extractNodePath(parameter));
71          setViewType(extractViewType(parameter));
72          setVersion(extractVersion(parameter));
73      }
74  
75      public DetailLocation(String appName, String subAppId, DetailView.ViewType viewType, String nodePath, String version) {
76          super(LOCATION_TYPE_APP, appName, subAppId);
77  
78          setNodePath(nodePath);
79          setViewType(viewType);
80          setVersion(version);
81          updateParameter();
82      }
83  
84      public String getNodePath() {
85          return unescapeSpecialCharacters(nodePath);
86      }
87  
88      /**
89       * If the node path is empty, assume root path.
90       */
91      private void setNodePath(String nodePath) {
92          this.nodePath = (nodePath == null || nodePath.isEmpty()) ? "/" : escapeSpecialCharacters(nodePath);
93      }
94  
95      public DetailView.ViewType getViewType() {
96          return viewType;
97      }
98  
99      public void setViewType(DetailView.ViewType viewType) {
100         this.viewType = viewType;
101     }
102 
103     public String getVersion() {
104         return unescapeSpecialCharacters(version);
105     }
106 
107     public void setVersion(String version) {
108         this.version = escapeSpecialCharacters(version);
109     }
110 
111     public boolean hasVersion() {
112         return StringUtils.isNotBlank(version);
113     }
114 
115     /**
116      * Extract the Node path from the parameter.
117      *
118      * @param parameter some/node/path:viewType:version
119      * @return some/node/path
120      */
121     private String extractNodePath(String parameter) {
122         return getParameter(parameter, NODE_PATH_PARAM_POSITION);
123     }
124 
125     /**
126      * Extract the viewType from the parameter.
127      *
128      * @param parameter some/node/path:viewType:version
129      * @return viewType
130      */
131     private DetailView.ViewType extractViewType(String parameter) {
132         String action = getParameter(parameter, VIEW_TYPE_PARAM_POSITION);
133         return DetailView.ViewType.fromString(action);
134     }
135 
136     /**
137      * Extract the Node Version from the parameter.
138      *
139      * @param parameter some/node/path:viewType:version
140      * @return version
141      */
142     private String extractVersion(String parameter) {
143         return getParameter(parameter, VERSION_PARAM_POSITION);
144     }
145 
146     protected String getParameter(String parameter, int position) {
147         String arguments[] = StringUtils.split(parameter, ':');
148         if (position <= arguments.length - 1) {
149             return arguments[position];
150         }
151         return "";
152     }
153 
154     protected void updateParameter() {
155         StringBuilder sb = new StringBuilder();
156         sb.append(nodePath);
157         sb.append(":");
158         sb.append(viewType.getText());
159         if (StringUtils.isNotBlank(version)) {
160             sb.append(":");
161             sb.append(version);
162         }
163         super.setParameter(sb.toString());
164     }
165 
166     public static DetailLocation wrap(Location location) {
167         return new DetailLocation(location.getAppName(), location.getSubAppId(), location.getParameter());
168     }
169 
170     public void updateNodePath(String newNodePath) {
171         setNodePath(newNodePath);
172         updateParameter();
173     }
174 
175     public void updateViewtype(DetailView.ViewType newViewType) {
176         setViewType(newViewType);
177         updateParameter();
178     }
179 
180     public void updateVersion(String newVersion) {
181         setVersion(newVersion);
182         updateParameter();
183     }
184 
185 }