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.softwarecatalog;
16  
17  import com.liferay.portal.kernel.portlet.BaseFriendlyURLMapper;
18  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
19  import com.liferay.portal.kernel.util.ArrayUtil;
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.PortletRequest;
30  import javax.portlet.WindowState;
31  
32  /**
33   * <a href="SCFriendlyURLMapper.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Jorge Ferrer
36   */
37  public class SCFriendlyURLMapper extends BaseFriendlyURLMapper {
38  
39      public String buildPath(LiferayPortletURL portletURL) {
40          String friendlyURL = null;
41  
42          if (!portletURL.getLifecycle().equals(PortletRequest.RENDER_PHASE)) {
43               return friendlyURL;
44          }
45  
46          String tabs1 = portletURL.getParameter("tabs1");
47  
48          String action = GetterUtil.getString(
49              portletURL.getParameter("struts_action"));
50  
51          if (action.equals("/software_catalog/view")) {
52              friendlyURL = "/software_catalog/" + tabs1;
53          }
54          else if (action.equals("/software_catalog/view_product_entry")) {
55              String productEntryId = portletURL.getParameter("productEntryId");
56  
57              friendlyURL = "/software_catalog/products/" + productEntryId;
58  
59              portletURL.addParameterIncludedInPath("productEntryId");
60          }
61          else if (action.equals("/software_catalog/edit_product_entry")) {
62              String productEntryId = portletURL.getParameter("productEntryId");
63  
64              if (Validator.isNotNull(productEntryId)) {
65                  friendlyURL = "/software_catalog/products/" +
66                      productEntryId + "/edit";
67  
68                  portletURL.addParameterIncludedInPath("productEntryId");
69              }
70              else {
71                  friendlyURL = "/software_catalog/products/new";
72              }
73          }
74          else if (action.equals("/software_catalog/edit_product_version")) {
75              String productEntryId = portletURL.getParameter("productEntryId");
76              String productVersionId = portletURL.getParameter(
77                  "productVersionId");
78  
79              if (Validator.isNotNull(productVersionId)) {
80                  friendlyURL = "/software_catalog/products/" +
81                      productEntryId + "/versions/" + productVersionId + "/edit";
82  
83                  portletURL.addParameterIncludedInPath("productEntryId");
84                  portletURL.addParameterIncludedInPath("productVersionId");
85              }
86              else {
87                  friendlyURL = "/software_catalog/products/" +
88                      productEntryId + "/versions/new";
89              }
90          }
91          else if (action.equals("/software_catalog/edit_framework_version")) {
92              String frameworkVersionId = portletURL.getParameter(
93                  "frameworkVersionId");
94  
95              if (Validator.isNotNull(frameworkVersionId)) {
96                  friendlyURL = "/software_catalog/framework_versions/" +
97                      frameworkVersionId + "/edit";
98  
99                  portletURL.addParameterIncludedInPath("frameworkVersionId");
100             }
101             else {
102                 friendlyURL = "/software_catalog/framework_versions/new";
103             }
104         }
105         else if (action.equals("/software_catalog/edit_license")) {
106             String licenseId = portletURL.getParameter("licenseId");
107 
108             if (Validator.isNotNull(licenseId)) {
109                 friendlyURL = "/software_catalog/licenses/" +
110                     licenseId + "/edit";
111 
112                 portletURL.addParameterIncludedInPath("licenseId");
113             }
114             else {
115                 friendlyURL = "/software_catalog/licenses/new";
116             }
117         }
118         else if (action.equals("/software_catalog/search")) {
119             friendlyURL = "/software_catalog/search";
120         }
121 
122         if (Validator.isNotNull(friendlyURL)) {
123             WindowState windowState = portletURL.getWindowState();
124 
125             if (!windowState.equals(WindowState.NORMAL)) {
126                 friendlyURL += StringPool.SLASH + windowState;
127             }
128 
129             portletURL.addParameterIncludedInPath("p_p_id");
130 
131             portletURL.addParameterIncludedInPath("struts_action");
132             portletURL.addParameterIncludedInPath("tabs1");
133         }
134 
135         return friendlyURL;
136     }
137 
138     public String getMapping() {
139         return _MAPPING;
140     }
141 
142     public String getPortletId() {
143         return _PORTLET_ID;
144     }
145 
146     public void populateParams(
147         String friendlyURLPath, Map<String, String[]> parameterMap,
148         Map<String, Object> requestContext) {
149 
150         addParameter(parameterMap, "p_p_id", _PORTLET_ID);
151 
152         if (!parameterMap.containsKey("p_p_lifecycle")) {
153             addParameter(parameterMap, "p_p_lifecycle", "0");
154         }
155 
156         addParameter(parameterMap, "p_p_mode", PortletMode.VIEW);
157 
158         int x = friendlyURLPath.indexOf(StringPool.SLASH, 1);
159 
160         String[] urlFragments = StringUtil.split(
161             friendlyURLPath.substring(x + 1), StringPool.SLASH);
162 
163         if (friendlyURLPath.indexOf("maximized", x) != -1) {
164             urlFragments = ArrayUtil.remove(urlFragments, "maximized");
165 
166             addParameter(parameterMap, "p_p_state", WindowState.MAXIMIZED);
167         }
168 
169         String resourceIdParam = getResourceIdParam(urlFragments[0]);
170 
171         if (urlFragments.length == 1) {
172             addParameter(
173                 parameterMap, "struts_action", "/software_catalog/view");
174             addParameter(parameterMap, "tabs1", urlFragments[0]);
175         }
176         else if (urlFragments.length == 2) {
177             if (urlFragments[1].equals("new")) {
178                 addParameter(
179                     parameterMap, "struts_action",
180                     getEditAction(urlFragments[0]));
181                 addParameter(parameterMap, "tabs1", urlFragments[0]);
182             }
183             else if (urlFragments[0].equals("products")) {
184                 addParameter(
185                     parameterMap,
186                     "struts_action", "/software_catalog/view_product_entry");
187                 addParameter(parameterMap, "tabs1", urlFragments[0]);
188                 addParameter(parameterMap, resourceIdParam, urlFragments[1]);
189             }
190         }
191         else if (urlFragments.length == 3) {
192             if (urlFragments[2].equals("edit")) {
193                 addParameter(
194                     parameterMap, "struts_action",
195                     getEditAction(urlFragments[0]));
196                 addParameter(parameterMap, "tabs1", urlFragments[0]);
197                 addParameter(parameterMap, resourceIdParam, urlFragments[1]);
198             }
199         }
200         else if (urlFragments.length == 4) {
201             if (urlFragments[3].equals("new")) {
202                 addParameter(
203                     parameterMap, "struts_action",
204                     getEditAction(urlFragments[2]));
205                 addParameter(parameterMap, "tabs1", urlFragments[0]);
206                 addParameter(parameterMap, resourceIdParam, urlFragments[1]);
207             }
208         }
209         else if (urlFragments.length == 5) {
210             if (urlFragments[0].equals("products") &&
211                 urlFragments[4].equals("edit")) {
212 
213                 addParameter(
214                     parameterMap, "struts_action",
215                     getEditAction(urlFragments[2]));
216                 addParameter(parameterMap, "tabs1", urlFragments[0]);
217                 addParameter(parameterMap, resourceIdParam, urlFragments[1]);
218                 addParameter(
219                     parameterMap, getResourceIdParam(urlFragments[2]),
220                     urlFragments[3]);
221             }
222         }
223     }
224 
225     protected String getEditAction(String resource) {
226         String action = null;
227 
228         if (resource.equals("my_products") || resource.equals("products")) {
229             action = "edit_product_entry";
230         }
231         else if (resource.equals("versions")) {
232             action = "edit_product_version";
233         }
234         else if (resource.equals("framework_versions")) {
235             action = "edit_framework_version";
236         }
237         else if (resource.equals("licenses")) {
238             action = "edit_license";
239         }
240         else {
241             return null;
242         }
243 
244         return "/software_catalog/" + action;
245     }
246 
247     protected String getResourceIdParam(String resource) {
248         if (resource.equals("my_products") || resource.equals("products")) {
249             return "productEntryId";
250         }
251         else if (resource.equals("versions")) {
252             return "productVersionId";
253         }
254         else if (resource.equals("framework_versions")) {
255             return "frameworkVersionId";
256         }
257         else if (resource.equals("licenses")) {
258             return "licenseId";
259         }
260         else if (resource.equals("discussion")) {
261             return "messageId";
262         }
263         else {
264             return null;
265         }
266     }
267 
268     private static final String _MAPPING = "software_catalog";
269 
270     private static final String _PORTLET_ID = PortletKeys.SOFTWARE_CATALOG;
271 
272 }