added command pattern for task handling.

This commit is contained in:
Andrew Lalis 2021-03-09 11:00:24 +01:00
parent d7334bd6b0
commit 684e47e00c
3 changed files with 38 additions and 10 deletions

View File

@ -1,5 +1,6 @@
package nl.andrewlalis.human_task_distributor; package nl.andrewlalis.human_task_distributor;
import nl.andrewlalis.human_task_distributor.commands.PrepareTasksList;
import org.apache.commons.cli.*; import org.apache.commons.cli.*;
import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVFormat;
@ -14,21 +15,13 @@ public class HumanTaskDistributor {
final Options options = getOptions(); final Options options = getOptions();
CommandLineParser cmdParser = new DefaultParser(); CommandLineParser cmdParser = new DefaultParser();
try { try {
FileParser fileParser = new FileParser(); FileParser fileParser = new FileParser();
FileWriter fileWriter = new FileWriter(); FileWriter fileWriter = new FileWriter();
CommandLine cmd = cmdParser.parse(options, args); CommandLine cmd = cmdParser.parse(options, args);
if (cmd.hasOption("ptl")) { if (cmd.hasOption("ptl")) {
String[] values = cmd.getOptionValues("ptl"); String[] values = cmd.getOptionValues("ptl");
if (values.length != 2) { new PrepareTasksList().execute(values);
throw new IllegalArgumentException("Expected exactly 2 parameters for ptl arg.");
}
String filePath = values[0].trim();
String regex = values[1].trim();
Set<Task> tasks = fileParser.parseTaskList(filePath, regex);
System.out.println("Read " + tasks.size() + " tasks from file.");
String outFilePath = filePath.replaceFirst("\\..*", ".csv");
fileWriter.write(tasks, outFilePath);
System.out.println("Wrote tasks to " + outFilePath);
return; return;
} }

View File

@ -0,0 +1,6 @@
package nl.andrewlalis.human_task_distributor.commands;
public interface Command {
void execute(String[] args);
}

View File

@ -0,0 +1,29 @@
package nl.andrewlalis.human_task_distributor.commands;
import nl.andrewlalis.human_task_distributor.FileParser;
import nl.andrewlalis.human_task_distributor.FileWriter;
import nl.andrewlalis.human_task_distributor.Task;
import java.io.IOException;
import java.util.Set;
public class PrepareTasksList implements Command {
@Override
public void execute(String[] args) {
if (args.length != 2) {
throw new IllegalArgumentException("Expected exactly 2 parameters for ptl arg.");
}
String filePath = args[0].trim();
String regex = args[1].trim();
Set<Task> tasks = new FileParser().parseTaskList(filePath, regex);
System.out.println("Read " + tasks.size() + " tasks from file.");
String outFilePath = filePath.replaceFirst("\\..*", ".csv");
try {
new FileWriter().write(tasks, outFilePath);
System.out.println("Wrote tasks to " + outFilePath);
} catch (IOException e) {
e.printStackTrace();
System.err.println("Couldn't write output file.");
}
}
}