Search in sources :

Example 1 with RelationshipModel

use of org.neo4j.ogm.response.model.RelationshipModel in project neo4j-ogm by neo4j.

the class MultiStatementCypherCompiler method groupRelationshipsByType.

private Map<String, Set<Edge>> groupRelationshipsByType(List<RelationshipBuilder> relationshipBuilders) {
    Map<String, Set<Edge>> relsByType = new HashMap<>();
    for (RelationshipBuilder relationshipBuilder : relationshipBuilders) {
        if (!relsByType.containsKey(relationshipBuilder.type())) {
            relsByType.put(relationshipBuilder.type(), new HashSet<>());
        }
        // Replace the node ids
        RelationshipModel edge = (RelationshipModel) relationshipBuilder.edge();
        edge.setStartNode(context.getId(edge.getStartNode()));
        edge.setEndNode(context.getId(edge.getEndNode()));
        relsByType.get(relationshipBuilder.type()).add(edge);
        relsByType.get(relationshipBuilder.type()).add(edge);
    }
    return relsByType;
}
Also used : RelationshipModel(org.neo4j.ogm.response.model.RelationshipModel) DefaultRelationshipBuilder(org.neo4j.ogm.cypher.compiler.builders.node.DefaultRelationshipBuilder)

Example 2 with RelationshipModel

use of org.neo4j.ogm.response.model.RelationshipModel in project neo4j-ogm by neo4j.

the class RestModelAdapter method buildRelationship.

private RelationshipModel buildRelationship(Object relationship) {
    RelationshipModel relationshipModel = new RelationshipModel();
    relationshipModel.setId(relationshipId(relationship));
    relationshipModel.setStartNode(startNodeId(relationship));
    relationshipModel.setEndNode(endNodeId(relationship));
    relationshipModel.setType(relationshipType(relationship));
    relationshipModel.setProperties(properties(relationship));
    return relationshipModel;
}
Also used : RelationshipModel(org.neo4j.ogm.response.model.RelationshipModel)

Example 3 with RelationshipModel

use of org.neo4j.ogm.response.model.RelationshipModel in project neo4j-ogm by neo4j.

the class RestModelAdapter method buildEntity.

private Object buildEntity(Map entity) {
    if (entity.containsKey("metadata") && ((Map) entity.get("metadata")).get("id") != null) {
        Map entityMetadata = (Map) entity.get("metadata");
        if (entityMetadata.containsKey("labels")) {
            List<String> labelList = (List<String>) (entityMetadata.get("labels"));
            String[] labels = new String[labelList.size()];
            labels = labelList.toArray(labels);
            NodeModel nodeModel = new NodeModel(((Number) entityMetadata.get("id")).longValue());
            nodeModel.setProperties((Map) entity.get("data"));
            nodeModel.setLabels(labels);
            return nodeModel;
        } else if (entityMetadata.containsKey("type")) {
            String relationshipType = (String) entityMetadata.get("type");
            RelationshipModel relationshipModel = new RelationshipModel();
            relationshipModel.setId(((Number) entityMetadata.get("id")).longValue());
            relationshipModel.setProperties((Map) entity.get("data"));
            relationshipModel.setType(relationshipType);
            String startURL = (String) entity.get("start");
            String endURL = (String) entity.get("end");
            relationshipModel.setStartNode(Long.valueOf(startURL.substring(startURL.lastIndexOf("/") + 1)));
            relationshipModel.setEndNode(Long.valueOf(endURL.substring(endURL.lastIndexOf("/") + 1)));
            return relationshipModel;
        }
    }
    return entity;
}
Also used : NodeModel(org.neo4j.ogm.response.model.NodeModel) RelationshipModel(org.neo4j.ogm.response.model.RelationshipModel) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 4 with RelationshipModel

use of org.neo4j.ogm.response.model.RelationshipModel in project neo4j-ogm by neo4j.

the class MultiStatementCypherCompiler method createRelationshipsStatements.

@Override
public List<Statement> createRelationshipsStatements() {
    assertStatementFactoryExists();
    // Group relationships by type and non-null properties
    // key: relationship type, value: Map where key=Set<Property strings>, value: Set of edges with those properties
    Map<String, Map<String, Set<Edge>>> relsByTypeAndProps = new HashMap<>();
    for (RelationshipBuilder relationshipBuilder : newRelationshipBuilders) {
        if (relationshipBuilder.edge().getStartNode() == null || relationshipBuilder.edge().getEndNode() == null) {
            // TODO this is a carry forward from the old emitters. We want to prevent this rel builder getting created or remove it
            continue;
        }
        Map<String, Set<Edge>> relsByProps = relsByTypeAndProps.computeIfAbsent(relationshipBuilder.type(), (key) -> new HashMap<>());
        RelationshipModel edge = (RelationshipModel) relationshipBuilder.edge();
        String primaryId = edge.getPrimaryIdName();
        Set<Edge> rels = relsByProps.computeIfAbsent(primaryId, (s) -> new HashSet<>());
        edge.setStartNode(context.getId(edge.getStartNode()));
        edge.setEndNode(context.getId(edge.getEndNode()));
        rels.add(edge);
    }
    List<Statement> statements = new ArrayList<>();
    // For each relationship type
    for (Map<String, Set<Edge>> edgesByProperties : relsByTypeAndProps.values()) {
        // For each set of unique property keys
        for (Set<Edge> edges : edgesByProperties.values()) {
            NewRelationshipStatementBuilder newRelationshipBuilder = new NewRelationshipStatementBuilder(edges, statementFactory);
            statements.add(newRelationshipBuilder.build());
        }
    }
    return statements;
}
Also used : Statement(org.neo4j.ogm.request.Statement) RelationshipModel(org.neo4j.ogm.response.model.RelationshipModel) NewRelationshipStatementBuilder(org.neo4j.ogm.cypher.compiler.builders.statement.NewRelationshipStatementBuilder) Edge(org.neo4j.ogm.model.Edge) DefaultRelationshipBuilder(org.neo4j.ogm.cypher.compiler.builders.node.DefaultRelationshipBuilder)

Example 5 with RelationshipModel

use of org.neo4j.ogm.response.model.RelationshipModel in project neo4j-ogm by neo4j.

the class GraphModelAdapter method buildRelationship.

void buildRelationship(Object relationship, DefaultGraphModel graphModel, Set<Long> edgeIdentities) {
    if (edgeIdentities.contains(relationshipId(relationship))) {
        return;
    }
    edgeIdentities.add(relationshipId(relationship));
    RelationshipModel edgeModel = new RelationshipModel();
    edgeModel.setId(relationshipId(relationship));
    edgeModel.setType(relationshipType(relationship));
    edgeModel.setStartNode(startNodeId(relationship));
    edgeModel.setEndNode(endNodeId(relationship));
    edgeModel.setProperties(convertArrayPropertiesToCollection(properties(relationship)));
    graphModel.addRelationship(edgeModel);
}
Also used : RelationshipModel(org.neo4j.ogm.response.model.RelationshipModel)

Aggregations

RelationshipModel (org.neo4j.ogm.response.model.RelationshipModel)5 DefaultRelationshipBuilder (org.neo4j.ogm.cypher.compiler.builders.node.DefaultRelationshipBuilder)2 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 NewRelationshipStatementBuilder (org.neo4j.ogm.cypher.compiler.builders.statement.NewRelationshipStatementBuilder)1 Edge (org.neo4j.ogm.model.Edge)1 Statement (org.neo4j.ogm.request.Statement)1 NodeModel (org.neo4j.ogm.response.model.NodeModel)1