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