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