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

com.liferay.change.tracking.internal.CTEnclosureUtil Maven / Gradle / Ivy

There is a newer version: 3.0.108
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.change.tracking.internal;

import com.liferay.change.tracking.closure.CTClosure;

import java.util.AbstractMap;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

/**
 * @author Preston Crary
 */
public class CTEnclosureUtil {

	public static Map> getEnclosureMap(
		CTClosure ctClosure, long modelClassNameId, long modelClassPK) {

		return getEnclosureMap(
			ctClosure,
			Collections.singleton(
				new AbstractMap.SimpleImmutableEntry<>(
					modelClassNameId, Collections.singleton(modelClassPK))));
	}

	public static Map> getEnclosureMap(
		CTClosure ctClosure, Set>> rootEntries) {

		Map> enclosureMap = new HashMap<>();

		Queue>> queue =
			new LinkedList<>(rootEntries);

		Map.Entry> entry = null;

		while ((entry = queue.poll()) != null) {
			long classNameId = entry.getKey();

			for (long classPK : entry.getValue()) {
				Set classPKs = enclosureMap.computeIfAbsent(
					classNameId, key -> new HashSet<>());

				if (classPKs.add(classPK)) {
					Map> childPKsMap =
						ctClosure.getChildPKsMap(classNameId, classPK);

					if (!childPKsMap.isEmpty()) {
						queue.addAll(childPKsMap.entrySet());
					}
				}
			}
		}

		return enclosureMap;
	}

	public static Set> getEnclosureParentEntries(
		CTClosure ctClosure, Map> enclosureMap) {

		Set> parentEntries = new HashSet<>();

		visitParentEntries(
			ctClosure,
			(classNameId, classPK, backtraceEntries) -> {
				Set classPKs = enclosureMap.get(classNameId);

				if ((classPKs != null) && classPKs.contains(classPK)) {
					parentEntries.addAll(backtraceEntries);

					return true;
				}

				return false;
			});

		return parentEntries;
	}

	public static void visitParentEntries(
		CTClosure ctClosure, BacktraceVisitor backtraceVisitor) {

		_visitParentEntries(
			ctClosure, ctClosure.getRootPKsMap(), new LinkedList<>(),
			backtraceVisitor);
	}

	public interface BacktraceVisitor {

		public boolean visit(
			long classNameId, long classPK,
			Deque> backtraceEntries);

	}

	private static void _visitParentEntries(
		CTClosure ctClosure, Map> childPKsMap,
		Deque> backtraceEntries,
		BacktraceVisitor backtraceVisitor) {

		for (Map.Entry> entry : childPKsMap.entrySet()) {
			long classNameId = entry.getKey();

			for (long classPK : entry.getValue()) {
				if (backtraceVisitor.visit(
						classNameId, classPK, backtraceEntries)) {

					continue;
				}

				backtraceEntries.push(
					new AbstractMap.SimpleImmutableEntry<>(
						classNameId, classPK));

				_visitParentEntries(
					ctClosure, ctClosure.getChildPKsMap(classNameId, classPK),
					backtraceEntries, backtraceVisitor);

				backtraceEntries.pop();
			}
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy