1 package at.ac.tuwien.sbc.valesriegler.group.gui;
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;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.List;
16 public class DeliveryOverviewModel extends TableModel<DeliveryGroup> {
17 private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class);
19 private static final String STATE = "State";
20 private static final String PIZZAS = "Pizzas";
21 private static final String ADDRESS = "Address";
22 private static final String ID = "ID";
23 private static final String PIZZERIA = "Pizzeria";
24 private static final String[] COLUMNS = new String[]{ID, ADDRESS, PIZZAS, STATE, PIZZERIA};
28 protected String[] getColumns() {
33 public Object getValueAt(int rowIndex, int columnIndex) {
34 List<DeliveryGroup> values = new ArrayList<>(items.values());
35 DeliveryGroup group = values.get(rowIndex);
36 String wantedColumn = COLUMNS[columnIndex];
37 switch (wantedColumn) {
39 int groupId = group.getId();
42 return group.getDeliveryGroupData().getAddress();
44 if (group.getDeliveryGroupData().getOrder() == null)
46 return Util.pizzaDisplay(group.getDeliveryGroupData().getOrder().getOrderedPizzas());
48 return group.getDeliveryGroupData().getDeliveryStatus();
50 return group.getDeliveryGroupData().getPizzeriaId();
52 throw new RuntimeException(UNHANDLEDCOLUMN);
56 public void addDeliveries(List<DeliveryGroupData> groups) {
57 synchronized (items) {
58 for (DeliveryGroupData group : groups) {
59 final DeliveryGroup deliveryGroup = items.get(group.getId());
60 if(deliveryGroup==null) {
61 log.error("Delivery group not found: {}", group.getId());
63 else deliveryGroup.setDeliveryGroupData(group);
66 fireTableDataChanged();
69 public void createStatistics() {
72 synchronized (items) {
73 final Collection<DeliveryGroup> values = items.values();
76 for (DeliveryGroup group : values) {
77 final DeliveryStatus status = group.getDeliveryGroupData().getDeliveryStatus();
78 if (status == DeliveryStatus.DELIVERED || status == DeliveryStatus.DELIVERY_FAILED) {
83 log.info("{} deliveries were ordered", size);
84 log.info("{} were finished", finished);
87 public boolean hasFinished() {
90 synchronized (items) {
91 final Collection<DeliveryGroup> values = items.values();
94 for (DeliveryGroup group : values) {
95 final DeliveryStatus status = group.getDeliveryGroupData().getDeliveryStatus();
96 if (status == DeliveryStatus.DELIVERED || status == DeliveryStatus.DELIVERY_FAILED) {
101 final boolean result = size == finished;