use of org.opennms.netmgt.topology.persistence.api.PointEntity in project opennms by OpenNMS.
the class LayoutDaoIT method createVertexPosition.
private VertexPositionEntity createVertexPosition(String vertexNamespace, String vertexId, int x, int y) {
VertexPositionEntity vertexPositionEntity = new VertexPositionEntity();
vertexPositionEntity.setPosition(new PointEntity(x, y));
vertexPositionEntity.setVertexRef(new VertexRefEntity(vertexNamespace, vertexId));
return vertexPositionEntity;
}
use of org.opennms.netmgt.topology.persistence.api.PointEntity in project opennms by OpenNMS.
the class LayoutManager method persistLayout.
public void persistLayout(GraphContainer graphContainer) {
final List<VertexRef> vertexRefs = toVertexRef(graphContainer.getGraph().getDisplayVertices());
final String id = calculateHash(vertexRefs);
LayoutEntity layoutEntity = layoutDao.get(id);
if (layoutEntity == null) {
layoutEntity = new LayoutEntity();
layoutEntity.setId(id);
layoutEntity.setCreated(new Date());
layoutEntity.setCreator(graphContainer.getApplicationContext().getUsername());
}
layoutEntity.setUpdated(new Date());
layoutEntity.setUpdator(graphContainer.getApplicationContext().getUsername());
final Layout layout = graphContainer.getGraph().getLayout();
final List<VertexPositionEntity> vertexPositionEntities = vertexRefs.stream().map(vertexRef -> {
final Point p = layout.getLocation(vertexRef);
PointEntity pointEntity = new PointEntity();
pointEntity.setX((int) p.getX());
pointEntity.setY((int) p.getY());
final VertexPositionEntity vertexEntity = new VertexPositionEntity();
vertexEntity.setVertexRef(toVertexRefEntity(vertexRef));
vertexEntity.setPosition(pointEntity);
return vertexEntity;
}).collect(Collectors.toList());
layoutEntity.getVertexPositions().clear();
for (VertexPositionEntity eachVertexPosition : vertexPositionEntities) {
layoutEntity.addVertexPosition(eachVertexPosition);
}
layoutDao.saveOrUpdate(layoutEntity);
}
use of org.opennms.netmgt.topology.persistence.api.PointEntity in project opennms by OpenNMS.
the class LayoutManager method isPersistedLayoutEqualToCurrentLayout.
public boolean isPersistedLayoutEqualToCurrentLayout(Graph graph) {
LayoutEntity layoutEntity = loadLayout(graph);
if (layoutEntity != null) {
// If we have a layout persisted, we verify if it is equal.
final Map<VertexRef, Point> persistedLocations = layoutEntity.getVertexPositions().stream().collect(Collectors.toMap((Function<VertexPositionEntity, VertexRef>) vertexPositionEntity -> {
VertexRefEntity vertexRefEntity = vertexPositionEntity.getVertexRef();
return new DefaultVertexRef(vertexRefEntity.getNamespace(), vertexRefEntity.getId());
}, vertexPositionEntity -> {
PointEntity position = vertexPositionEntity.getPosition();
return new Point(position.getX(), position.getY());
}));
// The locations may contain elements currently not visible, we filter them
final Map<VertexRef, Point> manualLocations = new HashMap<>();
graph.getLayout().getLocations().forEach((key, value) -> {
if (persistedLocations.containsKey(key)) {
// layoutEntity stores int coordinates, but manualLocations are stored as double.
// Convert to int to make it comparable.
manualLocations.put(key, new Point((int) value.getX(), (int) value.getY()));
}
});
final boolean layoutIsEqual = manualLocations.equals(persistedLocations);
return layoutIsEqual;
}
// We don't have anything persisted, so they are not equal
return false;
}
use of org.opennms.netmgt.topology.persistence.api.PointEntity in project opennms by OpenNMS.
the class ManualLayoutAlgorithm method updateLayout.
@Override
public void updateLayout(Graph graph) {
final LayoutEntity layoutEntity = layoutManager != null ? layoutManager.loadLayout(graph) : null;
if (layoutEntity != null) {
// if we have a persisted layout, we apply it ...
final Layout layout = graph.getLayout();
final Collection<Vertex> vertices = graph.getDisplayVertices();
for (Vertex vertex : vertices) {
PointEntity pointEntity = layoutEntity.getPosition(vertex.getNamespace(), vertex.getId());
layout.setLocation(vertex, new Point(pointEntity.getX(), pointEntity.getY()));
}
} else {
// otherwise we apply the manual layout ...
final Collection<Vertex> vertices = graph.getDisplayVertices();
final Layout layout = graph.getLayout();
final long notLayedOutCount = vertices.stream().filter(v -> {
Point location = layout.getLocation(v);
return location.getX() == 0 && location.getY() == 0;
}).count();
final long noVertexLocationCount = vertices.stream().filter(v -> {
boolean hasNoX = v.getX() == null || v.getX().intValue() == 0;
boolean hasNoY = v.getY() == null || v.getY().intValue() == 0;
return hasNoX && hasNoY;
}).count();
// manually apply the Grid Layout
if (notLayedOutCount == vertices.size() && noVertexLocationCount == vertices.size()) {
new GridLayoutAlgorithm().updateLayout(graph);
} else if (noVertexLocationCount != vertices.size()) {
// If we have at least one vertex with coordinates != (0,0), we apply them to the layout
for (Vertex vertex : vertices) {
layout.setLocation(vertex, new Point(vertex.getX(), vertex.getY()));
}
} else {
// This is only done when the user explicitly saves the layout
for (Vertex vertex : vertices) {
Point p = layout.getLocation(vertex);
layout.setLocation(vertex, p);
}
}
}
}
use of org.opennms.netmgt.topology.persistence.api.PointEntity in project opennms by OpenNMS.
the class ManualLayoutAlgorithmTest method verifyLayoutCoordinatesHavePriority.
/*
* If persisted layout is defined, verify that it has priority.
*/
@Test
public void verifyLayoutCoordinatesHavePriority() {
final GraphProvider graphProvider = new SimpleGraphBuilder("dummy").vertex("vertex1").vX(1).vY(1).get();
final ManualTest test = new ManualTest(graphProvider);
final LayoutEntity persistedLayout = new LayoutEntity();
int x = 5;
int y = 5;
for (VertexRef eachVertex : graphProvider.getVertices()) {
VertexPositionEntity vertexPositionEntity = new VertexPositionEntity();
vertexPositionEntity.setVertexRef(LayoutManager.toVertexRefEntity(eachVertex));
vertexPositionEntity.setPosition(new PointEntity(x++, y++));
persistedLayout.addVertexPosition(vertexPositionEntity);
}
Mockito.when(test.layoutManager.loadLayout(test.graph)).thenReturn(persistedLayout);
new ManualLayoutAlgorithm(test.layoutManager).updateLayout(test.graph);
Assert.assertEquals(ImmutableMap.builder().put(new DefaultVertexRef("dummy", "vertex1"), new Point(5, 5)).build(), test.layout.getLocations());
}
Aggregations