1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.blogs.action;
21  
22  import com.liferay.portal.NoSuchLayoutException;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.ParamUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.Layout;
29  import com.liferay.portal.model.LayoutConstants;
30  import com.liferay.portal.model.LayoutTypePortlet;
31  import com.liferay.portal.service.LayoutLocalServiceUtil;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portal.util.PortletKeys;
34  import com.liferay.portlet.PortletURLImpl;
35  import com.liferay.portlet.blogs.model.BlogsEntry;
36  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
37  
38  import javax.portlet.PortletMode;
39  import javax.portlet.PortletRequest;
40  import javax.portlet.PortletURL;
41  import javax.portlet.WindowState;
42  
43  import javax.servlet.http.HttpServletRequest;
44  import javax.servlet.http.HttpServletResponse;
45  
46  import org.apache.struts.action.Action;
47  import org.apache.struts.action.ActionForm;
48  import org.apache.struts.action.ActionForward;
49  import org.apache.struts.action.ActionMapping;
50  
51  /**
52   * <a href="FindEntryAction.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class FindEntryAction extends Action {
58  
59      public ActionForward execute(
60              ActionMapping mapping, ActionForm form, HttpServletRequest request,
61              HttpServletResponse response)
62          throws Exception {
63  
64          try {
65              long plid = ParamUtil.getLong(request, "p_l_id");
66              String redirect = ParamUtil.getString(request, "redirect");
67              long entryId = ParamUtil.getLong(request, "entryId");
68              boolean showAllEntries = ParamUtil.getBoolean(
69                  request, "showAllEntries");
70  
71              plid = getPlid(plid, entryId);
72  
73              String urlTitle = getUrlTitle(entryId);
74  
75              PortletURL portletURL = new PortletURLImpl(
76                  request, PortletKeys.BLOGS, plid, PortletRequest.RENDER_PHASE);
77  
78              portletURL.setWindowState(WindowState.NORMAL);
79              portletURL.setPortletMode(PortletMode.VIEW);
80  
81              if (Validator.isNotNull(redirect)) {
82                  portletURL.setParameter("redirect", redirect);
83              }
84  
85              if (showAllEntries) {
86                  portletURL.setParameter("struts_action", "/blogs/view");
87              }
88              else {
89                  portletURL.setParameter("struts_action", "/blogs/view_entry");
90  
91                  if (Validator.isNotNull(urlTitle)) {
92                      portletURL.setParameter("urlTitle", urlTitle);
93                  }
94                  else {
95                      portletURL.setParameter("entryId", String.valueOf(entryId));
96                  }
97              }
98  
99              response.sendRedirect(portletURL.toString());
100 
101             return null;
102         }
103         catch (Exception e) {
104             PortalUtil.sendError(e, request, response);
105 
106             return null;
107         }
108     }
109 
110     protected long getPlid(long plid, long entryId) throws Exception {
111         if (plid != LayoutConstants.DEFAULT_PLID) {
112             try {
113                 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
114 
115                 LayoutTypePortlet layoutTypePortlet =
116                     (LayoutTypePortlet)layout.getLayoutType();
117 
118                 if (layoutTypePortlet.hasPortletId(PortletKeys.BLOGS)) {
119                     return plid;
120                 }
121             }
122             catch (NoSuchLayoutException nsle) {
123             }
124         }
125 
126         BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
127 
128         plid = PortalUtil.getPlidFromPortletId(
129             entry.getGroupId(), PortletKeys.BLOGS);
130 
131         if (plid != LayoutConstants.DEFAULT_PLID) {
132             return plid;
133         }
134         else {
135             throw new NoSuchLayoutException(
136                 "No page was found with the Blogs portlet.");
137         }
138     }
139 
140     protected String getUrlTitle(long entryId) {
141         String urlTitle = StringPool.BLANK;
142 
143         try {
144             BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
145 
146             urlTitle = entry.getUrlTitle();
147         }
148         catch (Exception e) {
149             if (_log.isWarnEnabled()) {
150                 _log.warn(e);
151             }
152         }
153 
154         return urlTitle;
155     }
156 
157     private static Log _log = LogFactoryUtil.getLog(FindEntryAction.class);
158 
159 }