com.liferay.portal.upgrade.internal.graph.ReleaseGraphManager Maven / Gradle / Ivy
/**
* 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.portal.upgrade.internal.graph;
import com.liferay.portal.kernel.util.Function;
import com.liferay.portal.kernel.util.ListUtil;
import com.liferay.portal.upgrade.registry.UpgradeInfo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
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(final List upgradeInfos) {
_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)) {
return Collections.emptyList();
}
if (!_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 endVertices = getEndVertices();
endVertices.remove(fromVersionString);
List> upgradeInfosList = new ArrayList<>();
for (String endVertex : endVertices) {
List upgradeInfos = getUpgradeInfos(
fromVersionString, endVertex);
if (upgradeInfos.isEmpty()) {
continue;
}
upgradeInfosList.add(upgradeInfos);
}
return upgradeInfosList;
}
protected List getEndVertices() {
final 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;
}