Created basic recipe.
This commit is contained in:
parent
07d3fe304d
commit
646183174a
1 changed files with 166 additions and 0 deletions
166
commons/src/main/java/commons/Recipe.java
Normal file
166
commons/src/main/java/commons/Recipe.java
Normal file
|
|
@ -0,0 +1,166 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2021 Delft University of Technology
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package commons;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
// TABLE named recipes
|
||||||
|
@Entity
|
||||||
|
@Table(name = "recipes")
|
||||||
|
public class Recipe {
|
||||||
|
|
||||||
|
// PRIMARY Key, unique id for recipe, generated automatically.
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
@Column(name = "id", nullable = false, unique = true)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
// Recipe name
|
||||||
|
@Column(name = "name", nullable = false, unique = true)
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
// Creates another table named recipe_ingredients which stores:
|
||||||
|
// recipe_ingredients(recipe_id -> recipes(id), ingredient).
|
||||||
|
// Example recipe_ingredients table:
|
||||||
|
// | recipe_id | ingredient |
|
||||||
|
// |---------------------|------------|
|
||||||
|
// | 0 (Chocolate Cake) | Egg |
|
||||||
|
// | 0 (Chocolate Cake) | Egg |
|
||||||
|
// | 0 (Chocolate Cake) | Flour |
|
||||||
|
// | 1 (Steak) | 40g salt |
|
||||||
|
// | 1 (Steak) | 40g pepper |
|
||||||
|
// | 1 (Steak) | Meat |
|
||||||
|
// |----------------------------------|
|
||||||
|
@ElementCollection
|
||||||
|
@CollectionTable(name = "recipe_ingredients", joinColumns = @JoinColumn(name = "recipe_id"))
|
||||||
|
@Column(name = "ingredient")
|
||||||
|
// TODO: Replace String with Embeddable Ingredient Class
|
||||||
|
private List<String> ingredients = new ArrayList<>();
|
||||||
|
|
||||||
|
// Creates another table named recipe_preparation which stores:
|
||||||
|
// recipe_preparation(recipe_id -> recipes(id), preparation_step, step_order).
|
||||||
|
// Example recipe_preparation table:
|
||||||
|
// | recipe_id | preparation_step | step_order |
|
||||||
|
// |---------------------|----------------------|------------|
|
||||||
|
// | 0 (Chocolate Cake) | Preheat oven | 1 |
|
||||||
|
// | 0 (Chocolate Cake) | Mix eggs and sugar | 2 |
|
||||||
|
// | 0 (Chocolate Cake) | Add flour gradually | 3 |
|
||||||
|
// | 1 (Steak) | Season meat | 1 |
|
||||||
|
// | 1 (Steak) | Heat pan | 2 |
|
||||||
|
// |---------------------------------------------------------|
|
||||||
|
@ElementCollection
|
||||||
|
@CollectionTable(name = "recipe_preparation", joinColumns = @JoinColumn(name = "recipe_id"))
|
||||||
|
@Column(name = "preparation_step")
|
||||||
|
@OrderColumn(name = "step_order")
|
||||||
|
private List<String> preparationSteps = new ArrayList<>();
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
private Recipe() {
|
||||||
|
// for object mapper
|
||||||
|
}
|
||||||
|
|
||||||
|
public Recipe(Long id, String name) {
|
||||||
|
// Not used by JPA/Spring
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Replace String with Embeddable Ingredient Class for ingredients
|
||||||
|
public Recipe(Long id, String name, List<String> ingredients, List<String> preparationSteps) {
|
||||||
|
// Not used by JPA/Spring
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.ingredients = ingredients;
|
||||||
|
this.preparationSteps = preparationSteps;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Replace String with Embeddable Ingredient Class
|
||||||
|
public List<String> getIngredients() {
|
||||||
|
// Disallow modifying the returned list.
|
||||||
|
// You can still copy it with List.copyOf(...)
|
||||||
|
return Collections.unmodifiableList(ingredients);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Replace String with Embeddable Ingredient Class
|
||||||
|
public void setIngredients(List<String> ingredients) {
|
||||||
|
this.ingredients = ingredients;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getPreparationSteps() {
|
||||||
|
// Disallow modifying the returned list.
|
||||||
|
// You can still copy it with List.copyOf(...)
|
||||||
|
return Collections.unmodifiableList(preparationSteps);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPreparationSteps(List<String> preparationSteps) {
|
||||||
|
this.preparationSteps = preparationSteps;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
// Faster/Better equals than reflectionEquals
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
Recipe recipe = (Recipe) o;
|
||||||
|
return Objects.equals(id, recipe.id); // Check only for ID, as its unique!
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id); // Use ID only, as its unique.
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Recipe{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
", ingredientsCount=" + ingredients.size() +
|
||||||
|
", preparationStepsCount=" + preparationSteps.size() +
|
||||||
|
"}";
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public String toDetailedString() {
|
||||||
|
// More detailed toString for debugging.
|
||||||
|
return "Recipe{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
", ingredients=" + ingredients +
|
||||||
|
", preparationSteps=" + preparationSteps +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue