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