package de.vanitasvitae.imi.codes; import java.awt.*; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.net.URI; import java.net.URISyntaxException; import java.sql.SQLOutput; import java.util.List; import de.vanitasvitae.imi.codes.input.Arguments; import de.vanitasvitae.imi.codes.input.InputValidator; import de.vanitasvitae.imi.codes.input.InvalidOptionException; import de.vanitasvitae.imi.codes.persistence.FileRepository; import de.vanitasvitae.imi.codes.types.SampleTubeCode; import de.vanitasvitae.imi.codes.types.SampleType; import de.vanitasvitae.imi.codes.types.StudyNumber; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; public class Main { private static final String NAME_JAR = "imi.codes"; private static final String HELP_HEADER = "Generate ID codes for sample tubes."; private static final String HELP_FOOTER = "\nAuthor: Paul Schaub "; private static final File RECORD_DIR = new File(".imicodes"); /** * Entry point to the program. * Any provided arguments are parsed and the behaviour of the program is modified respectively. * A list of available options can be displayed using "-h". * * @param args arguments */ public static void main(String[] args) { Options options = Arguments.getCommandLineOptions(); CommandLineParser parser = new DefaultParser(); // Parse command line arguments CommandLine arguments; try { arguments = parser.parse(options, args); } catch (ParseException e) { System.out.println(e.getMessage()); printHelp(options); return; } // User issues '-h', so just show help text and exit right now. if (arguments.hasOption(Arguments.HELP)) { printHelp(options); return; } StudyNumber studyNumber; SampleType sampleType; int numberOfCodes; File outputPath; boolean externalBrowser; // Validate command line arguments try { studyNumber = InputValidator.validateStudyNumber(arguments.getOptionValue(Arguments.STUDY_NUMBER)); sampleType = InputValidator.validateSampleType(arguments.getOptionValue(Arguments.SAMPLE_TYPE)); numberOfCodes = InputValidator.validateNumberOfCodes(arguments.getOptionValue(Arguments.NUMBER_CODES)); if (arguments.hasOption(Arguments.OUTPUT_DESTINATION)) { outputPath = InputValidator.validateOutputPath(arguments.getOptionValue(Arguments.OUTPUT_DESTINATION)); } else { outputPath = InputValidator.validateOutputPath("./out.html"); } externalBrowser = arguments.hasOption(Arguments.EXTERNAL_BROWSER); } catch (InvalidOptionException e) { // Something is wrong with the users input, so exit. System.out.println(e.getMessage()); printHelp(options); return; } // Store/read number of generated codes to file FileRepository repository = new FileRepository(RECORD_DIR); // Generate codes List codes; try { codes = CodeGenerator.generateCodes(repository, studyNumber, sampleType, numberOfCodes); } catch (InvalidOptionException e) { System.out.println(e.getMessage()); return; } catch (IOException e) { System.out.println("Could not store records about already generated codes."); e.printStackTrace(); return; } // Build HTML String html = new HtmlTableStringBuilder("IMI Sample Tube Code Generator", codes).toString(); // Write HTML to output path try { writeHtml(html, outputPath); } catch (IOException e) { System.out.println("Could not write HTML to file " + outputPath.getAbsolutePath()); e.printStackTrace(); return; } // Upon users wish, open generated HTML in a browser. if (externalBrowser && Desktop.isDesktopSupported()) { try { Desktop.getDesktop().browse(new URI("file://" + outputPath.getAbsolutePath())); } catch (IOException | URISyntaxException e) { System.out.println("Could not open generated HTML in a browser."); e.printStackTrace(); } } } /* Write HTML to a file. */ private static void writeHtml(String html, File destination) throws IOException { try(Writer writer = new FileWriter(destination)) { writer.write(html); } } /** * Print a descriptive help text to the console. * * @param options {@link Options} for which we want to print a help text. */ private static void printHelp(Options options) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(NAME_JAR, HELP_HEADER, options, HELP_FOOTER, true); } }