104 lines
3.9 KiB
Java
104 lines
3.9 KiB
Java
package de.vanitasvitae.imi.codes;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
|
|
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 = "imicodes";
|
|
private static final String HELP_HEADER = "Generate ID codes for sample tubes.";
|
|
private static final String HELP_FOOTER = "\nAuthor: Paul Schaub <paul.schaub@wwu.de>";
|
|
|
|
public static void main(String[] args) {
|
|
Options options = Arguments.getCommandLineOptions();
|
|
CommandLineParser parser = new DefaultParser();
|
|
|
|
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;
|
|
}
|
|
|
|
// Parse arguments
|
|
StudyNumber studyNumber;
|
|
SampleType sampleType;
|
|
int numberOfCodes;
|
|
File outputPath;
|
|
boolean externalBrowser;
|
|
|
|
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));
|
|
outputPath = InputValidator.validateOutputPath(arguments.getOptionValue(Arguments.OUTPUT_DESTINATION));
|
|
externalBrowser = arguments.hasOption(Arguments.EXTERNAL_BROWSER);
|
|
} catch (InvalidOptionException e) {
|
|
// Something is wrong with the users input, so exit.
|
|
System.out.println(e.getMessage());
|
|
return;
|
|
}
|
|
|
|
FileRepository repository = new FileRepository(new File(".imicodes"));
|
|
|
|
// Read the next sample code from file
|
|
int nextSampleCode = repository.nextSampleCode(studyNumber);
|
|
|
|
int nextTotal = nextSampleCode + numberOfCodes;
|
|
// Check, if we'd have an overflow of sample numbers
|
|
// We check like this to prevent integer overflows
|
|
if (nextSampleCode > 9999 || numberOfCodes > 9999 || nextTotal - 1 > 9999) {
|
|
System.out.println("Study " + studyNumber + "would have too many sample tubes" +
|
|
" (" + (nextTotal - 1) + "). Aborting.");
|
|
return;
|
|
}
|
|
|
|
// Write back the number of the next sample number that should be generated next time
|
|
try {
|
|
repository.writeNextSampleCode(studyNumber, nextSampleCode + numberOfCodes);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
return;
|
|
}
|
|
// Now we are finished with dangerous IO...
|
|
|
|
// Generate codes
|
|
for (int i = 0; i < numberOfCodes; i++) {
|
|
System.out.println(new SampleTubeCode(studyNumber, sampleType, nextSampleCode + i));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
}
|