1
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
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 }