236 lines
7.1 KiB
Java
236 lines
7.1 KiB
Java
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<Integer, Person> persons = new HashMap<>();
|
|
|
|
|
|
/**
|
|
* Contains all places from contacts2021.db
|
|
*
|
|
* @see de.dhbw.contactdb.utils.Parser
|
|
*/
|
|
public static HashMap<Integer, Place> places = new HashMap<>();
|
|
|
|
|
|
/**
|
|
* Contains all visits from contacts2021.db
|
|
*
|
|
* @see de.dhbw.contactdb.utils.Parser
|
|
*/
|
|
public static HashSet<Visit> visits = new HashSet<>();
|
|
|
|
|
|
/**
|
|
* Main function
|
|
* Parse commandline parameters
|
|
*
|
|
* @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) {
|
|
parseArguments(args[0]);
|
|
} else {
|
|
argument_error();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Prints error message
|
|
*
|
|
* @author florian kaiser
|
|
*/
|
|
private static void argument_error() {
|
|
System.err.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
|
|
* @return String with space-separated places
|
|
*/
|
|
public static String searchForPlace(String search) {
|
|
StringBuilder result = new StringBuilder();
|
|
for (Place p : places.values()) {
|
|
if (p.getName().toLowerCase().contains(search)) {
|
|
result.append(p.getName()).append(" ");
|
|
}
|
|
}
|
|
return result.toString();
|
|
}
|
|
|
|
|
|
/**
|
|
* Searches the HashMap persons for a specific string
|
|
*
|
|
* @param search String that contains search value
|
|
* @author florian kaiser
|
|
* @return String with space-separated persons
|
|
*/
|
|
public static String searchForPerson(String search) {
|
|
StringBuilder result = new StringBuilder();
|
|
for (Person p : persons.values()) {
|
|
if (p.getName().toLowerCase().contains(search)) {
|
|
result.append(p.getName()).append(" ");
|
|
}
|
|
}
|
|
return result.toString();
|
|
}
|
|
|
|
|
|
/**
|
|
* 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<Visit> getContactPersons(int id) {
|
|
List<Visit> filtered_visits;
|
|
filtered_visits = visits.stream()
|
|
.filter(visit -> visit.getPlace().isIndoor())
|
|
.filter(visit -> visit.getPerson().getId() == id)
|
|
.toList();
|
|
|
|
List<Visit> contacts = new ArrayList<>();
|
|
for (Visit visit_1 : filtered_visits) {
|
|
for (Visit visit_2 : visits) {
|
|
if (visit_2.isOverlapping(visit_1.getStart(), visit_1.getEnd()) && visit_1.getPlace().getId() == visit_2.getPlace().getId()) {
|
|
contacts.add(visit_2);
|
|
}
|
|
}
|
|
}
|
|
|
|
return contacts;
|
|
}
|
|
|
|
|
|
/**
|
|
* 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<Visit> getVisitors(String s) {
|
|
int id = Integer.parseInt(s.split(",")[0].replaceAll("\"", ""));
|
|
LocalDateTime d;
|
|
|
|
try {
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd't'HH:mm:ss");
|
|
d = LocalDateTime.parse(s.split(",")[1].replaceAll("\"", ""), formatter);
|
|
} catch (DateTimeParseException e1) {
|
|
try {
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd't'HH:mm");
|
|
d = LocalDateTime.parse(s.split(",")[1].replaceAll("\"", ""), formatter);
|
|
} catch (DateTimeParseException e2) {
|
|
argument_error();
|
|
System.exit(-2);
|
|
throw e1;
|
|
}
|
|
}
|
|
|
|
LocalDateTime finalD = d;
|
|
List<Visit> filtered_visits = visits.stream()
|
|
.filter(visit -> visit.getPlace().getId() == id)
|
|
.filter(visit -> visit.isInTimePeriod(finalD))
|
|
.toList();
|
|
|
|
List<Visit> visitors = new ArrayList<>();
|
|
for (Visit visit : filtered_visits) {
|
|
if (visit.getPlace().isIndoor()) {
|
|
visitors.addAll(getContactPersons(visit.getPerson().getId()));
|
|
} else {
|
|
visitors.add(visit);
|
|
}
|
|
}
|
|
|
|
return visitors;
|
|
}
|
|
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
|
|
/**
|
|
* Parse commandline arguments and run specific functions after
|
|
*
|
|
* @param args Commandline arguments
|
|
*/
|
|
public static void parseArguments(String args) {
|
|
if (args.contains("--personensuche")) {
|
|
System.out.println(searchForPerson(getSearchString(args)));
|
|
} else if (args.contains("--ortssuche")) {
|
|
System.out.println(searchForPlace(getSearchString(args)));
|
|
} else if (args.contains("--kontaktpersonen")) {
|
|
int id = Integer.parseInt(getSearchString(args));
|
|
|
|
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.contains("--besucher")) {
|
|
System.out.println(getVisitors(getSearchString(args)).stream()
|
|
.map(visit -> visit.getPerson().getName())
|
|
.sorted()
|
|
.distinct()
|
|
.collect(Collectors.joining(", ")));
|
|
} else {
|
|
argument_error();
|
|
}
|
|
}
|
|
} |