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.frames.FramedGraph;
23  import eu.ehri.project.models.base.Accessor;
24  import eu.ehri.project.api.Api;
25  import eu.ehri.project.api.ApiFactory;
26  import org.apache.commons.cli.CommandLine;
27  import org.apache.commons.cli.CommandLineParser;
28  import org.apache.commons.cli.DefaultParser;
29  import org.apache.commons.cli.HelpFormatter;
30  import org.apache.commons.cli.Options;
31  import org.apache.commons.cli.ParseException;
32  import org.slf4j.Logger;
33  
34  import java.io.PrintWriter;
35  import java.io.StringWriter;
36  import java.util.Optional;
37  
38  /**
39   * Abstract base class for commands. Provides the main
40   * entry points for interaction:
41   * <p>
42   * <ul>
43   * <li>Set options</li>
44   * <li>Get help</li>
45   * <li>Get usage</li>
46   * <li>Execute with options</li>
47   * </ul>
48   */
49  public abstract class BaseCommand implements Command {
50  
51      static final Logger logger = org.slf4j.LoggerFactory.getLogger(Command.class);
52      protected final Options options = new Options();
53      private final CommandLineParser parser = new DefaultParser();
54  
55      protected void setCustomOptions(Options options) {
56      }
57  
58      public abstract String getHelp();
59  
60      public abstract String getUsage();
61  
62      /**
63       * Execute this command with the given database and un-parsed
64       * command line arguments
65       *
66       * @param graph the graph database
67       * @param args  the raw command line arguments
68       * @return a command return code
69       */
70      public final int exec(FramedGraph<?> graph, String[] args) throws Exception {
71          setCustomOptions(options);
72          return execWithOptions(graph, parser.parse(options, args));
73      }
74  
75      @Override
76      public String getDetailedHelp() {
77          HelpFormatter formatter = new HelpFormatter();
78          setCustomOptions(options);
79          StringWriter out = new StringWriter();
80          formatter.printHelp(
81                  new PrintWriter(out), 80, getUsage(), "\n" + getHelp() + "\n\n",
82                  options, 5, 0, "\n" + getHelpFooter());
83          return out.toString();
84      }
85  
86  
87      /**
88       * Execute this command with the given database and parsed command
89       * line arguments.
90       *
91       * @param graph   the graph database
92       * @param cmdLine the parsed command line object
93       * @return a command return code
94       */
95      public abstract int execWithOptions(FramedGraph<?> graph,
96              CommandLine cmdLine) throws Exception;
97  
98      /**
99       * Utility to get a parsed command line from some args. This is
100      * mainly helpful for testing.
101      *
102      * @param args A list of arg strings
103      * @return The parsed command line
104      */
105     CommandLine getCmdLine(String[] args) throws ParseException {
106         setCustomOptions(options);
107         return parser.parse(options, args);
108     }
109 
110     protected Api api(FramedGraph<?> graph, Accessor accessor) {
111         return ApiFactory.withLogging(graph, accessor);
112     }
113 
114     /**
115      * Get an optional log message given a string that may or may not
116      * be empty.
117      *
118      * @param msg a possibly null or empty string
119      * @return an optional message string
120      */
121     Optional<String> getLogMessage(String msg) {
122         return (msg == null || msg.trim().isEmpty()) ? Optional.empty() : Optional.of(msg);
123     }
124 }