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.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.impl.MBCategoryImpl;
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 tabs1 = GetterUtil.getString(portletURL.getParameter("tabs1"));
44          String tabs2 = GetterUtil.getString(portletURL.getParameter("tabs2"));
45  
46          if (Validator.isNotNull(tabs2)) {
47              return null;
48          }
49  
50          String strutsAction = GetterUtil.getString(
51              portletURL.getParameter("struts_action"));
52  
53          if (strutsAction.equals("/message_boards/search")) {
54              friendlyURLPath = "/message_boards/search";
55          }
56          else if (strutsAction.equals("/message_boards/view")) {
57              String categoryId = GetterUtil.getString(
58                  portletURL.getParameter("categoryId"));
59  
60              if (Validator.isNotNull(categoryId) && !categoryId.equals("0")) {
61                  friendlyURLPath = "/message_boards/category/" + categoryId;
62  
63                  portletURL.addParameterIncludedInPath("categoryId");
64              }
65              else {
66                  friendlyURLPath = "/message_boards";
67  
68                  if (Validator.isNotNull(tabs1) && !tabs1.equals("categories")) {
69                      friendlyURLPath += StringPool.SLASH + tabs1;
70                  }
71  
72                  portletURL.addParameterIncludedInPath("tabs1");
73  
74                  if (categoryId.equals("0")) {
75                      portletURL.addParameterIncludedInPath("categoryId");
76                  }
77              }
78          }
79          else if (strutsAction.equals("/message_boards/view_message")) {
80              String messageId = portletURL.getParameter("messageId");
81  
82              if (Validator.isNotNull(messageId)) {
83                  friendlyURLPath = "/message_boards/message/" + messageId;
84  
85                  portletURL.addParameterIncludedInPath("messageId");
86              }
87          }
88          else {
89              if (_log.isWarnEnabled()) {
90                  _log.warn(
91                      "Struts action " + strutsAction +
92                          " does not have a friendly URL path ");
93              }
94          }
95  
96          if (Validator.isNotNull(friendlyURLPath)) {
97              WindowState windowState = portletURL.getWindowState();
98  
99              if ((windowState != null) &&
100                 !windowState.equals(WindowState.NORMAL)) {
101 
102                 friendlyURLPath += StringPool.SLASH + windowState;
103             }
104 
105             portletURL.addParameterIncludedInPath("p_p_id");
106 
107             portletURL.addParameterIncludedInPath("struts_action");
108         }
109 
110         return friendlyURLPath;
111     }
112 
113     public String getMapping() {
114         return _MAPPING;
115     }
116 
117     public String getPortletId() {
118         return _PORTLET_ID;
119     }
120 
121     public void populateParams(
122         String friendlyURLPath, Map<String, String[]> parameterMap) {
123 
124         addParameter(parameterMap, "p_p_id", _PORTLET_ID);
125         addParameter(parameterMap, "p_p_lifecycle", "0");
126         addParameter(parameterMap, "p_p_mode", PortletMode.VIEW);
127 
128         int x = friendlyURLPath.indexOf("/", 1);
129 
130         if ((x + 1) == friendlyURLPath.length()) {
131             addParameter(parameterMap, "struts_action", "/message_boards/view");
132             addParameter(
133                 parameterMap, "categoryId",
134                 MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID);
135 
136             return;
137         }
138 
139         int y = friendlyURLPath.indexOf("/", x + 1);
140 
141         if (y == -1) {
142             y = friendlyURLPath.length();
143         }
144 
145         int z = friendlyURLPath.indexOf("/", y + 1);
146 
147         if (z == -1) {
148             z = friendlyURLPath.length();
149         }
150 
151         String type = friendlyURLPath.substring(x + 1, y);
152 
153         if (type.equals("category")) {
154             String categoryId =
155                 friendlyURLPath.substring(y + 1, z);
156 
157             addParameter(parameterMap, "struts_action", "/message_boards/view");
158             addParameter(parameterMap, "categoryId", categoryId);
159         }
160         else if (type.equals("message")) {
161             String messageId =
162                 friendlyURLPath.substring(y + 1, z);
163 
164             addParameter(
165                 parameterMap, "struts_action", "/message_boards/view_message");
166             addParameter(parameterMap, "messageId", messageId);
167         }
168         else if (type.equals("my_posts") || type.equals("my_subscriptions") ||
169                  type.equals("recent_posts") || type.equals("statistics") ||
170                  type.equals("banned_users")) {
171 
172             addParameter(parameterMap, "struts_action", "/message_boards/view");
173             addParameter(parameterMap, "tabs1", type);
174         }
175         else if (type.equals("search")) {
176             addParameter(
177                 parameterMap, "struts_action", "/message_boards/search");
178             addParameter(parameterMap, "tabs1", "categories");
179         }
180 
181         if (friendlyURLPath.indexOf("maximized", x) != -1) {
182             addParameter(parameterMap, "p_p_state", WindowState.MAXIMIZED);
183         }
184     }
185 
186     private static final String _MAPPING = "message_boards";
187 
188     private static final String _PORTLET_ID = PortletKeys.MESSAGE_BOARDS;
189 
190     private static Log _log = LogFactoryUtil.getLog(MBFriendlyURLMapper.class);
191 
192 }