1 package at.ac.tuwien.sbc.valesriegler.group.gui;
3 import java.util.ArrayList;
4 import java.util.Collection;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
10 import at.ac.tuwien.sbc.valesriegler.common.TableModel;
11 import at.ac.tuwien.sbc.valesriegler.common.Util;
12 import at.ac.tuwien.sbc.valesriegler.group.DeliveryGroup;
13 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
14 import at.ac.tuwien.sbc.valesriegler.types.DeliveryStatus;
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};
27 protected String[] getColumns() {
32 public void addItems(List<DeliveryGroup> newItems) {
33 log.info("addItems()");
34 super.addItems(newItems);
35 for (DeliveryGroup g : newItems) {
42 public Object getValueAt(int rowIndex, int columnIndex) {
43 List<DeliveryGroup> values = new ArrayList<>(items.values());
44 DeliveryGroup group = values.get(rowIndex);
45 String wantedColumn = COLUMNS[columnIndex];
46 switch (wantedColumn) {
48 int groupId = group.getId();
51 return group.getDeliveryGroupData().getAddress();
53 if (group.getDeliveryGroupData().getOrder() == null)
55 return Util.pizzaDisplay(group.getDeliveryGroupData().getOrder().getOrderedPizzas());
57 return group.getDeliveryGroupData().getDeliveryStatus();
59 return group.getDeliveryGroupData().getPizzeriaId();
61 throw new RuntimeException(UNHANDLEDCOLUMN);
65 public void setOrdersTaken(List<DeliveryGroupData> groups) {
66 for (DeliveryGroupData group : groups) {
67 changeStateOfGroup(group.getId(), DeliveryStatus.ORDERED);
70 fireTableDataChanged();
73 private void changeStateOfGroup(int groupId, DeliveryStatus state) {
74 items.get(groupId).getDeliveryGroupData().setDeliveryStatus(state);
77 public void setOrdersDelivered(List<DeliveryGroupData> groups) {
78 for (DeliveryGroupData group : groups) {
79 changeStateOfGroup(group.getId(), group.getDeliveryStatus());
82 fireTableDataChanged();
85 public void addDeliveries(List<DeliveryGroupData> groups) {
86 synchronized (items) {
87 for (DeliveryGroupData group : groups) {
88 final DeliveryGroup deliveryGroup = items.get(group.getId());
89 if (deliveryGroup == null) {
90 log.error("Delivery group not found!!!");
92 deliveryGroup.setDeliveryGroupData(group);
95 fireTableDataChanged();
98 public void createStatistics() {
101 synchronized (items) {
102 final Collection<DeliveryGroup> values = items.values();
103 size = values.size();
105 for (DeliveryGroup group : values) {
106 final DeliveryStatus status = group.getDeliveryGroupData().getDeliveryStatus();
107 if (status == DeliveryStatus.DELIVERED || status == DeliveryStatus.DELIVERY_FAILED) {
112 log.info("{} deliveries were ordered", size);
113 log.info("{} were finished", finished);
116 public boolean hasFinished() {
119 synchronized (items) {
120 final Collection<DeliveryGroup> values = items.values();
121 size = values.size();
123 for (DeliveryGroup group : values) {
124 final DeliveryStatus status = group.getDeliveryGroupData().getDeliveryStatus();
125 if (status == DeliveryStatus.DELIVERED || status == DeliveryStatus.DELIVERY_FAILED) {
130 return size == finished;