Search in sources :

Example 1 with LayoutEntity

use of org.opennms.netmgt.topology.persistence.api.LayoutEntity in project opennms by OpenNMS.

the class LayoutDaoIT method verifyCRUD.

@Test
@Transactional
public void verifyCRUD() {
    // Nothing created yet
    Assert.assertEquals(0, layoutDao.countAll());
    Assert.assertEquals(0, layoutDao.countMatching(new Criteria(VertexPositionEntity.class)));
    // Create dummy
    LayoutEntity layout = new LayoutEntity();
    layout.setId("hash");
    layout.setCreated(new Date());
    layout.setCreator("mvrueden");
    layout.setUpdated(layout.getCreated());
    layout.setUpdator(layout.getCreator());
    layout.addVertexPosition(createVertexPosition("dummy", "1", 0, 0));
    layout.addVertexPosition(createVertexPosition("dummy", "2", 1, 1));
    // create and verify creation
    layoutDao.saveOrUpdate(layout);
    Assert.assertEquals(1, layoutDao.countAll());
    Assert.assertEquals(2, layoutDao.countMatching(new Criteria(VertexPositionEntity.class)));
    // Update
    // Remove Vertex
    layout.getVertexPositions().remove(0);
    layoutDao.update(layout);
    Assert.assertEquals(1, layoutDao.countAll());
    Assert.assertEquals(1, layoutDao.countMatching(new Criteria(VertexPositionEntity.class)));
    // Add Vertex
    layout.addVertexPosition(createVertexPosition("dummy", "3", 2, 2));
    layoutDao.update(layout);
    Assert.assertEquals(1, layoutDao.countAll());
    Assert.assertEquals(2, layoutDao.countMatching(new Criteria(VertexPositionEntity.class)));
    // Update layout
    layout.setUpdated(new Date());
    layout.setUpdator("ulf");
    Assert.assertEquals(1, layoutDao.countAll());
    Assert.assertEquals(2, layoutDao.countMatching(new Criteria(VertexPositionEntity.class)));
    // Delete
    layoutDao.delete(layout);
    Assert.assertEquals(0, layoutDao.countAll());
    Assert.assertEquals(0, layoutDao.countMatching(new Criteria(VertexPositionEntity.class)));
}
Also used : Criteria(org.opennms.core.criteria.Criteria) LayoutEntity(org.opennms.netmgt.topology.persistence.api.LayoutEntity) Date(java.util.Date) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with LayoutEntity

use of org.opennms.netmgt.topology.persistence.api.LayoutEntity 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);
}
Also used : GraphContainer(org.opennms.features.topology.api.GraphContainer) Date(java.util.Date) Vertex(org.opennms.features.topology.api.topo.Vertex) HashMap(java.util.HashMap) Hashing(com.google.common.hash.Hashing) Function(java.util.function.Function) VertexPositionEntity(org.opennms.netmgt.topology.persistence.api.VertexPositionEntity) Point(org.opennms.features.topology.api.Point) Layout(org.opennms.features.topology.api.Layout) Map(java.util.Map) LayoutDao(org.opennms.netmgt.topology.persistence.api.LayoutDao) DefaultVertexRef(org.opennms.features.topology.api.topo.DefaultVertexRef) Collection(java.util.Collection) Graph(org.opennms.features.topology.api.Graph) Collectors(java.util.stream.Collectors) PointEntity(org.opennms.netmgt.topology.persistence.api.PointEntity) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) TransactionAwareBeanProxyFactory(org.opennms.netmgt.vaadin.core.TransactionAwareBeanProxyFactory) List(java.util.List) VertexRefEntity(org.opennms.netmgt.topology.persistence.api.VertexRefEntity) TransactionOperations(org.springframework.transaction.support.TransactionOperations) LayoutEntity(org.opennms.netmgt.topology.persistence.api.LayoutEntity) Comparator(java.util.Comparator) VertexRef(org.opennms.features.topology.api.topo.VertexRef) VertexPositionEntity(org.opennms.netmgt.topology.persistence.api.VertexPositionEntity) Layout(org.opennms.features.topology.api.Layout) LayoutEntity(org.opennms.netmgt.topology.persistence.api.LayoutEntity) Point(org.opennms.features.topology.api.Point) PointEntity(org.opennms.netmgt.topology.persistence.api.PointEntity) DefaultVertexRef(org.opennms.features.topology.api.topo.DefaultVertexRef) VertexRef(org.opennms.features.topology.api.topo.VertexRef) Date(java.util.Date)

Example 3 with LayoutEntity

