diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/AccountMgr.java b/src/main/java/com/bib/essensbestellungsverwaltung/AccountMgr.java index 558517f..cf4b1b8 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/AccountMgr.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/AccountMgr.java @@ -14,13 +14,14 @@ import java.util.List; public class AccountMgr { protected static double price = 5.0; + /** * 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 */ - public static long createWorker(String[] userData, String[] addressData){ + protected static long createWorker(String[] userData, String[] addressData){ long id = createUser(userData, addressData); String sId = String.valueOf(id); Database.insert("worker", new String[]{"userid"}, new String[]{sId}); @@ -33,7 +34,7 @@ public class AccountMgr { * @param addressData String[] street, number, plz, city * @return userid or -1 */ - public static long createParent(String[] userData, String[] addressData){ + protected static long createParent(String[] userData, String[] addressData){ long id = createUser(userData, addressData); String sId = String.valueOf(id); Database.insert("parent", new String[]{"userid"}, new String[]{sId}); @@ -46,7 +47,7 @@ public class AccountMgr { * @param addressData String[] street, number, plz, city * @return userid or -1 */ - private static long createUser(String[] userData, String[] addressData) { + protected static long createUser(String[] userData, String[] addressData) { String[] addressH = {"street", "number", "plz", "city"}; String[] userH = {"name", "firstname", "addressid", "password", "email"}; String name = userData[0]; @@ -61,7 +62,14 @@ public class AccountMgr { return id; } - public static long createChild(String[] childData,String[] allergyData, String[] severityData){ + /** + * 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){ String[] childH = {"name","firstname","addressid"}; String[] child_allergyH = {"childid","allergyid","severityid"}; long id = Database.insert("child", childH, childData); @@ -75,33 +83,60 @@ public class AccountMgr { return id; } - public static long matchParentChild(String parentId, String childId){ + /** + * 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 + */ + protected static long matchParentChild(String parentId, String childId){ String[] parent_childH = {"parentuserid","childid"}; String[] parent_childD = {parentId,childId}; return Database.insert("parent_child", parent_childH,parent_childD); } - public static long login(String email, String pw){ + /** + * a simple login to check if a given email matches a password + * @param email email + * @param pw password + * @return id or -1 + */ + protected static long login(String email, String pw){ String[] userH = {"email","password"}; String[] userD = {email,hashAndSalt(pw)}; return Database.getSingleId("user",userH,userD); } - public static boolean isWorker(String id){ + /** + * checks if id is in worker table + * @param id userid + * @return true if id is in worker table + */ + protected static boolean isWorker(String id){ String[] workerH = {"userid"}; String[] workerD = {id}; long workerId = Database.getSingleId("worker",workerH,workerD); return workerId > 0; } - public static boolean isParent(String id){ + /** + * checks if id is in parent table + * @param id userid + * @return true if id is in parent table + */ + protected static boolean isParent(String id){ String[] parentH = {"userid"}; String[] parentD = {id}; long parentId = Database.getSingleId("parent",parentH,parentD); return parentId > 0; } - public static String hashAndSalt(String pw){ + /** + * returns a hashed and salted password + * @param pw the password to hash + * @return hashed and salted password + */ + protected static String hashAndSalt(String pw){ //todo: find a better way to salt byte[] magicSalt = new byte[]{96, 13, 100, 85, -37, 52, -123, 86, -123, -92, 16, 15, -110, -42, -49, 0}; KeySpec spec = new PBEKeySpec(pw.toCharArray(), magicSalt,310001,256); @@ -117,7 +152,13 @@ public class AccountMgr { return hashedPw; } - public static List getInvoice(String date, String childId){ + /** + * 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 + */ + protected static List getInvoice(String date, String childId){ List invoice = new ArrayList<>(); invoice.add("Monatsabrechnung " + date); List child = Database.getEntryById("child", Long.parseLong(childId)); diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/Database.java b/src/main/java/com/bib/essensbestellungsverwaltung/Database.java index 0e5d949..a09068a 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/Database.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/Database.java @@ -13,6 +13,11 @@ import java.util.List; public class Database { private static final String dbLocation = "jdbc:sqlite:"+Path.of("").toAbsolutePath()+"/database.db"; + + /** + * creates new database.db if it doesn't exist + * @return true if a new database has been created + */ protected static boolean init(){ File db = new File(Path.of("").toAbsolutePath()+"/database.db"); try { @@ -21,6 +26,11 @@ public class Database { throw new RuntimeException(e); } } + + /** + * connects to the database + * @return Connection to the database + */ protected static Connection connect(){ Connection conn = null; try{ @@ -31,6 +41,9 @@ public class Database { return conn; } + /** + * creates the initial structure of the db + */ protected static void createDb(){ String[] sql = new String[14]; sql[0] = """ @@ -152,6 +165,9 @@ public class Database { } } + /** + * inserts fixed values into the database + */ protected static void fillDb(){ List sqls = new ArrayList<>(); sqls.add(""" @@ -249,6 +265,7 @@ public class Database { /** * inserts data into table and returns its id + * does not insert if the exact set already exists * @param table name of the database table * @param header String[] order should match with values * @param values String[] order should match with header @@ -288,6 +305,13 @@ public class Database { return id; } + /** + * returns a single id that matches the given data + * @param table the table that contains the searched entry + * @param header the header of the table, order should match with values + * @param values the data you want the id of, order should match witch values + * @return one id matching the given data or -1 if no match has been found + */ protected static long getSingleId(String table, String[] header, String[] values){ long id = -1; try(Connection conn = connect()){ @@ -307,6 +331,9 @@ public class Database { return id; } + /** + * @deprecated + */ protected static void printSampleQuery(){ String sql = """ SELECT * FROM food_type WHERE id > ?;"""; @@ -332,6 +359,11 @@ public class Database { } } + /** + * deletes an entry from table with matching id + * @param table the table that contains the entry you want to delete + * @param id the id of the entry you want to delete + */ protected static void delete(String table, long id){ String sql = "DELETE FROM " + table + " WHERE id = ?;"; try(Connection conn = connect();PreparedStatement ps = conn.prepareStatement(sql)){ @@ -434,6 +466,11 @@ public class Database { return sql; } + /** + * returns a list of all entries + * @param table the table you want + * @return a list of all entries as String with the fields separated by ":" + */ protected static List getTable(String table){ List data = new ArrayList<>(); StringBuilder sb; @@ -458,6 +495,13 @@ public class Database { return data; } + /** + * issues a select query on the database for the given table and the given values checked with LIKE + * @param table the table you want the data from + * @param header header for the WHERE portion, order should match with values + * @param values values for the WHERE portion, order should match with header + * @return a list of the matching data as String separated by ":" + */ protected static List select(String table,String[] header, String[] values){ List data = new ArrayList<>(); StringBuilder sb; @@ -483,6 +527,12 @@ public class Database { return data; } + /** + * returns the entry from table with the given id + * @param table the table you want the entry from + * @param id the id of the entry you want + * @return a list of String separated by ":" + */ protected static List getEntryById(String table, long id){ List data = new ArrayList<>(); StringBuilder sb; @@ -509,6 +559,13 @@ public class Database { return data; } + /** + * counts the number of matching entries + * @param table the table you want to count + * @param header the properties you want to count on + * @param values the values for the properties + * @return the number of found rows + */ protected static int count(String table,String[] header,String[] values){ String sql = queryBuilder("count",table,header,values); try(Connection conn = connect()) { @@ -520,6 +577,13 @@ public class Database { } } + /** + * updates an entry in the database + * @param table the table you want to update + * @param header [0] is used as WHERE, everything else in SET + * @param values [0] is used as WHERE, everything else in SET + * @return number of rows affected or -1 on error + */ protected static int update(String table,String[] header,String[] values){ try(Connection conn = connect()) { String sql = queryBuilder("update",table,header,values); diff --git a/src/main/java/com/bib/essensbestellungsverwaltung/FoodMgr.java b/src/main/java/com/bib/essensbestellungsverwaltung/FoodMgr.java index 50d791b..5aa44b8 100644 --- a/src/main/java/com/bib/essensbestellungsverwaltung/FoodMgr.java +++ b/src/main/java/com/bib/essensbestellungsverwaltung/FoodMgr.java @@ -7,6 +7,12 @@ import java.util.ArrayList; import java.util.List; public class FoodMgr { + /** + * inserts a food int to the database and creates the food_restriction entries + * @param foodData name, description, isdessert, food_typeid + * @param allergyData allergyids + * @return id of food or -1 + */ public static long createFood(String[] foodData, String[] allergyData){ String[] foodH = {"name","description","isDessert","food_typeid"}; String[] food_restrictionH = {"foodid","allergyid"}; @@ -21,23 +27,43 @@ public class FoodMgr { return id; } + /** + * inserts a food_plan into the database + * @param food_planData date[YYYY-MM-DD], foodid[vegan], foodid, foodid[dessert,vegan], foodid[dessert] + * @return id of food_plan or -1 + */ public static long createFood_plan(String[] food_planData){ String[] food_planH = {"date","food1","food2","dessert1","dessert2"}; return Database.insert("food_plan",food_planH,food_planData); } + /** + * returns all non desserts or all desserts + * @param isDessert true for only desserts false for non desserts + * @return a list of all non desserts or all desserts + */ public static List getFood(boolean isDessert){ String[] foodH = {"isDessert"}; String[] foodD = {(isDessert ? "1" : "0")}; return Database.select("food",foodH,foodD); } + /** + * getFood but returns only vegan food + * @param isDessert true for only desserts false for non desserts + * @return a list of all vegan non desserts or all vegan desserts + */ public static List getVeganFood(boolean isDessert){ String[] foodH = {"isDessert","food_typeid"}; String[] foodD = {(isDessert ? "1" : "0"),"1"}; return Database.select("food",foodH,foodD); } + /** + * returns a food_plan for a day + * @param date YYYY-MM-DD one day + * @return food_plan for date + */ public static List getFood_plan(String date){ String[] food_planH = {"date"}; String[] food_planD = {date}; @@ -48,6 +74,11 @@ public class FoodMgr { return Database.getEntryById("food",id); } + /** + * inserts the selected food into food_Selection if the food_plan has not been sent + * @param food_selectionData childid, food_planid, foodid + * @return id or -1 + */ public static long createFood_selection(String[] food_selectionData){ String[] food_selectionH = {"childid","food_planid","foodid"}; List food_plan = Database.getEntryById("food_plan",Long.parseLong(food_selectionData[1])); @@ -59,6 +90,11 @@ public class FoodMgr { } } + /** + * accumulates the selected food for a given day and locks the corresponding food_plan + * @param date YYYY-MM-DD day + * @return the accumulated orders + */ public static List getDayOrder(String date){ List orders = new ArrayList<>(); List food_plan = getFood_plan(date);