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 setOrdersTaken(List<DeliveryGroupData> groups) {
57 for (DeliveryGroupData group : groups) {
58 changeStateOfGroup(group.getId(), DeliveryStatus.ORDERED);
61 fireTableDataChanged();
64 private void changeStateOfGroup(int groupId, DeliveryStatus state) {
65 items.get(groupId).getDeliveryGroupData().setDeliveryStatus(state);
68 public void setOrdersDelivered(List<DeliveryGroupData> groups) {
69 for (DeliveryGroupData group : groups) {
70 changeStateOfGroup(group.getId(), group.getDeliveryStatus());
73 fireTableDataChanged();
76 public void addDeliveries(List<DeliveryGroupData> groups) {
77 synchronized (items) {
78 for (DeliveryGroupData group : groups) {
79 final DeliveryGroup deliveryGroup = items.get(group.getId());
80 if(deliveryGroup==null) {
81 log.error("Delivery group not found!!!");
83 deliveryGroup.setDeliveryGroupData(group);
86 fireTableDataChanged();
89 public void createStatistics() {
92 synchronized (items) {
93 final Collection<DeliveryGroup> values = items.values();
96 for (DeliveryGroup group : values) {
97 final DeliveryStatus status = group.getDeliveryGroupData().getDeliveryStatus();
98 if (status == DeliveryStatus.DELIVERED || status == DeliveryStatus.DELIVERY_FAILED) {
103 log.info("{} deliveries were ordered", size);
104 log.info("{} were finished", finished);
107 public boolean hasFinished() {
110 synchronized (items) {
111 final Collection<DeliveryGroup> values = items.values();
112 size = values.size();
114 for (DeliveryGroup group : values) {
115 final DeliveryStatus status = group.getDeliveryGroupData().getDeliveryStatus();
116 if (status == DeliveryStatus.DELIVERED || status == DeliveryStatus.DELIVERY_FAILED) {
121 return size == finished;