1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.util.PortletKeys;
25  
26  import java.util.Map;
27  
28  import javax.portlet.PortletMode;
29  import javax.portlet.WindowState;
30  
31  /**
32   * <a href="AssetPublisherFriendlyURLMapper.java.html"><b><i>View Source</i></b>
33   * </a>
34   *
35   * @author Julio Camarero
36   */
37  public class AssetPublisherFriendlyURLMapper extends BaseFriendlyURLMapper {
38  
39      public String buildPath(LiferayPortletURL portletURL) {
40          String friendlyURLPath = null;
41  
42          String strutsAction = GetterUtil.getString(
43              portletURL.getParameter("struts_action"));
44  
45          WindowState windowState = portletURL.getWindowState();
46  
47          if ((strutsAction.equals("/asset_publisher/view_content"))  &&
48              ((windowState == null) ||
49               (!windowState.equals(LiferayWindowState.EXCLUSIVE) &&
50                !windowState.equals(LiferayWindowState.POP_UP)))) {
51  
52              String portletId = portletURL.getPortletId();
53              String assetId = portletURL.getParameter("assetId");
54              String type = GetterUtil.getString(
55                  portletURL.getParameter("type"), "content");
56              String urlTitle = portletURL.getParameter("urlTitle");
57  
58              if (Validator.isNotNull(portletId) &&
59                  Validator.isNotNull(assetId)) {
60  
61                  if (portletId.equals(_PORTLET_DEFAULT_INSTANCE)) {
62                      portletId = _PORTLET_ID;
63                  }
64  
65                  int pos = portletId.indexOf("_INSTANCE_");
66  
67                  String instanceId = null;
68  
69                  if (pos > 0) {
70                      instanceId = portletId.substring(pos + 10);
71                  }
72                  else {
73                      instanceId = portletId;
74                  }
75  
76                  friendlyURLPath =
77                      "/asset_publisher/" + instanceId + StringPool.SLASH + type +
78                          StringPool.SLASH;
79  
80                  if (Validator.isNotNull(urlTitle)) {
81                      friendlyURLPath += urlTitle;
82                      portletURL.addParameterIncludedInPath("urlTitle");
83                  }
84                  else {
85                      friendlyURLPath += "id/" + assetId;
86                  }
87  
88                  portletURL.addParameterIncludedInPath("type");
89                  portletURL.addParameterIncludedInPath("assetId");
90              }
91          }
92  
93          if (Validator.isNotNull(friendlyURLPath)) {
94              portletURL.addParameterIncludedInPath("p_p_id");
95  
96              portletURL.addParameterIncludedInPath("struts_action");
97          }
98  
99          return friendlyURLPath;
100     }
101 
102     public String getMapping() {
103         return _MAPPING;
104     }
105 
106     public String getPortletId() {
107         return _PORTLET_ID;
108     }
109 
110     public void populateParams(
111         String friendlyURLPath, Map<String, String[]> params) {
112 
113         int x = friendlyURLPath.indexOf(StringPool.SLASH, 1);
114 
115         String[] urlFragments = StringUtil.split(
116             friendlyURLPath.substring(x + 1), StringPool.SLASH);
117 
118         if (urlFragments.length > 2) {
119             String instanceId = urlFragments[0];
120             String type = urlFragments[1];
121             String assetId = null;
122             String urlTitle = null;
123 
124             if (urlFragments.length > 3) {
125                 assetId = urlFragments[3];
126             }
127             else {
128                 urlTitle = urlFragments[2];
129             }
130 
131             String portletId = _PORTLET_ID + "_INSTANCE_" + instanceId;
132 
133            if (Validator.equals(portletId, _PORTLET_ID)) {
134                 portletId = _PORTLET_DEFAULT_INSTANCE;
135 
136                 params.put("p_p_id", new String[] {portletId});
137                 params.put(
138                     "p_p_state",
139                     new String[] {WindowState.MAXIMIZED.toString()});
140             }
141             else {
142                 params.put("p_p_id", new String[] {portletId});
143                 params.put(
144                     "p_p_state", new String[] {WindowState.NORMAL.toString()});
145             }
146 
147             params.put("p_p_lifecycle", new String[] {"0"});
148             params.put("p_p_mode", new String[] {PortletMode.VIEW.toString()});
149 
150             String namespace =
151                 StringPool.UNDERLINE + portletId + StringPool.UNDERLINE;
152 
153             params.put(
154                 namespace + "struts_action",
155                 new String[] {"/asset_publisher/view_content"});
156 
157             if (Validator.isNotNull(assetId)) {
158                 params.put(namespace + "assetId", new String[] {assetId});
159             }
160             else {
161                 params.put(namespace + "type", new String[] {type});
162                 params.put(namespace + "urlTitle", new String[] {urlTitle});
163             }
164         }
165     }
166 
167     private static final String _MAPPING = "asset_publisher";
168 
169     private static final String _PORTLET_DEFAULT_INSTANCE =
170         PortletKeys.ASSET_PUBLISHER + "_INSTANCE_0000";
171 
172     private static final String _PORTLET_ID = PortletKeys.ASSET_PUBLISHER;
173 
174 }