1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.messageboards;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.portlet.BaseFriendlyURLMapper;
25  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.StringPool;
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 buildPath(LiferayPortletURL portletURL) {
47          String friendlyURLPath = null;
48  
49          String tabs1 = GetterUtil.getString(portletURL.getParameter("tabs1"));
50          String tabs2 = GetterUtil.getString(portletURL.getParameter("tabs2"));
51  
52          if (Validator.isNotNull(tabs2)) {
53              return null;
54          }
55  
56          String strutsAction = GetterUtil.getString(
57              portletURL.getParameter("struts_action"));
58  
59          if (strutsAction.equals("/message_boards/search")) {
60              friendlyURLPath = "/message_boards/search";
61          }
62          else if (strutsAction.equals("/message_boards/view")) {
63              String categoryId = GetterUtil.getString(
64                  portletURL.getParameter("categoryId"));
65  
66              if (Validator.isNotNull(categoryId) && !categoryId.equals("0")) {
67                  friendlyURLPath = "/message_boards/category/" + categoryId;
68  
69                  portletURL.addParameterIncludedInPath("categoryId");
70              }
71              else {
72                  friendlyURLPath = "/message_boards";
73  
74                  if (Validator.isNotNull(tabs1) && !tabs1.equals("categories")) {
75                      friendlyURLPath += StringPool.SLASH + tabs1;
76                  }
77  
78                  portletURL.addParameterIncludedInPath("tabs1");
79  
80                  if (categoryId.equals("0")) {
81                      portletURL.addParameterIncludedInPath("categoryId");
82                  }
83              }
84          }
85          else if (strutsAction.equals("/message_boards/view_message")) {
86              String messageId = portletURL.getParameter("messageId");
87  
88              if (Validator.isNotNull(messageId)) {
89                  friendlyURLPath = "/message_boards/message/" + messageId;
90  
91                  portletURL.addParameterIncludedInPath("messageId");
92              }
93          }
94          else {
95              if (_log.isWarnEnabled()) {
96                  _log.warn(
97                      "Struts action " + strutsAction +
98                          " does not have a friendly URL path ");
99              }
100         }
101 
102         if (Validator.isNotNull(friendlyURLPath)) {
103             WindowState windowState = portletURL.getWindowState();
104 
105             if (!windowState.equals(WindowState.NORMAL)) {
106                 friendlyURLPath += StringPool.SLASH + windowState;
107             }
108 
109             portletURL.addParameterIncludedInPath("p_p_id");
110 
111             portletURL.addParameterIncludedInPath("struts_action");
112         }
113 
114         return friendlyURLPath;
115     }
116 
117     public String getMapping() {
118         return _MAPPING;
119     }
120 
121     public String getPortletId() {
122         return _PORTLET_ID;
123     }
124 
125     public void populateParams(
126         String friendlyURLPath, Map<String, String[]> params) {
127 
128         addParam(params, "p_p_id", _PORTLET_ID);
129         addParam(params, "p_p_lifecycle", "0");
130         addParam(params, "p_p_mode", PortletMode.VIEW);
131 
132         int x = friendlyURLPath.indexOf("/", 1);
133 
134         if ((x + 1) == friendlyURLPath.length()) {
135             addParam(params, "struts_action", "/message_boards/view");
136             addParam(
137                 params, "categoryId",
138                 MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID);
139 
140             return;
141         }
142 
143         int y = friendlyURLPath.indexOf("/", x + 1);
144 
145         if (y == -1) {
146             y = friendlyURLPath.length();
147         }
148 
149         int z = friendlyURLPath.indexOf("/", y + 1);
150 
151         if (z == -1) {
152             z = friendlyURLPath.length();
153         }
154 
155         String type = friendlyURLPath.substring(x + 1, y);
156 
157         if (type.equals("category")) {
158             String categoryId =
159                 friendlyURLPath.substring(y + 1, z);
160 
161             addParam(params, "struts_action", "/message_boards/view");
162             addParam(params, "categoryId", categoryId);
163         }
164         else if (type.equals("message")) {
165             String messageId =
166                 friendlyURLPath.substring(y + 1, z);
167 
168             addParam(params, "struts_action", "/message_boards/view_message");
169             addParam(params, "messageId", messageId);
170         }
171         else if (type.equals("my_posts") || type.equals("my_subscriptions") ||
172                  type.equals("recent_posts") || type.equals("statistics") ||
173                  type.equals("banned_users")) {
174 
175             addParam(params, "struts_action", "/message_boards/view");
176             addParam(params, "tabs1", type);
177         }
178         else if (type.equals("search")) {
179             addParam(params, "struts_action", "/message_boards/search");
180             addParam(params, "tabs1", "category");
181         }
182 
183         if (friendlyURLPath.indexOf("maximized", x) != -1) {
184             addParam(params, "p_p_state", WindowState.MAXIMIZED);
185         }
186     }
187 
188     private static final String _MAPPING = "message_boards";
189 
190     private static final String _PORTLET_ID = PortletKeys.MESSAGE_BOARDS;
191 
192     private static Log _log = LogFactoryUtil.getLog(MBFriendlyURLMapper.class);
193 
194 }