feat/kindErstellen #9

Merged
PBS2H21ASH merged 23 commits from feat/kindErstellen into stable 2023-02-05 20:40:12 +01:00
Showing only changes of commit 944ddbff60 - Show all commits

View File

@ -13,6 +13,7 @@ import java.util.List;
/** /**
* A collection of functions loosely related to account management * A collection of functions loosely related to account management
* Acts as an abstraction layer to the database * Acts as an abstraction layer to the database
*
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
public class AccountMgr { public class AccountMgr {
@ -21,226 +22,354 @@ public class AccountMgr {
/** /**
* creates a user with createUser(...) and adds its id to the 'worker' table * creates a user with createUser(...) and adds its id to the 'worker' table
*
* @param worker the worker to be created * @param worker the worker to be created
* @return userid or -1 * @return userid or -1
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static long createWorker(Worker worker){ protected static long createWorker(Worker worker) {
long id = createUser(worker); long id = createUser(worker);
String sId = String.valueOf(id); String sId = String.valueOf(id);
Database.insert("worker", new String[]{"userid"}, new String[]{sId}); Database.insert("worker", new String[] { "userid" }, new String[] { sId });
return id; return id;
} }
/** /**
* creates a user with createUser(...) and adds its id to the 'parent' table * creates a user with createUser(...) and adds its id to the 'parent' table
*
* @param parent the parent to be created * @param parent the parent to be created
* @return userid or -1 * @return userid or -1
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static long createParent(Parent parent){ protected static long createParent(Parent parent) {
long id = createUser(parent); long id = createUser(parent);
String sId = String.valueOf(id); String sId = String.valueOf(id);
Database.insert("parent", new String[]{"userid"}, new String[]{sId}); Database.insert("parent", new String[] { "userid" }, new String[] { sId });
return id; return id;
} }
/** /**
* adds a user to the database * adds a user to the database
*
* @param user the user to be created * @param user the user to be created
* @return userid or -1 * @return userid or -1
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static long createUser(User user) { protected static long createUser(User user) {
String[] userH = {"name", "firstname", "addressid", "password", "email"}; String[] userH = { "name", "firstname", "addressid", "password", "email" };
String name = user.getName(); String name = user.getName();
String firstname = user.getFirstname(); String firstname = user.getFirstname();
String pw = hashAndSalt(user.getPassword(), getSalt()); String pw = hashAndSalt(user.getPassword(), getSalt());
String email = user.getEmail(); String email = user.getEmail();
long addressId = user.getAddress().getId(); long addressId = user.getAddress().getId();
if(addressId < 1){ if (addressId < 1) {
addressId = createAddress(user.getAddress()); addressId = createAddress(user.getAddress());
} }
String[] userD = {name, firstname, String.valueOf(addressId), pw, email}; String[] userD = { name, firstname, String.valueOf(addressId), pw, email };
return Database.insert("user", userH, userD); return Database.insert("user", userH, userD);
} }
/** /**
* adds an address to the database * adds an address to the database
*
* @param address the address to be created * @param address the address to be created
* @return id or -1 * @return id or -1
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static long createAddress(Address address){ protected static long createAddress(Address address) {
String[] addressH = {"street", "number", "plz", "city"}; String[] addressH = { "street", "number", "plz", "city" };
String[] addressD = {address.getStreet(),address.getNumber(),address.getPlz(),address.getCity()}; String[] addressD = { address.getStreet(), address.getNumber(), address.getPlz(), address.getCity() };
return Database.insert("address",addressH,addressD); return Database.insert("address", addressH, addressD);
} }
/** /**
* adds a child and allergies to the database * adds a child and allergies to the database
*
* @param child the child to be created * @param child the child to be created
* @return id of child or -1 * @return id of child or -1
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static long createChild(Child child){ protected static long createChild(Child child) {
String[] childH = {"name","firstname","addressid"}; String[] childH = { "name", "firstname", "addressid" };
String[] childD = {child.getName(), child.getFirstname(), String.valueOf(child.getAddress().getId())}; String[] childD = { child.getName(), child.getFirstname(), String.valueOf(child.getAddress().getId()) };
long id = Database.insert("child", childH, childD); long id = Database.insert("child", childH, childD);
String[] child_allergyH = {"childid","allergyid","severityid"}; String[] child_allergyH = { "childid", "allergyid", "severityid" };
for (AllergySeverity allergy: child.getAllergies()) { for (AllergySeverity allergy : child.getAllergies()) {
String sId = String.valueOf(id); String sId = String.valueOf(id);
String sAllergyId = String.valueOf(allergy.getAllergy().getId()); String sAllergyId = String.valueOf(allergy.getAllergy().getId());
String sSeverityId = String.valueOf(allergy.getSeverityId()); String sSeverityId = String.valueOf(allergy.getSeverityId());
String[] child_allergyD = {sId,sAllergyId,sSeverityId}; String[] child_allergyD = { sId, sAllergyId, sSeverityId };
Database.insert("child_allergy",child_allergyH,child_allergyD); Database.insert("child_allergy", child_allergyH, child_allergyD);
} }
return id; return id;
} }
/** /**
* returns a User(Worker | Parent) for a given id or null if no unique id was found * update Child
*
* @param child the child to be updated
* @return id of child
* @author Johannes Kantz
*/
protected static long updateChild(Child child) {
String[] childH = { "id", "name", "firstname", "addressid" };
String[] childD = { String.valueOf(child.getId()), child.getName(), child.getFirstname(),
String.valueOf(child.getAddress().getId()) };
long updates = Database.update("child", childH, childD);
String[] child_allergyH = { "childid", "allergyid", "severityid" };
Database.select("child_allergy", new String[] { "childid" }, new String[] { String.valueOf(child.getId()) })
.stream().forEach(row -> {
String allergyId = row.split(":")[0];
Database.delete("child_allergy", Integer.parseInt(allergyId));
});
for (AllergySeverity allergy : child.getAllergies()) {
String sId = String.valueOf(child.getId());
String sAllergyId = String.valueOf(allergy.getAllergy().getId());
String sSeverityId = String.valueOf(allergy.getSeverityId());
String[] child_allergyD = { sId, sAllergyId, sSeverityId };
Database.insert("child_allergy", child_allergyH, child_allergyD);
}
return updates;
}
/**
* delete Child
*
* @author Johannes Kantz
*/
protected static void deleteChildWithId(long id) {
Database.delete("child", id);
Database.select("child_allergy", new String[] { "childid" }, new String[] { String.valueOf(id) }).stream()
.forEach(row -> {
String allergyId = row.split(":")[0];
Database.delete("child_allergy", Integer.parseInt(allergyId));
});
}
/**
* returns a User(Worker | Parent) for a given id or null if no unique id was
* found
*
* @param id id of the User * @param id id of the User
* @return User(Worker | Parent) or null * @return User(Worker | Parent) or null
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static User getUserById(long id){ protected static User getUserById(long id) {
List<String> entry = Database.getEntryById("user",id); List<String> entry = Database.getEntryById("user", id);
if(entry.size() != 1){ if (entry.size() != 1) {
return null; return null;
} }
String[] parts = entry.get(0).split(":"); String[] parts = entry.get(0).split(":");
Address address = getAddressById(id); Address address = getAddressById(id);
if(isWorker(String.valueOf(id))){ if (isWorker(String.valueOf(id))) {
return new Worker(id,parts[1],parts[2],parts[4],parts[5],address); return new Worker(id, parts[1], parts[2], parts[4], parts[5], address);
}else{ } else {
String[] parent_childH = {"parentuserid"}; String[] parent_childH = { "parentuserid" };
String[] parent_childD = {String.valueOf(id)}; String[] parent_childD = { String.valueOf(id) };
List<Child> children = new ArrayList<>(); List<Child> children = new ArrayList<>();
List<String> parent_childEntries = Database.select("parent_child",parent_childH,parent_childD); List<String> parent_childEntries = Database.select("parent_child", parent_childH, parent_childD);
for (String parent_childEntry: parent_childEntries) { for (String parent_childEntry : parent_childEntries) {
String[] parent_childParts = parent_childEntry.split(":"); String[] parent_childParts = parent_childEntry.split(":");
children.add(getChildById(Long.parseLong(parent_childParts[2]))); children.add(getChildById(Long.parseLong(parent_childParts[2])));
} }
return new Parent(id,parts[1],parts[2],parts[4],parts[5],address,children); return new Parent(id, parts[1], parts[2], parts[4], parts[5], address, children);
} }
} }
/** /**
* returns a Child for a given id or null if no unique id was found * returns a Child for a given id or null if no unique id was found
*
* @param id id of child * @param id id of child
* @return Child or null * @return Child or null
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static Child getChildById(long id){ protected static Child getChildById(long id) {
List<String> entry = Database.getEntryById("child",id); List<String> entry = Database.getEntryById("child", id);
if(entry.size() != 1){ if (entry.size() != 1) {
return null; return null;
} }
String[] parts = entry.get(0).split(":"); String[] parts = entry.get(0).split(":");
String[] child_allergyH = {"childid"}; String[] child_allergyH = { "childid" };
String[] child_allergyD = {String.valueOf(id)}; String[] child_allergyD = { String.valueOf(id) };
List<String> entriesAllergy = Database.select("child_allergy",child_allergyH,child_allergyD); List<String> entriesAllergy = Database.select("child_allergy", child_allergyH, child_allergyD);
List<AllergySeverity> allergySeverities = new ArrayList<>(); List<AllergySeverity> allergySeverities = new ArrayList<>();
for (String entryAllergy : entriesAllergy) { for (String entryAllergy : entriesAllergy) {
String[] allergyParts = entryAllergy.split(":"); String[] allergyParts = entryAllergy.split(":");
List<String> severity = Database.getEntryById("severity", Long.parseLong(allergyParts[3])); List<String> severity = Database.getEntryById("severity", Long.parseLong(allergyParts[3]));
String sSeverity = severity.get(0).split(":")[1]; String sSeverity = severity.get(0).split(":")[1];
long lSeverity = Long.parseLong(severity.get(0).split(":")[0]); long lSeverity = Long.parseLong(severity.get(0).split(":")[0]);
allergySeverities.add(new AllergySeverity(FoodMgr.getAllergyById(Long.parseLong(allergyParts[2])),lSeverity,sSeverity)); allergySeverities.add(
new AllergySeverity(FoodMgr.getAllergyById(Long.parseLong(allergyParts[2])), lSeverity, sSeverity));
} }
return new Child(id,parts[1],parts[2],getAddressById(Long.parseLong(parts[3])),allergySeverities); return new Child(id, parts[1], parts[2], getAddressById(Long.parseLong(parts[3])), allergySeverities);
}
/**
* returns all Children
*
* @return {List<Child} List with Childen or empty List
* @author Johannes Kantz
*/
protected static List<Child> getAllChildren() {
List<String> entry = Database.getTable("child");
if (entry.size() < 1) {
return new ArrayList<>();
}
List<Child> children = new ArrayList<>();
for (String s : entry) {
String[] parts = s.split(":");
String[] child_allergyH = { "childid" };
String[] child_allergyD = { String.valueOf(parts[0]) };
List<String> entriesAllergy = Database.select("child_allergy", child_allergyH, child_allergyD);
List<AllergySeverity> allergySeverities = new ArrayList<>();
for (String entryAllergy : entriesAllergy) {
String[] allergyParts = entryAllergy.split(":");
List<String> severity = Database.getEntryById("severity", Long.parseLong(allergyParts[3]));
String sSeverity = severity.get(0).split(":")[1];
long lSeverity = Long.parseLong(severity.get(0).split(":")[0]);
allergySeverities.add(new AllergySeverity(FoodMgr.getAllergyById(Long.parseLong(allergyParts[2])),
lSeverity, sSeverity));
}
children.add(new Child(Long.parseLong(parts[0]), parts[1], parts[2],
getAddressById(Long.parseLong(parts[3])), allergySeverities));
}
return children;
}
/**
* returns all Children from parent
* @param id parentid
* @return {List<Child} List with Childen or empty List
* @author Johannes Kantz
*/
protected static List<Child> getAllChildrenFromParentWithId(long id) {
List<String> entry = Database.select("parent_child", new String[] { "parentuserid" },
new String[] { String.valueOf(id) });
if (entry.size() < 1) {
return new ArrayList<>();
}
List<String> childIds = new ArrayList<>();
for (String s : entry) {
String[] parts = s.split(":");
childIds.add(parts[2]);
}
List<Child> children = new ArrayList<>();
for (String s : childIds) {
List<String> child = Database.getEntryById("child", Long.parseLong(s));
String[] parts = child.get(0).split(":");
String[] child_allergyH = { "childid" };
String[] child_allergyD = { String.valueOf(parts[0]) };
List<String> entriesAllergy = Database.select("child_allergy", child_allergyH, child_allergyD);
List<AllergySeverity> allergySeverities = new ArrayList<>();
for (String entryAllergy : entriesAllergy) {
String[] allergyParts = entryAllergy.split(":");
List<String> severity = Database.getEntryById("severity", Long.parseLong(allergyParts[3]));
String sSeverity = severity.get(0).split(":")[1];
long lSeverity = Long.parseLong(severity.get(0).split(":")[0]);
allergySeverities.add(new AllergySeverity(FoodMgr.getAllergyById(Long.parseLong(allergyParts[2])),
lSeverity, sSeverity));
}
children.add(new Child(Long.parseLong(parts[0]), parts[1], parts[2],
getAddressById(Long.parseLong(parts[3])), allergySeverities));
}
return children;
} }
/** /**
* returns an Address for a given id or null if no unique id was found * returns an Address for a given id or null if no unique id was found
*
* @param id id of the address * @param id id of the address
* @return Address or null * @return Address or null
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static Address getAddressById(long id){ protected static Address getAddressById(long id) {
List<String> entry = Database.getEntryById("address",id); List<String> entry = Database.getEntryById("address", id);
if(entry.size() != 1){ if (entry.size() != 1) {
return null; return null;
} }
String[] parts = entry.get(0).split(":"); String[] parts = entry.get(0).split(":");
return new Address(Long.parseLong(parts[0]),parts[1],parts[2],parts[3],parts[4]); return new Address(Long.parseLong(parts[0]), parts[1], parts[2], parts[3], parts[4]);
} }
/** /**
* creates entries in the database to match parent to child * creates entries in the database to match parent to child
*
* @param parentId id of parent * @param parentId id of parent
* @param childId id of child * @param childId id of child
* @return id of parent_child or -1 * @return id of parent_child or -1
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static long matchParentChild(String parentId, String childId){ protected static long matchParentChild(String parentId, String childId) {
String[] parent_childH = {"parentuserid","childid"}; String[] parent_childH = { "parentuserid", "childid" };
String[] parent_childD = {parentId,childId}; String[] parent_childD = { parentId, childId };
return Database.insert("parent_child", parent_childH,parent_childD); return Database.insert("parent_child", parent_childH, parent_childD);
} }
/** /**
* a simple login to check if a given email matches a password * a simple login to check if a given email matches a password
*
* @param email email * @param email email
* @param pw password * @param pw password
* @return id or -1 * @return id or -1
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static long login(String email, String pw){ protected static long login(String email, String pw) {
String[] pwH = {"email"}; String[] pwH = { "email" };
String[] pwD = {email}; String[] pwD = { email };
List<String> foundEmail = Database.select("user",pwH,pwD); List<String> foundEmail = Database.select("user", pwH, pwD);
String salt; String salt;
if(foundEmail.size() == 1){ if (foundEmail.size() == 1) {
String[] userParts = foundEmail.get(0).split(":"); String[] userParts = foundEmail.get(0).split(":");
String[] pwParts = userParts[4].split("\\."); String[] pwParts = userParts[4].split("\\.");
salt = pwParts[1]; salt = pwParts[1];
}else{ } else {
//no unique user found; still calculating a hash for security reasons // no unique user found; still calculating a hash for security reasons
salt = getSalt(); salt = getSalt();
} }
String[] userH = {"email","password"}; String[] userH = { "email", "password" };
String[] userD = {email,hashAndSalt(pw,salt)}; String[] userD = { email, hashAndSalt(pw, salt) };
return Database.getSingleId("user",userH,userD); return Database.getSingleId("user", userH, userD);
} }
/** /**
* checks if id is in worker table * checks if id is in worker table
*
* @param id userid * @param id userid
* @return true if id is in worker table * @return true if id is in worker table
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static boolean isWorker(String id){ protected static boolean isWorker(String id) {
String[] workerH = {"userid"}; String[] workerH = { "userid" };
String[] workerD = {id}; String[] workerD = { id };
long workerId = Database.getSingleId("worker",workerH,workerD); long workerId = Database.getSingleId("worker", workerH, workerD);
return workerId > 0; return workerId > 0;
} }
/** /**
* checks if id is in parent table * checks if id is in parent table
*
* @param id userid * @param id userid
* @return true if id is in parent table * @return true if id is in parent table
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static boolean isParent(String id){ protected static boolean isParent(String id) {
String[] parentH = {"userid"}; String[] parentH = { "userid" };
String[] parentD = {id}; String[] parentD = { id };
long parentId = Database.getSingleId("parent",parentH,parentD); long parentId = Database.getSingleId("parent", parentH, parentD);
return parentId > 0; return parentId > 0;
} }
/** /**
* returns a hashed and salted password * returns a hashed and salted password
*
* @param pw the password to hash * @param pw the password to hash
* @return hashed and salted password * @return hashed and salted password
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
private static String hashAndSalt(String pw, String salt){ private static String hashAndSalt(String pw, String salt) {
Base64.Decoder dec = Base64.getDecoder(); Base64.Decoder dec = Base64.getDecoder();
byte[] bySalt = dec.decode(salt); byte[] bySalt = dec.decode(salt);
KeySpec spec = new PBEKeySpec(pw.toCharArray(), bySalt,310001,256); KeySpec spec = new PBEKeySpec(pw.toCharArray(), bySalt, 310001, 256);
String hashedPw; String hashedPw;
try { try {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
@ -256,10 +385,11 @@ public class AccountMgr {
/** /**
* generates a secure random salt, Base64 encoded * generates a secure random salt, Base64 encoded
*
* @return String Base64 encoded * @return String Base64 encoded
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
private static String getSalt(){ private static String getSalt() {
SecureRandom sec = new SecureRandom(); SecureRandom sec = new SecureRandom();
byte[] salt = new byte[16]; byte[] salt = new byte[16];
sec.nextBytes(salt); sec.nextBytes(salt);
@ -269,59 +399,62 @@ public class AccountMgr {
/** /**
* gives the invoice for one month and one child * gives the invoice for one month and one child
*
* @param date YYYY-MM the month * @param date YYYY-MM the month
* @param childId id of child * @param childId id of child
* @return the invoice as a List * @return the invoice as a List
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static List<String> getInvoice(String date, String childId){ protected static List<String> getInvoice(String date, String childId) {
List<String> invoice = new ArrayList<>(); List<String> invoice = new ArrayList<>();
List<String> child = Database.getEntryById("child", Long.parseLong(childId)); List<String> child = Database.getEntryById("child", Long.parseLong(childId));
if(child.size() != 1){ if (child.size() != 1) {
return invoice; return invoice;
} }
invoice.add("Monatsabrechnung " + date); invoice.add("Monatsabrechnung " + date);
String[] childParts = child.get(0).split(":"); String[] childParts = child.get(0).split(":");
invoice.add(childParts[1] + ", " + childParts[2]); invoice.add(childParts[1] + ", " + childParts[2]);
String[] food_planH = {"date"}; String[] food_planH = { "date" };
String[] food_planD = {date+"%"}; String[] food_planD = { date + "%" };
List<String> food_plan = Database.select("food_plan",food_planH,food_planD); List<String> food_plan = Database.select("food_plan", food_planH, food_planD);
for (String day : food_plan) { for (String day : food_plan) {
String[] food_planParts = day.split(":"); String[] food_planParts = day.split(":");
String[] food_selectionH = {"childid","food_planid"}; String[] food_selectionH = { "childid", "food_planid" };
String[] food_selectionD = {childId,food_planParts[0]}; String[] food_selectionD = { childId, food_planParts[0] };
List<String> food_selection = Database.select("food_selection",food_selectionH,food_selectionD); List<String> food_selection = Database.select("food_selection", food_selectionH, food_selectionD);
for (String food_select : food_selection) { for (String food_select : food_selection) {
String[] food_selectParts = food_select.split(":"); String[] food_selectParts = food_select.split(":");
List<String> food = Database.getEntryById("food",Long.parseLong(food_selectParts[3])); List<String> food = Database.getEntryById("food", Long.parseLong(food_selectParts[3]));
String[] foodParts = food.get(0).split(":"); String[] foodParts = food.get(0).split(":");
String line = food_planParts[1] + ": " + foodParts[1]; String line = food_planParts[1] + ": " + foodParts[1];
invoice.add(line); invoice.add(line);
} }
} }
double price = getPrice(); double price = getPrice();
invoice.add("Total: " + (invoice.size()-2) + " X " + price + "€ = " + ((invoice.size()-2)*price) + ""); invoice.add("Total: " + (invoice.size() - 2) + " X " + price + "€ = " + ((invoice.size() - 2) * price) + "");
return invoice; return invoice;
} }
/** /**
* gets the price per meal from the database and converts it to double * gets the price per meal from the database and converts it to double
*
* @return double price * @return double price
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static double getPrice(){ protected static double getPrice() {
List<String> priceEntry = Database.getEntryById("price",1); List<String> priceEntry = Database.getEntryById("price", 1);
return Double.parseDouble(priceEntry.get(0).split(":")[1])/100.0; return Double.parseDouble(priceEntry.get(0).split(":")[1]) / 100.0;
} }
/** /**
* converts the price per meal to integer and updates it in the database * converts the price per meal to integer and updates it in the database
*
* @param price double * @param price double
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static void setPrice(double price){ protected static void setPrice(double price) {
String[] priceH = {"id","price"}; String[] priceH = { "id", "price" };
String[] priceD = {"1", String.valueOf((int)(price*100))}; String[] priceD = { "1", String.valueOf((int) (price * 100)) };
Database.update("price",priceH,priceD); Database.update("price", priceH, priceD);
} }
} }