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