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.blogs.action;
16  
17  import com.liferay.portal.kernel.util.ParamUtil;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.portal.kernel.util.StringUtil;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portal.theme.ThemeDisplay;
22  import com.liferay.portal.util.PortalUtil;
23  import com.liferay.portal.util.WebKeys;
24  import com.liferay.portlet.blogs.NoSuchEntryException;
25  import com.liferay.portlet.blogs.model.BlogsEntry;
26  import com.liferay.portlet.blogs.service.BlogsEntryServiceUtil;
27  
28  import javax.portlet.ActionRequest;
29  import javax.portlet.RenderRequest;
30  
31  import javax.servlet.http.HttpServletRequest;
32  
33  /**
34   * <a href="ActionUtil.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class ActionUtil {
39  
40      public static void getEntry(ActionRequest actionRequest) throws Exception {
41          HttpServletRequest request = PortalUtil.getHttpServletRequest(
42              actionRequest);
43  
44          getEntry(request);
45      }
46  
47      public static void getEntry(RenderRequest renderRequest) throws Exception {
48          HttpServletRequest request = PortalUtil.getHttpServletRequest(
49              renderRequest);
50  
51          getEntry(request);
52      }
53  
54      public static void getEntry(HttpServletRequest request) throws Exception {
55          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
56              WebKeys.THEME_DISPLAY);
57  
58          long entryId = ParamUtil.getLong(request, "entryId");
59  
60          String urlTitle = ParamUtil.getString(request, "urlTitle");
61  
62          BlogsEntry entry = null;
63  
64          if (entryId > 0) {
65              entry = BlogsEntryServiceUtil.getEntry(entryId);
66          }
67          else if (Validator.isNotNull(urlTitle)) {
68              try {
69                  entry = BlogsEntryServiceUtil.getEntry(
70                      themeDisplay.getScopeGroupId(), urlTitle);
71              }
72              catch (NoSuchEntryException nsee) {
73                  if (urlTitle.indexOf(StringPool.UNDERLINE) != -1) {
74  
75                      // Check another URL title for backwards compatibility. See
76                      // LEP-5733.
77  
78                      urlTitle = StringUtil.replace(
79                          urlTitle, StringPool.UNDERLINE, StringPool.DASH);
80  
81                      entry = BlogsEntryServiceUtil.getEntry(
82                          themeDisplay.getScopeGroupId(), urlTitle);
83                  }
84                  else {
85                      throw nsee;
86                  }
87              }
88          }
89  
90          request.setAttribute(WebKeys.BLOGS_ENTRY, entry);
91      }
92  
93  }