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              plid = getPlid(plid, entryId);
66  
67              String urlTitle = getUrlTitle(entryId);
68  
69              PortletURL portletURL = new PortletURLImpl(
70                  request, PortletKeys.BLOGS, plid, PortletRequest.RENDER_PHASE);
71  
72              portletURL.setWindowState(WindowState.NORMAL);
73              portletURL.setPortletMode(PortletMode.VIEW);
74  
75              if (Validator.isNotNull(redirect)) {
76                  portletURL.setParameter("redirect", redirect);
77              }
78  
79              if (showAllEntries) {
80                  portletURL.setParameter("struts_action", "/blogs/view");
81              }
82              else {
83                  portletURL.setParameter("struts_action", "/blogs/view_entry");
84  
85                  if (Validator.isNotNull(urlTitle)) {
86                      portletURL.setParameter("urlTitle", urlTitle);
87                  }
88                  else {
89                      portletURL.setParameter("entryId", String.valueOf(entryId));
90                  }
91              }
92  
93              response.sendRedirect(portletURL.toString());
94  
95              return null;
96          }
97          catch (Exception e) {
98              PortalUtil.sendError(e, request, response);
99  
100             return null;
101         }
102     }
103 
104     protected long getPlid(long plid, long entryId) throws Exception {
105         if (plid != LayoutConstants.DEFAULT_PLID) {
106             try {
107                 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
108 
109                 LayoutTypePortlet layoutTypePortlet =
110                     (LayoutTypePortlet)layout.getLayoutType();
111 
112                 if (layoutTypePortlet.hasPortletId(PortletKeys.BLOGS)) {
113                     return plid;
114                 }
115             }
116             catch (NoSuchLayoutException nsle) {
117             }
118         }
119 
120         BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
121 
122         plid = PortalUtil.getPlidFromPortletId(
123             entry.getGroupId(), PortletKeys.BLOGS);
124 
125         if (plid != LayoutConstants.DEFAULT_PLID) {
126             return plid;
127         }
128         else {
129             throw new NoSuchLayoutException(
130                 "No page was found with the Blogs portlet.");
131         }
132     }
133 
134     protected String getUrlTitle(long entryId) {
135         String urlTitle = StringPool.BLANK;
136 
137         try {
138             BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
139 
140             urlTitle = entry.getUrlTitle();
141         }
142         catch (Exception e) {
143             if (_log.isWarnEnabled()) {
144                 _log.warn(e);
145             }
146         }
147 
148         return urlTitle;
149     }
150 
151     private static Log _log = LogFactoryUtil.getLog(FindEntryAction.class);
152 
153 }