1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.softwarecatalog;
24  
25  import com.liferay.portal.kernel.portlet.BaseFriendlyURLMapper;
26  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.util.PortletKeys;
32  
33  import java.util.Map;
34  
35  import javax.portlet.PortletMode;
36  import javax.portlet.PortletRequest;
37  import javax.portlet.WindowState;
38  
39  /**
40   * <a href="SCFriendlyURLMapper.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Jorge Ferrer
43   */
44  public class SCFriendlyURLMapper extends BaseFriendlyURLMapper {
45  
46      public String buildPath(LiferayPortletURL portletURL) {
47          String friendlyURL = null;
48  
49          if (!portletURL.getLifecycle().equals(PortletRequest.RENDER_PHASE)) {
50               return friendlyURL;
51          }
52  
53          String tabs1 = portletURL.getParameter("tabs1");
54  
55          String action = GetterUtil.getString(
56              portletURL.getParameter("struts_action"));
57  
58          if (action.equals("/software_catalog/view")) {
59              friendlyURL = "/software_catalog/" + tabs1;
60          }
61          else if (action.equals("/software_catalog/view_product_entry")) {
62              String productEntryId = portletURL.getParameter("productEntryId");
63  
64              friendlyURL = "/software_catalog/products/" + productEntryId;
65  
66              portletURL.addParameterIncludedInPath("productEntryId");
67          }
68          else if (action.equals("/software_catalog/edit_product_entry")) {
69              String productEntryId = portletURL.getParameter("productEntryId");
70  
71              if (Validator.isNotNull(productEntryId)) {
72                  friendlyURL = "/software_catalog/products/" +
73                      productEntryId + "/edit";
74  
75                  portletURL.addParameterIncludedInPath("productEntryId");
76              }
77              else {
78                  friendlyURL = "/software_catalog/products/new";
79              }
80          }
81          else if (action.equals("/software_catalog/edit_product_version")) {
82              String productEntryId = portletURL.getParameter("productEntryId");
83              String productVersionId = portletURL.getParameter(
84                  "productVersionId");
85  
86              if (Validator.isNotNull(productVersionId)) {
87                  friendlyURL = "/software_catalog/products/" +
88                      productEntryId + "/versions/" + productVersionId + "/edit";
89  
90                  portletURL.addParameterIncludedInPath("productEntryId");
91                  portletURL.addParameterIncludedInPath("productVersionId");
92              }
93              else {
94                  friendlyURL = "/software_catalog/products/" +
95                      productEntryId + "/versions/new";
96              }
97          }
98          else if (action.equals(
99                      "/software_catalog/edit_framework_version")) {
100 
101             String frameworkVersionId = portletURL.getParameter(
102                 "frameworkVersionId");
103 
104             if (Validator.isNotNull(frameworkVersionId)) {
105                 friendlyURL = "/software_catalog/framework_versions/" +
106                     frameworkVersionId + "/edit";
107 
108                 portletURL.addParameterIncludedInPath("frameworkVersionId");
109             }
110             else {
111                 friendlyURL = "/software_catalog/framework_versions/new";
112             }
113         }
114         else if (action.equals(
115                     "/software_catalog/edit_license")) {
116 
117             String licenseId = portletURL.getParameter("licenseId");
118 
119             if (Validator.isNotNull(licenseId)) {
120                 friendlyURL = "/software_catalog/licenses/" +
121                     licenseId + "/edit";
122 
123                 portletURL.addParameterIncludedInPath("licenseId");
124             }
125             else {
126                 friendlyURL = "/software_catalog/licenses/new";
127             }
128         }
129         else if (action.equals(
130                     "/software_catalog/search")) {
131 
132             friendlyURL = "/software_catalog/search";
133         }
134 
135         if (Validator.isNotNull(friendlyURL)) {
136             portletURL.addParameterIncludedInPath("p_p_id");
137 
138             portletURL.addParameterIncludedInPath("struts_action");
139             portletURL.addParameterIncludedInPath("tabs1");
140         }
141 
142         return friendlyURL;
143     }
144 
145     public String getMapping() {
146         return _MAPPING;
147     }
148 
149     public String getPortletId() {
150         return _PORTLET_ID;
151     }
152 
153     public void populateParams(
154         String friendlyURLPath, Map<String, String[]> params) {
155 
156         addParam(params, "p_p_id", _PORTLET_ID);
157 
158         if (!params.containsKey("p_p_lifecycle")) {
159             addParam(params, "p_p_lifecycle", "0");
160         }
161 
162         addParam(params, "p_p_state", WindowState.MAXIMIZED);
163         addParam(params, "p_p_mode", PortletMode.VIEW);
164 
165         int x = friendlyURLPath.indexOf(StringPool.SLASH, 1);
166 
167         String[] urlFragments = StringUtil.split(
168             friendlyURLPath.substring(x + 1), StringPool.SLASH);
169 
170         String resourceIdParam = getResourceIdParam(urlFragments[0]);
171 
172         if (urlFragments.length == 1) {
173             addParam(params, "struts_action", "/software_catalog/view");
174             addParam(params, "tabs1", urlFragments[0]);
175         }
176         else if (urlFragments.length == 2) {
177             if (urlFragments[1].equals("new")) {
178                 addParam(
179                     params, "struts_action", getEditAction(urlFragments[0]));
180                 addParam(params, "tabs1", urlFragments[0]);
181             }
182             else if (urlFragments[0].equals("products")) {
183                 addParam(
184                     params,
185                     "struts_action", "/software_catalog/view_product_entry");
186                 addParam(params, "tabs1", urlFragments[0]);
187                 addParam(params, resourceIdParam, urlFragments[1]);
188             }
189         }
190         else if (urlFragments.length == 3) {
191             if (urlFragments[2].equals("edit")) {
192                 addParam(
193                     params, "struts_action", getEditAction(urlFragments[0]));
194                 addParam(params, "tabs1", urlFragments[0]);
195                 addParam(params, resourceIdParam, urlFragments[1]);
196             }
197         }
198         else if (urlFragments.length == 4) {
199             if (urlFragments[3].equals("new")) {
200                 addParam(
201                     params, "struts_action", getEditAction(urlFragments[2]));
202                 addParam(params, "tabs1", urlFragments[0]);
203                 addParam(params, resourceIdParam, urlFragments[1]);
204             }
205         }
206         else if (urlFragments.length == 5) {
207             if (urlFragments[0].equals("products") &&
208                 urlFragments[4].equals("edit")) {
209 
210                 addParam(
211                     params, "struts_action", getEditAction(urlFragments[2]));
212                 addParam(params, "tabs1", urlFragments[0]);
213                 addParam(params, resourceIdParam, urlFragments[1]);
214                 addParam(
215                     params, getResourceIdParam(urlFragments[2]),
216                     urlFragments[3]);
217             }
218         }
219     }
220 
221     protected String getEditAction(String resource) {
222         String action = null;
223 
224         if (resource.equals("my_products") || resource.equals("products")) {
225             action = "edit_product_entry";
226         }
227         else if (resource.equals("versions")) {
228             action = "edit_product_version";
229         }
230         else if (resource.equals("framework_versions")) {
231             action = "edit_framework_version";
232         }
233         else if (resource.equals("licenses")) {
234             action = "edit_license";
235         }
236         else {
237             return null;
238         }
239 
240         return "/software_catalog/" + action;
241     }
242 
243     protected String getResourceIdParam(String resource) {
244         if (resource.equals("my_products") || resource.equals("products")) {
245             return "productEntryId";
246         }
247         else if (resource.equals("versions")) {
248             return "productVersionId";
249         }
250         else if (resource.equals("framework_versions")) {
251             return "frameworkVersionId";
252         }
253         else if (resource.equals("licenses")) {
254             return "licenseId";
255         }
256         else if (resource.equals("discussion")) {
257             return "messageId";
258         }
259         else {
260             return null;
261         }
262     }
263 
264     private static final String _MAPPING = "software_catalog";
265 
266     private static final String _PORTLET_ID = PortletKeys.SOFTWARE_CATALOG;
267 
268 }