com.liferay.fragment.web.internal.portlet.action.UpdateFragmentEntryPreviewMVCActionCommand Maven / Gradle / Ivy
/**
* SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/
package com.liferay.fragment.web.internal.portlet.action;
import com.liferay.document.library.kernel.service.DLAppLocalService;
import com.liferay.fragment.constants.FragmentPortletKeys;
import com.liferay.fragment.model.FragmentEntry;
import com.liferay.fragment.service.FragmentEntryService;
import com.liferay.portal.kernel.model.Repository;
import com.liferay.portal.kernel.portlet.bridges.mvc.BaseMVCActionCommand;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand;
import com.liferay.portal.kernel.portletfilerepository.PortletFileRepositoryUtil;
import com.liferay.portal.kernel.repository.model.FileEntry;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.TempFileEntryUtil;
import com.liferay.portal.kernel.util.WebKeys;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* @author Eudaldo Alonso
*/
@Component(
property = {
"javax.portlet.name=" + FragmentPortletKeys.FRAGMENT,
"mvc.command.name=/fragment/update_fragment_entry_preview"
},
service = MVCActionCommand.class
)
public class UpdateFragmentEntryPreviewMVCActionCommand
extends BaseMVCActionCommand {
@Override
protected void doProcessAction(
ActionRequest actionRequest, ActionResponse actionResponse)
throws Exception {
ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
WebKeys.THEME_DISPLAY);
long fragmentEntryId = ParamUtil.getLong(
actionRequest, "fragmentEntryId");
long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
FileEntry fileEntry = _dlAppLocalService.getFileEntry(fileEntryId);
FileEntry tempFileEntry = fileEntry;
Repository repository =
PortletFileRepositoryUtil.fetchPortletRepository(
themeDisplay.getScopeGroupId(), FragmentPortletKeys.FRAGMENT);
if (repository == null) {
ServiceContext serviceContext = new ServiceContext();
serviceContext.setAddGroupPermissions(true);
serviceContext.setAddGuestPermissions(true);
repository = PortletFileRepositoryUtil.addPortletRepository(
themeDisplay.getScopeGroupId(), FragmentPortletKeys.FRAGMENT,
serviceContext);
}
String fileName =
fragmentEntryId + "_preview." + fileEntry.getExtension();
FileEntry oldFileEntry =
PortletFileRepositoryUtil.fetchPortletFileEntry(
themeDisplay.getScopeGroupId(), repository.getDlFolderId(),
fileName);
if (oldFileEntry != null) {
PortletFileRepositoryUtil.deletePortletFileEntry(
oldFileEntry.getFileEntryId());
}
fileEntry = PortletFileRepositoryUtil.addPortletFileEntry(
null, themeDisplay.getScopeGroupId(), themeDisplay.getUserId(),
FragmentEntry.class.getName(), fragmentEntryId,
FragmentPortletKeys.FRAGMENT, repository.getDlFolderId(),
fileEntry.getContentStream(), fileName, fileEntry.getMimeType(),
false);
_fragmentEntryService.updateFragmentEntry(
fragmentEntryId, fileEntry.getFileEntryId());
TempFileEntryUtil.deleteTempFileEntry(tempFileEntry.getFileEntryId());
String redirect = ParamUtil.getString(actionRequest, "redirect");
sendRedirect(actionRequest, actionResponse, redirect);
}
@Reference
private DLAppLocalService _dlAppLocalService;
@Reference
private FragmentEntryService _fragmentEntryService;
}