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[]> params) {
148 
149         addParam(params, "p_p_id", _PORTLET_ID);
150 
151         if (!params.containsKey("p_p_lifecycle")) {
152             addParam(params, "p_p_lifecycle", "0");
153         }
154 
155         addParam(params, "p_p_mode", PortletMode.VIEW);
156 
157         int x = friendlyURLPath.indexOf(StringPool.SLASH, 1);
158 
159         String[] urlFragments = StringUtil.split(
160             friendlyURLPath.substring(x + 1), StringPool.SLASH);
161 
162         if (friendlyURLPath.indexOf("maximized", x) != -1) {
163             urlFragments = ArrayUtil.remove(urlFragments, "maximized");
164 
165             addParam(params, "p_p_state", WindowState.MAXIMIZED);
166         }
167 
168         String resourceIdParam = getResourceIdParam(urlFragments[0]);
169 
170         if (urlFragments.length == 1) {
171             addParam(params, "struts_action", "/software_catalog/view");
172             addParam(params, "tabs1", urlFragments[0]);
173         }
174         else if (urlFragments.length == 2) {
175             if (urlFragments[1].equals("new")) {
176                 addParam(
177                     params, "struts_action", getEditAction(urlFragments[0]));
178                 addParam(params, "tabs1", urlFragments[0]);
179             }
180             else if (urlFragments[0].equals("products")) {
181                 addParam(
182                     params,
183                     "struts_action", "/software_catalog/view_product_entry");
184                 addParam(params, "tabs1", urlFragments[0]);
185                 addParam(params, resourceIdParam, urlFragments[1]);
186             }
187         }
188         else if (urlFragments.length == 3) {
189             if (urlFragments[2].equals("edit")) {
190                 addParam(
191                     params, "struts_action", getEditAction(urlFragments[0]));
192                 addParam(params, "tabs1", urlFragments[0]);
193                 addParam(params, resourceIdParam, urlFragments[1]);
194             }
195         }
196         else if (urlFragments.length == 4) {
197             if (urlFragments[3].equals("new")) {
198                 addParam(
199                     params, "struts_action", getEditAction(urlFragments[2]));
200                 addParam(params, "tabs1", urlFragments[0]);
201                 addParam(params, resourceIdParam, urlFragments[1]);
202             }
203         }
204         else if (urlFragments.length == 5) {
205             if (urlFragments[0].equals("products") &&
206                 urlFragments[4].equals("edit")) {
207 
208                 addParam(
209                     params, "struts_action", getEditAction(urlFragments[2]));
210                 addParam(params, "tabs1", urlFragments[0]);
211                 addParam(params, resourceIdParam, urlFragments[1]);
212                 addParam(
213                     params, getResourceIdParam(urlFragments[2]),
214                     urlFragments[3]);
215             }
216         }
217     }
218 
219     protected String getEditAction(String resource) {
220         String action = null;
221 
222         if (resource.equals("my_products") || resource.equals("products")) {
223             action = "edit_product_entry";
224         }
225         else if (resource.equals("versions")) {
226             action = "edit_product_version";
227         }
228         else if (resource.equals("framework_versions")) {
229             action = "edit_framework_version";
230         }
231         else if (resource.equals("licenses")) {
232             action = "edit_license";
233         }
234         else {
235             return null;
236         }
237 
238         return "/software_catalog/" + action;
239     }
240 
241     protected String getResourceIdParam(String resource) {
242         if (resource.equals("my_products") || resource.equals("products")) {
243             return "productEntryId";
244         }
245         else if (resource.equals("versions")) {
246             return "productVersionId";
247         }
248         else if (resource.equals("framework_versions")) {
249             return "frameworkVersionId";
250         }
251         else if (resource.equals("licenses")) {
252             return "licenseId";
253         }
254         else if (resource.equals("discussion")) {
255             return "messageId";
256         }
257         else {
258             return null;
259         }
260     }
261 
262     private static final String _MAPPING = "software_catalog";
263 
264     private static final String _PORTLET_ID = PortletKeys.SOFTWARE_CATALOG;
265 
266 }