001    /**
002     * Copyright (c) 2000-2010 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.NoSuchLayoutException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.model.LayoutConstants;
025    import com.liferay.portal.model.LayoutTypePortlet;
026    import com.liferay.portal.service.LayoutLocalServiceUtil;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.PortletKeys;
029    import com.liferay.portlet.PortletURLImpl;
030    import com.liferay.portlet.blogs.model.BlogsEntry;
031    import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
032    
033    import javax.portlet.PortletMode;
034    import javax.portlet.PortletRequest;
035    import javax.portlet.PortletURL;
036    import javax.portlet.WindowState;
037    
038    import javax.servlet.http.HttpServletRequest;
039    import javax.servlet.http.HttpServletResponse;
040    
041    import org.apache.struts.action.Action;
042    import org.apache.struts.action.ActionForm;
043    import org.apache.struts.action.ActionForward;
044    import org.apache.struts.action.ActionMapping;
045    
046    /**
047     * @author Brian Wing Shun Chan
048     */
049    public class FindEntryAction extends Action {
050    
051            public ActionForward execute(
052                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
053                            HttpServletResponse response)
054                    throws Exception {
055    
056                    try {
057                            long plid = ParamUtil.getLong(request, "p_l_id");
058                            String redirect = ParamUtil.getString(request, "redirect");
059                            long entryId = ParamUtil.getLong(request, "entryId");
060                            boolean showAllEntries = ParamUtil.getBoolean(
061                                    request, "showAllEntries");
062    
063                            String portletId = PortletKeys.BLOGS;
064    
065                            try {
066                                    plid = getPlid(plid, portletId, entryId);
067                            }
068                            catch (NoSuchLayoutException nsle) {
069                                    portletId = PortletKeys.BLOGS_AGGREGATOR;
070    
071                                    plid = getPlid(plid, portletId, entryId);
072                            }
073    
074                            String urlTitle = getUrlTitle(entryId);
075    
076                            PortletURL portletURL = new PortletURLImpl(
077                                    request, portletId, plid, PortletRequest.RENDER_PHASE);
078    
079                            portletURL.setWindowState(WindowState.NORMAL);
080                            portletURL.setPortletMode(PortletMode.VIEW);
081    
082                            if (Validator.isNotNull(redirect)) {
083                                    portletURL.setParameter("redirect", redirect);
084                            }
085    
086                            String strutsAction = getStrutsAction(portletId, showAllEntries);
087    
088                            portletURL.setParameter("struts_action", strutsAction);
089    
090                            if (!showAllEntries) {
091                                    if (Validator.isNotNull(urlTitle)) {
092                                            portletURL.setParameter("urlTitle", urlTitle);
093                                    }
094                                    else {
095                                            portletURL.setParameter("entryId", String.valueOf(entryId));
096                                    }
097                            }
098    
099                            response.sendRedirect(portletURL.toString());
100    
101                            return null;
102                    }
103                    catch (Exception e) {
104                            String noSuchEntryRedirect = ParamUtil.getString(
105                                    request, "noSuchEntryRedirect");
106    
107                            if (e.getClass().equals(NoSuchLayoutException.class) &&
108                                    Validator.isNotNull(noSuchEntryRedirect)) {
109    
110                                    response.sendRedirect(noSuchEntryRedirect);
111                            }
112                            else {
113                                    PortalUtil.sendError(e, request, response);
114                            }
115    
116                            return null;
117                    }
118            }
119    
120            protected long getPlid(long plid, String portletId, long entryId)
121                    throws Exception {
122    
123                    if (plid != LayoutConstants.DEFAULT_PLID) {
124                            try {
125                                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
126    
127                                    LayoutTypePortlet layoutTypePortlet =
128                                            (LayoutTypePortlet)layout.getLayoutType();
129    
130                                    if (layoutTypePortlet.hasPortletId(portletId)) {
131                                            return plid;
132                                    }
133                            }
134                            catch (NoSuchLayoutException nsle) {
135                            }
136                    }
137    
138                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
139    
140                    plid = PortalUtil.getPlidFromPortletId(entry.getGroupId(), portletId);
141    
142                    if (plid != LayoutConstants.DEFAULT_PLID) {
143                            return plid;
144                    }
145                    else {
146                            throw new NoSuchLayoutException(
147                                    "No page was found with the Blogs portlet.");
148                    }
149            }
150    
151            protected String getStrutsAction(String portletId, boolean showAllEntries) {
152                    String strutsAction = StringPool.BLANK;
153    
154                    if (portletId.equals(PortletKeys.BLOGS)) {
155                            strutsAction = "/blogs";
156                    }
157                    else {
158                            strutsAction = "/blogs_aggregator";
159                    }
160    
161                    if (showAllEntries) {
162                            strutsAction += "/view";
163                    }
164                    else {
165                            strutsAction += "/view_entry";
166                    }
167    
168                    return strutsAction;
169            }
170    
171            protected String getUrlTitle(long entryId) {
172                    String urlTitle = StringPool.BLANK;
173    
174                    try {
175                            BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
176    
177                            urlTitle = entry.getUrlTitle();
178                    }
179                    catch (Exception e) {
180                            if (_log.isWarnEnabled()) {
181                                    _log.warn(e);
182                            }
183                    }
184    
185                    return urlTitle;
186            }
187    
188            private static Log _log = LogFactoryUtil.getLog(FindEntryAction.class);
189    
190    }