1 package at.ac.tuwien.sbc.valesriegler.group.gui;
3 import at.ac.tuwien.sbc.valesriegler.common.TableModel;
4 import at.ac.tuwien.sbc.valesriegler.common.Util;
5 import at.ac.tuwien.sbc.valesriegler.group.DeliveryGroup;
6 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
7 import at.ac.tuwien.sbc.valesriegler.types.DeliveryStatus;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.List;
15 public class DeliveryOverviewModel extends TableModel<DeliveryGroup> {
16 private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class);
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};
26 protected String[] getColumns() {
31 public void addItems(List<DeliveryGroup> newItems) {
32 log.info("addItems()");
33 super.addItems(newItems);
34 for (DeliveryGroup g : newItems) {
41 public Object getValueAt(int rowIndex, int columnIndex) {
42 List<DeliveryGroup> values = new ArrayList<DeliveryGroup>(items.values());
43 DeliveryGroup group = values.get(rowIndex);
44 String wantedColumn = COLUMNS[columnIndex];
45 switch (wantedColumn) {
47 int groupId = group.getId();
50 return group.getDeliveryGroupData().getAddress();
52 if (group.getDeliveryGroupData().getOrder() == null)
54 return Util.pizzaDisplay(group.getDeliveryGroupData().getOrder().getOrderedPizzas());
56 return group.getDeliveryGroupData().getDeliveryStatus();
58 return group.getDeliveryGroupData().getPizzeriaId();
60 throw new RuntimeException(UNHANDLEDCOLUMN);
64 public void addDeliveries(List<DeliveryGroupData> groups) {
65 synchronized (items) {
66 for (DeliveryGroupData group : groups) {
67 if(group.getDeliveryStatus() == DeliveryStatus.MOVED) {
70 final DeliveryGroup deliveryGroup = items.get(group.getId());
71 if (deliveryGroup == null) {
72 log.error("Delivery group not found: {}", group.getId());
74 deliveryGroup.setDeliveryGroupData(group);
78 fireTableDataChanged();
81 public void createStatistics() {
84 synchronized (items) {
85 final Collection<DeliveryGroup> values = items.values();
88 for (DeliveryGroup group : values) {
89 final DeliveryStatus status = group.getDeliveryGroupData().getDeliveryStatus();
90 if (status == DeliveryStatus.DELIVERED || status == DeliveryStatus.DELIVERY_FAILED) {
95 log.warn("{} deliveries were ordered", size);
96 log.warn("{} were finished", finished);
99 public boolean hasFinished() {
102 synchronized (items) {
103 final Collection<DeliveryGroup> values = items.values();
104 size = values.size();
106 for (DeliveryGroup group : values) {
107 final DeliveryStatus status = group.getDeliveryGroupData().getDeliveryStatus();
108 if (status == DeliveryStatus.DELIVERED || status == DeliveryStatus.DELIVERY_FAILED) {
113 final boolean result = size == finished;