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.NoSuchLayoutException;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Layout;
32  import com.liferay.portal.model.LayoutConstants;
33  import com.liferay.portal.model.LayoutTypePortlet;
34  import com.liferay.portal.service.LayoutLocalServiceUtil;
35  import com.liferay.portal.util.PortalUtil;
36  import com.liferay.portal.util.PortletKeys;
37  import com.liferay.portlet.PortletURLImpl;
38  import com.liferay.portlet.blogs.model.BlogsEntry;
39  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
40  
41  import javax.portlet.PortletMode;
42  import javax.portlet.PortletRequest;
43  import javax.portlet.PortletURL;
44  import javax.portlet.WindowState;
45  
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpServletResponse;
48  
49  import org.apache.struts.action.Action;
50  import org.apache.struts.action.ActionForm;
51  import org.apache.struts.action.ActionForward;
52  import org.apache.struts.action.ActionMapping;
53  
54  /**
55   * <a href="FindEntryAction.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   *
59   */
60  public class FindEntryAction extends Action {
61  
62      public ActionForward execute(
63              ActionMapping mapping, ActionForm form, HttpServletRequest request,
64              HttpServletResponse response)
65          throws Exception {
66  
67          try {
68              long plid = ParamUtil.getLong(request, "p_l_id");
69              String redirect = ParamUtil.getString(request, "redirect");
70              long entryId = ParamUtil.getLong(request, "entryId");
71              boolean showAllEntries = ParamUtil.getBoolean(
72                  request, "showAllEntries");
73  
74              plid = getPlid(plid, entryId);
75  
76              String urlTitle = getUrlTitle(entryId);
77  
78              PortletURL portletURL = new PortletURLImpl(
79                  request, PortletKeys.BLOGS, plid, PortletRequest.RENDER_PHASE);
80  
81              portletURL.setWindowState(WindowState.NORMAL);
82              portletURL.setPortletMode(PortletMode.VIEW);
83  
84              if (Validator.isNotNull(redirect)) {
85                  portletURL.setParameter("redirect", redirect);
86              }
87  
88              if (showAllEntries) {
89                  portletURL.setParameter("struts_action", "/blogs/view");
90              }
91              else {
92                  portletURL.setParameter("struts_action", "/blogs/view_entry");
93  
94                  if (Validator.isNotNull(urlTitle)) {
95                      portletURL.setParameter("urlTitle", urlTitle);
96                  }
97                  else {
98                      portletURL.setParameter("entryId", String.valueOf(entryId));
99                  }
100             }
101 
102             response.sendRedirect(portletURL.toString());
103 
104             return null;
105         }
106         catch (Exception e) {
107             String noSuchEntryRedirect = ParamUtil.getString(
108                 request, "noSuchEntryRedirect");
109 
110             if (e.getClass().equals(NoSuchLayoutException.class) &&
111                 Validator.isNotNull(noSuchEntryRedirect)) {
112 
113                 response.sendRedirect(noSuchEntryRedirect);
114             }
115             else {
116                 PortalUtil.sendError(e, request, response);
117             }
118 
119             return null;
120         }
121     }
122 
123     protected long getPlid(long plid, long entryId) throws Exception {
124         if (plid != LayoutConstants.DEFAULT_PLID) {
125             try {
126                 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
127 
128                 LayoutTypePortlet layoutTypePortlet =
129                     (LayoutTypePortlet)layout.getLayoutType();
130 
131                 if (layoutTypePortlet.hasPortletId(PortletKeys.BLOGS)) {
132                     return plid;
133                 }
134             }
135             catch (NoSuchLayoutException nsle) {
136             }
137         }
138 
139         BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
140 
141         plid = PortalUtil.getPlidFromPortletId(
142             entry.getGroupId(), PortletKeys.BLOGS);
143 
144         if (plid != LayoutConstants.DEFAULT_PLID) {
145             return plid;
146         }
147         else {
148             throw new NoSuchLayoutException(
149                 "No page was found with the Blogs portlet.");
150         }
151     }
152 
153     protected String getUrlTitle(long entryId) {
154         String urlTitle = StringPool.BLANK;
155 
156         try {
157             BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
158 
159             urlTitle = entry.getUrlTitle();
160         }
161         catch (Exception e) {
162             if (_log.isWarnEnabled()) {
163                 _log.warn(e);
164             }
165         }
166 
167         return urlTitle;
168     }
169 
170     private static Log _log = LogFactoryUtil.getLog(FindEntryAction.class);
171 
172 }