View Javadoc

1   /*
2    * Copyright 2015 Data Archiving and Networked Services (an institute of
3    * Koninklijke Nederlandse Akademie van Wetenschappen), King's College London,
4    * Georg-August-Universitaet Goettingen Stiftung Oeffentlichen Rechts
5    *
6    * Licensed under the EUPL, Version 1.1 or – as soon they will be approved by
7    * the European Commission - subsequent versions of the EUPL (the "Licence");
8    * You may not use this work except in compliance with the Licence.
9    * You may obtain a copy of the Licence at:
10   *
11   * https://joinup.ec.europa.eu/software/page/eupl
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the Licence is distributed on an "AS IS" basis,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the Licence for the specific language governing
17   * permissions and limitations under the Licence.
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   * Add an arbitrary edge between two nodes.
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  }