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