1
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.PortletRequest;
43 import javax.portlet.PortletURL;
44 import javax.portlet.WindowState;
45
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48 import javax.servlet.jsp.PageContext;
49
50 import org.apache.commons.logging.Log;
51 import org.apache.commons.logging.LogFactory;
52 import org.apache.struts.action.Action;
53 import org.apache.struts.action.ActionForm;
54 import org.apache.struts.action.ActionForward;
55 import org.apache.struts.action.ActionMapping;
56
57
63 public class FindEntryAction extends Action {
64
65 public ActionForward execute(
66 ActionMapping mapping, ActionForm form, HttpServletRequest req,
67 HttpServletResponse res)
68 throws Exception {
69
70 try {
71 long plid = ParamUtil.getLong(req, "p_l_id");
72 String redirect = ParamUtil.getString(req, "redirect");
73 long entryId = ParamUtil.getLong(req, "entryId");
74 boolean showAllEntries = ParamUtil.getBoolean(
75 req, "showAllEntries");
76
77 plid = getPlid(plid, entryId);
78
79 String urlTitle = getUrlTitle(entryId);
80
81 PortletURL portletURL = new PortletURLImpl(
82 req, PortletKeys.BLOGS, plid, PortletRequest.RENDER_PHASE);
83
84 portletURL.setWindowState(WindowState.NORMAL);
85 portletURL.setPortletMode(PortletMode.VIEW);
86
87 if (Validator.isNotNull(redirect)) {
88 portletURL.setParameter("redirect", redirect);
89 }
90
91 if (showAllEntries) {
92 portletURL.setParameter("struts_action", "/blogs/view");
93 }
94 else {
95 portletURL.setParameter("struts_action", "/blogs/view_entry");
96
97 if (Validator.isNotNull(urlTitle)) {
98 portletURL.setParameter("urlTitle", urlTitle);
99 }
100 else {
101 portletURL.setParameter("entryId", String.valueOf(entryId));
102 }
103 }
104
105 res.sendRedirect(portletURL.toString());
106
107 return null;
108 }
109 catch (Exception e) {
110 req.setAttribute(PageContext.EXCEPTION, e);
111
112 return mapping.findForward(ActionConstants.COMMON_ERROR);
113 }
114 }
115
116 protected long getPlid(long plid, long entryId) throws Exception {
117 if (plid != 0) {
118 try {
119 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
120
121 LayoutTypePortlet layoutTypePortlet =
122 (LayoutTypePortlet)layout.getLayoutType();
123
124 if (layoutTypePortlet.hasPortletId(PortletKeys.BLOGS)) {
125 return plid;
126 }
127 }
128 catch (NoSuchLayoutException nsle) {
129 }
130 }
131
132 BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
133
134 long groupId = entry.getGroupId();
135 boolean privateLayout = false;
136
137 List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
138 groupId, privateLayout);
139
140 for (Layout layout : layouts) {
141 if (!layout.getType().equals(LayoutImpl.TYPE_PORTLET)) {
142 continue;
143 }
144
145 LayoutTypePortlet layoutTypePortlet =
146 (LayoutTypePortlet)layout.getLayoutType();
147
148 if (layoutTypePortlet.hasPortletId(PortletKeys.BLOGS)) {
149 return layout.getPlid();
150 }
151 }
152
153 throw new NoSuchLayoutException(
154 "No public page was found with the Blogs portlet.");
155 }
156
157 protected String getUrlTitle(long entryId) {
158 String urlTitle = StringPool.BLANK;
159
160 try {
161 BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
162
163 urlTitle = entry.getUrlTitle();
164 }
165 catch (Exception e) {
166 if (_log.isWarnEnabled()) {
167 _log.warn(e);
168 }
169 }
170
171 return urlTitle;
172 }
173
174 private static Log _log = LogFactory.getLog(FindEntryAction.class);
175
176 }