All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.journal.service.impl.JournalFolderServiceImpl Maven / Gradle / Ivy

There is a newer version: 7.0.162
Show newest version
/**
 * 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.journal.service.impl;

import com.liferay.dynamic.data.mapping.model.DDMStructure;
import com.liferay.dynamic.data.mapping.model.DDMStructureLink;
import com.liferay.dynamic.data.mapping.service.DDMStructureLinkService;
import com.liferay.dynamic.data.mapping.service.DDMStructureService;
import com.liferay.dynamic.data.mapping.util.comparator.StructureLinkStructureModifiedDateComparator;
import com.liferay.dynamic.data.mapping.util.comparator.StructureLinkStructureNameComparator;
import com.liferay.journal.constants.JournalFolderConstants;
import com.liferay.journal.model.JournalArticle;
import com.liferay.journal.model.JournalFolder;
import com.liferay.journal.service.base.JournalFolderServiceBaseImpl;
import com.liferay.journal.service.persistence.JournalArticleFinder;
import com.liferay.petra.function.transform.TransformUtil;
import com.liferay.portal.aop.AopService;
import com.liferay.portal.kernel.dao.db.DBManagerUtil;
import com.liferay.portal.kernel.dao.orm.QueryDefinition;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.security.permission.ActionKeys;
import com.liferay.portal.kernel.security.permission.PermissionChecker;
import com.liferay.portal.kernel.security.permission.resource.ModelResourcePermission;
import com.liferay.portal.kernel.security.permission.resource.ModelResourcePermissionUtil;
import com.liferay.portal.kernel.service.ClassNameLocalService;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.ListUtil;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.workflow.WorkflowConstants;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
 * @author Juan Fernández
 */
@Component(
	property = {
		"json.web.service.context.name=journal",
		"json.web.service.context.path=JournalFolder"
	},
	service = AopService.class
)
public class JournalFolderServiceImpl extends JournalFolderServiceBaseImpl {

	@Override
	public JournalFolder addFolder(
			String externalReferenceCode, long groupId, long parentFolderId,
			String name, String description, ServiceContext serviceContext)
		throws PortalException {

		ModelResourcePermissionUtil.check(
			_journalFolderModelResourcePermission, getPermissionChecker(),
			groupId, parentFolderId, ActionKeys.ADD_FOLDER);

		return journalFolderLocalService.addFolder(
			externalReferenceCode, getUserId(), groupId, parentFolderId, name,
			description, serviceContext);
	}

	@Override
	public void deleteFolder(long folderId) throws PortalException {
		_journalFolderModelResourcePermission.check(
			getPermissionChecker(),
			journalFolderLocalService.getFolder(folderId), ActionKeys.DELETE);

		journalFolderLocalService.deleteFolder(folderId);
	}

	@Override
	public void deleteFolder(long folderId, boolean includeTrashedEntries)
		throws PortalException {

		_journalFolderModelResourcePermission.check(
			getPermissionChecker(),
			journalFolderLocalService.getFolder(folderId), ActionKeys.DELETE);

		journalFolderLocalService.deleteFolder(folderId, includeTrashedEntries);
	}

	@Override
	public JournalFolder fetchFolder(long folderId) throws PortalException {
		JournalFolder folder = journalFolderLocalService.fetchFolder(folderId);

		if (folder != null) {
			_journalFolderModelResourcePermission.check(
				getPermissionChecker(), folder, ActionKeys.VIEW);
		}

		return folder;
	}

	@Override
	public List getDDMStructures(
			long[] groupIds, long folderId, int restrictionType)
		throws PortalException {

		return _filterStructures(
			journalFolderLocalService.getDDMStructures(
				groupIds, folderId, restrictionType));
	}

	@Override
	public List getDDMStructures(
			long[] groupIds, long folderId, int restrictionType,
			OrderByComparator orderByComparator)
		throws PortalException {

		return _filterStructures(
			journalFolderLocalService.getDDMStructures(
				groupIds, folderId, restrictionType, orderByComparator));
	}

	@Override
	public JournalFolder getFolder(long folderId) throws PortalException {
		JournalFolder folder = journalFolderLocalService.getFolder(folderId);

		_journalFolderModelResourcePermission.check(
			getPermissionChecker(), folder, ActionKeys.VIEW);

		return folder;
	}

	@Override
	public JournalFolder getFolderByExternalReferenceCode(
			long groupId, String externalReferenceCode)
		throws PortalException {

		JournalFolder folder =
			journalFolderLocalService.getJournalFolderByExternalReferenceCode(
				externalReferenceCode, groupId);

		_journalFolderModelResourcePermission.check(
			getPermissionChecker(), folder, ActionKeys.VIEW);

		return folder;
	}

