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