1   /**
2    * Copyright (c) 2000-2007 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              long entryId = ParamUtil.getLong(req, "entryId");
72              boolean showAllEntries = ParamUtil.getBoolean(
73                  req, "showAllEntries");
74  
75              try {
76                  plid = getPlid(plid, entryId);
77              }
78              catch (Exception e) {
79                  if (_log.isWarnEnabled()) {
80                      _log.warn(e);
81                  }
82              }
83  
84              String urlTitle = getUrlTitle(entryId);
85  
86              PortletURL portletURL = new PortletURLImpl(
87                  req, PortletKeys.BLOGS, plid, false);
88  
89              portletURL.setWindowState(WindowState.NORMAL);
90              portletURL.setPortletMode(PortletMode.VIEW);
91  
92              if (showAllEntries) {
93                  portletURL.setParameter("struts_action", "/blogs/view");
94              }
95              else {
96                  portletURL.setParameter("struts_action", "/blogs/view_entry");
97  
98                  if (Validator.isNotNull(urlTitle)) {
99                      portletURL.setParameter("urlTitle", urlTitle);
100                 }
101                 else {
102                     portletURL.setParameter("entryId", String.valueOf(entryId));
103                 }
104             }
105 
106             String redirect = portletURL.toString();
107 
108             res.sendRedirect(redirect);
109 
110             return null;
111         }
112         catch (Exception e) {
113             req.setAttribute(PageContext.EXCEPTION, e);
114 
115             return mapping.findForward(ActionConstants.COMMON_ERROR);
116         }
117     }
118 
119     protected long getPlid(long plid, long entryId) throws Exception {
120         if (plid != 0) {
121             try {
122                 LayoutLocalServiceUtil.getLayout(plid);
123 
124                 return plid;
125             }
126             catch (NoSuchLayoutException nsle) {
127             }
128         }
129 
130         BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
131 
132         long groupId = entry.getGroupId();
133         boolean privateLayout = false;
134 
135         List layouts = LayoutLocalServiceUtil.getLayouts(
136             groupId, privateLayout);
137 
138         for (int i = 0; i < layouts.size(); i++) {
139             Layout layout = (Layout)layouts.get(i);
140 
141             if (!layout.getType().equals(LayoutImpl.TYPE_PORTLET)) {
142                 continue;
143             }
144 
145             if (i == 0) {
146                 plid = layout.getPlid();
147             }
148 
149             LayoutTypePortlet layoutTypePortlet =
150                 (LayoutTypePortlet)layout.getLayoutType();
151 
152             if (layoutTypePortlet.hasPortletId(PortletKeys.BLOGS)) {
153                 plid = layout.getPlid();
154 
155                 break;
156             }
157         }
158 
159         return plid;
160     }
161 
162     protected String getUrlTitle(long entryId) {
163         String urlTitle = StringPool.BLANK;
164 
165         try {
166             BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
167 
168             urlTitle = entry.getUrlTitle();
169         }
170         catch (Exception e) {
171             if (_log.isWarnEnabled()) {
172                 _log.warn(e);
173             }
174         }
175 
176         return urlTitle;
177     }
178 
179     private static Log _log = LogFactory.getLog(FindEntryAction.class);
180 
181 }