1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.assetpublisher;
16  
17  import com.liferay.portal.kernel.portlet.BaseFriendlyURLMapper;
18  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
19  import com.liferay.portal.kernel.portlet.LiferayWindowState;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.StringUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.model.PortletConstants;
25  import com.liferay.portal.util.PortletKeys;
26  
27  import java.util.Map;
28  
29  import javax.portlet.PortletMode;
30  import javax.portlet.WindowState;
31  
32  /**
33   * <a href="AssetPublisherFriendlyURLMapper.java.html"><b><i>View Source</i></b>
34   * </a>
35   *
36   * @author Julio Camarero
37   */
38  public class AssetPublisherFriendlyURLMapper extends BaseFriendlyURLMapper {
39  
40      public String buildPath(LiferayPortletURL portletURL) {
41          String friendlyURLPath = null;
42  
43          String strutsAction = GetterUtil.getString(
44              portletURL.getParameter("struts_action"));
45  
46          WindowState windowState = portletURL.getWindowState();
47  
48          if ((strutsAction.equals("/asset_publisher/view_content"))  &&
49              ((windowState == null) ||
50               (!windowState.equals(LiferayWindowState.EXCLUSIVE) &&
51                !windowState.equals(LiferayWindowState.POP_UP)))) {
52  
53              String portletId = portletURL.getPortletId();
54              String assetId = portletURL.getParameter("assetId");
55              String type = GetterUtil.getString(
56                  portletURL.getParameter("type"), "content");
57              String urlTitle = portletURL.getParameter("urlTitle");
58  
59              if (Validator.isNotNull(portletId) &&
60                  Validator.isNotNull(assetId)) {
61  
62                  if (portletId.equals(_PORTLET_DEFAULT_INSTANCE)) {
63                      portletId = _PORTLET_ID;
64                  }
65  
66                  int pos = portletId.indexOf(
67                      PortletConstants.INSTANCE_SEPARATOR);
68  
69                  String instanceId = null;
70  
71                  if (pos > 0) {
72                      instanceId = portletId.substring(pos + 10);
73                  }
74                  else {
75                      instanceId = portletId;
76                  }
77  
78                  friendlyURLPath =
79                      "/asset_publisher/" + instanceId + StringPool.SLASH + type +
80                          StringPool.SLASH;
81  
82                  if (Validator.isNotNull(urlTitle)) {
83                      friendlyURLPath += urlTitle;
84                      portletURL.addParameterIncludedInPath("urlTitle");
85                  }
86                  else {
87                      friendlyURLPath += "id/" + assetId;
88                  }
89  
90                  portletURL.addParameterIncludedInPath("type");
91                  portletURL.addParameterIncludedInPath("assetId");
92              }
93          }
94          else if ((windowState != null) &&
95                   windowState.equals(WindowState.MAXIMIZED)) {
96  
97              friendlyURLPath += StringPool.SLASH + windowState;
98          }
99  
100         if (Validator.isNotNull(friendlyURLPath)) {
101             portletURL.addParameterIncludedInPath("p_p_id");
102 
103             portletURL.addParameterIncludedInPath("struts_action");
104         }
105 
106         return friendlyURLPath;
107     }
108 
109     public String getMapping() {
110         return _MAPPING;
111     }
112 
113     public String getPortletId() {
114         return _PORTLET_ID;
115     }
116 
117     public void populateParams(
118         String friendlyURLPath, Map<String, String[]> parameterMap) {
119 
120         int x = friendlyURLPath.indexOf(StringPool.SLASH, 1);
121 
122         String[] urlFragments = StringUtil.split(
123             friendlyURLPath.substring(x + 1), StringPool.SLASH);
124 
125         if (urlFragments.length > 2) {
126             String instanceId = urlFragments[0];
127             String type = urlFragments[1];
128             String assetId = null;
129             String urlTitle = null;
130 
131             if ((urlFragments.length > 3) && urlFragments[2].equals("id")) {
132                 assetId = urlFragments[3];
133             }
134             else {
135                 urlTitle = urlFragments[2];
136             }
137 
138             String portletId =
139                 _PORTLET_ID + PortletConstants.INSTANCE_SEPARATOR + instanceId;
140 
141             parameterMap.put("p_p_id", new String[] {portletId});
142             parameterMap.put("p_p_lifecycle", new String[] {"0"});
143 
144             if (friendlyURLPath.indexOf("maximized", x) != -1) {
145                 addParameter(parameterMap, "p_p_state", WindowState.MAXIMIZED);
146             }
147 
148             parameterMap.put(
149                 "p_p_mode", new String[] {PortletMode.VIEW.toString()});
150 
151             String namespace =
152                 StringPool.UNDERLINE + portletId + StringPool.UNDERLINE;
153 
154             parameterMap.put(
155                 namespace + "struts_action",
156                 new String[] {"/asset_publisher/view_content"});
157 
158             if (Validator.isNotNull(assetId)) {
159                 parameterMap.put(namespace + "assetId", new String[] {assetId});
160             }
161             else {
162                 parameterMap.put(namespace + "type", new String[] {type});
163                 parameterMap.put(
164                     namespace + "urlTitle", new String[] {urlTitle});
165             }
166         }
167     }
168 
169     private static final String _MAPPING = "asset_publisher";
170 
171     private static final String _PORTLET_DEFAULT_INSTANCE =
172         PortletKeys.ASSET_PUBLISHER + "_INSTANCE_0000";
173 
174     private static final String _PORTLET_ID = PortletKeys.ASSET_PUBLISHER;
175 
176 }