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.frames.FramedGraph;
23  import eu.ehri.project.core.GraphManager;
24  import eu.ehri.project.core.GraphManagerFactory;
25  import eu.ehri.project.models.UserProfile;
26  import org.apache.commons.cli.CommandLine;
27  import org.apache.commons.cli.Option;
28  import org.apache.commons.cli.Options;
29  
30  import java.util.Optional;
31  
32  /**
33   * Delete a single item via the command line.
34   */
35  public class EntityDelete extends BaseCommand {
36  
37      final static String NAME = "delete";
38  
39      @Override
40      protected void setCustomOptions(Options options) {
41          options.addOption(Option.builder()
42                  .longOpt("user")
43                  .hasArg()
44                  .type(String.class)
45                  .desc("Identifier of user to import as")
46                  .build());
47          options.addOption(Option.builder()
48                  .longOpt("log")
49                  .hasArg()
50                  .type(String.class)
51                  .desc("Log message for import action.")
52                  .build());
53      }
54  
55      @Override
56      public String getUsage() {
57          return String.format("%s --user <user> [OPTIONS] <id>", NAME);
58      }
59  
60      @Override
61      public String getHelp() {
62          return "Delete an item by ID.";
63      }
64  
65      @Override
66      public int execWithOptions(FramedGraph<?> graph,
67              CommandLine cmdLine) throws Exception {
68  
69          // the first argument is the item ID, and that must be specified
70          if (cmdLine.getArgList().size() < 1)
71              throw new RuntimeException(getUsage());
72          String id = cmdLine.getArgs()[0];
73  
74          String logMessage = "Deleting item " + id + " via the command-line";
75          if (cmdLine.hasOption("log")) {
76              logMessage = cmdLine.getOptionValue("log");
77          }
78  
79          GraphManager manager = GraphManagerFactory.getInstance(graph);
80  
81          // Find the user
82          UserProfile user = manager.getEntity(cmdLine.getOptionValue("user"),
83                  UserProfile.class);
84          api(graph, user).delete(id, Optional.of(logMessage));
85  
86          return 0;
87      }
88  }