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