1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class MBFriendlyURLMapper extends BaseFriendlyURLMapper {
47  
48      public String buildPath(LiferayPortletURL portletURL) {
49          String friendlyURLPath = null;
50  
51          String tabs1 = GetterUtil.getString(portletURL.getParameter("tabs1"));
52          String tabs2 = GetterUtil.getString(portletURL.getParameter("tabs2"));
53  
54          if (Validator.isNotNull(tabs2)) {
55              return null;
56          }
57  
58          String strutsAction = GetterUtil.getString(
59              portletURL.getParameter("struts_action"));
60  
61          if (strutsAction.equals("/message_boards/search")) {
62              friendlyURLPath = "/message_boards/search";
63          }
64          else if (strutsAction.equals("/message_boards/view")) {
65              String categoryId = GetterUtil.getString(
66                  portletURL.getParameter("categoryId"));
67  
68              if (Validator.isNotNull(categoryId) && !categoryId.equals("0")) {
69                  friendlyURLPath = "/message_boards/category/" + categoryId;
70  
71                  portletURL.addParameterIncludedInPath("categoryId");
72              }
73              else {
74                  friendlyURLPath = "/message_boards";
75  
76                  if (Validator.isNotNull(tabs1) && !tabs1.equals("categories")) {
77                      friendlyURLPath += StringPool.SLASH + tabs1;
78                  }
79  
80                  portletURL.addParameterIncludedInPath("tabs1");
81  
82                  if (categoryId.equals("0")) {
83                      portletURL.addParameterIncludedInPath("categoryId");
84                  }
85              }
86          }
87          else if (strutsAction.equals("/message_boards/view_message")) {
88              String messageId = portletURL.getParameter("messageId");
89  
90              if (Validator.isNotNull(messageId)) {
91                  friendlyURLPath = "/message_boards/message/" + messageId;
92  
93                  portletURL.addParameterIncludedInPath("messageId");
94              }
95          }
96          else {
97              if (_log.isWarnEnabled()) {
98                  _log.warn(
99                      "Struts action " + strutsAction +
100                         " does not have a friendly URL path ");
101             }
102         }
103 
104         if (Validator.isNotNull(friendlyURLPath)) {
105             WindowState windowState = portletURL.getWindowState();
106 
107             if (!windowState.equals(WindowState.NORMAL)) {
108                 friendlyURLPath += StringPool.SLASH + windowState;
109             }
110 
111             portletURL.addParameterIncludedInPath("p_p_id");
112 
113             portletURL.addParameterIncludedInPath("struts_action");
114         }
115 
116         return friendlyURLPath;
117     }
118 
119     public String getMapping() {
120         return _MAPPING;
121     }
122 
123     public String getPortletId() {
124         return _PORTLET_ID;
125     }
126 
127     public void populateParams(
128         String friendlyURLPath, Map<String, String[]> params) {
129 
130         addParam(params, "p_p_id", _PORTLET_ID);
131         addParam(params, "p_p_lifecycle", "0");
132         addParam(params, "p_p_mode", PortletMode.VIEW);
133 
134         int x = friendlyURLPath.indexOf("/", 1);
135 
136         if ((x + 1) == friendlyURLPath.length()) {
137             addParam(params, "struts_action", "/message_boards/view");
138             addParam(
139                 params, "categoryId",
140                 MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID);
141 
142             return;
143         }
144 
145         int y = friendlyURLPath.indexOf("/", x + 1);
146 
147         if (y == -1) {
148             y = friendlyURLPath.length();
149         }
150 
151         int z = friendlyURLPath.indexOf("/", y + 1);
152 
153         if (z == -1) {
154             z = friendlyURLPath.length();
155         }
156 
157         String type = friendlyURLPath.substring(x + 1, y);
158 
159         if (type.equals("category")) {
160             String categoryId =
161                 friendlyURLPath.substring(y + 1, z);
162 
163             addParam(params, "struts_action", "/message_boards/view");
164             addParam(params, "categoryId", categoryId);
165         }
166         else if (type.equals("message")) {
167             String messageId =
168                 friendlyURLPath.substring(y + 1, z);
169 
170             addParam(params, "struts_action", "/message_boards/view_message");
171             addParam(params, "messageId", messageId);
172         }
173         else if (type.equals("my_posts") || type.equals("my_subscriptions") ||
174                  type.equals("recent_posts") || type.equals("statistics") ||
175                  type.equals("banned_users")) {
176 
177             addParam(params, "struts_action", "/message_boards/view");
178             addParam(params, "tabs1", type);
179         }
180         else if (type.equals("search")) {
181             addParam(params, "struts_action", "/message_boards/search");
182             addParam(params, "tabs1", "category");
183         }
184 
185         if (friendlyURLPath.indexOf("maximized", x) != -1) {
186             addParam(params, "p_p_state", WindowState.MAXIMIZED);
187         }
188     }
189 
190     private static final String _MAPPING = "message_boards";
191 
192     private static final String _PORTLET_ID = PortletKeys.MESSAGE_BOARDS;
193 
194     private static Log _log = LogFactoryUtil.getLog(MBFriendlyURLMapper.class);
195 
196 }