use of org.opennms.netmgt.topology.persistence.api.LayoutEntity 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;
}
Also used : GraphContainer(org.opennms.features.topology.api.GraphContainer) Date(java.util.Date) Vertex(org.opennms.features.topology.api.topo.Vertex) HashMap(java.util.HashMap) Hashing(com.google.common.hash.Hashing) Function(java.util.function.Function) VertexPositionEntity(org.opennms.netmgt.topology.persistence.api.VertexPositionEntity) Point(org.opennms.features.topology.api.Point) Layout(org.opennms.features.topology.api.Layout) Map(java.util.Map) LayoutDao(org.opennms.netmgt.topology.persistence.api.LayoutDao) DefaultVertexRef(org.opennms.features.topology.api.topo.DefaultVertexRef) Collection(java.util.Collection) Graph(org.opennms.features.topology.api.Graph) Collectors(java.util.stream.Collectors) PointEntity(org.opennms.netmgt.topology.persistence.api.PointEntity) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) TransactionAwareBeanProxyFactory(org.opennms.netmgt.vaadin.core.TransactionAwareBeanProxyFactory) List(java.util.List) VertexRefEntity(org.opennms.netmgt.topology.persistence.api.VertexRefEntity) TransactionOperations(org.springframework.transaction.support.TransactionOperations) LayoutEntity(org.opennms.netmgt.topology.persistence.api.LayoutEntity) Comparator(java.util.Comparator) VertexRef(org.opennms.features.topology.api.topo.VertexRef) Function(java.util.function.Function) VertexRefEntity(org.opennms.netmgt.topology.persistence.api.VertexRefEntity) DefaultVertexRef(org.opennms.features.topology.api.topo.DefaultVertexRef) HashMap(java.util.HashMap) LayoutEntity(org.opennms.netmgt.topology.persistence.api.LayoutEntity) PointEntity(org.opennms.netmgt.topology.persistence.api.PointEntity) Point(org.opennms.features.topology.api.Point) DefaultVertexRef(org.opennms.features.topology.api.topo.DefaultVertexRef) VertexRef(org.opennms.features.topology.api.topo.VertexRef)

Example 4 with LayoutEntity

use of org.opennms.netmgt.topology.persistence.api.LayoutEntity in project opennms by OpenNMS.

the class LayoutHintComponent method graphChanged.

@Override
public void graphChanged(GraphContainer graphContainer) {
    if (!(graphContainer.getLayoutAlgorithm() instanceof ManualLayoutAlgorithm)) {
        LayoutEntity layoutEntity = layoutManager.loadLayout(graphContainer.getGraph());
        if (layoutEntity != null) {
            boolean isEqualLayout = layoutManager.isPersistedLayoutEqualToCurrentLayout(graphContainer.getGraph());
            getCompositionRoot().setVisible(!isEqualLayout);
        } else {
            getCompositionRoot().setVisible(false);
        }
    } else {
        getCompositionRoot().setVisible(false);
    }
}
Also used : ManualLayoutAlgorithm(org.opennms.features.topology.app.internal.ManualLayoutAlgorithm) LayoutEntity(org.opennms.netmgt.topology.persistence.api.LayoutEntity)

Example 5 with LayoutEntity

use of org.opennms.netmgt.topology.persistence.api.LayoutEntity 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);
            }
        }
    }
}
Also used : LayoutManager(org.opennms.features.topology.app.internal.support.LayoutManager) Point(org.opennms.features.topology.api.Point) GridLayoutAlgorithm(org.opennms.features.topology.app.internal.jung.GridLayoutAlgorithm) Layout(org.opennms.features.topology.api.Layout) Collection(java.util.Collection) Vertex(org.opennms.features.topology.api.topo.Vertex) LayoutEntity(org.opennms.netmgt.topology.persistence.api.LayoutEntity) Graph(org.opennms.features.topology.api.Graph) LayoutAlgorithm(org.opennms.features.topology.api.LayoutAlgorithm) PointEntity(org.opennms.netmgt.topology.persistence.api.PointEntity) Vertex(org.opennms.features.topology.api.topo.Vertex) Layout(org.opennms.features.topology.api.Layout) LayoutEntity(org.opennms.netmgt.topology.persistence.api.LayoutEntity) PointEntity(org.opennms.netmgt.topology.persistence.api.PointEntity) Point(org.opennms.features.topology.api.Point) GridLayoutAlgorithm(org.opennms.features.topology.app.internal.jung.GridLayoutAlgorithm)

Aggregations

LayoutEntity (org.opennms.netmgt.topology.persistence.api.LayoutEntity)7 Date (java.util.Date)4 Point (org.opennms.features.topology.api.Point)4 PointEntity (org.opennms.netmgt.topology.persistence.api.PointEntity)4 Collection (java.util.Collection)3 Graph (org.opennms.features.topology.api.Graph)3 Layout (org.opennms.features.topology.api.Layout)3 DefaultVertexRef (org.opennms.features.topology.api.topo.DefaultVertexRef)3 Vertex (org.opennms.features.topology.api.topo.Vertex)3 VertexRef (org.opennms.features.topology.api.topo.VertexRef)3 VertexPositionEntity (org.opennms.netmgt.topology.persistence.api.VertexPositionEntity)3 Hashing (com.google.common.hash.Hashing)2 StandardCharsets (java.nio.charset.StandardCharsets)2 Comparator (java.util.Comparator)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2