/**
* 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.dynamic.data.lists.form.web.internal.exportimport.portlet.preferences.processor;
import com.liferay.dynamic.data.lists.form.web.constants.DDLFormPortletKeys;
import com.liferay.dynamic.data.lists.model.DDLRecordSet;
import com.liferay.dynamic.data.lists.service.DDLRecordSetLocalService;
import com.liferay.dynamic.data.lists.service.permission.DDLPermission;
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.exportimport.portlet.preferences.processor.ExportImportPortletPreferencesProcessor;
import com.liferay.exportimport.portlet.preferences.processor.capability.ReferencedStagedModelImporterCapability;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.ListUtil;
import com.liferay.portal.kernel.util.MapUtil;
import java.util.List;
import java.util.Map;
import javax.portlet.PortletPreferences;
import javax.portlet.ReadOnlyException;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* @author Marcellus Tavares
*/
@Component(
immediate = true,
property = {
"javax.portlet.name=" + DDLFormPortletKeys.DYNAMIC_DATA_LISTS_FORM
},
service = ExportImportPortletPreferencesProcessor.class
)
public class DDLFormExportImportPortletPreferencesProcessor
implements ExportImportPortletPreferencesProcessor {
@Override
public List<Capability> getExportCapabilities() {
return null;
}
@Override
public List<Capability> getImportCapabilities() {
return ListUtil.toList(
new Capability[] {_referencedStagedModelImporterCapability});
}
@Override
public PortletPreferences processExportPortletPreferences(
PortletDataContext portletDataContext,
PortletPreferences portletPreferences)
throws PortletDataException {
try {
portletDataContext.addPortletPermissions(
DDLPermission.RESOURCE_NAME);
}
catch (PortalException pe) {
throw new PortletDataException(
"Unable to export portlet permissions", pe);
}
String portletId = portletDataContext.getPortletId();
long recordSetId = GetterUtil.getLong(
portletPreferences.getValue("recordSetId", null));
if (recordSetId == 0) {
if (_log.isWarnEnabled()) {
_log.warn(
"Record set ID is not set for preferences of portlet " +
portletId);
}
return portletPreferences;
}
DDLRecordSet recordSet = _ddlRecordSetLocalService.fetchRecordSet(
recordSetId);
if (recordSet != null) {
StagedModelDataHandlerUtil.exportReferenceStagedModel(
portletDataContext, portletId, recordSet);
}
return portletPreferences;
}
@Override
public PortletPreferences processImportPortletPreferences(
PortletDataContext portletDataContext,
PortletPreferences portletPreferences)
throws PortletDataException {
try {
portletDataContext.importPortletPermissions(
DDLPermission.RESOURCE_NAME);
}
catch (PortalException pe) {
throw new PortletDataException(
"Unable to export portlet permissions", pe);
}
long importedRecordSetId = GetterUtil.getLong(
portletPreferences.getValue("recordSetId", null));
Map<Long, Long> recordSetIds =
(Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
DDLRecordSet.class);
long recordSetId = MapUtil.getLong(
recordSetIds, importedRecordSetId, importedRecordSetId);
try {
portletPreferences.setValue(
"recordSetId", String.valueOf(recordSetId));
}
catch (ReadOnlyException roe) {
throw new PortletDataException(
"Unable to update portlet preferences during import", roe);
}
return portletPreferences;
}
private static final Log _log = LogFactoryUtil.getLog(
DDLFormExportImportPortletPreferencesProcessor.class);
@Reference
private DDLRecordSetLocalService _ddlRecordSetLocalService;
@Reference
private ReferencedStagedModelImporterCapability
_referencedStagedModelImporterCapability;
}