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.importers.ImportLog;
26  import eu.ehri.project.importers.cvoc.SkosImporter;
27  import eu.ehri.project.importers.cvoc.SkosImporterFactory;
28  import eu.ehri.project.models.UserProfile;
29  import eu.ehri.project.models.cvoc.Vocabulary;
30  import org.apache.commons.cli.CommandLine;
31  import org.apache.commons.cli.Option;
32  import org.apache.commons.cli.Options;
33  
34  import java.util.Map.Entry;
35  
36  
37  
38  
39  public class SkosVocabularyImport extends BaseCommand {
40  
41      final static String NAME = "skos-import";
42  
43      @Override
44      protected void setCustomOptions(Options options) {
45          options.addOption(Option.builder()
46                  .longOpt("scope")
47                  .hasArg()
48                  .required()
49                  .type(String.class)
50                  .desc("Identifier of scope to import into, i.e. repository")
51                  .build());
52          options.addOption(Option.builder()
53                  .longOpt("user")
54                  .hasArg()
55                  .required()
56                  .type(String.class)
57                  .desc("Identifier of user to import as")
58                  .build());
59          options.addOption(Option.builder()
60                  .longOpt("tolerant")
61                  .desc("Don't error if a file is not valid.")
62                  .build());
63          options.addOption(Option.builder()
64                  .longOpt("log")
65                  .hasArg()
66                  .type(String.class)
67                  .desc("Log message for action.")
68                  .build());
69      }
70  
71      @Override
72      public String getUsage() {
73          return NAME + " [OPTIONS] -user <user-id> -scope <vocabulary-id> <skos.rdf>";
74      }
75  
76      @Override
77      public String getHelp() {
78          return "Import a Skos file into the graph database, using the specified " +
79                  "Vocabulary and User.";
80      }
81  
82      public int execWithOptions(FramedGraph<?> graph,
83              CommandLine cmdLine) throws Exception {
84  
85          GraphManager manager = GraphManagerFactory.getInstance(graph);
86          String logMessage = "Imported from command-line";
87          if (cmdLine.hasOption("log")) {
88              logMessage = cmdLine.getOptionValue("log");
89          }
90  
91          
92          if (cmdLine.getArgList().size() < 1)
93              throw new RuntimeException(getUsage());
94  
95          if (!cmdLine.hasOption("scope")) {
96              throw new RuntimeException("No scope (vocabulary) given for SKOS import");
97          }
98  
99          String filePath = cmdLine.getArgList().get(0);
100 
101         try {
102             
103             Vocabulary vocabulary = manager.getEntity(
104                     cmdLine.getOptionValue("scope"), Vocabulary.class);
105             UserProfile user = manager.getEntity(
106                     cmdLine.getOptionValue("user"), UserProfile.class);
107 
108             SkosImporter importer = SkosImporterFactory.newSkosImporter(graph, user, vocabulary);
109             ImportLog log = importer
110                     .setTolerant(cmdLine.hasOption("tolerant"))
111                     .importFile(filePath, logMessage);
112             System.out.println(log);
113             if (log.getErrored() > 0) {
114                 System.out.println("Errors:");
115                 for (Entry<String, String> entry : log.getErrors().entrySet()) {
116                     System.out.printf(" - %-20s : %s%n", entry.getKey(),
117                             entry.getValue());
118                 }
119             }
120         } catch (Exception e) {
121             e.printStackTrace();
122             return 1;
123         }
124         return 0;
125     }
126 }