1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.MBCategoryConstants;
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 topLink = GetterUtil.getString(
44              portletURL.getParameter("topLink"));
45  
46          String strutsAction = GetterUtil.getString(
47              portletURL.getParameter("struts_action"));
48  
49          if (strutsAction.equals("/message_boards/search")) {
50              friendlyURLPath = "/message_boards/search";
51          }
52          else if (strutsAction.equals("/message_boards/view")) {
53              String categoryId = GetterUtil.getString(
54                  portletURL.getParameter("mbCategoryId"));
55  
56              if (Validator.isNotNull(categoryId) && !categoryId.equals("0")) {
57                  friendlyURLPath = "/message_boards/category/" + categoryId;
58  
59                  portletURL.addParameterIncludedInPath("mbCategoryId");
60              }
61              else {
62                  friendlyURLPath = "/message_boards";
63  
64                  if (Validator.isNotNull(topLink) &&
65                      !topLink.equals("message-boards-home")) {
66  
67                      friendlyURLPath += StringPool.SLASH + topLink;
68                  }
69  
70                  portletURL.addParameterIncludedInPath("topLink");
71  
72                  if (categoryId.equals("0")) {
73                      portletURL.addParameterIncludedInPath("mbCategoryId");
74                  }
75              }
76          }
77          else if (strutsAction.equals("/message_boards/view_message")) {
78              String messageId = portletURL.getParameter("messageId");
79  
80              if (Validator.isNotNull(messageId)) {
81                  friendlyURLPath = "/message_boards/message/" + messageId;
82  
83                  portletURL.addParameterIncludedInPath("messageId");
84              }
85          }
86          else {
87              if (_log.isWarnEnabled()) {
88                  _log.warn(
89                      "Struts action " + strutsAction +
90                          " does not have a friendly URL path ");
91              }
92          }
93  
94          if (Validator.isNotNull(friendlyURLPath)) {
95              WindowState windowState = portletURL.getWindowState();
96  
97              if (!windowState.equals(WindowState.NORMAL)) {
98                  friendlyURLPath += StringPool.SLASH + windowState;
99              }
100 
101             portletURL.addParameterIncludedInPath("p_p_id");
102 
103             portletURL.addParameterIncludedInPath("struts_action");
104         }
105 
106         return friendlyURLPath;
107     }
108 
109     public String getMapping() {
110         return _MAPPING;
111     }
112 
113     public String getPortletId() {
114         return _PORTLET_ID;
115     }
116 
117     public void populateParams(
118         String friendlyURLPath, Map<String, String[]> parameterMap,
119         Map<String, Object> requestContext) {
120 
121         addParameter(parameterMap, "p_p_id", _PORTLET_ID);
122         addParameter(parameterMap, "p_p_lifecycle", "0");
123         addParameter(parameterMap, "p_p_mode", PortletMode.VIEW);
124 
125         int x = friendlyURLPath.indexOf("/", 1);
126 
127         if ((x + 1) == friendlyURLPath.length()) {
128             addParameter(parameterMap, "struts_action", "/message_boards/view");
129             addParameter(
130                 parameterMap, "mbCategoryId",
131                 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
132 
133             return;
134         }
135 
136         int y = friendlyURLPath.indexOf("/", x + 1);
137 
138         if (y == -1) {
139             y = friendlyURLPath.length();
140         }
141 
142         int z = friendlyURLPath.indexOf("/", y + 1);
143 
144         if (z == -1) {
145             z = friendlyURLPath.length();
146         }
147 
148         String type = friendlyURLPath.substring(x + 1, y);
149 
150         if (type.equals("category")) {
151             String categoryId =
152                 friendlyURLPath.substring(y + 1, z);
153 
154             addParameter(parameterMap, "struts_action", "/message_boards/view");
155             addParameter(parameterMap, "mbCategoryId", categoryId);
156         }
157         else if (type.equals("message")) {
158             String messageId =
159                 friendlyURLPath.substring(y + 1, z);
160 
161             addParameter(
162                 parameterMap, "struts_action", "/message_boards/view_message");
163             addParameter(parameterMap, "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             addParameter(parameterMap, "struts_action", "/message_boards/view");
170             addParameter(parameterMap, "topLink", type);
171         }
172         else if (type.equals("search")) {
173             addParameter(
174                 parameterMap, "struts_action", "/message_boards/search");
175             addParameter(parameterMap, "topLink", "message-boards-home");
176         }
177 
178         if (friendlyURLPath.indexOf("maximized", x) != -1) {
179             addParameter(parameterMap, "p_p_state", WindowState.MAXIMIZED);
180         }
181     }
182 
183     private static final String _MAPPING = "message_boards";
184 
185     private static final String _PORTLET_ID = PortletKeys.MESSAGE_BOARDS;
186 
187     private static Log _log = LogFactoryUtil.getLog(MBFriendlyURLMapper.class);
188 
189 }