/** * Copyright (c) 2000-present Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package com.liferay.knowledge.base.web.internal.portlet; import com.liferay.knowledge.base.constants.KBFolderConstants; import com.liferay.knowledge.base.constants.KBPortletKeys; import com.liferay.knowledge.base.exception.NoSuchArticleException; import com.liferay.knowledge.base.exception.NoSuchCommentException; import com.liferay.knowledge.base.model.KBArticle; import com.liferay.knowledge.base.service.KBArticleLocalService; import com.liferay.knowledge.base.web.internal.constants.KBWebKeys; import com.liferay.portal.kernel.exception.NoSuchSubscriptionException; import com.liferay.portal.kernel.exception.PortalException; import com.liferay.portal.kernel.model.Release; import com.liferay.portal.kernel.security.auth.PrincipalException; import com.liferay.portal.kernel.servlet.SessionErrors; import com.liferay.portal.kernel.util.ParamUtil; import com.liferay.portal.kernel.util.Validator; import com.liferay.portal.kernel.workflow.WorkflowConstants; import java.io.IOException; import javax.portlet.Portlet; import javax.portlet.PortletException; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; /** * @author Peter Shin * @author Brian Wing Shun Chan */ @Component( immediate = true, property = { "com.liferay.portlet.css-class-wrapper=knowledge-base-portlet knowledge-base-portlet-section", "com.liferay.portlet.display-category=category.cms", "com.liferay.portlet.header-portlet-css=/admin/css/common.css", "com.liferay.portlet.header-portlet-css=/section/css/main.css", "com.liferay.portlet.icon=/icons/section.png", "com.liferay.portlet.instanceable=true", "com.liferay.portlet.scopeable=true", "com.liferay.portlet.struts-path=knowledge_base", "javax.portlet.display-name=Knowledge Base Section", "javax.portlet.expiration-cache=0", "javax.portlet.init-param.always-send-redirect=true", "javax.portlet.init-param.copy-request-parameters=true", "javax.portlet.init-param.template-path=/section/", "javax.portlet.init-param.view-template=/section/view.jsp", "javax.portlet.name=" + KBPortletKeys.KNOWLEDGE_BASE_SECTION, "javax.portlet.resource-bundle=content.Language", "javax.portlet.security-role-ref=administrator,guest,power-user,user", "javax.portlet.supported-public-render-parameter=categoryId", "javax.portlet.supported-public-render-parameter=tag", "javax.portlet.supports.mime-type=text/html" }, service = Portlet.class ) public class SectionPortlet extends BaseKBPortlet { @Override protected void doDispatch( RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { if (SessionErrors.contains( renderRequest, NoSuchArticleException.class.getName()) || SessionErrors.contains( renderRequest, NoSuchCommentException.class.getName()) || SessionErrors.contains( renderRequest, NoSuchSubscriptionException.class.getName()) || SessionErrors.contains( renderRequest, PrincipalException.getNestedClasses())) { include(templatePath + "error.jsp", renderRequest, renderResponse); } else { super.doDispatch(renderRequest, renderResponse); } } @Override protected void doRender( RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { try { renderRequest.setAttribute( KBWebKeys.DL_MIME_TYPE_DISPLAY_CONTEXT, dlMimeTypeDisplayContext); KBArticle kbArticle = getKBArticle(renderRequest); renderRequest.setAttribute( KBWebKeys.KNOWLEDGE_BASE_KB_ARTICLE, kbArticle); renderRequest.setAttribute( KBWebKeys.KNOWLEDGE_BASE_STATUS, WorkflowConstants.STATUS_APPROVED); } catch (Exception e) { if (e instanceof NoSuchArticleException || e instanceof PrincipalException) { SessionErrors.add(renderRequest, e.getClass()); } else { throw new PortletException(e); } } } protected KBArticle getKBArticle(RenderRequest renderRequest) throws PortalException { long resourcePrimKey = ParamUtil.getLong( renderRequest, "resourcePrimKey"); if (resourcePrimKey > 0) { return kbArticleService.getLatestKBArticle( resourcePrimKey, WorkflowConstants.STATUS_APPROVED); } String urlTitle = ParamUtil.getString(renderRequest, "urlTitle"); if (Validator.isNull(urlTitle)) { return null; } long groupId = portal.getScopeGroupId(renderRequest); String kbFolderUrlTitle = ParamUtil.getString( renderRequest, "kbFolderUrlTitle"); if (Validator.isNotNull(kbFolderUrlTitle)) { return _kbArticleLocalService.getKBArticleByUrlTitle( groupId, kbFolderUrlTitle, urlTitle); } return _kbArticleLocalService.getKBArticleByUrlTitle( groupId, KBFolderConstants.DEFAULT_PARENT_FOLDER_ID, urlTitle); } @Reference(unbind = "-") protected void setKBArticleLocalService( KBArticleLocalService kbArticleLocalService) { _kbArticleLocalService = kbArticleLocalService; } @Reference( target = "(&(release.bundle.symbolic.name=com.liferay.knowledge.base.web)(release.schema.version=1.0.0))", unbind = "-" ) protected void setRelease(Release release) { } private KBArticleLocalService _kbArticleLocalService; }