]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/DeliveryOverviewModel.java
[XVSM] Some GroupAgent Container Refactoring. Full Driver<->DeliveryCustomer communic...
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / group / gui / DeliveryOverviewModel.java
1 package at.ac.tuwien.sbc.valesriegler.group.gui;
2
3
4 import at.ac.tuwien.sbc.valesriegler.common.TableModel;
5 import at.ac.tuwien.sbc.valesriegler.common.Util;
6 import at.ac.tuwien.sbc.valesriegler.group.DeliveryGroup;
7 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
8 import at.ac.tuwien.sbc.valesriegler.types.DeliveryStatus;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
15 public class DeliveryOverviewModel extends TableModel<DeliveryGroup> {
16     private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class);
17
18     private static final String STATE = "State";
19     private static final String PIZZAS = "Pizzas";
20     private static final String ADDRESS = "Address";
21     private static final String ID = "ID";
22     private static final String PIZZERIA = "Pizzeria";
23     private static final String[] COLUMNS = new String[]{ID, ADDRESS, PIZZAS, STATE, PIZZERIA};
24
25
26     @Override
27     protected String[] getColumns() {
28         return COLUMNS;
29     }
30
31     @Override
32     public Object getValueAt(int rowIndex, int columnIndex) {
33         List<DeliveryGroup> values = new ArrayList<>(items.values());
34         DeliveryGroup group = values.get(rowIndex);
35         String wantedColumn = COLUMNS[columnIndex];
36         switch (wantedColumn) {
37             case ID:
38                 int groupId = group.getId();
39                 return groupId;
40             case ADDRESS:
41                 return group.getDeliveryGroupData().getAddress();
42             case PIZZAS:
43                 if (group.getDeliveryGroupData().getOrder() == null)
44                     return "none";
45                 return Util.pizzaDisplay(group.getDeliveryGroupData().getOrder().getOrderedPizzas());
46             case STATE:
47                 return group.getDeliveryGroupData().getDeliveryStatus();
48             case PIZZERIA:
49                 return group.getDeliveryGroupData().getPizzeriaId();
50             default:
51                 throw new RuntimeException(UNHANDLEDCOLUMN);
52         }
53     }
54
55     public void setOrdersTaken(List<DeliveryGroupData> groups) {
56         for (DeliveryGroupData group : groups) {
57             changeStateOfGroup(group.getId(), DeliveryStatus.ORDERED);
58         }
59
60         fireTableDataChanged();
61     }
62
63     private void changeStateOfGroup(int groupId, DeliveryStatus state) {
64         items.get(groupId).getDeliveryGroupData().setDeliveryStatus(state);
65     }
66
67     public void setOrdersDelivered(List<DeliveryGroupData> groups) {
68         for (DeliveryGroupData group : groups) {
69             changeStateOfGroup(group.getId(), group.getDeliveryStatus());
70         }
71
72         fireTableDataChanged();
73     }
74
75     public void addDeliveries(List<DeliveryGroupData> groups) {
76         synchronized (items) {
77             for (DeliveryGroupData group : groups) {
78                 final DeliveryGroup deliveryGroup = items.get(group.getId());
79                 if(deliveryGroup==null) {
80                     log.error("Delivery group not found!!!");
81                 }
82                 deliveryGroup.setDeliveryGroupData(group);
83             }
84         }
85         fireTableDataChanged();
86     }
87 }