1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.softwarecatalog;
16  
17  import com.liferay.portal.kernel.portlet.BaseFriendlyURLMapper;
18  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
19  import com.liferay.portal.kernel.util.ArrayUtil;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.StringUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.util.PortletKeys;
25  
26  import java.util.Map;
27  
28  import javax.portlet.PortletMode;
29  import javax.portlet.PortletRequest;
30  import javax.portlet.WindowState;
31  
32  /**
33   * <a href="SCFriendlyURLMapper.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Jorge Ferrer
36   */
37  public class SCFriendlyURLMapper extends BaseFriendlyURLMapper {
38  
39      public String buildPath(LiferayPortletURL portletURL) {
40          String friendlyURL = null;
41  
42          if (!portletURL.getLifecycle().equals(PortletRequest.RENDER_PHASE)) {
43               return friendlyURL;
44          }
45  
46          String tabs1 = portletURL.getParameter("tabs1");
47  
48          String action = GetterUtil.getString(
49              portletURL.getParameter("struts_action"));
50  
51          if (action.equals("/software_catalog/view")) {
52              friendlyURL = "/software_catalog/" + tabs1;
53          }
54          else if (action.equals("/software_catalog/view_product_entry")) {
55              String productEntryId = portletURL.getParameter("productEntryId");
56  
57              friendlyURL = "/software_catalog/products/" + productEntryId;
58  
59              portletURL.addParameterIncludedInPath("productEntryId");
60          }
61          else if (action.equals("/software_catalog/edit_product_entry")) {
62              String productEntryId = portletURL.getParameter("productEntryId");
63  
64              if (Validator.isNotNull(productEntryId)) {
65                  friendlyURL = "/software_catalog/products/" +
66                      productEntryId + "/edit";
67  
68                  portletURL.addParameterIncludedInPath("productEntryId");
69              }
70              else {
71                  friendlyURL = "/software_catalog/products/new";
72              }
73          }
74          else if (action.equals("/software_catalog/edit_product_version")) {
75              String productEntryId = portletURL.getParameter("productEntryId");
76              String productVersionId = portletURL.getParameter(
77                  "productVersionId");
78  
79              if (Validator.isNotNull(productVersionId)) {
80                  friendlyURL = "/software_catalog/products/" +
81                      productEntryId + "/versions/" + productVersionId + "/edit";
82  
83                  portletURL.addParameterIncludedInPath("productEntryId");
84                  portletURL.addParameterIncludedInPath("productVersionId");
85              }
86              else {
87                  friendlyURL = "/software_catalog/products/" +
88                      productEntryId + "/versions/new";
89              }
90          }
91          else if (action.equals("/software_catalog/edit_framework_version")) {
92              String frameworkVersionId = portletURL.getParameter(
93                  "frameworkVersionId");
94  
95              if (Validator.isNotNull(frameworkVersionId)) {
96                  friendlyURL = "/software_catalog/framework_versions/" +
97                      frameworkVersionId + "/edit";
98  
99                  portletURL.addParameterIncludedInPath("frameworkVersionId");
100             }
101             else {
102                 friendlyURL = "/software_catalog/framework_versions/new";
103             }
104         }
105         else if (action.equals("/software_catalog/edit_license")) {
106             String licenseId = portletURL.getParameter("licenseId");
107 
108             if (Validator.isNotNull(licenseId)) {
109                 friendlyURL = "/software_catalog/licenses/" +
110                     licenseId + "/edit";
111 
112                 portletURL.addParameterIncludedInPath("licenseId");
113             }
114             else {
115                 friendlyURL = "/software_catalog/licenses/new";
116             }
117         }
118         else if (action.equals("/software_catalog/search")) {
119             friendlyURL = "/software_catalog/search";
120         }
121 
122         if (Validator.isNotNull(friendlyURL)) {
123             WindowState windowState = portletURL.getWindowState();
124 
125             if ((windowState != null) &&
126                 !windowState.equals(WindowState.NORMAL)) {
127 
128                 friendlyURL += StringPool.SLASH + windowState;
129             }
130 
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[]> parameterMap) {
150 
151         addParameter(parameterMap, "p_p_id", _PORTLET_ID);
152 
153         if (!parameterMap.containsKey("p_p_lifecycle")) {
154             addParameter(parameterMap, "p_p_lifecycle", "0");
155         }
156 
157         addParameter(parameterMap, "p_p_mode", PortletMode.VIEW);
158 
159         int x = friendlyURLPath.indexOf(StringPool.SLASH, 1);
160 
161         String[] urlFragments = StringUtil.split(
162             friendlyURLPath.substring(x + 1), StringPool.SLASH);
163 
164         if (friendlyURLPath.indexOf("maximized", x) != -1) {
165             urlFragments = ArrayUtil.remove(urlFragments, "maximized");
166 
167             addParameter(parameterMap, "p_p_state", WindowState.MAXIMIZED);
168         }
169 
170         String resourceIdParam = getResourceIdParam(urlFragments[0]);
171 
172         if (urlFragments.length == 1) {
173             addParameter(
174                 parameterMap, "struts_action", "/software_catalog/view");
175             addParameter(parameterMap, "tabs1", urlFragments[0]);
176         }
177         else if (urlFragments.length == 2) {
178             if (urlFragments[1].equals("new")) {
179                 addParameter(
180                     parameterMap, "struts_action",
181                     getEditAction(urlFragments[0]));
182                 addParameter(parameterMap, "tabs1", urlFragments[0]);
183             }
184             else if (urlFragments[0].equals("products")) {
185                 addParameter(
186                     parameterMap,
187                     "struts_action", "/software_catalog/view_product_entry");
188                 addParameter(parameterMap, "tabs1", urlFragments[0]);
189                 addParameter(parameterMap, resourceIdParam, urlFragments[1]);
190             }
191         }
192         else if (urlFragments.length == 3) {
193             if (urlFragments[2].equals("edit")) {
194                 addParameter(
195                     parameterMap, "struts_action",
196                     getEditAction(urlFragments[0]));
197                 addParameter(parameterMap, "tabs1", urlFragments[0]);
198                 addParameter(parameterMap, resourceIdParam, urlFragments[1]);
199             }
200         }
201         else if (urlFragments.length == 4) {
202             if (urlFragments[3].equals("new")) {
203                 addParameter(
204                     parameterMap, "struts_action",
205                     getEditAction(urlFragments[2]));
206                 addParameter(parameterMap, "tabs1", urlFragments[0]);
207                 addParameter(parameterMap, resourceIdParam, urlFragments[1]);
208             }
209         }
210         else if (urlFragments.length == 5) {
211             if (urlFragments[0].equals("products") &&
212                 urlFragments[4].equals("edit")) {
213 
214                 addParameter(
215                     parameterMap, "struts_action",
216                     getEditAction(urlFragments[2]));
217                 addParameter(parameterMap, "tabs1", urlFragments[0]);
218                 addParameter(parameterMap, resourceIdParam, urlFragments[1]);
219                 addParameter(
220                     parameterMap, getResourceIdParam(urlFragments[2]),
221                     urlFragments[3]);
222             }
223         }
224     }
225 
226     protected String getEditAction(String resource) {
227         String action = null;
228 
229         if (resource.equals("my_products") || resource.equals("products")) {
230             action = "edit_product_entry";
231         }
232         else if (resource.equals("versions")) {
233             action = "edit_product_version";
234         }
235         else if (resource.equals("framework_versions")) {
236             action = "edit_framework_version";
237         }
238         else if (resource.equals("licenses")) {
239             action = "edit_license";
240         }
241         else {
242             return null;
243         }
244 
245         return "/software_catalog/" + action;
246     }
247 
248     protected String getResourceIdParam(String resource) {
249         if (resource.equals("my_products") || resource.equals("products")) {
250             return "productEntryId";
251         }
252         else if (resource.equals("versions")) {
253             return "productVersionId";
254         }
255         else if (resource.equals("framework_versions")) {
256             return "frameworkVersionId";
257         }
258         else if (resource.equals("licenses")) {
259             return "licenseId";
260         }
261         else if (resource.equals("discussion")) {
262             return "messageId";
263         }
264         else {
265             return null;
266         }
267     }
268 
269     private static final String _MAPPING = "software_catalog";
270 
271     private static final String _PORTLET_ID = PortletKeys.SOFTWARE_CATALOG;
272 
273 }