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.google.common.collect.Lists;
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.importers.base.ItemImporter;
28 import eu.ehri.project.importers.managers.CsvImportManager;
29 import eu.ehri.project.importers.ImportLog;
30 import eu.ehri.project.models.UserProfile;
31 import eu.ehri.project.models.base.PermissionScope;
32 import org.apache.commons.cli.CommandLine;
33 import org.apache.commons.cli.Option;
34 import org.apache.commons.cli.Options;
35
36 import java.util.List;
37 import java.util.Map;
38
39
40
41
42
43 public abstract class ImportCsvCommand extends BaseCommand {
44 private final Class<? extends ItemImporter> importer;
45
46 public ImportCsvCommand(Class<? extends ItemImporter> importer) {
47 this.importer = importer;
48 }
49
50 @Override
51 protected void setCustomOptions(Options options) {
52 options.addOption(Option.builder()
53 .longOpt("scope")
54 .hasArg()
55 .required()
56 .type(String.class)
57 .desc("Identifier of scope to import into, i.e. repository")
58 .build());
59 options.addOption(Option.builder()
60 .longOpt("user")
61 .hasArg()
62 .required()
63 .type(String.class)
64 .desc("Identifier of user to import as")
65 .build());
66 options.addOption(Option.builder()
67 .longOpt("tolerant")
68 .desc("Don't error if a file is not valid.")
69 .build());
70 options.addOption(Option.builder()
71 .longOpt("allow-updates")
72 .desc("Allow the ingest process to update existing items.")
73 .build());
74 options.addOption(Option.builder()
75 .longOpt("log")
76 .hasArg()
77 .type(String.class)
78 .desc("Log message for action.")
79 .build());
80 }
81
82 @Override
83 public int execWithOptions(FramedGraph<?> graph,
84 CommandLine cmdLine) throws Exception {
85
86 GraphManager manager = GraphManagerFactory.getInstance(graph);
87 String logMessage = "Imported from command-line";
88 if (cmdLine.hasOption("log")) {
89 logMessage = cmdLine.getOptionValue("log");
90 }
91
92 boolean tolerant = cmdLine.hasOption("tolerant");
93 boolean allowUpdates = cmdLine.hasOption("allow-updates");
94
95 if (cmdLine.getArgList().size() < 1)
96 throw new RuntimeException(getUsage());
97
98 List<String> filePaths = Lists.newLinkedList();
99 filePaths.addAll(cmdLine.getArgList());
100
101 try {
102
103 PermissionScope scope = SystemScope.getInstance();
104 if (cmdLine.hasOption("scope")) {
105 scope = manager.getEntity(cmdLine.getOptionValue("scope"), PermissionScope.class);
106 }
107
108
109 UserProfile user = manager.getEntity(cmdLine.getOptionValue("user"),
110 UserProfile.class);
111
112 ImportLog log = new CsvImportManager(graph, scope, user, tolerant, allowUpdates, importer)
113 .importFiles(filePaths, logMessage);
114
115 System.out.println(log);
116 if (log.getErrored() > 0) {
117 System.out.println("Errors:");
118 for (Map.Entry<String, String> entry : log.getErrors().entrySet()) {
119 System.out.printf(" - %-20s : %s%n", entry.getKey(),
120 entry.getValue());
121 }
122 }
123 } catch (Exception e) {
124 e.printStackTrace();
125 return 1;
126 }
127 return 0;
128 }
129 }