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.CloseableIterable;
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.definitions.EventTypes;
27 import eu.ehri.project.exceptions.ItemNotFound;
28 import eu.ehri.project.exceptions.PermissionDenied;
29 import eu.ehri.project.exceptions.SerializationError;
30 import eu.ehri.project.exceptions.ValidationError;
31 import eu.ehri.project.models.EntityClass;
32 import eu.ehri.project.models.UserProfile;
33 import eu.ehri.project.models.base.Accessible;
34 import eu.ehri.project.persistence.ActionManager;
35 import eu.ehri.project.api.Api;
36 import org.apache.commons.cli.CommandLine;
37 import org.apache.commons.cli.Option;
38 import org.apache.commons.cli.Options;
39
40
41
42
43 public class DeleteEntities extends BaseCommand {
44
45 final static String NAME = "delete-all";
46
47 @Override
48 protected void setCustomOptions(Options options) {
49 options.addOption(Option.builder()
50 .longOpt("user")
51 .hasArg()
52 .required()
53 .type(String.class)
54 .hasArg().desc("Identifier of user taking action")
55 .build());
56 options.addOption(Option.builder()
57 .longOpt("log")
58 .hasArg()
59 .type(String.class)
60 .desc("Log message for delete action.")
61 .build());
62 }
63
64 @Override
65 public String getUsage() {
66 return String.format("%s [OPTIONS] <type>", NAME);
67 }
68
69 @Override
70 public String getHelp() {
71 return "Delete ALL entities of a given type.";
72 }
73
74 @Override
75 public int execWithOptions(FramedGraph<?> graph,
76 CommandLine cmdLine) throws Exception {
77
78
79 if (cmdLine.getArgList().size() < 1)
80 throw new RuntimeException(getUsage());
81 EntityClass type = EntityClass.withName(cmdLine.getArgs()[0]);
82 Class<?> cls = type.getJavaClass();
83
84 String logMessage = "Deleting items of type " + type + " via the command-line";
85 if (cmdLine.hasOption("log")) {
86 logMessage = cmdLine.getOptionValue("log");
87 }
88
89 if (!Accessible.class.isAssignableFrom(cls))
90 throw new RuntimeException("Unknown accessible entity: " + type);
91
92 GraphManager manager = GraphManagerFactory.getInstance(graph);
93
94
95 UserProfile user = manager.getEntity(cmdLine.getOptionValue("user"),
96 UserProfile.class);
97
98 new ActionManager(graph).newEventContext(user,
99 EventTypes.deletion,
100 getLogMessage(logMessage))
101 .commit();
102 deleteIds(graph, manager, type, user);
103
104 return 0;
105 }
106
107 private void deleteIds(FramedGraph<?> graph, GraphManager manager, EntityClass type, UserProfile user)
108 throws SerializationError, ValidationError, ItemNotFound, PermissionDenied {
109 Api api = api(graph, user).enableLogging(false);
110 try (CloseableIterable<Accessible> items = manager.getEntities(type, Accessible.class)) {
111 for (Accessible acc : items) {
112 System.out.println(acc.getId());
113 api.delete(acc.getId());
114 }
115 }
116 }
117 }