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

com.liferay.portal.upgrade.internal.graph.ReleaseGraphManager 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.portal.upgrade.internal.graph;

import com.liferay.portal.kernel.util.ListUtil;
import com.liferay.portal.upgrade.internal.registry.UpgradeInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.Function;

import org.jgrapht.DirectedGraph;
import org.jgrapht.alg.DijkstraShortestPath;
import org.jgrapht.graph.DefaultDirectedGraph;

/**
 * @author Miguel Pastor
 * @author Carlos Sierra Andrés
 */
public class ReleaseGraphManager {

	public ReleaseGraphManager(List upgradeInfos) {
		if (upgradeInfos == null) {
			upgradeInfos = new ArrayList<>();
		}

		_directedGraph = new DefaultDirectedGraph<>(
			new UpgradeProcessEdgeFactory(upgradeInfos));

		for (UpgradeInfo upgradeInfo : upgradeInfos) {
			_directedGraph.addVertex(upgradeInfo.getFromSchemaVersionString());
			_directedGraph.addVertex(upgradeInfo.getToSchemaVersionString());

			_directedGraph.addEdge(
				upgradeInfo.getFromSchemaVersionString(),
				upgradeInfo.getToSchemaVersionString(),
				new UpgradeProcessEdge(upgradeInfo));
		}
	}

	public List getUpgradeInfos(
		String fromVersionString, String toVersionString) {

		if (!_directedGraph.containsVertex(fromVersionString) ||
			!_directedGraph.containsVertex(toVersionString)) {

			return Collections.emptyList();
		}

		DijkstraShortestPath dijkstraShortestPath =
			new DijkstraShortestPath<>(
				_directedGraph, fromVersionString, toVersionString);

		List upgradeProcessEdges =
			dijkstraShortestPath.getPathEdgeList();

		if (upgradeProcessEdges == null) {
			return Collections.emptyList();
		}

		return ListUtil.toList(
			upgradeProcessEdges,
			new Function() {

				@Override
				public UpgradeInfo apply(
					UpgradeProcessEdge upgradeProcessEdge) {

					return upgradeProcessEdge.getUpgradeInfo();
				}

			});
	}

	public List> getUpgradeInfosList(
		String fromVersionString) {

		List> upgradeInfosList = new ArrayList<>();

		List endVertices = getEndVertices();

		endVertices.remove(fromVersionString);

		for (String endVertex : endVertices) {
			List upgradeInfos = getUpgradeInfos(
				fromVersionString, endVertex);

			if (!upgradeInfos.isEmpty()) {
				upgradeInfosList.add(upgradeInfos);
			}
		}

		return upgradeInfosList;
	}

	protected List getEndVertices() {
		List endVertices = new ArrayList<>();

		Set vertices = _directedGraph.vertexSet();

		for (String vertex : vertices) {
			Set upgradeProcessEdges =
				_directedGraph.outgoingEdgesOf(vertex);

			if (upgradeProcessEdges.isEmpty()) {
				endVertices.add(vertex);
			}
		}

		return endVertices;
	}

	private final DirectedGraph _directedGraph;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy