diff --git a/.gitignore b/.gitignore index 098ad5e..c5d7d73 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea/* target/* *.iml +log/* diff --git a/src/main/java/nl/andrewlalis/Main.java b/src/main/java/nl/andrewlalis/Main.java index ae73668..83c50f2 100644 --- a/src/main/java/nl/andrewlalis/Main.java +++ b/src/main/java/nl/andrewlalis/Main.java @@ -1,11 +1,13 @@ package nl.andrewlalis; import nl.andrewlalis.model.Team; +import nl.andrewlalis.util.Logging; import nl.andrewlalis.util.TeamGenerator; import java.io.IOException; import java.util.List; import java.util.Map; +import java.util.logging.Logger; import nl.andrewlalis.util.CommandLine; @@ -14,18 +16,27 @@ import nl.andrewlalis.util.CommandLine; */ public class Main { + private static final Logger logger = Logger.getGlobal(); + public static void main(String[] args) throws IOException { - System.out.println("Initializer for Github Repositories in Educational Organizations."); - + // Parsed command line arguments. Map userOptions = CommandLine.parseArgs(args); + // Initialize logger. + try { + Logging.setup(true); // TODO: Replace true with command line arg. + } catch (IOException e) { + logger.severe("Unable to save log to file."); + } + + logger.info("Initializer for Github Repositories in Educational Organizations."); + List teams = TeamGenerator.generateFromCSV( userOptions.get("input"), Integer.parseInt(userOptions.get("teamsize")) ); - System.out.println(teams); - + logger.info("Teams created: " + teams); } } diff --git a/src/main/java/nl/andrewlalis/util/Logging.java b/src/main/java/nl/andrewlalis/util/Logging.java new file mode 100644 index 0000000..625ef1e --- /dev/null +++ b/src/main/java/nl/andrewlalis/util/Logging.java @@ -0,0 +1,31 @@ +package nl.andrewlalis.util; + +import java.io.IOException; +import java.util.logging.*; + +/** + * Responsible for creating logs to standard output and writing to files. + */ +public class Logging { + + private static FileHandler outputFile; + private static SimpleFormatter formatter; + + public static void setup(boolean verbose) throws IOException { + Logger logger = Logger.getGlobal(); + + if (verbose) { + logger.setLevel(Level.FINEST); + } else { + logger.setLevel(Level.INFO); + } + + outputFile = new FileHandler("log/latest.txt"); + formatter = new SimpleFormatter(); + + outputFile.setFormatter(formatter); + logger.addHandler(outputFile); + + } + +} diff --git a/src/main/java/nl/andrewlalis/util/TeamGenerator.java b/src/main/java/nl/andrewlalis/util/TeamGenerator.java index f88606f..c0b4ecf 100644 --- a/src/main/java/nl/andrewlalis/util/TeamGenerator.java +++ b/src/main/java/nl/andrewlalis/util/TeamGenerator.java @@ -8,14 +8,43 @@ import org.apache.commons.csv.CSVRecord; import java.io.FileReader; import java.io.IOException; import java.util.*; +import java.util.logging.Logger; public class TeamGenerator { - public static List generateFromCSV(String filename, int teamSize) throws IOException { - System.out.println("Generating teams of size " + teamSize); + private static final Logger logger = Logger.getLogger(TeamGenerator.class.getName()); + static { + logger.setParent(Logger.getGlobal()); + } + + /** + * Creates a list of teams by reading a CSV file of a certain format. The format for each column is as follows: + * 1. Timestamp - The date and time the record was entered. + * 2. Username - The email address. + * 3. Name - The student's name. + * 4. Student Number + * 5. Github Username + * 6. I have chosen a partner. (Yes / No) If yes: + * 7. Your Partner's Student Number + * @param filename The CSV file to load from. + * @param teamSize The preferred teamsize used in creating teams. + * @return A list of teams. + * @throws IOException If the file is unable to be read. + * @throws IllegalArgumentException If an invalid teamsize is given. + */ + public static List generateFromCSV(String filename, int teamSize) throws IOException, IllegalArgumentException { + logger.info("Generating teams of size " + teamSize); + if (teamSize < 1) { + logger.severe("Invalid team size."); + throw new IllegalArgumentException("Team size must be greater than or equal to 1. Got " + teamSize); + } + logger.finest("Parsing CSV file."); Iterable records = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(new FileReader(filename)); + logger.finest("Reading all records into map."); Map studentMap = readAllStudents(records, teamSize); + + logger.finest("Generating all valid teams from student map."); return generateAllValidTeams(studentMap, teamSize); }