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.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
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
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
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 }