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             PortalUtil.sendError(e, request, response);
108 
109             return null;
110         }
111     }
112 
113     protected long getPlid(long plid, long entryId) throws Exception {
114         if (plid != LayoutConstants.DEFAULT_PLID) {
115             try {
116                 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
117 
118                 LayoutTypePortlet layoutTypePortlet =
119                     (LayoutTypePortlet)layout.getLayoutType();
120 
121                 if (layoutTypePortlet.hasPortletId(PortletKeys.BLOGS)) {
122                     return plid;
123                 }
124             }
125             catch (NoSuchLayoutException nsle) {
126             }
127         }
128 
129         BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
130 
131         plid = PortalUtil.getPlidFromPortletId(
132             entry.getGroupId(), false, PortletKeys.BLOGS);
133 
134         if (plid != LayoutConstants.DEFAULT_PLID) {
135             return plid;
136         }
137 
138         plid = PortalUtil.getPlidFromPortletId(
139             entry.getGroupId(), true, PortletKeys.BLOGS);
140 
141         if (plid != LayoutConstants.DEFAULT_PLID) {
142             return plid;
143         }
144         else {
145             throw new NoSuchLayoutException(
146                 "No page was found with the Blogs portlet.");
147         }
148     }
149 
150     protected String getUrlTitle(long entryId) {
151         String urlTitle = StringPool.BLANK;
152 
153         try {
154             BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
155 
156             urlTitle = entry.getUrlTitle();
157         }
158         catch (Exception e) {
159             if (_log.isWarnEnabled()) {
160                 _log.warn(e);
161             }
162         }
163 
164         return urlTitle;
165     }
166 
167     private static Log _log = LogFactoryUtil.getLog(FindEntryAction.class);
168 
169 }