/** * 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.staging.bar.web.internal.portlet; import com.liferay.exportimport.kernel.staging.StagingUtil; import com.liferay.portal.kernel.exception.LayoutBranchNameException; import com.liferay.portal.kernel.exception.LayoutSetBranchNameException; import com.liferay.portal.kernel.exception.NoSuchGroupException; import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.kernel.model.LayoutRevision; import com.liferay.portal.kernel.model.Release; import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet; import com.liferay.portal.kernel.security.auth.PrincipalException; import com.liferay.portal.kernel.service.LayoutRevisionLocalService; import com.liferay.portal.kernel.service.LayoutSetBranchLocalService; import com.liferay.portal.kernel.service.LayoutSetBranchService; import com.liferay.portal.kernel.service.LayoutSetLocalService; import com.liferay.portal.kernel.service.ServiceContext; import com.liferay.portal.kernel.service.ServiceContextFactory; import com.liferay.portal.kernel.servlet.MultiSessionMessages; import com.liferay.portal.kernel.servlet.SessionErrors; import com.liferay.portal.kernel.theme.ThemeDisplay; import com.liferay.portal.kernel.util.ParamUtil; import com.liferay.portal.kernel.util.Portal; import com.liferay.portal.kernel.util.WebKeys; import com.liferay.portal.kernel.workflow.WorkflowConstants; import com.liferay.staging.bar.web.internal.portlet.constants.StagingBarPortletKeys; import java.io.IOException; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.Portlet; import javax.portlet.PortletException; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import javax.servlet.http.HttpServletRequest; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; /** * @author Levente Hudák */ @Component( immediate = true, property = { "com.liferay.portlet.add-default-resource=true", "com.liferay.portlet.css-class-wrapper=portlet-staging-bar", "com.liferay.portlet.header-portlet-css=/css/main.css", "com.liferay.portlet.header-portlet-javascript=/js/staging.js", "com.liferay.portlet.header-portlet-javascript=/js/staging_branch.js", "com.liferay.portlet.header-portlet-javascript=/js/staging_version.js", "com.liferay.portlet.private-request-attributes=false", "com.liferay.portlet.private-session-attributes=false", "com.liferay.portlet.render-weight=50", "com.liferay.portlet.show-portlet-access-denied=false", "com.liferay.portlet.show-portlet-inactive=false", "com.liferay.portlet.system=true", "com.liferay.portlet.use-default-template=false", "javax.portlet.display-name=Staging Bar", "javax.portlet.expiration-cache=0", "javax.portlet.init-param.template-path=/", "javax.portlet.init-param.view-template=/view.jsp", "javax.portlet.name=" + StagingBarPortletKeys.STAGING_BAR, "javax.portlet.resource-bundle=content.Language", "javax.portlet.security-role-ref=power-user,user", "javax.portlet.supports.mime-type=text/html" }, service = Portlet.class ) public class StagingBarPortlet extends MVCPortlet { public void deleteLayoutRevision( ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { HttpServletRequest request = _portal.getHttpServletRequest( actionRequest); long layoutRevisionId = ParamUtil.getLong( actionRequest, "layoutRevisionId"); LayoutRevision layoutRevision = _layoutRevisionLocalService.getLayoutRevision(layoutRevisionId); _layoutRevisionLocalService.deleteLayoutRevision(layoutRevision); boolean updateRecentLayoutRevisionId = ParamUtil.getBoolean( actionRequest, "updateRecentLayoutRevisionId"); if (updateRecentLayoutRevisionId) { StagingUtil.setRecentLayoutRevisionId( request, layoutRevision.getLayoutSetBranchId(), layoutRevision.getPlid(), layoutRevision.getParentLayoutRevisionId()); } addLayoutRevisionSessionMessages(actionRequest, actionResponse); } public void updateLayoutRevision( ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute( WebKeys.THEME_DISPLAY); long layoutRevisionId = ParamUtil.getLong( actionRequest, "layoutRevisionId"); LayoutRevision layoutRevision = _layoutRevisionLocalService.getLayoutRevision(layoutRevisionId); ServiceContext serviceContext = ServiceContextFactory.getInstance( actionRequest); LayoutRevision enableLayoutRevision = _layoutRevisionLocalService.updateLayoutRevision( serviceContext.getUserId(), layoutRevisionId, layoutRevision.getLayoutBranchId(), layoutRevision.getName(), layoutRevision.getTitle(), layoutRevision.getDescription(), layoutRevision.getKeywords(), layoutRevision.getRobots(), layoutRevision.getTypeSettings(), layoutRevision.getIconImage(), layoutRevision.getIconImageId(), layoutRevision.getThemeId(), layoutRevision.getColorSchemeId(), layoutRevision.getCss(), serviceContext); if (layoutRevision.getStatus() != WorkflowConstants.STATUS_INCOMPLETE) { StagingUtil.setRecentLayoutRevisionId( themeDisplay.getUser(), layoutRevision.getLayoutSetBranchId(), layoutRevision.getPlid(), layoutRevision.getLayoutRevisionId()); addLayoutRevisionSessionMessages(actionRequest, actionResponse); return; } LayoutRevision lastLayoutRevision = _layoutRevisionLocalService.fetchLastLayoutRevision( enableLayoutRevision.getPlid(), true); if (lastLayoutRevision != null) { LayoutRevision newLayoutRevision = _layoutRevisionLocalService.addLayoutRevision( serviceContext.getUserId(), layoutRevision.getLayoutSetBranchId(), layoutRevision.getLayoutBranchId(), enableLayoutRevision.getLayoutRevisionId(), false, layoutRevision.getPlid(), lastLayoutRevision.getLayoutRevisionId(), lastLayoutRevision.isPrivateLayout(), lastLayoutRevision.getName(), lastLayoutRevision.getTitle(), lastLayoutRevision.getDescription(), lastLayoutRevision.getKeywords(), lastLayoutRevision.getRobots(), lastLayoutRevision.getTypeSettings(), lastLayoutRevision.isIconImage(), lastLayoutRevision.getIconImageId(), lastLayoutRevision.getThemeId(), lastLayoutRevision.getColorSchemeId(), lastLayoutRevision.getCss(), serviceContext); StagingUtil.setRecentLayoutRevisionId( themeDisplay.getUser(), newLayoutRevision.getLayoutSetBranchId(), newLayoutRevision.getPlid(), newLayoutRevision.getLayoutRevisionId()); } addLayoutRevisionSessionMessages(actionRequest, actionResponse); } protected void addLayoutRevisionSessionMessages( ActionRequest actionRequest, ActionResponse actionResponse) throws IOException { MultiSessionMessages.add( actionRequest, _portal.getPortletId(actionRequest) + "requestProcessed"); sendRedirect(actionRequest, actionResponse); } @Override protected void doDispatch( RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { if (SessionErrors.contains( renderRequest, NoSuchGroupException.class.getName()) || SessionErrors.contains( renderRequest, PrincipalException.getNestedClasses()) || SessionErrors.contains( renderRequest, SystemException.class.getName())) { include("/error.jsp", renderRequest, renderResponse); } else { super.doDispatch(renderRequest, renderResponse); } } @Override protected boolean isSessionErrorException(Throwable cause) { if (cause instanceof LayoutBranchNameException || cause instanceof LayoutSetBranchNameException || super.isSessionErrorException(cause)) { return true; } return false; } @Reference protected void setLayoutRevisionLocalService( LayoutRevisionLocalService layoutRevisionLocalService) { _layoutRevisionLocalService = layoutRevisionLocalService; } @Reference protected void setLayoutSetBranchLocalService( LayoutSetBranchLocalService layoutSetBranchLocalService) { _layoutSetBranchLocalService = layoutSetBranchLocalService; } @Reference protected void setLayoutSetBranchService( LayoutSetBranchService layoutSetBranchService) { _layoutSetBranchService = layoutSetBranchService; } @Reference protected void setLayoutSetLocalService( LayoutSetLocalService layoutSetLocalService) { _layoutSetLocalService = layoutSetLocalService; } @Reference( target = "(&(release.bundle.symbolic.name=com.liferay.staging.bar.web)(release.schema.version=1.0.0))", unbind = "-" ) protected void setRelease(Release release) { } protected void unsetLayoutRevisionLocalService( LayoutRevisionLocalService layoutRevisionLocalService) { _layoutRevisionLocalService = null; } protected void unsetLayoutSetBranchLocalService( LayoutSetBranchLocalService layoutSetBranchLocalService) { _layoutSetBranchLocalService = null; } protected void unsetLayoutSetBranchService( LayoutSetBranchService layoutSetBranchService) { _layoutSetBranchService = null; } protected void unsetLayoutSetLocalService( LayoutSetLocalService layoutSetLocalService) { _layoutSetLocalService = null; } private LayoutRevisionLocalService _layoutRevisionLocalService; private LayoutSetBranchLocalService _layoutSetBranchLocalService; private LayoutSetBranchService _layoutSetBranchService; private LayoutSetLocalService _layoutSetLocalService; @Reference private Portal _portal; }