1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.messageboards;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.portlet.BaseFriendlyURLMapper;
20  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
21  import com.liferay.portal.kernel.util.GetterUtil;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.util.PortletKeys;
25  import com.liferay.portlet.messageboards.model.MBCategoryConstants;
26  
27  import java.util.Map;
28  
29  import javax.portlet.PortletMode;
30  import javax.portlet.WindowState;
31  
32  /**
33   * <a href="MBFriendlyURLMapper.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   * @author Jorge Ferrer
37   */
38  public class MBFriendlyURLMapper extends BaseFriendlyURLMapper {
39  
40      public String buildPath(LiferayPortletURL portletURL) {
41          String friendlyURLPath = null;
42  
43          String topLink = GetterUtil.getString(
44              portletURL.getParameter("topLink"));
45  
46          String strutsAction = GetterUtil.getString(
47              portletURL.getParameter("struts_action"));
48  
49          if (strutsAction.equals("/message_boards/search")) {
50              friendlyURLPath = "/message_boards/search";
51          }
52          else if (strutsAction.equals("/message_boards/view")) {
53              String categoryId = GetterUtil.getString(
54                  portletURL.getParameter("mbCategoryId"));
55  
56              if (Validator.isNotNull(categoryId) && !categoryId.equals("0")) {
57                  friendlyURLPath = "/message_boards/category/" + categoryId;
58  
59                  portletURL.addParameterIncludedInPath("mbCategoryId");
60              }
61              else {
62                  friendlyURLPath = "/message_boards";
63  
64                  if (Validator.isNotNull(topLink) &&
65                      !topLink.equals("message-boards-home")) {
66  
67                      friendlyURLPath += StringPool.SLASH + topLink;
68                  }
69  
70                  portletURL.addParameterIncludedInPath("topLink");
71  
72                  if (categoryId.equals("0")) {
73                      portletURL.addParameterIncludedInPath("mbCategoryId");
74                  }
75              }
76          }
77          else if (strutsAction.equals("/message_boards/view_message")) {
78              String messageId = portletURL.getParameter("messageId");
79  
80              if (Validator.isNotNull(messageId)) {
81                  friendlyURLPath = "/message_boards/message/" + messageId;
82  
83                  portletURL.addParameterIncludedInPath("messageId");
84              }
85          }
86          else {
87              if (_log.isWarnEnabled()) {
88                  _log.warn(
89                      "Struts action " + strutsAction +
90                          " does not have a friendly URL path ");
91              }
92          }
93  
94          if (Validator.isNotNull(friendlyURLPath)) {
95              WindowState windowState = portletURL.getWindowState();
96  
97              if (!windowState.equals(WindowState.NORMAL)) {
98                  friendlyURLPath += StringPool.SLASH + windowState;
99              }
100 
101             portletURL.addParameterIncludedInPath("p_p_id");
102 
103             portletURL.addParameterIncludedInPath("struts_action");
104         }
105 
106         return friendlyURLPath;
107     }
108 
109     public String getMapping() {
110         return _MAPPING;
111     }
112 
113     public String getPortletId() {
114         return _PORTLET_ID;
115     }
116 
117     public void populateParams(
118         String friendlyURLPath, Map<String, String[]> params) {
119 
120         addParam(params, "p_p_id", _PORTLET_ID);
121         addParam(params, "p_p_lifecycle", "0");
122         addParam(params, "p_p_mode", PortletMode.VIEW);
123 
124         int x = friendlyURLPath.indexOf("/", 1);
125 
126         if ((x + 1) == friendlyURLPath.length()) {
127             addParam(params, "struts_action", "/message_boards/view");
128             addParam(
129                 params, "mbCategoryId",
130                 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
131 
132             return;
133         }
134 
135         int y = friendlyURLPath.indexOf("/", x + 1);
136 
137         if (y == -1) {
138             y = friendlyURLPath.length();
139         }
140 
141         int z = friendlyURLPath.indexOf("/", y + 1);
142 
143         if (z == -1) {
144             z = friendlyURLPath.length();
145         }
146 
147         String type = friendlyURLPath.substring(x + 1, y);
148 
149         if (type.equals("category")) {
150             String categoryId =
151                 friendlyURLPath.substring(y + 1, z);
152 
153             addParam(params, "struts_action", "/message_boards/view");
154             addParam(params, "mbCategoryId", categoryId);
155         }
156         else if (type.equals("message")) {
157             String messageId =
158                 friendlyURLPath.substring(y + 1, z);
159 
160             addParam(params, "struts_action", "/message_boards/view_message");
161             addParam(params, "messageId", messageId);
162         }
163         else if (type.equals("my-posts") || type.equals("my-subscriptions") ||
164                  type.equals("recent-posts") || type.equals("statistics") ||
165                  type.equals("banned-users")) {
166 
167             addParam(params, "struts_action", "/message_boards/view");
168             addParam(params, "topLink", type);
169         }
170         else if (type.equals("search")) {
171             addParam(params, "struts_action", "/message_boards/search");
172             addParam(params, "topLink", "message-boards-home");
173         }
174 
175         if (friendlyURLPath.indexOf("maximized", x) != -1) {
176             addParam(params, "p_p_state", WindowState.MAXIMIZED);
177         }
178     }
179 
180     private static final String _MAPPING = "message_boards";
181 
182     private static final String _PORTLET_ID = PortletKeys.MESSAGE_BOARDS;
183 
184     private static Log _log = LogFactoryUtil.getLog(MBFriendlyURLMapper.class);
185 
186 }