From 497f28602a83f50cd7e659ff222487ce880dceeb Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Thu, 6 Jun 2013 14:14:00 +0200 Subject: [PATCH] [XVSM] Add forgotten file... --- .../gui/tablemodels/AbstractOrdersModel.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/AbstractOrdersModel.java diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/AbstractOrdersModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/AbstractOrdersModel.java new file mode 100644 index 0000000..a26f78c --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/AbstractOrdersModel.java @@ -0,0 +1,75 @@ +package at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels; + + +import at.ac.tuwien.sbc.valesriegler.common.TableModel; +import at.ac.tuwien.sbc.valesriegler.common.Util; +import at.ac.tuwien.sbc.valesriegler.types.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public abstract class AbstractOrdersModel extends TableModel { + private static final Logger log = LoggerFactory.getLogger(AbstractOrdersModel.class); + /** + * Traverse the Groups and search for an order of which the orderId + * matches the orderId of one of the pizzas. Then set the status and the + * cookId of the respective pizza. + */ + public void updatePizzasInPreparation(List pizzas) { + synchronized (items) { + for (Pizza pizza : pizzas) { + List groups = new ArrayList(items.values()); + for (T groupData : groups) { + Order order = groupData.getOrder(); + if (order.getId() == pizza.getOrderId()) { + + List orderedPizzas = order.getOrderedPizzas(); + Map orderedPizzasById = Util.intoMapById(orderedPizzas); + PizzaOrder pizzaOrder = orderedPizzasById.get(pizza.getId()); + if (pizzaOrder == null) { + throw new RuntimeException("A pizza which has never been ordered is in preparation... damn!"); + } + pizzaOrder.setStatus(PizzaOrderStatus.IN_PREPARATION); + pizzaOrder.setCookId(pizza.getCookId()); + } + } + } + } + + fireTableDataChanged(); + } + + protected Order getOrderById(int orderId) { + List groups = new ArrayList(items.values()); + for (T groupData : groups) { + if (groupData.getOrder().getId() == orderId) { + return groupData.getOrder(); + } + } + return null; + } + + public void updateStatusOfPizzasDone(List pizzas) { + synchronized (items) { + for (Pizza pizza : pizzas) { + int orderId = pizza.getOrderId(); + log.info("pizza was done: {}", pizza.toString()); + Order order = getOrderById(orderId); + if(order == null) { + return; + } + List orderedPizzas = order.getOrderedPizzas(); + for (PizzaOrder pizzaOrder : orderedPizzas) { + if (pizzaOrder.getId() == pizza.getId()) { + pizzaOrder.setStatus(PizzaOrderStatus.DONE); + } + } + } + } + + fireTableDataChanged(); + } +} -- 2.43.0