Clean up code and comment
This commit is contained in:
parent
3c9344dcef
commit
32c1bb2999
7 changed files with 82 additions and 27 deletions
|
@ -1,11 +1,13 @@
|
||||||
plugins {
|
plugins {
|
||||||
id 'java'
|
id 'java'
|
||||||
|
id 'application'
|
||||||
}
|
}
|
||||||
|
|
||||||
group 'de.vanitasvitae'
|
group 'de.vanitasvitae'
|
||||||
version '1.0-SNAPSHOT'
|
version '1.0-SNAPSHOT'
|
||||||
|
|
||||||
sourceCompatibility = 1.8
|
sourceCompatibility = 1.8
|
||||||
|
mainClassName = "de.vanitasvitae.imi.codes.Main"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package de.vanitasvitae.imi.codes;
|
package de.vanitasvitae.imi.codes;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -11,15 +10,31 @@ import de.vanitasvitae.imi.codes.types.SampleTubeCode;
|
||||||
import de.vanitasvitae.imi.codes.types.SampleType;
|
import de.vanitasvitae.imi.codes.types.SampleType;
|
||||||
import de.vanitasvitae.imi.codes.types.StudyNumber;
|
import de.vanitasvitae.imi.codes.types.StudyNumber;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class that generates {@link SampleTubeCode SampleTubeCodes}.
|
||||||
|
*/
|
||||||
public class CodeGenerator {
|
public class CodeGenerator {
|
||||||
|
|
||||||
private final FileRepository repository = new FileRepository(new File(".imicodes"));
|
// Pls do not instantiate.
|
||||||
|
private CodeGenerator() {
|
||||||
public CodeGenerator() {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<SampleTubeCode> generateCodes(StudyNumber studyNumber, SampleType sampleType, int numberOfCodes)
|
/**
|
||||||
|
* Generate {@code numberOfCodes} new codes for samples of type {@code sampleType}, which are associated to study
|
||||||
|
* {@code studyNumber}.
|
||||||
|
*
|
||||||
|
* @param repository provides access to records of existing codes
|
||||||
|
* @param studyNumber number of the study
|
||||||
|
* @param sampleType type of the samples
|
||||||
|
* @param numberOfCodes number of codes to be generated
|
||||||
|
*
|
||||||
|
* @return list of {@link SampleTubeCode SampleTubeCodes}
|
||||||
|
*
|
||||||
|
* @throws InvalidOptionException if too many codes would be generated.
|
||||||
|
* @throws IOException if IO goes wrong reading or writing sample code records.
|
||||||
|
*/
|
||||||
|
public static List<SampleTubeCode> generateCodes(FileRepository repository, StudyNumber studyNumber, SampleType sampleType, int numberOfCodes)
|
||||||
throws InvalidOptionException, IOException {
|
throws InvalidOptionException, IOException {
|
||||||
List<SampleTubeCode> codes = new ArrayList<>();
|
List<SampleTubeCode> codes = new ArrayList<>();
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,12 @@ public class HtmlTableStringBuilder {
|
||||||
|
|
||||||
private StringBuilder html;
|
private StringBuilder html;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generated a HTML String which contains a table of {@link SampleTubeCode SampleTubeCodes}.
|
||||||
|
*
|
||||||
|
* @param title Title which gets inserted as level 2 header.
|
||||||
|
* @param codes list of codes
|
||||||
|
*/
|
||||||
public HtmlTableStringBuilder(String title, List<SampleTubeCode> codes) {
|
public HtmlTableStringBuilder(String title, List<SampleTubeCode> codes) {
|
||||||
html = new StringBuilder("<!DOCTYPE html>\n")
|
html = new StringBuilder("<!DOCTYPE html>\n")
|
||||||
.append("<html>\n")
|
.append("<html>\n")
|
||||||
|
|
|
@ -7,11 +7,13 @@ import java.io.IOException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
import java.sql.SQLOutput;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import de.vanitasvitae.imi.codes.input.Arguments;
|
import de.vanitasvitae.imi.codes.input.Arguments;
|
||||||
import de.vanitasvitae.imi.codes.input.InputValidator;
|
import de.vanitasvitae.imi.codes.input.InputValidator;
|
||||||
import de.vanitasvitae.imi.codes.input.InvalidOptionException;
|
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.SampleTubeCode;
|
||||||
import de.vanitasvitae.imi.codes.types.SampleType;
|
import de.vanitasvitae.imi.codes.types.SampleType;
|
||||||
import de.vanitasvitae.imi.codes.types.StudyNumber;
|
import de.vanitasvitae.imi.codes.types.StudyNumber;
|
||||||
|
@ -24,9 +26,10 @@ import org.apache.commons.cli.ParseException;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
private static final String NAME_JAR = "java -jar imicodes.jar";
|
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_HEADER = "Generate ID codes for sample tubes.";
|
||||||
private static final String HELP_FOOTER = "\nAuthor: Paul Schaub <paul.schaub@wwu.de>";
|
private static final String HELP_FOOTER = "\nAuthor: Paul Schaub <paul.schaub@wwu.de>";
|
||||||
|
private static final File RECORD_DIR = new File(".imicodes");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Entry point to the program.
|
* Entry point to the program.
|
||||||
|
@ -39,6 +42,7 @@ public class Main {
|
||||||
Options options = Arguments.getCommandLineOptions();
|
Options options = Arguments.getCommandLineOptions();
|
||||||
CommandLineParser parser = new DefaultParser();
|
CommandLineParser parser = new DefaultParser();
|
||||||
|
|
||||||
|
// Parse command line arguments
|
||||||
CommandLine arguments;
|
CommandLine arguments;
|
||||||
try {
|
try {
|
||||||
arguments = parser.parse(options, args);
|
arguments = parser.parse(options, args);
|
||||||
|
@ -60,7 +64,7 @@ public class Main {
|
||||||
File outputPath;
|
File outputPath;
|
||||||
boolean externalBrowser;
|
boolean externalBrowser;
|
||||||
|
|
||||||
// Parse arguments
|
// Validate command line arguments
|
||||||
try {
|
try {
|
||||||
studyNumber = InputValidator.validateStudyNumber(arguments.getOptionValue(Arguments.STUDY_NUMBER));
|
studyNumber = InputValidator.validateStudyNumber(arguments.getOptionValue(Arguments.STUDY_NUMBER));
|
||||||
sampleType = InputValidator.validateSampleType(arguments.getOptionValue(Arguments.SAMPLE_TYPE));
|
sampleType = InputValidator.validateSampleType(arguments.getOptionValue(Arguments.SAMPLE_TYPE));
|
||||||
|
@ -76,39 +80,55 @@ public class Main {
|
||||||
} catch (InvalidOptionException e) {
|
} catch (InvalidOptionException e) {
|
||||||
// Something is wrong with the users input, so exit.
|
// Something is wrong with the users input, so exit.
|
||||||
System.out.println(e.getMessage());
|
System.out.println(e.getMessage());
|
||||||
|
printHelp(options);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
CodeGenerator generator = new CodeGenerator();
|
// Store/read number of generated codes to file
|
||||||
|
FileRepository repository = new FileRepository(RECORD_DIR);
|
||||||
|
|
||||||
|
// Generate codes
|
||||||
List<SampleTubeCode> codes;
|
List<SampleTubeCode> codes;
|
||||||
try {
|
try {
|
||||||
codes = generator.generateCodes(studyNumber, sampleType, numberOfCodes);
|
codes = CodeGenerator.generateCodes(repository, studyNumber, sampleType, numberOfCodes);
|
||||||
} catch (InvalidOptionException e) {
|
} catch (InvalidOptionException e) {
|
||||||
System.out.println(e.getMessage());
|
System.out.println(e.getMessage());
|
||||||
return;
|
return;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
System.out.println("Could not store records about already generated codes.");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Build HTML
|
||||||
String html = new HtmlTableStringBuilder("IMI Sample Tube Code Generator", codes).toString();
|
String html = new HtmlTableStringBuilder("IMI Sample Tube Code Generator", codes).toString();
|
||||||
writeHtml(html, outputPath);
|
|
||||||
|
|
||||||
|
// 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()) {
|
if (externalBrowser && Desktop.isDesktopSupported()) {
|
||||||
try {
|
try {
|
||||||
Desktop.getDesktop().browse(new URI("file://" + outputPath.getAbsolutePath()));
|
Desktop.getDesktop().browse(new URI("file://" + outputPath.getAbsolutePath()));
|
||||||
} catch (IOException | URISyntaxException e) {
|
} catch (IOException | URISyntaxException e) {
|
||||||
|
System.out.println("Could not open generated HTML in a browser.");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void writeHtml(String html, File destination) {
|
/*
|
||||||
|
Write HTML to a file.
|
||||||
|
*/
|
||||||
|
private static void writeHtml(String html, File destination) throws IOException {
|
||||||
try(Writer writer = new FileWriter(destination)) {
|
try(Writer writer = new FileWriter(destination)) {
|
||||||
writer.write(html);
|
writer.write(html);
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,11 +5,22 @@ import java.util.stream.IntStream;
|
||||||
|
|
||||||
import de.vanitasvitae.imi.codes.input.InvalidOptionException;
|
import de.vanitasvitae.imi.codes.input.InvalidOptionException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract class, that represents a {@link CharSequence} which MUST match a given REGEX.
|
||||||
|
*/
|
||||||
public abstract class BoundedCharSequence implements CharSequence {
|
public abstract class BoundedCharSequence implements CharSequence {
|
||||||
|
|
||||||
private final Pattern regex;
|
private final Pattern regex;
|
||||||
private final String value;
|
private final String value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a {@link BoundedCharSequence}. If the {@code value} doesn't match the {@code regex},
|
||||||
|
* a {@link InvalidOptionException} is thrown.
|
||||||
|
*
|
||||||
|
* @param regex regular expression
|
||||||
|
* @param value value of the {@link BoundedCharSequence}.
|
||||||
|
* @throws InvalidOptionException thrown when the value doesn't match the regex.
|
||||||
|
*/
|
||||||
protected BoundedCharSequence(Pattern regex, String value) throws InvalidOptionException {
|
protected BoundedCharSequence(Pattern regex, String value) throws InvalidOptionException {
|
||||||
this.regex = regex;
|
this.regex = regex;
|
||||||
if (!matchPattern(value)) {
|
if (!matchPattern(value)) {
|
||||||
|
|
|
@ -2,12 +2,26 @@ package de.vanitasvitae.imi.codes.types;
|
||||||
|
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Identification Code for Sample Tubes.
|
||||||
|
*/
|
||||||
public class SampleTubeCode implements CharSequence {
|
public class SampleTubeCode implements CharSequence {
|
||||||
|
|
||||||
private final StudyNumber studyNumber;
|
private final StudyNumber studyNumber;
|
||||||
private final SampleType sampleType;
|
private final SampleType sampleType;
|
||||||
private final int sampleNumber;
|
private final int sampleNumber;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@link SampleTubeCode} object.
|
||||||
|
* This constructor throws an {@link IllegalArgumentException}, if one of the following cases is happening:
|
||||||
|
* - {@code studyNumber} is null
|
||||||
|
* - {@code sampleType} is null
|
||||||
|
* - {@code sampleNumber is negative or greater than 9999}
|
||||||
|
*
|
||||||
|
* @param studyNumber code that identifies the associated study
|
||||||
|
* @param sampleType type of the sample
|
||||||
|
* @param sampleNumber number of the sample
|
||||||
|
*/
|
||||||
public SampleTubeCode(StudyNumber studyNumber, SampleType sampleType, int sampleNumber) {
|
public SampleTubeCode(StudyNumber studyNumber, SampleType sampleType, int sampleNumber) {
|
||||||
if (studyNumber == null) {
|
if (studyNumber == null) {
|
||||||
throw new IllegalArgumentException("StudyNumber MUST NOT be null.");
|
throw new IllegalArgumentException("StudyNumber MUST NOT be null.");
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
package de.vanitasvitae.imi.codes.types;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class Study {
|
|
||||||
private final StudyNumber number;
|
|
||||||
private final List<SampleTubeCode> codes = new ArrayList<>();
|
|
||||||
|
|
||||||
public Study(StudyNumber number) {
|
|
||||||
this.number = number;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue