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 long entryId = ParamUtil.getLong(req, "entryId");
72 boolean showAllEntries = ParamUtil.getBoolean(
73 req, "showAllEntries");
74
75 try {
76 plid = getPlid(plid, entryId);
77 }
78 catch (Exception e) {
79 if (_log.isWarnEnabled()) {
80 _log.warn(e);
81 }
82 }
83
84 String urlTitle = getUrlTitle(entryId);
85
86 PortletURL portletURL = new PortletURLImpl(
87 req, PortletKeys.BLOGS, plid, false);
88
89 portletURL.setWindowState(WindowState.NORMAL);
90 portletURL.setPortletMode(PortletMode.VIEW);
91
92 if (showAllEntries) {
93 portletURL.setParameter("struts_action", "/blogs/view");
94 }
95 else {
96 portletURL.setParameter("struts_action", "/blogs/view_entry");
97
98 if (Validator.isNotNull(urlTitle)) {
99 portletURL.setParameter("urlTitle", urlTitle);
100 }
101 else {
102 portletURL.setParameter("entryId", String.valueOf(entryId));
103 }
104 }
105
106 String redirect = portletURL.toString();
107
108 res.sendRedirect(redirect);
109
110 return null;
111 }
112 catch (Exception e) {
113 req.setAttribute(PageContext.EXCEPTION, e);
114
115 return mapping.findForward(ActionConstants.COMMON_ERROR);
116 }
117 }
118
119 protected long getPlid(long plid, long entryId) throws Exception {
120 if (plid != 0) {
121 try {
122 LayoutLocalServiceUtil.getLayout(plid);
123
124 return plid;
125 }
126 catch (NoSuchLayoutException nsle) {
127 }
128 }
129
130 BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
131
132 long groupId = entry.getGroupId();
133 boolean privateLayout = false;
134
135 List layouts = LayoutLocalServiceUtil.getLayouts(
136 groupId, privateLayout);
137
138 for (int i = 0; i < layouts.size(); i++) {
139 Layout layout = (Layout)layouts.get(i);
140
141 if (!layout.getType().equals(LayoutImpl.TYPE_PORTLET)) {
142 continue;
143 }
144
145 if (i == 0) {
146 plid = layout.getPlid();
147 }
148
149 LayoutTypePortlet layoutTypePortlet =
150 (LayoutTypePortlet)layout.getLayoutType();
151
152 if (layoutTypePortlet.hasPortletId(PortletKeys.BLOGS)) {
153 plid = layout.getPlid();
154
155 break;
156 }
157 }
158
159 return plid;
160 }
161
162 protected String getUrlTitle(long entryId) {
163 String urlTitle = StringPool.BLANK;
164
165 try {
166 BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
167
168 urlTitle = entry.getUrlTitle();
169 }
170 catch (Exception e) {
171 if (_log.isWarnEnabled()) {
172 _log.warn(e);
173 }
174 }
175
176 return urlTitle;
177 }
178
179 private static Log _log = LogFactory.getLog(FindEntryAction.class);
180
181 }