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