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  /**
38   * <a href="MBFriendlyURLMapper.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   * @author Jorge Ferrer
42   *
43   */
44  public class MBFriendlyURLMapper extends BaseFriendlyURLMapper {
45  
46      public String getMapping() {
47          return _MAPPING;
48      }
49  
50      public String getPortletId() {
51          return _PORTLET_ID;
52      }
53  
54      public String buildPath(LiferayPortletURL portletURL) {
55          String friendlyURLPath = null;
56  
57          String tabs1 = GetterUtil.getString(portletURL.getParameter("tabs1"));
58          String tabs2 = GetterUtil.getString(portletURL.getParameter("tabs2"));
59  
60          if (Validator.isNotNull(tabs2)) {
61              return null;
62          }
63  
64          String strutsAction = GetterUtil.getString(
65              portletURL.getParameter("struts_action"));
66  
67          if (strutsAction.equals("/message_boards/view")) {
68              String categoryId = GetterUtil.getString(
69                  portletURL.getParameter("categoryId"));
70  
71              if (Validator.isNotNull(categoryId) && !categoryId.equals("0")) {
72                  friendlyURLPath = "/message_boards/category/" + categoryId;
73  
74                  portletURL.addParameterIncludedInPath("categoryId");
75              }
76              else {
77                  friendlyURLPath = "/message_boards";
78  
79                  if (Validator.isNotNull(tabs1) && !tabs1.equals("categories")) {
80                      friendlyURLPath += "/" + tabs1;
81                  }
82  
83                  portletURL.addParameterIncludedInPath("tabs1");
84  
85                  if (categoryId.equals("0")) {
86                      portletURL.addParameterIncludedInPath("categoryId");
87                  }
88              }
89          }
90          else if (strutsAction.equals("/message_boards/view_message")) {
91              String messageId = portletURL.getParameter("messageId");
92  
93              if (Validator.isNotNull(messageId)) {
94                  friendlyURLPath = "/message_boards/message/" + messageId;
95  
96                  portletURL.addParameterIncludedInPath("messageId");
97              }
98          }
99  
100         if (Validator.isNotNull(friendlyURLPath)) {
101             portletURL.addParameterIncludedInPath("p_p_id");
102             portletURL.addParameterIncludedInPath("struts_action");
103         }
104 
105         return friendlyURLPath;
106     }
107 
108     public void populateParams(
109         String friendlyURLPath, Map<String, String[]> params) {
110 
111         addParam(params, "p_p_id", _PORTLET_ID);
112         addParam(params, "p_p_lifecycle", "0");
113         addParam(params, "p_p_state", WindowState.MAXIMIZED);
114         addParam(params, "p_p_mode", PortletMode.VIEW);
115 
116         int x = friendlyURLPath.indexOf("/", 1);
117 
118         if ((x + 1) == friendlyURLPath.length()) {
119             addParam(params, "struts_action", "/message_boards/view");
120             addParam(
121                 params, "categoryId",
122                 MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID);
123 
124             return;
125         }
126 
127         int y = friendlyURLPath.indexOf("/", x + 1);
128 
129         if (y == -1) {
130             y = friendlyURLPath.length();
131         }
132 
133         String type = friendlyURLPath.substring(x + 1, y);
134 
135         if (type.equals("category")) {
136             String categoryId =
137                 friendlyURLPath.substring(y + 1, friendlyURLPath.length());
138 
139             addParam(params, "struts_action", "/message_boards/view");
140             addParam(params, "categoryId", categoryId);
141         }
142         else if (type.equals("message")) {
143             String messageId =
144                 friendlyURLPath.substring(y + 1, friendlyURLPath.length());
145 
146             addParam(params, "struts_action", "/message_boards/view_message");
147             addParam(params, "messageId", messageId);
148         }
149         else if (type.equals("my_posts") || type.equals("my_subscriptions") ||
150                  type.equals("recent_posts") || type.equals("statistics") ||
151                  type.equals("banned_users")) {
152 
153             addParam(params, "struts_action", "/message_boards/view");
154             addParam(params, "tabs1", type);
155         }
156     }
157 
158     private static final String _MAPPING = "message_boards";
159 
160     private static final String _PORTLET_ID = PortletKeys.MESSAGE_BOARDS;
161 
162 }