package de.dhbw.contactdb; import de.dhbw.contactdb.utils.Parser; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.stream.Collectors; /** * Main class * * @author florian kaiser * @version 1.0 * @since 21.06.2021 */ public class Main { /** * Contains all persons from contacts2021.db * * @see de.dhbw.contactdb.utils.Parser */ public static HashMap persons = new HashMap<>(); /** * Contains all places from contacts2021.db * * @see de.dhbw.contactdb.utils.Parser */ public static HashMap places = new HashMap<>(); /** * Contains all visits from contacts2021.db * * @see de.dhbw.contactdb.utils.Parser */ public static HashSet visits = new HashSet<>(); /** * Main function * * @param args String that contains program arguments */ public static void main(String[] args) { Parser.parse("/de/dhbw/contactdb/db/contacts2021.db"); if (args.length == 1) { if (args[0].contains("--personensuche")) { searchForPerson(getSearchString(args[0])); } else if (args[0].contains("--ortssuche")) { searchForPlace(getSearchString(args[0])); } else if (args[0].contains("--kontaktpersonen")) { int id = Integer.parseInt(getSearchString(args[0])); System.out.println(getContactPersons(id).stream() .filter(visit -> visit.getPerson().getId() != id) .map(visit -> visit.getPerson().getName()) .sorted() .distinct() .collect(Collectors.joining(", "))); } else if (args[0].contains("--besucher")) { System.out.println(getVisitors(args[0]).stream() .map(visit -> visit.getPerson().getName()) .sorted() .distinct() .collect(Collectors.joining(", "))); } else { argument_error(); } } else { argument_error(); } } /** * Prints error message * * @author florian kaiser */ private static void argument_error() { System.out.println("Invalid parameter! \nUsage [Option]=\"[Value]\"\n--personensuche\n--ortssuche\n--kontaktpersonen\n--besucher"); } /** * Searches the HashMap places for a specific string * @param search String that contains search value * * @author florian kaiser */ public static void searchForPlace(String search) { places.forEach((key, value) -> { if (value.getName().toLowerCase().contains(search)) { System.out.println(value); } }); } /** * Searches the HashMap persons for a specific string * @param search String that contains search value * * @author florian kaiser */ public static void searchForPerson(String search) { persons.forEach((key, value) -> { if (value.getName().toLowerCase().contains(search)) { System.out.println(value); } }); } /** * Lists all persons that were at the same place at the same time as a specific person. * Only if the place is indoor * * @param id Contains the id of a person * @return List that contains visits * * @author florian kaiser */ public static List getContactPersons(int id) { List places_where_id_was_indoor; places_where_id_was_indoor = visits.stream() .filter(visit -> visit.getPlace().isIndoor()) .filter(visit -> visit.getPerson().getId() == id) .toList(); List contacts_of_id = new ArrayList<>(); for (Visit filtered_b : places_where_id_was_indoor) { for (Visit b : visits) { if (b.isOverlapping(filtered_b.getStart(), filtered_b.getEnd()) && filtered_b.getPlace().getId() == b.getPlace().getId()) { contacts_of_id.add(b); } } } return contacts_of_id; } /** * Lists all Persons that were at the same place (id) at the same time. * If the place is indoor, the contact persons of each person is added to the result too. * * @param s contains program argument (id of place and date string, separated by comma) * @return List that contains visits * * @author florian kaiser */ public static List getVisitors(String s) { int id = Integer.parseInt(s.split("=")[1].split(",")[0].replaceAll("\"", "")); LocalDateTime d; try { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); d = LocalDateTime.parse(s.split("=")[1].split(",")[1].replaceAll("\"", ""), formatter); } catch (DateTimeParseException e1) { try { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm"); d = LocalDateTime.parse(s.split("=")[1].split(",")[1].replaceAll("\"", ""), formatter); } catch (DateTimeParseException e2) { argument_error(); System.exit(-2); throw e1; } } LocalDateTime finalD = d; List place_date_visits = visits.stream() .filter(visit -> visit.getPlace().getId() == id) .filter(visit -> visit.isInTimePeriod(finalD)) .toList(); List all = new ArrayList<>(); for (Visit visit : place_date_visits) { if (visit.getPlace().isIndoor()) { all.addAll(getContactPersons(visit.getPerson().getId())); } else { all.add(visit); } } return all; } /** * Extracts string from String. * Exits program if string is empty, blank or not available * * @param s Contains program argument * @return extracted string */ public static String getSearchString(String s) { try { s = s.split("=")[1].replaceAll("\"", "").toLowerCase(); } catch (ArrayIndexOutOfBoundsException e) { argument_error(); System.exit(-1); } if (s.isEmpty() || s.isBlank()) { argument_error(); System.exit(-1); } return s; } }