001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.blogs.action;
022:
023: import com.liferay.portal.NoSuchLayoutException;
024: import com.liferay.portal.kernel.util.ParamUtil;
025: import com.liferay.portal.kernel.util.StringPool;
026: import com.liferay.portal.kernel.util.Validator;
027: import com.liferay.portal.model.Layout;
028: import com.liferay.portal.model.LayoutTypePortlet;
029: import com.liferay.portal.model.impl.LayoutImpl;
030: import com.liferay.portal.service.LayoutLocalServiceUtil;
031: import com.liferay.portal.struts.ActionConstants;
032: import com.liferay.portal.util.PortletKeys;
033: import com.liferay.portlet.PortletURLImpl;
034: import com.liferay.portlet.blogs.model.BlogsEntry;
035: import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
036:
037: import java.util.List;
038:
039: import javax.portlet.PortletMode;
040: import javax.portlet.PortletURL;
041: import javax.portlet.WindowState;
042:
043: import javax.servlet.http.HttpServletRequest;
044: import javax.servlet.http.HttpServletResponse;
045: import javax.servlet.jsp.PageContext;
046:
047: import org.apache.commons.logging.Log;
048: import org.apache.commons.logging.LogFactory;
049: import org.apache.struts.action.Action;
050: import org.apache.struts.action.ActionForm;
051: import org.apache.struts.action.ActionForward;
052: import org.apache.struts.action.ActionMapping;
053:
054: /**
055: * <a href="FindEntryAction.java.html"><b><i>View Source</i></b></a>
056: *
057: * @author Brian Wing Shun Chan
058: *
059: */
060: public class FindEntryAction extends Action {
061:
062: public ActionForward execute(ActionMapping mapping,
063: ActionForm form, HttpServletRequest req,
064: HttpServletResponse res) throws Exception {
065:
066: try {
067: long plid = ParamUtil.getLong(req, "p_l_id");
068: String redirect = ParamUtil.getString(req, "redirect");
069: long entryId = ParamUtil.getLong(req, "entryId");
070: boolean showAllEntries = ParamUtil.getBoolean(req,
071: "showAllEntries");
072:
073: plid = getPlid(plid, entryId);
074:
075: String urlTitle = getUrlTitle(entryId);
076:
077: PortletURL portletURL = new PortletURLImpl(req,
078: PortletKeys.BLOGS, plid, false);
079:
080: portletURL.setWindowState(WindowState.NORMAL);
081: portletURL.setPortletMode(PortletMode.VIEW);
082:
083: if (Validator.isNotNull(redirect)) {
084: portletURL.setParameter("redirect", redirect);
085: }
086:
087: if (showAllEntries) {
088: portletURL.setParameter("struts_action", "/blogs/view");
089: } else {
090: portletURL.setParameter("struts_action",
091: "/blogs/view_entry");
092:
093: if (Validator.isNotNull(urlTitle)) {
094: portletURL.setParameter("urlTitle", urlTitle);
095: } else {
096: portletURL.setParameter("entryId", String
097: .valueOf(entryId));
098: }
099: }
100:
101: res.sendRedirect(portletURL.toString());
102:
103: return null;
104: } catch (Exception e) {
105: req.setAttribute(PageContext.EXCEPTION, e);
106:
107: return mapping.findForward(ActionConstants.COMMON_ERROR);
108: }
109: }
110:
111: protected long getPlid(long plid, long entryId) throws Exception {
112: if (plid != 0) {
113: try {
114: Layout layout = LayoutLocalServiceUtil.getLayout(plid);
115:
116: LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout
117: .getLayoutType();
118:
119: if (layoutTypePortlet.hasPortletId(PortletKeys.BLOGS)) {
120: return plid;
121: }
122: } catch (NoSuchLayoutException nsle) {
123: }
124: }
125:
126: BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
127:
128: long groupId = entry.getGroupId();
129: boolean privateLayout = false;
130:
131: List layouts = LayoutLocalServiceUtil.getLayouts(groupId,
132: privateLayout);
133:
134: for (int i = 0; i < layouts.size(); i++) {
135: Layout layout = (Layout) layouts.get(i);
136:
137: if (!layout.getType().equals(LayoutImpl.TYPE_PORTLET)) {
138: continue;
139: }
140:
141: LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout
142: .getLayoutType();
143:
144: if (layoutTypePortlet.hasPortletId(PortletKeys.BLOGS)) {
145: return layout.getPlid();
146: }
147: }
148:
149: throw new NoSuchLayoutException(
150: "No public page was found with the Blogs portlet.");
151: }
152:
153: protected String getUrlTitle(long entryId) {
154: String urlTitle = StringPool.BLANK;
155:
156: try {
157: BlogsEntry entry = BlogsEntryLocalServiceUtil
158: .getEntry(entryId);
159:
160: urlTitle = entry.getUrlTitle();
161: } catch (Exception e) {
162: if (_log.isWarnEnabled()) {
163: _log.warn(e);
164: }
165: }
166:
167: return urlTitle;
168: }
169:
170: private static Log _log = LogFactory.getLog(FindEntryAction.class);
171:
172: }
|