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