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