most Objects done
This commit is contained in:
@@ -18,12 +18,10 @@ public class AccountMgr {
|
||||
|
||||
/**
|
||||
* creates a user with createUser(...) and adds its id to the 'worker' table
|
||||
* @param userData String[] name, firstname, password, email
|
||||
* @param addressData String[] street, number, plz, city
|
||||
* @return userid or -1
|
||||
*/
|
||||
protected static long createWorker(String[] userData, String[] addressData){
|
||||
long id = createUser(userData, addressData);
|
||||
protected static long createWorker(Worker worker){
|
||||
long id = createUser(worker);
|
||||
String sId = String.valueOf(id);
|
||||
Database.insert("worker", new String[]{"userid"}, new String[]{sId});
|
||||
return id;
|
||||
@@ -31,12 +29,10 @@ public class AccountMgr {
|
||||
|
||||
/**
|
||||
* creates a user with createUser(...) and adds its id to the 'parent' table
|
||||
* @param userData String[] name, firstname, password, email
|
||||
* @param addressData String[] street, number, plz, city
|
||||
* @return userid or -1
|
||||
*/
|
||||
protected static long createParent(String[] userData, String[] addressData){
|
||||
long id = createUser(userData, addressData);
|
||||
protected static long createParent(Parent parent){
|
||||
long id = createUser(parent);
|
||||
String sId = String.valueOf(id);
|
||||
Database.insert("parent", new String[]{"userid"}, new String[]{sId});
|
||||
return id;
|
||||
@@ -44,42 +40,43 @@ public class AccountMgr {
|
||||
|
||||
/**
|
||||
* adds a user to the database
|
||||
* @param userData String[] name, firstname, password, email
|
||||
* @param addressData String[] street, number, plz, city
|
||||
* @return userid or -1
|
||||
*/
|
||||
protected static long createUser(String[] userData, String[] addressData) {
|
||||
String[] addressH = {"street", "number", "plz", "city"};
|
||||
protected static long createUser(User user) {
|
||||
String[] userH = {"name", "firstname", "addressid", "password", "email"};
|
||||
String name = userData[0];
|
||||
String firstname = userData[1];
|
||||
String pw = hashAndSalt(userData[2], getSalt());
|
||||
String email = userData[3];
|
||||
String name = user.getName();
|
||||
String firstname = user.getFirstname();
|
||||
String pw = hashAndSalt(user.getPassword(), getSalt());
|
||||
String email = user.getEmail();
|
||||
long addressId = user.getAddress().getId();
|
||||
if(addressId < 1){
|
||||
addressId = createAddress(user.getAddress());
|
||||
}
|
||||
String[] userD = {name, firstname, String.valueOf(addressId), pw, email};
|
||||
return Database.insert("user", userH, userD);
|
||||
}
|
||||
|
||||
long id = Database.insert("address", addressH, addressData);
|
||||
String sId = String.valueOf(id);
|
||||
String[] userD = {name, firstname, sId, pw, email};
|
||||
id = Database.insert("user", userH, userD);
|
||||
return id;
|
||||
protected static long createAddress(Address address){
|
||||
String[] addressH = {"street", "number", "plz", "city"};
|
||||
String[] addressD = {address.getStreet(),address.getNumber(),address.getPlz(),address.getCity()};
|
||||
return Database.insert("address",addressH,addressD);
|
||||
}
|
||||
|
||||
/**
|
||||
* adds a child and allergies to the database
|
||||
* @param childData name, firstname, addressid
|
||||
* @param allergyData id of allergies order matching with severityData
|
||||
* @param severityData id of severity order matching with allergyData
|
||||
* @return id of child or -1
|
||||
*/
|
||||
protected static long createChild(String[] childData,String[] allergyData, String[] severityData){
|
||||
protected static long createChild(Child child){
|
||||
String[] childH = {"name","firstname","addressid"};
|
||||
String[] childD = {child.getName(), child.getFirstname(), String.valueOf(child.getAddress().getId())};
|
||||
long id = Database.insert("child", childH, childD);
|
||||
String[] child_allergyH = {"childid","allergyid","severityid"};
|
||||
long id = Database.insert("child", childH, childData);
|
||||
if(allergyData.length > 0){
|
||||
for(int i = 0; i < allergyData.length; i++){
|
||||
String sId = String.valueOf(id);
|
||||
String[] child_allergyD = {sId,allergyData[i],severityData[i]};
|
||||
Database.insert("child_allergy",child_allergyH,child_allergyD);
|
||||
}
|
||||
for (AllergySeverity allergy: child.getAllergies()) {
|
||||
String sId = String.valueOf(id);
|
||||
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 id;
|
||||
}
|
||||
@@ -88,7 +85,36 @@ public class AccountMgr {
|
||||
List<String> entry = Database.getEntryById("user",id);
|
||||
String[] parts = entry.get(0).split(":");
|
||||
Address address = getAddressById(id);
|
||||
return new User(id,parts[1],parts[2],parts[4],parts[5],address,isWorker(String.valueOf(id)),isParent(String.valueOf(id)));
|
||||
if(isWorker(String.valueOf(id))){
|
||||
return new Worker(id,parts[1],parts[2],parts[4],parts[5],address);
|
||||
}else{
|
||||
String[] parent_childH = {"parentuserid"};
|
||||
String[] parent_childD = {String.valueOf(id)};
|
||||
List<Child> children = new ArrayList<>();
|
||||
List<String> parent_childEntries = Database.select("parent_child",parent_childH,parent_childD);
|
||||
for (String parent_childEntry: parent_childEntries) {
|
||||
String[] parent_childParts = parent_childEntry.split(":");
|
||||
children.add(getChildById(Long.parseLong(parent_childParts[2])));
|
||||
}
|
||||
return new Parent(id,parts[1],parts[2],parts[4],parts[5],address,children);
|
||||
}
|
||||
}
|
||||
|
||||
protected static Child getChildById(long id){
|
||||
List<String> entry = Database.getEntryById("child",id);
|
||||
String[] parts = entry.get(0).split(":");
|
||||
String[] child_allergyH = {"childid"};
|
||||
String[] child_allergyD = {String.valueOf(id)};
|
||||
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));
|
||||
}
|
||||
return new Child(id,parts[1],parts[2],getAddressById(Long.parseLong(parts[3])),allergySeverities);
|
||||
}
|
||||
|
||||
protected static Address getAddressById(long id){
|
||||
@@ -162,7 +188,7 @@ public class AccountMgr {
|
||||
* @param pw the password to hash
|
||||
* @return hashed and salted password
|
||||
*/
|
||||
protected static String hashAndSalt(String pw, String salt){
|
||||
private static String hashAndSalt(String pw, String salt){
|
||||
Base64.Decoder dec = Base64.getDecoder();
|
||||
byte[] bySalt = dec.decode(salt);
|
||||
KeySpec spec = new PBEKeySpec(pw.toCharArray(), bySalt,310001,256);
|
||||
@@ -218,4 +244,15 @@ public class AccountMgr {
|
||||
invoice.add("Total: " + (invoice.size()-2) + " X " + price + "€ = " + ((invoice.size()-2)*price) + "€");
|
||||
return invoice;
|
||||
}
|
||||
|
||||
protected static void getPriceFromDb(){
|
||||
List<String> priceEntry = Database.getEntryById("price",1);
|
||||
price = Double.parseDouble(priceEntry.get(0).split(":")[1])/100.0;
|
||||
}
|
||||
|
||||
protected static void setPriceInDb(){
|
||||
String[] priceH = {"id","price"};
|
||||
String[] priceD = {"1", String.valueOf(price*100)};
|
||||
Database.update("price",priceH,priceD);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user