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.google.common.collect.Maps;
23  import com.tinkerpop.frames.FramedGraph;
24  import eu.ehri.project.acl.SystemScope;
25  import eu.ehri.project.core.GraphManager;
26  import eu.ehri.project.core.GraphManagerFactory;
27  import eu.ehri.project.definitions.Ontology;
28  import eu.ehri.project.exceptions.DeserializationError;
29  import eu.ehri.project.exceptions.ItemNotFound;
30  import eu.ehri.project.exceptions.PermissionDenied;
31  import eu.ehri.project.exceptions.ValidationError;
32  import eu.ehri.project.models.Country;
33  import eu.ehri.project.models.EntityClass;
34  import eu.ehri.project.models.Group;
35  import eu.ehri.project.models.base.Accessor;
36  import eu.ehri.project.persistence.Bundle;
37  import org.apache.commons.cli.CommandLine;
38  import org.apache.commons.cli.Option;
39  import org.apache.commons.cli.Options;
40  
41  /**
42   * Add a country.
43   */
44  public class CountryAdd extends BaseCommand {
45  
46      final static String NAME = "countryadd";
47  
48      @Override
49      protected void setCustomOptions(Options options) {
50          options.addOption(Option.builder()
51                  .longOpt("user")
52                  .hasArg()
53                  .type(String.class)
54                  .desc("Identifier of user to import as")
55                  .build());
56          options.addOption(Option.builder("n")
57                  .longOpt("name")
58                  .hasArg()
59                  .type(String.class)
60                  .desc("Country's full name")
61                  .build());
62          options.addOption(Option.builder()
63                  .longOpt("log")
64                  .hasArg()
65                  .type(String.class)
66                  .desc("Log message for create action.")
67                  .build());
68      }
69  
70      @Override
71      public String getUsage() {
72          return String.format("%s <country-identifier> [-n <full country name>] [-c <log comment>]", NAME);
73      }
74  
75      @Override
76      public String getHelp() {
77          return "Create a new country";
78      }
79  
80      @Override
81      public int execWithOptions(FramedGraph<?> graph,
82              CommandLine cmdLine) throws ItemNotFound, ValidationError, PermissionDenied, DeserializationError {
83  
84          GraphManager manager = GraphManagerFactory.getInstance(graph);
85          String logMessage = cmdLine.getOptionValue("c",
86                  "Created via command-line");
87  
88          if (cmdLine.getArgList().size() < 1)
89              throw new RuntimeException(getUsage());
90  
91          // Fetch the admin accessor, who's going to do the work.
92          Accessor admin = manager.getEntity(Group.ADMIN_GROUP_IDENTIFIER,
93                  Accessor.class);
94  
95          String countryId = cmdLine.getArgList().get(0);
96          String countryName = cmdLine.getOptionValue("n", countryId);
97  
98          Bundle bundle = Bundle.of(EntityClass.COUNTRY,
99                  Maps.<String, Object>newHashMap())
100                 .withDataValue(Ontology.IDENTIFIER_KEY, countryId)
101                 .withDataValue(Ontology.NAME_KEY, countryName)
102                 .generateIds(SystemScope.getInstance().idPath());
103 
104         try {
105             api(graph, admin).create(bundle, Country.class, getLogMessage(logMessage));
106         } catch (ValidationError e) {
107             System.err.printf("A country with id: '%s' already exists%n", bundle.getId());
108             return 9;
109         } catch (Exception e) {
110             throw new RuntimeException(e);
111         }
112 
113         return 0;
114     }
115 }