1   /**
2    * Copyright (c) 2000-2008 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   */
44  public class SCFriendlyURLMapper extends BaseFriendlyURLMapper {
45  
46      public String getMapping() {
47          return _MAPPING;
48      }
49  
50      public String getPortletId() {
51          return _PORTLET_ID;
52      }
53  
54      public String buildPath(LiferayPortletURL portletURL) {
55          String friendlyURL = null;
56  
57          String tabs1 = portletURL.getParameter("tabs1");
58  
59          String action = GetterUtil.getString(
60              portletURL.getParameter("struts_action"));
61  
62          if (action.equals("/software_catalog/view")) {
63              friendlyURL = "/software_catalog/" + tabs1;
64          }
65          else if (action.equals("/software_catalog/view_product_entry")) {
66              String productEntryId = portletURL.getParameter("productEntryId");
67  
68              friendlyURL = "/software_catalog/products/" + productEntryId;
69  
70              portletURL.addParameterIncludedInPath("productEntryId");
71          }
72          else if (action.equals("/software_catalog/edit_product_entry")) {
73              String productEntryId = portletURL.getParameter("productEntryId");
74  
75              if (Validator.isNotNull(productEntryId)) {
76                  friendlyURL = "/software_catalog/products/" +
77                      productEntryId + "/edit";
78  
79                  portletURL.addParameterIncludedInPath("productEntryId");
80              }
81              else {
82                  friendlyURL = "/software_catalog/products/new";
83              }
84          }
85          else if (action.equals("/software_catalog/edit_product_version")){
86              String productEntryId = portletURL.getParameter("productEntryId");
87              String productVersionId = portletURL.getParameter(
88                  "productVersionId");
89  
90              if (Validator.isNotNull(productVersionId)) {
91                  friendlyURL = "/software_catalog/products/" +
92                      productEntryId + "/versions/" + productVersionId + "/edit";
93  
94                  portletURL.addParameterIncludedInPath("productEntryId");
95                  portletURL.addParameterIncludedInPath("productVersionId");
96              }
97              else {
98                  friendlyURL = "/software_catalog/products/" +
99                      productEntryId + "/versions/new";
100             }
101         }
102         else if (action.equals(
103                     "/software_catalog/edit_framework_version")) {
104 
105             String frameworkVersionId = portletURL.getParameter(
106                 "frameworkVersionId");
107 
108             if (Validator.isNotNull(frameworkVersionId)) {
109                 friendlyURL = "/software_catalog/framework_versions/" +
110                     frameworkVersionId + "/edit";
111 
112                 portletURL.addParameterIncludedInPath("frameworkVersionId");
113             }
114             else {
115                 friendlyURL = "/software_catalog/framework_versions/new";
116             }
117         }
118         else if (action.equals(
119                     "/software_catalog/edit_license")) {
120 
121             String licenseId = portletURL.getParameter("licenseId");
122 
123             if (Validator.isNotNull(licenseId)) {
124                 friendlyURL = "/software_catalog/licenses/" +
125                     licenseId + "/edit";
126 
127                 portletURL.addParameterIncludedInPath("licenseId");
128             }
129             else {
130                 friendlyURL = "/software_catalog/licenses/new";
131             }
132         }
133         else if (action.equals(
134                     "/software_catalog/search")) {
135 
136             friendlyURL = "/software_catalog/search";
137         }
138 
139         if (Validator.isNotNull(friendlyURL)) {
140             portletURL.addParameterIncludedInPath("p_p_id");
141             portletURL.addParameterIncludedInPath("struts_action");
142             portletURL.addParameterIncludedInPath("tabs1");
143         }
144 
145         return friendlyURL;
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 }