]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/OrdersOverviewModel.java
Space cooks prepare pizzas and Pizzeria GUI shows that
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / pizzeria / gui / tablemodels / OrdersOverviewModel.java
1 package at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11 import at.ac.tuwien.sbc.valesriegler.common.TableModel;
12 import at.ac.tuwien.sbc.valesriegler.common.Util;
13 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
14 import at.ac.tuwien.sbc.valesriegler.types.Order;
15 import at.ac.tuwien.sbc.valesriegler.types.Pizza;
16 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder;
17 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrderStatus;
18 import at.ac.tuwien.sbc.valesriegler.xvsm.PizzeriaAgentXVSM;
19
20 public class OrdersOverviewModel extends TableModel<GroupData> {
21         private static final Logger log = LoggerFactory.getLogger(OrdersOverviewModel.class);
22         
23         private static final String ID = "Order ID";
24         private static final String TABLE_ID = "Table ID";
25         private static final String GROUP_ID = "Group ID";
26         private static final String STATUS = "Status";
27
28         private static final String[] COLUMNS = new String[] { ID, TABLE_ID, GROUP_ID, STATUS };
29
30         @Override
31         public Object getValueAt(int rowIndex, int columnIndex) {
32                 List<GroupData> values = new ArrayList<>(items.values());
33                 GroupData group = values.get(rowIndex);
34                 Order order = group.getOrder();
35                 String wantedColumn = COLUMNS[columnIndex];
36                 switch (wantedColumn) {
37                 case ID:
38                         return order.getId();
39                 case TABLE_ID: 
40                         return group.getTable().getId() ;
41                 case GROUP_ID:
42                         return order.getGroupId();
43                 case STATUS:
44                         return order.getStatus();
45                 default:
46                         throw new RuntimeException(UNHANDLEDCOLUMN);
47                 }
48         }
49         
50         public Order getOrderById(int orderId) {
51                 synchronized (items) {
52                         List<GroupData> groups = new ArrayList<GroupData>(items.values());
53                         for (GroupData groupData : groups) {
54                                 if(groupData.getOrder().getId() == orderId) {
55                                         return groupData.getOrder();
56                                 }
57                         }
58                         throw new RuntimeException(String.format("Order with orderId %d not found!", orderId));
59                 }
60         }
61         
62         public GroupData getGroupOfRow(int rowIndex) {
63                 synchronized (items) {
64                         return new ArrayList<>(items.values()).get(rowIndex);
65                 }
66         }
67
68         @Override
69         protected String[] getColumns() {
70                 return COLUMNS;
71         }
72
73         /**
74          * Traverse the GroupDatas 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.
75          */
76         public void updatePizzasInPreparation(List<Pizza> pizzas) {
77                 synchronized(items) {
78                         for (Pizza pizza : pizzas) {
79                                 List<GroupData> groups = new ArrayList<GroupData>(items.values());
80                                 for (GroupData groupData : groups) {
81                                         Order order = groupData.getOrder();
82                                         if(order.getId() == pizza.getOrderId()) {
83                                                 
84                                                 List<PizzaOrder> orderedPizzas = order.getOrderedPizzas();
85                                                 Map<Integer, PizzaOrder> orderedPizzasById = Util.intoMapById(orderedPizzas);
86                                                 PizzaOrder pizzaOrder = orderedPizzasById.get(pizza.getId());
87                                                 if(pizzaOrder == null) {
88                                                         throw new RuntimeException("A pizza which has never been ordered is in preparation... damn!");
89                                                 }
90                                                 pizzaOrder.setStatus(PizzaOrderStatus.IN_PREPARATION);
91                                                 pizzaOrder.setCookId(pizza.getCookId());
92                                         }
93                                 }
94                         }
95                 }
96                 
97                 fireTableDataChanged();
98         }
99 }
100