add: child methods

This commit is contained in:
Johannes Kantz 2023-02-04 18:30:14 +01:00
parent 7534df3263
commit 944ddbff60

View File

@ -13,6 +13,7 @@ import java.util.List;
/**
* A collection of functions loosely related to account management
* Acts as an abstraction layer to the database
*
* @author Malte Schulze Hobeling
*/
public class AccountMgr {
@ -21,6 +22,7 @@ public class AccountMgr {
/**
* creates a user with createUser(...) and adds its id to the 'worker' table
*
* @param worker the worker to be created
* @return userid or -1
* @author Malte Schulze Hobeling
@ -34,6 +36,7 @@ public class AccountMgr {
/**
* creates a user with createUser(...) and adds its id to the 'parent' table
*
* @param parent the parent to be created
* @return userid or -1
* @author Malte Schulze Hobeling
@ -47,6 +50,7 @@ public class AccountMgr {
/**
* adds a user to the database
*
* @param user the user to be created
* @return userid or -1
* @author Malte Schulze Hobeling
@ -67,6 +71,7 @@ public class AccountMgr {
/**
* adds an address to the database
*
* @param address the address to be created
* @return id or -1
* @author Malte Schulze Hobeling
@ -79,6 +84,7 @@ public class AccountMgr {
/**
* adds a child and allergies to the database
*
* @param child the child to be created
* @return id of child or -1
* @author Malte Schulze Hobeling
@ -99,7 +105,51 @@ public class AccountMgr {
}
/**
* 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
* @return User(Worker | Parent) or null
* @author Malte Schulze Hobeling
@ -128,6 +178,7 @@ public class AccountMgr {
/**
* returns a Child for a given id or null if no unique id was found
*
* @param id id of child
* @return Child or null
* @author Malte Schulze Hobeling
@ -147,13 +198,86 @@ public class AccountMgr {
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));
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);
}
/**
* 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
*
* @param id id of the address
* @return Address or null
* @author Malte Schulze Hobeling
@ -169,6 +293,7 @@ public class AccountMgr {
/**
* creates entries in the database to match parent to child
*
* @param parentId id of parent
* @param childId id of child
* @return id of parent_child or -1
@ -182,6 +307,7 @@ public class AccountMgr {
/**
* a simple login to check if a given email matches a password
*
* @param email email
* @param pw password
* @return id or -1
@ -207,6 +333,7 @@ public class AccountMgr {
/**
* checks if id is in worker table
*
* @param id userid
* @return true if id is in worker table
* @author Malte Schulze Hobeling
@ -220,6 +347,7 @@ public class AccountMgr {
/**
* checks if id is in parent table
*
* @param id userid
* @return true if id is in parent table
* @author Malte Schulze Hobeling
@ -233,6 +361,7 @@ public class AccountMgr {
/**
* returns a hashed and salted password
*
* @param pw the password to hash
* @return hashed and salted password
* @author Malte Schulze Hobeling
@ -256,6 +385,7 @@ public class AccountMgr {
/**
* generates a secure random salt, Base64 encoded
*
* @return String Base64 encoded
* @author Malte Schulze Hobeling
*/
@ -269,6 +399,7 @@ public class AccountMgr {
/**
* gives the invoice for one month and one child
*
* @param date YYYY-MM the month
* @param childId id of child
* @return the invoice as a List
@ -306,6 +437,7 @@ public class AccountMgr {
/**
* gets the price per meal from the database and converts it to double
*
* @return double price
* @author Malte Schulze Hobeling
*/
@ -316,6 +448,7 @@ public class AccountMgr {
/**
* converts the price per meal to integer and updates it in the database
*
* @param price double
* @author Malte Schulze Hobeling
*/