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             String noSuchEntryRedirect = ParamUtil.getString(
105                 request, "noSuchEntryRedirect");
106 
107             if (e.getClass().equals(NoSuchLayoutException.class) &&
108                 Validator.isNotNull(noSuchEntryRedirect)) {
109 
110                 response.sendRedirect(noSuchEntryRedirect);
111             }
112             else {
113                 PortalUtil.sendError(e, request, response);
114             }
115 
116             return null;
117         }
118     }
119 
120     protected long getPlid(long plid, long entryId) throws Exception {
121         if (plid != LayoutConstants.DEFAULT_PLID) {
122             try {
123                 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
124 
125                 LayoutTypePortlet layoutTypePortlet =
126                     (LayoutTypePortlet)layout.getLayoutType();
127 
128                 if (layoutTypePortlet.hasPortletId(PortletKeys.BLOGS)) {
129                     return plid;
130                 }
131             }
132             catch (NoSuchLayoutException nsle) {
133             }
134         }
135 
136         BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
137 
138         plid = PortalUtil.getPlidFromPortletId(
139             entry.getGroupId(), 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 }