001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.blogs.action;
016    
017    import com.liferay.portal.kernel.util.CharPool;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.theme.ThemeDisplay;
022    import com.liferay.portal.util.PortalUtil;
023    import com.liferay.portal.util.WebKeys;
024    import com.liferay.portlet.blogs.NoSuchEntryException;
025    import com.liferay.portlet.blogs.model.BlogsEntry;
026    import com.liferay.portlet.blogs.service.BlogsEntryServiceUtil;
027    
028    import javax.portlet.PortletRequest;
029    
030    import javax.servlet.http.HttpServletRequest;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class ActionUtil {
036    
037            public static void getEntry(HttpServletRequest request) throws Exception {
038                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
039                            WebKeys.THEME_DISPLAY);
040    
041                    long entryId = ParamUtil.getLong(request, "entryId");
042    
043                    String urlTitle = ParamUtil.getString(request, "urlTitle");
044    
045                    BlogsEntry entry = null;
046    
047                    if (entryId > 0) {
048                            entry = BlogsEntryServiceUtil.getEntry(entryId);
049                    }
050                    else if (Validator.isNotNull(urlTitle)) {
051                            try {
052                                    entry = BlogsEntryServiceUtil.getEntry(
053                                            themeDisplay.getScopeGroupId(), urlTitle);
054                            }
055                            catch (NoSuchEntryException nsee) {
056                                    if (urlTitle.indexOf(CharPool.UNDERLINE) != -1) {
057    
058                                            // Check another URL title for backwards compatibility. See
059                                            // LEP-5733.
060    
061                                            urlTitle = StringUtil.replace(
062                                                    urlTitle, CharPool.UNDERLINE, CharPool.DASH);
063    
064                                            entry = BlogsEntryServiceUtil.getEntry(
065                                                    themeDisplay.getScopeGroupId(), urlTitle);
066                                    }
067                                    else {
068                                            throw nsee;
069                                    }
070                            }
071                    }
072    
073                    if ((entry != null) && entry.isInTrash()) {
074                            throw new NoSuchEntryException("{entryId=" + entryId + "}");
075                    }
076    
077                    request.setAttribute(WebKeys.BLOGS_ENTRY, entry);
078            }
079    
080            public static void getEntry(PortletRequest portletRequest)
081                    throws Exception {
082    
083                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
084                            portletRequest);
085    
086                    getEntry(request);
087            }
088    
089    }