2023-01-05 05:06:19 +01:00
|
|
|
package com.bib.essensbestellungsverwaltung;
|
2023-01-06 01:51:42 +01:00
|
|
|
/*
|
|
|
|
@author Malte Schulze Hobeling
|
|
|
|
*/
|
2023-01-05 05:06:19 +01:00
|
|
|
|
2023-01-07 23:14:41 +01:00
|
|
|
import java.util.ArrayList;
|
2023-01-05 05:06:19 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2023-01-27 15:14:36 +01:00
|
|
|
|
2023-01-05 05:06:19 +01:00
|
|
|
public class FoodMgr {
|
2023-01-16 16:35:45 +01:00
|
|
|
/**
|
|
|
|
* inserts a food int to the database and creates the food_restriction entries
|
|
|
|
* @return id of food or -1
|
|
|
|
*/
|
2023-01-27 15:14:36 +01:00
|
|
|
public static long createFood(Food food){
|
2023-01-05 05:06:19 +01:00
|
|
|
String[] foodH = {"name","description","isDessert","food_typeid"};
|
|
|
|
String[] food_restrictionH = {"foodid","allergyid"};
|
2023-01-27 15:14:36 +01:00
|
|
|
String[] foodD = {food.getName(),food.getDescription(),(food.isDessert() ? "1" : "0"), String.valueOf(food.getFoodType().getId())};
|
|
|
|
long id = Database.insert("food",foodH,foodD);
|
|
|
|
if(food.getAllergies().size() > 0){
|
2023-01-05 05:06:19 +01:00
|
|
|
String sId = String.valueOf(id);
|
2023-01-27 15:14:36 +01:00
|
|
|
for (Allergy allergy : food.getAllergies()) {
|
|
|
|
String[] food_restrictionD = {sId, String.valueOf(allergy.getId())};
|
2023-01-05 05:06:19 +01:00
|
|
|
Database.insert("food_restriction",food_restrictionH, food_restrictionD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2023-01-16 16:35:45 +01:00
|
|
|
/**
|
|
|
|
* inserts a food_plan into the database
|
|
|
|
* @return id of food_plan or -1
|
|
|
|
*/
|
2023-01-30 04:26:30 +01:00
|
|
|
public static long createFood_plan(FoodPlan foodPlan){
|
2023-01-05 05:06:19 +01:00
|
|
|
String[] food_planH = {"date","food1","food2","dessert1","dessert2"};
|
2023-01-30 04:26:30 +01:00
|
|
|
String[] food_planD = {foodPlan.getDate(),
|
|
|
|
String.valueOf(foodPlan.getFoodVegan().getId()),
|
|
|
|
String.valueOf(foodPlan.getFoodSecond().getId()),
|
|
|
|
String.valueOf(foodPlan.getDessertVegan().getId()),
|
|
|
|
String.valueOf(foodPlan.getDessertSecond().getId())};
|
|
|
|
return Database.insert("food_plan",food_planH,food_planD);
|
2023-01-05 05:06:19 +01:00
|
|
|
}
|
|
|
|
|
2023-01-16 16:35:45 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2023-01-27 15:14:36 +01:00
|
|
|
public static List<Food> getFood(boolean isDessert){
|
2023-01-05 05:06:19 +01:00
|
|
|
String[] foodH = {"isDessert"};
|
|
|
|
String[] foodD = {(isDessert ? "1" : "0")};
|
2023-01-27 15:14:36 +01:00
|
|
|
List<String> entries = Database.select("food",foodH,foodD);
|
|
|
|
List<Food> foods = new ArrayList<>();
|
|
|
|
for (String entry : entries) {
|
|
|
|
String[] parts = entry.split(":");
|
|
|
|
foods.add(getFoodById(Long.parseLong(parts[0])));
|
|
|
|
}
|
|
|
|
return foods;
|
2023-01-05 05:06:19 +01:00
|
|
|
}
|
|
|
|
|
2023-01-16 16:35:45 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2023-01-27 15:14:36 +01:00
|
|
|
public static List<Food> getVeganFood(boolean isDessert){
|
2023-01-05 05:06:19 +01:00
|
|
|
String[] foodH = {"isDessert","food_typeid"};
|
|
|
|
String[] foodD = {(isDessert ? "1" : "0"),"1"};
|
2023-01-27 15:14:36 +01:00
|
|
|
List<String> entries = Database.select("food",foodH,foodD);
|
|
|
|
List<Food> foods = new ArrayList<>();
|
|
|
|
for (String entry : entries) {
|
|
|
|
String[] parts = entry.split(":");
|
|
|
|
foods.add(getFoodById(Long.parseLong(parts[0])));
|
|
|
|
}
|
|
|
|
return foods;
|
2023-01-05 05:06:19 +01:00
|
|
|
}
|
|
|
|
|
2023-01-16 16:35:45 +01:00
|
|
|
/**
|
|
|
|
* returns a food_plan for a day
|
|
|
|
* @param date YYYY-MM-DD one day
|
|
|
|
* @return food_plan for date
|
|
|
|
*/
|
2023-01-30 04:26:30 +01:00
|
|
|
public static FoodPlan getFoodPlan(String date){
|
2023-01-05 05:06:19 +01:00
|
|
|
String[] food_planH = {"date"};
|
|
|
|
String[] food_planD = {date};
|
2023-01-30 04:26:30 +01:00
|
|
|
List<String> entry = Database.select("food_plan",food_planH,food_planD);
|
|
|
|
String[] parts = entry.get(0).split(":");
|
|
|
|
Food foodVegan = getFoodById(Long.parseLong(parts[2]));
|
|
|
|
Food foodSecond = getFoodById(Long.parseLong(parts[3]));
|
|
|
|
Food dessertVegan = getFoodById(Long.parseLong(parts[4]));
|
|
|
|
Food dessertSecond = getFoodById(Long.parseLong(parts[5]));
|
|
|
|
boolean isSent = !parts[6].equals("0");
|
|
|
|
return new FoodPlan(Long.parseLong(parts[0]),date,foodVegan,foodSecond,dessertVegan,dessertSecond,isSent);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static FoodPlan getFoodPlanById(long id){
|
|
|
|
List<String> entry = Database.getEntryById("food_plan",id);
|
|
|
|
String[] parts = entry.get(0).split(":");
|
|
|
|
Food foodVegan = getFoodById(Long.parseLong(parts[2]));
|
|
|
|
Food foodSecond = getFoodById(Long.parseLong(parts[3]));
|
|
|
|
Food dessertVegan = getFoodById(Long.parseLong(parts[4]));
|
|
|
|
Food dessertSecond = getFoodById(Long.parseLong(parts[5]));
|
|
|
|
boolean isSent = !parts[6].equals("0");
|
|
|
|
return new FoodPlan(id,parts[1], foodVegan,foodSecond,dessertVegan,dessertSecond,isSent);
|
2023-01-05 05:06:19 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 15:14:36 +01:00
|
|
|
public static Food getFoodById(long id){
|
|
|
|
List<String> entry = Database.getEntryById("food",id);
|
|
|
|
String[] parts = entry.get(0).split(":");
|
|
|
|
String name = parts[1];
|
|
|
|
String description = parts[2];
|
|
|
|
boolean isDessert;
|
|
|
|
isDessert = parts[3].equals("0");
|
|
|
|
FoodType foodType = getFoodTypeById(Long.parseLong(parts[4]));
|
|
|
|
List<Allergy> allergies = getAllergies(id);
|
|
|
|
return new Food(id,name,description,isDessert,foodType,allergies);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static FoodType getFoodTypeById(long id){
|
2023-01-30 04:26:30 +01:00
|
|
|
List<String> entry = Database.getEntryById("food_type",id);
|
2023-01-27 15:14:36 +01:00
|
|
|
String[] typeParts = entry.get(0).split(":");
|
|
|
|
return new FoodType(Long.parseLong(typeParts[0]),typeParts[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Allergy getAllergyById(long id){
|
|
|
|
String[] allergyH = {"id"};
|
|
|
|
String[] allergyD = {String.valueOf(id)};
|
|
|
|
List<String> allergies = Database.select("allergy",allergyH,allergyD);
|
|
|
|
String[] allergyParts = allergies.get(0).split(":");
|
|
|
|
return new Allergy(id,allergyParts[1],allergyParts[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<Allergy> getAllergies(long foodId){
|
|
|
|
List<Allergy> allergies = new ArrayList<>();
|
|
|
|
String[] restrictionsH = {"foodid"};
|
|
|
|
String[] restrictionsD = {String.valueOf(foodId)};
|
2023-01-30 04:26:30 +01:00
|
|
|
List<String> restrictions = Database.select("food_restriction",restrictionsH,restrictionsD);
|
2023-01-27 15:14:36 +01:00
|
|
|
for (String restriction : restrictions) {
|
|
|
|
String[] partsRestrictions = restriction.split(":");
|
|
|
|
allergies.add(getAllergyById(Long.parseLong(partsRestrictions[2])));
|
|
|
|
}
|
|
|
|
return allergies;
|
2023-01-05 05:06:19 +01:00
|
|
|
}
|
2023-01-06 01:51:42 +01:00
|
|
|
|
2023-01-16 16:35:45 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2023-01-06 01:51:42 +01:00
|
|
|
public static long createFood_selection(String[] food_selectionData){
|
2023-01-07 23:14:41 +01:00
|
|
|
String[] food_selectionH = {"childid","food_planid","foodid"};
|
2023-01-30 04:26:30 +01:00
|
|
|
FoodPlan food_plan = getFoodPlanById(Long.parseLong(food_selectionData[1]));
|
|
|
|
if(!food_plan.isSent()){
|
2023-01-07 23:14:41 +01:00
|
|
|
return Database.insert("food_selection",food_selectionH,food_selectionData);
|
|
|
|
}else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 16:35:45 +01:00
|
|
|
/**
|
|
|
|
* accumulates the selected food for a given day and locks the corresponding food_plan
|
|
|
|
* @param date YYYY-MM-DD day
|
|
|
|
* @return the accumulated orders
|
|
|
|
*/
|
2023-01-07 23:14:41 +01:00
|
|
|
public static List<String> getDayOrder(String date){
|
|
|
|
List<String> orders = new ArrayList<>();
|
2023-01-30 04:26:30 +01:00
|
|
|
FoodPlan food_plan = getFoodPlan(date);
|
|
|
|
String sId = String.valueOf(food_plan.getId());
|
|
|
|
String[] food_selectionH = {"food_planid","foodid"};
|
|
|
|
Food[] foodArray = {
|
|
|
|
food_plan.getFoodVegan(),
|
|
|
|
food_plan.getFoodSecond(),
|
|
|
|
food_plan.getDessertVegan(),
|
|
|
|
food_plan.getDessertSecond()
|
|
|
|
};
|
|
|
|
for(int i = 0; i < 4; i++){
|
|
|
|
String foodName = foodArray[i].getName();
|
|
|
|
String[] food_selectionD = {sId, String.valueOf(foodArray[i].getId())};
|
2023-01-07 23:14:41 +01:00
|
|
|
int count = Database.count("food_selection",food_selectionH,food_selectionD);
|
2023-01-30 04:26:30 +01:00
|
|
|
orders.add(count+" X "+foodName);
|
2023-01-07 23:14:41 +01:00
|
|
|
}
|
|
|
|
String[] updateH = {"id","issent"};
|
2023-01-30 04:26:30 +01:00
|
|
|
String[] updateD = {sId,"1"};
|
2023-01-07 23:14:41 +01:00
|
|
|
if(Database.update("food_plan",updateH,updateD) < 0){
|
|
|
|
System.out.println("Fehler");
|
|
|
|
}
|
|
|
|
return orders;
|
2023-01-06 01:51:42 +01:00
|
|
|
}
|
2023-01-05 05:06:19 +01:00
|
|
|
}
|