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