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