	@Override
	public List getFolderIds(long groupId, long folderId)
		throws PortalException {

		ModelResourcePermissionUtil.check(
			_journalFolderModelResourcePermission, getPermissionChecker(),
			groupId, folderId, ActionKeys.VIEW);

		List folderIds = getSubfolderIds(groupId, folderId, true);

		folderIds.add(0, folderId);

		return folderIds;
	}

	@Override
	public List getFolders(long groupId) {
		return journalFolderPersistence.filterFindByGroupId(groupId);
	}

	@Override
	public List getFolders(long groupId, long parentFolderId) {
		return getFolders(
			groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED);
	}

	@Override
	public List getFolders(
		long groupId, long parentFolderId, int status) {

		return journalFolderPersistence.filterFindByG_P_S(
			groupId, parentFolderId, status);
	}

	@Override
	public List getFolders(
		long groupId, long parentFolderId, int start, int end) {

		return getFolders(
			groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED, start,
			end);
	}

	@Override
	public List getFolders(
		long groupId, long parentFolderId, int status, int start, int end) {

		return journalFolderPersistence.filterFindByG_P_S(
			groupId, parentFolderId, status, start, end);
	}

	@Override
	public List getFoldersAndArticles(
		long groupId, long folderId, int status, int start, int end,
		OrderByComparator orderByComparator) {

		return getFoldersAndArticles(
			groupId, 0, folderId, status, start, end, orderByComparator);
	}

	@Override
	public List getFoldersAndArticles(
		long groupId, long folderId, int start, int end,
		OrderByComparator orderByComparator) {

		return getFoldersAndArticles(
			groupId, folderId, WorkflowConstants.STATUS_ANY, start, end,
			orderByComparator);
	}

	@Override
	public List getFoldersAndArticles(
		long groupId, long userId, long folderId, int status, int start,
		int end, OrderByComparator orderByComparator) {

		QueryDefinition queryDefinition = new QueryDefinition<>(
			status, userId, true, start, end,
			(OrderByComparator)orderByComparator);

		return journalFolderFinder.filterFindF_A_ByG_F_DDMSI(
			groupId, folderId, 0, queryDefinition);
	}

	@Override
	public List getFoldersAndArticles(
		long groupId, long userId, long folderId, int status, Locale locale,
		int start, int end, OrderByComparator orderByComparator) {

		QueryDefinition queryDefinition = new QueryDefinition<>(
			status, userId, true, start, end,
			(OrderByComparator)orderByComparator);

		return journalFolderFinder.filterFindF_A_ByG_F_DDMSI_L(
			groupId, folderId, 0, locale, queryDefinition);
	}

	@Override
	public List getFoldersAndArticles(
		long groupId, long userId, long folderId, long ddmStructureId,
		int status, Locale locale, int start, int end,
		OrderByComparator orderByComparator) {

		QueryDefinition queryDefinition = new QueryDefinition<>(
			status, userId, true, start, end,
			(OrderByComparator)orderByComparator);

		return journalFolderFinder.filterFindF_A_ByG_F_DDMSI_L(
			groupId, folderId, ddmStructureId, locale, queryDefinition);
	}

	@Override
	public int getFoldersAndArticlesCount(
		long groupId, List folderIds, int status) {

		QueryDefinition queryDefinition = new QueryDefinition<>(
			status);

		if (folderIds.size() <= DBManagerUtil.getDBMaxParameters()) {
			return _journalArticleFinder.filterCountByG_F(
				groupId, folderIds, queryDefinition);
		}

		int start = 0;
		int end = DBManagerUtil.getDBMaxParameters();

		int articlesCount = _journalArticleFinder.filterCountByG_F(
			groupId, folderIds.subList(start, end), queryDefinition);

		List sublist = folderIds.subList(start, end);

		sublist.clear();

		articlesCount += getFoldersAndArticlesCount(groupId, folderIds, status);

		return articlesCount;
	}

	@Override
	public int getFoldersAndArticlesCount(long groupId, long folderId) {
		return getFoldersAndArticlesCount(
			groupId, folderId, WorkflowConstants.STATUS_ANY);
	}

	@Override
	public int getFoldersAndArticlesCount(
		long groupId, long folderId, int status) {

		return getFoldersAndArticlesCount(groupId, 0, folderId, status);
	}

	@Override
	public int getFoldersAndArticlesCount(
		long groupId, long userId, long folderId, int status) {

		QueryDefinition queryDefinition = new QueryDefinition<>(
			status, userId, true);

		return journalFolderFinder.filterCountF_A_ByG_F_DDMSI(
			groupId, folderId, 0, queryDefinition);
	}

