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 assetEntryId = portletURL.getParameter("assetEntryId");
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(assetEntryId)) {
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  
83                      portletURL.addParameterIncludedInPath("urlTitle");
84                  }
85                  else {
86                      friendlyURLPath += "id/" + assetEntryId;
87                  }
88  
89                  portletURL.addParameterIncludedInPath("type");
90                  portletURL.addParameterIncludedInPath("assetEntryId");
91              }
92          }
93  
94          if (Validator.isNotNull(friendlyURLPath)) {
95              portletURL.addParameterIncludedInPath("p_p_id");
96  
97              portletURL.addParameterIncludedInPath("struts_action");
98          }
99  
100         return friendlyURLPath;
101     }
102 
103     public String getMapping() {
104         return _MAPPING;
105     }
106 
107     public String getPortletId() {
108         return _PORTLET_ID;
109     }
110 
111     public void populateParams(
112         String friendlyURLPath, Map<String, String[]> params) {
113 
114         int x = friendlyURLPath.indexOf(StringPool.SLASH, 1);
115 
116         String[] urlFragments = StringUtil.split(
117             friendlyURLPath.substring(x + 1), StringPool.SLASH);
118 
119         if (urlFragments.length > 2) {
120             String instanceId = urlFragments[0];
121             String type = urlFragments[1];
122             String assetEntryId = null;
123             String urlTitle = null;
124 
125             if (urlFragments.length > 3) {
126                 assetEntryId = urlFragments[3];
127             }
128             else {
129                 urlTitle = urlFragments[2];
130             }
131 
132             String portletId = _PORTLET_ID + "_INSTANCE_" + instanceId;
133 
134            if (Validator.equals(portletId, _PORTLET_ID)) {
135                 portletId = _PORTLET_DEFAULT_INSTANCE;
136 
137                 params.put("p_p_id", new String[] {portletId});
138                 params.put(
139                     "p_p_state",
140                     new String[] {WindowState.MAXIMIZED.toString()});
141             }
142             else {
143                 params.put("p_p_id", new String[] {portletId});
144                 params.put(
145                     "p_p_state", new String[] {WindowState.NORMAL.toString()});
146             }
147 
148             params.put("p_p_lifecycle", new String[] {"0"});
149             params.put("p_p_mode", new String[] {PortletMode.VIEW.toString()});
150 
151             String namespace =
152                 StringPool.UNDERLINE + portletId + StringPool.UNDERLINE;
153 
154             params.put(
155                 namespace + "struts_action",
156                 new String[] {"/asset_publisher/view_content"});
157             params.put(namespace + "type", new String[] {type});
158 
159             if (Validator.isNotNull(assetEntryId)) {
160                 params.put(
161                     namespace + "assetEntryId", new String[] {assetEntryId});
162             }
163             else {
164                 params.put(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 }