/** * 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.portlet.display.template.exportimport.portlet.preferences.processor; import com.liferay.dynamic.data.mapping.model.DDMTemplate; import com.liferay.exportimport.kernel.lar.PortletDataContext; import com.liferay.exportimport.kernel.lar.PortletDataException; import com.liferay.exportimport.kernel.lar.StagedModelDataHandlerUtil; import com.liferay.exportimport.portlet.preferences.processor.Capability; import com.liferay.portal.kernel.model.Portlet; import com.liferay.portal.kernel.service.PortletLocalService; import com.liferay.portal.kernel.template.TemplateHandler; import com.liferay.portal.kernel.util.GetterUtil; import com.liferay.portal.kernel.util.Portal; import com.liferay.portal.kernel.util.Validator; import com.liferay.portlet.display.template.PortletDisplayTemplate; import com.liferay.portlet.display.template.PortletDisplayTemplateUtil; import javax.portlet.PortletPreferences; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; /** * @author Mate Thurzo */ @Component( immediate = true, service = {Capability.class, PortletDisplayTemplateExportCapability.class} ) public class PortletDisplayTemplateExportCapability implements Capability { @Override public PortletPreferences process( PortletDataContext portletDataContext, PortletPreferences portletPreferences) throws PortletDataException { exportDisplayStyle( portletDataContext, portletDataContext.getPortletId(), portletPreferences); return portletPreferences; } protected void exportDisplayStyle( PortletDataContext portletDataContext, String portletId, PortletPreferences portletPreferences) throws PortletDataException { String displayStyle = getDisplayStyle( portletDataContext, portletId, portletPreferences); if (Validator.isNull(displayStyle) || !displayStyle.startsWith( PortletDisplayTemplate.DISPLAY_STYLE_PREFIX)) { return; } long displayStyleGroupId = getDisplayStyleGroupId( portletDataContext, portletId, portletPreferences); long previousScopeGroupId = portletDataContext.getScopeGroupId(); if (displayStyleGroupId != portletDataContext.getScopeGroupId()) { portletDataContext.setScopeGroupId(displayStyleGroupId); } DDMTemplate ddmTemplate = PortletDisplayTemplateUtil.getPortletDisplayTemplateDDMTemplate( portletDataContext.getGroupId(), getClassNameId(portletDataContext, portletId), displayStyle, false); if (ddmTemplate != null) { StagedModelDataHandlerUtil.exportReferenceStagedModel( portletDataContext, portletId, ddmTemplate); } portletDataContext.setScopeGroupId(previousScopeGroupId); } protected long getClassNameId( PortletDataContext portletDataContext, String portletId) { Portlet portlet = _portletLocalService.getPortletById( portletDataContext.getCompanyId(), portletId); TemplateHandler templateHandler = portlet.getTemplateHandlerInstance(); if (templateHandler == null) { return 0; } String className = templateHandler.getClassName(); return portal.getClassNameId(className); } protected String getDisplayStyle( PortletDataContext portletDataContext, String portletId, PortletPreferences portletPreferences) { try { Portlet portlet = _portletLocalService.getPortletById( portletDataContext.getCompanyId(), portletId); if (Validator.isNotNull(portlet.getTemplateHandlerClass())) { return portletPreferences.getValue("displayStyle", null); } } catch (Exception e) { } return null; } protected long getDisplayStyleGroupId( PortletDataContext portletDataContext, String portletId, PortletPreferences portletPreferences) { try { Portlet portlet = _portletLocalService.getPortletById( portletDataContext.getCompanyId(), portletId); if (Validator.isNotNull(portlet.getTemplateHandlerClass())) { return GetterUtil.getLong( portletPreferences.getValue("displayStyleGroupId", null)); } } catch (Exception e) { } return 0; } @Reference(unbind = "-") protected void setPortletLocalService( PortletLocalService portletLocalService) { _portletLocalService = portletLocalService; } @Reference protected Portal portal; private PortletLocalService _portletLocalService; }