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  public class DetailLocation extends DefaultLocation {
53  
54      private DetailView.ViewType viewType;
55      private String nodePath;
56      private String version;
57      // Position of the parameter based on the ':' used as separator.
58      private final static int NODE_PATH_PARAM_POSITION = 0;
59      private final static int VIEW_TYPE_PARAM_POSITION = 1;
60      private final static int VERSION_PARAM_POSITION = 2;
61  
62      public DetailLocation(String appName, String subAppId, String parameter) {
63          super(LOCATION_TYPE_APP, appName, subAppId, parameter);
64  
65          setNodePath(extractNodePath(parameter));
66          setViewType(extractViewType(parameter));
67          setVersion(extractVersion(parameter));
68      }
69  
70      public DetailLocation(String appName, String subAppId, DetailView.ViewType viewType, String nodePath, String version) {
71          super(LOCATION_TYPE_APP, appName, subAppId);
72  
73          setNodePath(nodePath);
74          setViewType(viewType);
75          setVersion(version);
76          updateParameter();
77      }
78  
79      public String getNodePath() {
80          return unescapeSpecialCharacters(nodePath);
81      }
82  
83      /**
84       * If the node path is empty, assume root path.
85       */
86      private void setNodePath(String nodePath) {
87          this.nodePath = (nodePath == null || nodePath.isEmpty()) ? "/" : escapeSpecialCharacters(nodePath);
88      }
89  
90      public DetailView.ViewType getViewType() {
91          return viewType;
92      }
93  
94      public void setViewType(DetailView.ViewType viewType) {
95          this.viewType = viewType;
96      }
97  
98      public String getVersion() {
99          return unescapeSpecialCharacters(version);
100     }
101 
102     public void setVersion(String version) {
103         this.version = escapeSpecialCharacters(version);
104     }
105 
106     public boolean hasVersion() {
107         return StringUtils.isNotBlank(version);
108     }
109 
110     /**
111      * Extract the Node path from the parameter.
112      *
113      * @param parameter some/node/path:viewType:version
114      * @return some/node/path
115      */
116     private String extractNodePath(String parameter) {
117         return getParameter(parameter, NODE_PATH_PARAM_POSITION);
118     }
119 
120     /**
121      * Extract the viewType from the parameter.
122      *
123      * @param parameter some/node/path:viewType:version
124      * @return viewType
125      */
126     private DetailView.ViewType extractViewType(String parameter) {
127         String action = getParameter(parameter, VIEW_TYPE_PARAM_POSITION);
128         return DetailView.ViewType.fromString(action);
129     }
130 
131     /**
132      * Extract the Node Version from the parameter.
133      *
134      * @param parameter some/node/path:viewType:version
135      * @return version
136      */
137     private String extractVersion(String parameter) {
138         return getParameter(parameter, VERSION_PARAM_POSITION);
139     }
140 
141     protected String getParameter(String parameter, int position) {
142         String arguments[] = StringUtils.split(parameter, ':');
143         if (position <= arguments.length - 1) {
144             return arguments[position];
145         }
146         return "";
147     }
148 
149     protected void updateParameter() {
150         StringBuilder sb = new StringBuilder();
151         sb.append(nodePath);
152         sb.append(":");
153         sb.append(viewType.getText());
154         if (StringUtils.isNotBlank(version)) {
155             sb.append(":");
156             sb.append(version);
157         }
158         super.setParameter(sb.toString());
159     }
160 
161     public static DetailLocation wrap(Location location) {
162         return new DetailLocation(location.getAppName(), location.getSubAppId(), location.getParameter());
163     }
164 
165     public void updateNodePath(String newNodePath) {
166         setNodePath(newNodePath);
167         updateParameter();
168     }
169 
170     public void updateViewtype(DetailView.ViewType newViewType) {
171         setViewType(newViewType);
172         updateParameter();
173     }
174 
175     public void updateVersion(String newVersion) {
176         setVersion(newVersion);
177         updateParameter();
178     }
179 
180 }