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