eu.drus.jpa.unit.neo4j.operation.UpdateOperation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jpa-unit-neo4j Show documentation
Show all versions of jpa-unit-neo4j Show documentation
JUnit extension for simple testing of JPA entities and components
The newest version!
package eu.drus.jpa.unit.neo4j.operation;
import static java.util.stream.Collectors.toList;
import static org.neo4j.cypherdsl.CypherQuery.identifier;
import static org.neo4j.cypherdsl.CypherQuery.literal;
import static org.neo4j.cypherdsl.CypherQuery.match;
import static org.neo4j.cypherdsl.CypherQuery.property;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.jgrapht.Graph;
import org.neo4j.cypherdsl.expression.SetExpression;
import org.neo4j.cypherdsl.grammar.UpdateNext;
import eu.drus.jpa.unit.neo4j.dataset.Edge;
import eu.drus.jpa.unit.neo4j.dataset.Node;
public class UpdateOperation extends AbstractNeo4JOperation {
@Override
public void execute(final Connection connection, final Graph graph) throws SQLException {
for (final Node node : graph.vertexSet()) {
final List attributes = node.getAttributes().stream().filter(a -> !a.isId())
.map(a -> property(identifier(node.getId()).property(a.getName()), literal(a.getValue()))).collect(toList());
if (!attributes.isEmpty()) {
// it is only empty if the node itself contains only the id attribute, which is not
// going to be set
final UpdateNext query = match(node.toPath().withIdAttributes().build()).set(attributes);
executeQuery(connection, query.toString());
}
}
for (final Edge edge : graph.edgeSet()) {
final Node sourceNode = edge.getSourceNode();
final Node targetNode = edge.getTargetNode();
final List attributes = edge.getAttributes().stream()
.map(a -> property(identifier(edge.getId()).property(a.getName()), literal(a.getValue()))).collect(toList());
final UpdateNext query = match(sourceNode.toPath().withIdAttributes().build(), targetNode.toPath().withIdAttributes().build())
.merge(edge.toPath().build()).set(attributes);
executeQuery(connection, query.toString());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy