1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class FindEntryAction extends Action {
60  
61      public ActionForward execute(
62              ActionMapping mapping, ActionForm form, HttpServletRequest request,
63              HttpServletResponse response)
64          throws Exception {
65  
66          try {
67              long plid = ParamUtil.getLong(request, "p_l_id");
68              String redirect = ParamUtil.getString(request, "redirect");
69              long entryId = ParamUtil.getLong(request, "entryId");
70              boolean showAllEntries = ParamUtil.getBoolean(
71                  request, "showAllEntries");
72  
73              plid = getPlid(plid, entryId);
74  
75              String urlTitle = getUrlTitle(entryId);
76  
77              PortletURL portletURL = new PortletURLImpl(
78                  request, PortletKeys.BLOGS, plid, PortletRequest.RENDER_PHASE);
79  
80              portletURL.setWindowState(WindowState.NORMAL);
81              portletURL.setPortletMode(PortletMode.VIEW);
82  
83              if (Validator.isNotNull(redirect)) {
84                  portletURL.setParameter("redirect", redirect);
85              }
86  
87              if (showAllEntries) {
88                  portletURL.setParameter("struts_action", "/blogs/view");
89              }
90              else {
91                  portletURL.setParameter("struts_action", "/blogs/view_entry");
92  
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             PortalUtil.sendError(e, request, response);
107 
108             return null;
109         }
110     }
111 
112     protected long getPlid(long plid, long entryId) throws Exception {
113         if (plid != LayoutConstants.DEFAULT_PLID) {
114             try {
115                 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
116 
117                 LayoutTypePortlet layoutTypePortlet =
118                     (LayoutTypePortlet)layout.getLayoutType();
119 
120                 if (layoutTypePortlet.hasPortletId(PortletKeys.BLOGS)) {
121                     return plid;
122                 }
123             }
124             catch (NoSuchLayoutException nsle) {
125             }
126         }
127 
128         BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
129 
130         plid = PortalUtil.getPlidFromPortletId(
131             entry.getGroupId(), PortletKeys.BLOGS);
132 
133         if (plid != LayoutConstants.DEFAULT_PLID) {
134             return plid;
135         }
136         else {
137             throw new NoSuchLayoutException(
138                 "No page was found with the Blogs portlet.");
139         }
140     }
141 
142     protected String getUrlTitle(long entryId) {
143         String urlTitle = StringPool.BLANK;
144 
145         try {
146             BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
147 
148             urlTitle = entry.getUrlTitle();
149         }
150         catch (Exception e) {
151             if (_log.isWarnEnabled()) {
152                 _log.warn(e);
153             }
154         }
155 
156         return urlTitle;
157     }
158 
159     private static Log _log = LogFactoryUtil.getLog(FindEntryAction.class);
160 
161 }