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.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   * Delete an entire class of entities via the command line.
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          // the first argument is the entity type, and that must be specified
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          // Find the user
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 }