Improved functions, fixed functions, refactoring
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
package de.dhbw.contactdb.utils;
|
||||
|
||||
import de.dhbw.contactdb.Besuch;
|
||||
import de.dhbw.contactdb.Place;
|
||||
import de.dhbw.contactdb.Visit;
|
||||
import de.dhbw.contactdb.Main;
|
||||
import de.dhbw.contactdb.Ort;
|
||||
import de.dhbw.contactdb.Person;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
@@ -11,41 +11,81 @@ import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
|
||||
|
||||
/**
|
||||
* Parser for parsing contacts2021.db
|
||||
*
|
||||
* @author florian kaiser
|
||||
* @version 1.0
|
||||
* @since 21.06.2021
|
||||
*/
|
||||
public class Parser {
|
||||
/**
|
||||
* Saves type of current line in database
|
||||
*/
|
||||
private static db_new_entity new_entity = null;
|
||||
|
||||
|
||||
/**
|
||||
* Parses contacts.db, creates objects of content and saves these objects in HashMaps
|
||||
*
|
||||
* @param filename Filename and path of contacts.db
|
||||
*/
|
||||
public static void parse(String filename) {
|
||||
InputStream db = Main.class.getResourceAsStream(filename);
|
||||
|
||||
if(db == null) {
|
||||
System.err.println("Cannot find database!");
|
||||
System.exit(-1);
|
||||
System.exit(-3);
|
||||
}
|
||||
|
||||
try (BufferedReader bf = new BufferedReader(new InputStreamReader(db))) {
|
||||
String line;
|
||||
while ((line = bf.readLine()) != null) {
|
||||
if (line.toLowerCase().contains("new_entity")) {
|
||||
if (line.toLowerCase().contains("start_date")) new_entity = db_new_entity.BESUCH;
|
||||
else if (line.toLowerCase().contains("location_id")) new_entity = db_new_entity.ORT;
|
||||
if (line.toLowerCase().contains("start_date")) new_entity = db_new_entity.VISIT;
|
||||
else if (line.toLowerCase().contains("location_id")) new_entity = db_new_entity.PLACE;
|
||||
else if (line.toLowerCase().contains("person_id")) new_entity = db_new_entity.PERSON;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (new_entity == db_new_entity.BESUCH) {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
|
||||
LocalDateTime start = LocalDateTime.parse(line.split(",")[0].replaceAll("\"", ""), formatter);
|
||||
LocalDateTime end = LocalDateTime.parse(line.split(",")[1].replaceAll("\"", ""), formatter);
|
||||
Person p = Main.persons.get(Integer.parseInt(line.split(",")[2].replaceAll("\"", "")));
|
||||
Ort o = Main.orte.get(Integer.parseInt(line.split(",")[3].replaceAll("\"", "")));
|
||||
Main.besuche.add(new Besuch(start, end, p, o));
|
||||
} else if (new_entity == db_new_entity.ORT) {
|
||||
if (new_entity == db_new_entity.VISIT) {
|
||||
LocalDateTime start = null;
|
||||
try {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
|
||||
start = LocalDateTime.parse(line.split(",")[0].replaceAll("\"", ""), formatter);
|
||||
} catch (DateTimeParseException e1) {
|
||||
try {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm");
|
||||
start = LocalDateTime.parse(line.split(",")[0].replaceAll("\"", ""), formatter);
|
||||
} catch (DateTimeParseException e2) {
|
||||
System.exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
LocalDateTime end = null;
|
||||
try {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
|
||||
end = LocalDateTime.parse(line.split(",")[1].replaceAll("\"", ""), formatter);
|
||||
} catch (DateTimeParseException e1) {
|
||||
try {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm");
|
||||
end = LocalDateTime.parse(line.split(",")[1].replaceAll("\"", ""), formatter);
|
||||
} catch (DateTimeParseException e2) {
|
||||
System.exit(-1);
|
||||
}
|
||||
}
|
||||
Person person = Main.persons.get(Integer.parseInt(line.split(",")[2].replaceAll("\"", "")));
|
||||
Place place = Main.places.get(Integer.parseInt(line.split(",")[3].replaceAll("\"", "")));
|
||||
Main.visits.add(new Visit(start, end, person, place));
|
||||
} else if (new_entity == db_new_entity.PLACE) {
|
||||
int id = Integer.parseInt(line.split(",")[0].replaceAll("\"", ""));
|
||||
String name = line.split(",")[1].replaceAll("\"", "");
|
||||
boolean indoor = line.split(",")[2].replaceAll("\"", "").equals("in_door");
|
||||
Main.orte.put(id, new Ort(id, name, indoor));
|
||||
Main.places.put(id, new Place(id, name, indoor));
|
||||
} else if (new_entity == db_new_entity.PERSON) {
|
||||
int id = Integer.parseInt(line.split(",")[0].replaceAll("\"", ""));
|
||||
String name = line.split(",")[1].replaceAll("\"", "");
|
||||
|
@@ -1,5 +1,29 @@
|
||||
package de.dhbw.contactdb.utils;
|
||||
|
||||
|
||||
/**
|
||||
* Used by Parser
|
||||
*
|
||||
* @author florian kaiser
|
||||
* @version 1.0
|
||||
* @since 21.06.2021
|
||||
* @see de.dhbw.contactdb.utils.Parser
|
||||
*/
|
||||
public enum db_new_entity {
|
||||
PERSON, ORT, BESUCH
|
||||
/**
|
||||
* Person
|
||||
*/
|
||||
PERSON,
|
||||
|
||||
|
||||
/**
|
||||
* Place
|
||||
*/
|
||||
PLACE,
|
||||
|
||||
|
||||
/**
|
||||
* Visit
|
||||
*/
|
||||
VISIT
|
||||
}
|
||||
|
Reference in New Issue
Block a user