1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.assetpublisher;
24  
25  import com.liferay.portal.kernel.portlet.BaseFriendlyURLMapper;
26  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
27  import com.liferay.portal.kernel.portlet.LiferayWindowState;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.util.PortletKeys;
33  
34  import java.util.Map;
35  
36  import javax.portlet.PortletMode;
37  import javax.portlet.WindowState;
38  
39  /**
40   * <a href="AssetPublisherFriendlyURLMapper.java.html"><b><i>View Source</i></b>
41   * </a>
42   *
43   * @author Julio Camarero
44   *
45   */
46  public class AssetPublisherFriendlyURLMapper extends BaseFriendlyURLMapper {
47  
48      public String buildPath(LiferayPortletURL portletURL) {
49          String friendlyURLPath = null;
50  
51          String strutsAction = GetterUtil.getString(
52              portletURL.getParameter("struts_action"));
53  
54          WindowState windowState = portletURL.getWindowState();
55  
56          if ((strutsAction.equals("/asset_publisher/view_content"))  &&
57              ((windowState == null) ||
58               (!windowState.equals(LiferayWindowState.EXCLUSIVE)))) {
59  
60              String portletId = portletURL.getPortletId();
61              String assetId = portletURL.getParameter("assetId");
62              String type = GetterUtil.getString(
63                  portletURL.getParameter("type"), "content");
64              String urlTitle = portletURL.getParameter("urlTitle");
65  
66              if (Validator.isNotNull(portletId) &&
67                  Validator.isNotNull(assetId)) {
68  
69                  if (portletId.equals(_PORTLET_DEFAULT_INSTANCE)) {
70                      portletId = _PORTLET_ID;
71                  }
72  
73                  int pos = portletId.indexOf("_INSTANCE_");
74  
75                  String instanceId = null;
76  
77                  if (pos > 0) {
78                      instanceId = portletId.substring(pos + 10);
79                  }
80                  else {
81                      instanceId = portletId;
82                  }
83  
84                  friendlyURLPath =
85                      "/asset_publisher/" + instanceId + StringPool.SLASH + type +
86                          StringPool.SLASH;
87  
88                  if (Validator.isNotNull(urlTitle)){
89                      friendlyURLPath += urlTitle;
90                      portletURL.addParameterIncludedInPath("urlTitle");
91                  }
92                  else {
93                      friendlyURLPath += "id/" + assetId;
94                  }
95  
96                  portletURL.addParameterIncludedInPath("type");
97                  portletURL.addParameterIncludedInPath("assetId");
98              }
99          }
100 
101         if (Validator.isNotNull(friendlyURLPath)) {
102             portletURL.addParameterIncludedInPath("p_p_id");
103 
104             portletURL.addParameterIncludedInPath("struts_action");
105         }
106 
107         return friendlyURLPath;
108     }
109 
110     public String getMapping() {
111         return _MAPPING;
112     }
113 
114     public String getPortletId() {
115         return _PORTLET_ID;
116     }
117 
118     public void populateParams(
119         String friendlyURLPath, Map<String, String[]> params) {
120 
121         int x = friendlyURLPath.indexOf(StringPool.SLASH, 1);
122 
123         String[] urlFragments = StringUtil.split(
124             friendlyURLPath.substring(x + 1), StringPool.SLASH);
125 
126         if (urlFragments.length > 2) {
127             String instanceId = urlFragments[0];
128             String type = urlFragments[1];
129             String assetId = null;
130             String urlTitle = null;
131 
132             if (urlFragments.length > 3) {
133                 assetId = urlFragments[3];
134             }
135             else {
136                 urlTitle = urlFragments[2];
137             }
138 
139             String portletId = _PORTLET_ID + "_INSTANCE_" + instanceId;
140 
141            if (Validator.equals(portletId, _PORTLET_ID)) {
142                 portletId = _PORTLET_DEFAULT_INSTANCE;
143 
144                 params.put("p_p_id", new String[] {portletId});
145                 params.put(
146                     "p_p_state",
147                     new String[] {WindowState.MAXIMIZED.toString()});
148             }
149             else {
150                 params.put("p_p_id", new String[] {portletId});
151                 params.put(
152                     "p_p_state", new String[] {WindowState.NORMAL.toString()});
153             }
154 
155             params.put("p_p_lifecycle", new String[] {"0"});
156             params.put("p_p_mode", new String[] {PortletMode.VIEW.toString()});
157 
158             String namespace =
159                 StringPool.UNDERLINE + portletId + StringPool.UNDERLINE;
160 
161             params.put(
162                 namespace + "struts_action",
163                 new String[] {"/asset_publisher/view_content"});
164 
165             if (Validator.isNotNull(assetId)) {
166                 params.put(namespace + "assetId", new String[] {assetId});
167             }
168             else {
169                 params.put(namespace + "type", new String[] {type});
170                 params.put(namespace + "urlTitle", new String[] {urlTitle});
171             }
172         }
173     }
174 
175     private static final String _MAPPING = "asset_publisher";
176 
177     private static final String _PORTLET_DEFAULT_INSTANCE =
178         PortletKeys.ASSET_PUBLISHER + "_INSTANCE_0000";
179 
180     private static final String _PORTLET_ID = PortletKeys.ASSET_PUBLISHER;
181 
182 }