1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet.blogs.action;
16  
17  import com.liferay.portal.NoSuchLayoutException;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.ParamUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.model.Layout;
24  import com.liferay.portal.model.LayoutConstants;
25  import com.liferay.portal.model.LayoutTypePortlet;
26  import com.liferay.portal.service.LayoutLocalServiceUtil;
27  import com.liferay.portal.util.PortalUtil;
28  import com.liferay.portal.util.PortletKeys;
29  import com.liferay.portlet.PortletURLImpl;
30  import com.liferay.portlet.blogs.model.BlogsEntry;
31  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
32  
33  import javax.portlet.PortletMode;
34  import javax.portlet.PortletRequest;
35  import javax.portlet.PortletURL;
36  import javax.portlet.WindowState;
37  
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  
41  import org.apache.struts.action.Action;
42  import org.apache.struts.action.ActionForm;
43  import org.apache.struts.action.ActionForward;
44  import org.apache.struts.action.ActionMapping;
45  
46  /**
47   * <a href="FindEntryAction.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   */
51  public class FindEntryAction extends Action {
52  
53      public ActionForward execute(
54              ActionMapping mapping, ActionForm form, HttpServletRequest request,
55              HttpServletResponse response)
56          throws Exception {
57  
58          try {
59              long plid = ParamUtil.getLong(request, "p_l_id");
60              String redirect = ParamUtil.getString(request, "redirect");
61              long entryId = ParamUtil.getLong(request, "entryId");
62              boolean showAllEntries = ParamUtil.getBoolean(
63                  request, "showAllEntries");
64  
65              String portletId = PortletKeys.BLOGS;
66  
67              try {
68                  plid = getPlid(plid, portletId, entryId);
69              }
70              catch (NoSuchLayoutException nsle) {
71                  portletId = PortletKeys.BLOGS_AGGREGATOR;
72  
73                  plid = getPlid(plid, portletId, entryId);
74              }
75  
76              String urlTitle = getUrlTitle(entryId);
77  
78              PortletURL portletURL = new PortletURLImpl(
79                  request, portletId, 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              String strutsAction = getStrutsAction(portletId, showAllEntries);
89  
90              portletURL.setParameter("struts_action", strutsAction);
91  
92              if (!showAllEntries) {
93                  if (Validator.isNotNull(urlTitle)) {
94                      portletURL.setParameter("urlTitle", urlTitle);
95                  }
96                  else {
97                      portletURL.setParameter("entryId", String.valueOf(entryId));
98                  }
99              }
100 
101             response.sendRedirect(portletURL.toString());
102 
103             return null;
104         }
105         catch (Exception e) {
106             String noSuchEntryRedirect = ParamUtil.getString(
107                 request, "noSuchEntryRedirect");
108 
109             if (e.getClass().equals(NoSuchLayoutException.class) &&
110                 Validator.isNotNull(noSuchEntryRedirect)) {
111 
112                 response.sendRedirect(noSuchEntryRedirect);
113             }
114             else {
115                 PortalUtil.sendError(e, request, response);
116             }
117 
118             return null;
119         }
120     }
121 
122     protected long getPlid(long plid, String portletId, long entryId)
123         throws Exception {
124 
125         if (plid != LayoutConstants.DEFAULT_PLID) {
126             try {
127                 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
128 
129                 LayoutTypePortlet layoutTypePortlet =
130                     (LayoutTypePortlet)layout.getLayoutType();
131 
132                 if (layoutTypePortlet.hasPortletId(portletId)) {
133                     return plid;
134                 }
135             }
136             catch (NoSuchLayoutException nsle) {
137             }
138         }
139 
140         BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
141 
142         plid = PortalUtil.getPlidFromPortletId(entry.getGroupId(), portletId);
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 getStrutsAction(String portletId, boolean showAllEntries) {
154         String strutsAction = StringPool.BLANK;
155 
156         if (portletId.equals(PortletKeys.BLOGS)) {
157             strutsAction = "/blogs";
158         }
159         else {
160             strutsAction = "/blogs_aggregator";
161         }
162 
163         if (showAllEntries) {
164             strutsAction += "/view";
165         }
166         else {
167             strutsAction += "/view_entry";
168         }
169 
170         return strutsAction;
171     }
172 
173     protected String getUrlTitle(long entryId) {
174         String urlTitle = StringPool.BLANK;
175 
176         try {
177             BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
178 
179             urlTitle = entry.getUrlTitle();
180         }
181         catch (Exception e) {
182             if (_log.isWarnEnabled()) {
183                 _log.warn(e);
184             }
185         }
186 
187         return urlTitle;
188     }
189 
190     private static Log _log = LogFactoryUtil.getLog(FindEntryAction.class);
191 
192 }