1   /**
2    * Copyright (c) 2000-2007 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(String friendlyURLPath, Map params) {
109         params.put("p_p_id", _PORTLET_ID);
110         params.put("p_p_action", "0");
111         params.put("p_p_state", WindowState.MAXIMIZED.toString());
112         params.put("p_p_mode", PortletMode.VIEW.toString());
113 
114         int x = friendlyURLPath.indexOf("/", 1);
115 
116         if ((x + 1) == friendlyURLPath.length()) {
117             addParam(params, "struts_action", "/message_boards/view");
118             addParam(
119                 params, "categoryId",
120                 MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID);
121 
122             return;
123         }
124 
125         int y = friendlyURLPath.indexOf("/", x + 1);
126 
127         if (y == -1) {
128             y = friendlyURLPath.length();
129         }
130 
131         String type = friendlyURLPath.substring(x + 1, y);
132 
133         if (type.equals("category")) {
134             String categoryId =
135                 friendlyURLPath.substring(y + 1, friendlyURLPath.length());
136 
137             addParam(params, "struts_action", "/message_boards/view");
138             addParam(params, "categoryId", categoryId);
139         }
140         else if (type.equals("message")) {
141             String messageId =
142                 friendlyURLPath.substring(y + 1, friendlyURLPath.length());
143 
144             addParam(params, "struts_action", "/message_boards/view_message");
145             addParam(params, "messageId", messageId);
146         }
147         else if (type.equals("my_posts") || type.equals("my_subscriptions") ||
148                  type.equals("recent_posts") || type.equals("statistics") ||
149                  type.equals("banned_users")) {
150 
151             addParam(params, "struts_action", "/message_boards/view");
152             params.put("tabs1", type);
153         }
154     }
155 
156     private static final String _MAPPING = "message_boards";
157 
158     private static final String _PORTLET_ID = PortletKeys.MESSAGE_BOARDS;
159 
160 }