1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package eu.ehri.project.commands;
21
22 import com.tinkerpop.blueprints.Vertex;
23 import com.tinkerpop.frames.FramedGraph;
24 import eu.ehri.project.core.GraphManager;
25 import eu.ehri.project.core.GraphManagerFactory;
26 import eu.ehri.project.exceptions.ItemNotFound;
27 import eu.ehri.project.models.utils.JavaHandlerUtils;
28 import org.apache.commons.cli.CommandLine;
29 import org.apache.commons.cli.Option;
30 import org.apache.commons.cli.Options;
31
32
33
34
35 public class RelationAdd extends BaseCommand {
36
37 final static String NAME = "add-rel";
38
39 @Override
40 protected void setCustomOptions(Options options) {
41 options.addOption(Option.builder("s")
42 .longOpt("single")
43 .desc("Ensure the out entity only has one relationship of this type by removing any others")
44 .build());
45 options.addOption(Option.builder("d")
46 .longOpt("allow-duplicates")
47 .desc("Allow creating multiple edges with the same label between the same two nodes")
48 .build());
49 }
50
51 @Override
52 public String getUsage() {
53 return "Usage: add-rel [OPTIONS] <source> <rel-name> <target>";
54 }
55
56 @Override
57 public String getHelp() {
58 return "Create a relationship between a source and a target";
59 }
60
61 @Override
62 public int execWithOptions(FramedGraph<?> graph,
63 CommandLine cmdLine) throws ItemNotFound {
64
65 GraphManager manager = GraphManagerFactory.getInstance(graph);
66
67 if (cmdLine.getArgList().size() < 3)
68 throw new RuntimeException(getUsage());
69
70 String src = cmdLine.getArgList().get(0);
71 String label = cmdLine.getArgList().get(1);
72 String dst = cmdLine.getArgList().get(2);
73
74 Vertex source = manager.getVertex(src);
75 Vertex target = manager.getVertex(dst);
76
77 if (cmdLine.hasOption("allow-duplicates")) {
78 source.addEdge(label, target);
79 } else if (cmdLine.hasOption("unique")) {
80 if (!JavaHandlerUtils.addUniqueRelationship(source, target, label)) {
81 System.err.println("Relationship already exists");
82 }
83 } else {
84 if (!JavaHandlerUtils.addSingleRelationship(source, target, label)) {
85 System.err.println("Relationship already exists");
86 }
87 }
88
89 return 0;
90 }
91 }