	@Override
	public int getFoldersAndArticlesCount(
		long groupId, long userId, long folderId, long ddmStructureId,
		int status) {

		QueryDefinition queryDefinition = new QueryDefinition<>(
			status, userId, true);

		return journalFolderFinder.filterCountF_A_ByG_F_DDMSI(
			groupId, folderId, ddmStructureId, queryDefinition);
	}

	@Override
	public int getFoldersCount(long groupId, long parentFolderId) {
		return getFoldersCount(
			groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED);
	}

	@Override
	public int getFoldersCount(long groupId, long parentFolderId, int status) {
		if (status == WorkflowConstants.STATUS_ANY) {
			return journalFolderPersistence.filterCountByG_P_NotS(
				groupId, parentFolderId, WorkflowConstants.STATUS_IN_TRASH);
		}

		return journalFolderPersistence.filterCountByG_P_S(
			groupId, parentFolderId, status);
	}

	@Override
	public void getSubfolderIds(
		List folderIds, long groupId, long folderId, boolean recurse) {

		List folders =
			journalFolderPersistence.filterFindByG_P_NotS(
				groupId, folderId, WorkflowConstants.STATUS_IN_TRASH);

		for (JournalFolder folder : folders) {
			folderIds.add(folder.getFolderId());

			if (recurse) {
				getSubfolderIds(
					folderIds, folder.getGroupId(), folder.getFolderId(),
					recurse);
			}
		}
	}

	@Override
	public List getSubfolderIds(
		long groupId, long folderId, boolean recurse) {

		List folderIds = new ArrayList<>();

		getSubfolderIds(folderIds, groupId, folderId, recurse);

		return folderIds;
	}

	@Override
	public JournalFolder moveFolder(
			long folderId, long parentFolderId, ServiceContext serviceContext)
		throws PortalException {

		_journalFolderModelResourcePermission.check(
			getPermissionChecker(),
			journalFolderLocalService.getFolder(folderId), ActionKeys.UPDATE);

		return journalFolderLocalService.moveFolder(
			folderId, parentFolderId, serviceContext);
	}

	@Override
	public JournalFolder moveFolderFromTrash(
			long folderId, long parentFolderId, ServiceContext serviceContext)
		throws PortalException {

		_journalFolderModelResourcePermission.check(
			getPermissionChecker(),
			journalFolderLocalService.getFolder(folderId), ActionKeys.UPDATE);

		return journalFolderLocalService.moveFolderFromTrash(
			getUserId(), folderId, parentFolderId, serviceContext);
	}

	@Override
	public JournalFolder moveFolderToTrash(long folderId)
		throws PortalException {

		_journalFolderModelResourcePermission.check(
			getPermissionChecker(),
			journalFolderLocalService.getFolder(folderId), ActionKeys.DELETE);

		return journalFolderLocalService.moveFolderToTrash(
			getUserId(), folderId);
	}

	@Override
	public void restoreFolderFromTrash(long folderId) throws PortalException {
		_journalFolderModelResourcePermission.check(
			getPermissionChecker(),
			journalFolderLocalService.getFolder(folderId), ActionKeys.UPDATE);

		journalFolderLocalService.restoreFolderFromTrash(getUserId(), folderId);
	}

	@Override
	public List searchDDMStructures(
			long companyId, long[] groupIds, long folderId, int restrictionType,
			String keywords, int start, int end,
			OrderByComparator orderByComparator)
		throws PortalException {

		if (restrictionType ==
				JournalFolderConstants.
					RESTRICTION_TYPE_DDM_STRUCTURES_AND_WORKFLOW) {

			return TransformUtil.transform(
				_ddmStructureLinkService.getStructureLinks(
					_classNameLocalService.getClassNameId(JournalFolder.class),
					folderId, groupIds, keywords,
					JournalArticle.class.getName(), start, end,
					_getDDMStructureLinkOrderByComparator(orderByComparator)),
				structureLink -> structureLink.getStructure());
		}

		folderId = journalFolderLocalService.getOverridedDDMStructuresFolderId(
			folderId);

		if (folderId != JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
			return TransformUtil.transform(
				_ddmStructureLinkService.getStructureLinks(
					_classNameLocalService.getClassNameId(JournalFolder.class),
					folderId, groupIds, keywords,
					JournalArticle.class.getName(), start, end,
					_getDDMStructureLinkOrderByComparator(orderByComparator)),
				structureLink -> structureLink.getStructure());
		}

		return _ddmStructureService.search(
			companyId, groupIds,
			_classNameLocalService.getClassNameId(JournalArticle.class),
			keywords, WorkflowConstants.STATUS_ANY, start, end,
			orderByComparator);
	}

	@Override
	public int searchDDMStructuresCount(
			long companyId, long[] groupIds, long folderId, int restrictionType,
			String keywords)
		throws PortalException {

		if (restrictionType ==
				JournalFolderConstants.
					RESTRICTION_TYPE_DDM_STRUCTURES_AND_WORKFLOW) {

			return _ddmStructureLinkService.getStructureLinksCount(
				_classNameLocalService.getClassNameId(JournalFolder.class),
				folderId, groupIds, keywords, JournalArticle.class.getName());
		}

		folderId = journalFolderLocalService.getOverridedDDMStructuresFolderId(
			folderId);

		if (folderId != JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
			return _ddmStructureLinkService.getStructureLinksCount(
				_classNameLocalService.getClassNameId(JournalFolder.class),
				folderId, groupIds, keywords, JournalArticle.class.getName());
		}

		return _ddmStructureService.searchCount(
			companyId, groupIds,
			_classNameLocalService.getClassNameId(JournalArticle.class),
			keywords, WorkflowConstants.STATUS_ANY);
	}

	@Override
	public void subscribe(long groupId, long folderId) throws PortalException {
		ModelResourcePermissionUtil.check(
			_journalFolderModelResourcePermission, getPermissionChecker(),
			groupId, folderId, ActionKeys.SUBSCRIBE);

		journalFolderLocalService.subscribe(getUserId(), groupId, folderId);
	}

	@Override
	public void unsubscribe(long groupId, long folderId)
		throws PortalException {

		ModelResourcePermissionUtil.check(
			_journalFolderModelResourcePermission, getPermissionChecker(),
			groupId, folderId, ActionKeys.SUBSCRIBE);

		journalFolderLocalService.unsubscribe(getUserId(), groupId, folderId);
	}

	@Override
	public JournalFolder updateFolder(
			long groupId, long folderId, long parentFolderId, String name,
			String description, boolean mergeWithParentFolder,
			ServiceContext serviceContext)
		throws PortalException {

		_journalFolderModelResourcePermission.check(
			getPermissionChecker(),
			journalFolderLocalService.getFolder(folderId), ActionKeys.UPDATE);

		return journalFolderLocalService.updateFolder(
			getUserId(), groupId, folderId, parentFolderId, name, description,
			mergeWithParentFolder, serviceContext);
	}

	@Override
	public JournalFolder updateFolder(
			long groupId, long folderId, long parentFolderId, String name,
			String description, long[] ddmStructureIds, int restrictionType,
			boolean mergeWithParentFolder, ServiceContext serviceContext)
		throws PortalException {

		ModelResourcePermissionUtil.check(
			_journalFolderModelResourcePermission, getPermissionChecker(),
			groupId, folderId, ActionKeys.UPDATE);

		return journalFolderLocalService.updateFolder(
			getUserId(), groupId, folderId, parentFolderId, name, description,
			ddmStructureIds, restrictionType, mergeWithParentFolder,
			serviceContext);
	}

	private List _filterStructures(
			List ddmStructures)
		throws PortalException {

		PermissionChecker permissionChecker = getPermissionChecker();

		ddmStructures = ListUtil.copy(ddmStructures);

		Iterator iterator = ddmStructures.iterator();

		while (iterator.hasNext()) {
			DDMStructure ddmStructure = iterator.next();

			if (!_ddmStructureModelResourcePermission.contains(
					permissionChecker, ddmStructure, ActionKeys.VIEW)) {

				iterator.remove();
			}
		}

		return ddmStructures;
	}

	private OrderByComparator
		_getDDMStructureLinkOrderByComparator(
			OrderByComparator orderByComparator) {

		if (orderByComparator == null) {
			return null;
		}

		if (ArrayUtil.contains(
				orderByComparator.getOrderByFields(), "modifiedDate")) {

			return new StructureLinkStructureModifiedDateComparator(
				orderByComparator.isAscending());
		}

		if (ArrayUtil.contains(orderByComparator.getOrderByFields(), "name")) {
			return new StructureLinkStructureNameComparator(
				orderByComparator.isAscending());
		}

		return null;
	}

	@Reference
	private ClassNameLocalService _classNameLocalService;

	@Reference
	private DDMStructureLinkService _ddmStructureLinkService;

	@Reference(
		target = "(model.class.name=com.liferay.dynamic.data.mapping.model.DDMStructure)"
	)
	private ModelResourcePermission
		_ddmStructureModelResourcePermission;

	@Reference
	private DDMStructureService _ddmStructureService;

	@Reference
	private JournalArticleFinder _journalArticleFinder;

	@Reference(
		target = "(model.class.name=com.liferay.journal.model.JournalFolder)"
	)
	private ModelResourcePermission
		_journalFolderModelResourcePermission;

}