From 9df2d71d7d092d6dae74418f843181668b367425 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Mon, 10 Jun 2013 16:13:13 +0200 Subject: [PATCH] [JMS] GG should be able to launch deliveryorders now. --- runJMS.sh | 2 +- .../sbc/valesriegler/group/DeliveryGroup.java | 27 +- .../group/gui/DeliveryOverviewModel.java | 233 +++++++++--------- .../valesriegler/types/DeliveryStatus.java | 3 +- 4 files changed, 141 insertions(+), 124 deletions(-) diff --git a/runJMS.sh b/runJMS.sh index 9294948..fad31d5 100755 --- a/runJMS.sh +++ b/runJMS.sh @@ -16,7 +16,7 @@ mvn exec:java -Dexec.mainClass="org.apache.activemq.console.Main" -Dexec.args="s # wait for broakers to start -sleep 120 +sleep 60 echo "*** continuing launch process ***" echo "*** continuing launch process ***" echo "*** continuing launch process ***" diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java index 9832e2f..38aa521 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java @@ -7,6 +7,7 @@ import at.ac.tuwien.sbc.valesriegler.common.HasId; import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest; import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector; import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; +import at.ac.tuwien.sbc.valesriegler.types.DeliveryStatus; public class DeliveryGroup implements Runnable, HasId { private static final Logger log = LoggerFactory.getLogger(DeliveryGroup.class); @@ -41,23 +42,23 @@ public class DeliveryGroup implements Runnable, HasId { try { Thread.sleep((long) (Math.random() * 10000)); switch (deliveryGroupData.getDeliveryStatus()) { - case START : + case START : // should not happen. + break; + case ORDER_PENDING : JMSGroupConnector conn = JMSGroupConnector.getConnector(deliveryGroupData.getPizzeriaId()); conn.send(new DeliveryOrderRequest(deliveryGroupData)); break; - case ORDERED : - break; - case IN_PROGRESS :// TODO + case IN_PROGRESS : // do nothing break; - case DELIVERED : + case DELIVERED : // do nothing break; - case PAID : + case PAID : // do nothing break; - case IS_DELIVERED : + case IS_DELIVERED : // do nothing break; - case DELIVERY_FAILED : + case DELIVERY_FAILED : // do nothing break; - default : + default : // do nothing break; } @@ -65,4 +66,12 @@ public class DeliveryGroup implements Runnable, HasId { e.printStackTrace(); } } + + // lets order at the pizzaria. + public void orderSomeFood() { + log.debug("orderSomeFood(): " + this); + deliveryGroupData.setDeliveryStatus(DeliveryStatus.ORDER_PENDING); + GroupAgent.getInstance().getGroupModel().fireTableDataChanged(); + new Thread(this).start(); + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/DeliveryOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/DeliveryOverviewModel.java index 53613e0..e91b45f 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/DeliveryOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/DeliveryOverviewModel.java @@ -1,123 +1,132 @@ package at.ac.tuwien.sbc.valesriegler.group.gui; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.TableModel; import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.group.DeliveryGroup; import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData; import at.ac.tuwien.sbc.valesriegler.types.DeliveryStatus; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; public class DeliveryOverviewModel extends TableModel { - private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class); - - private static final String STATE = "State"; - private static final String PIZZAS = "Pizzas"; - private static final String ADDRESS = "Address"; - private static final String ID = "ID"; - private static final String PIZZERIA = "Pizzeria"; - private static final String[] COLUMNS = new String[]{ID, ADDRESS, PIZZAS, STATE, PIZZERIA}; - - - @Override - protected String[] getColumns() { - return COLUMNS; - } - - @Override - public Object getValueAt(int rowIndex, int columnIndex) { - List values = new ArrayList<>(items.values()); - DeliveryGroup group = values.get(rowIndex); - String wantedColumn = COLUMNS[columnIndex]; - switch (wantedColumn) { - case ID: - int groupId = group.getId(); - return groupId; - case ADDRESS: - return group.getDeliveryGroupData().getAddress(); - case PIZZAS: - if (group.getDeliveryGroupData().getOrder() == null) - return "none"; - return Util.pizzaDisplay(group.getDeliveryGroupData().getOrder().getOrderedPizzas()); - case STATE: - return group.getDeliveryGroupData().getDeliveryStatus(); - case PIZZERIA: - return group.getDeliveryGroupData().getPizzeriaId(); - default: - throw new RuntimeException(UNHANDLEDCOLUMN); - } - } - - public void setOrdersTaken(List groups) { - for (DeliveryGroupData group : groups) { - changeStateOfGroup(group.getId(), DeliveryStatus.ORDERED); - } - - fireTableDataChanged(); - } - - private void changeStateOfGroup(int groupId, DeliveryStatus state) { - items.get(groupId).getDeliveryGroupData().setDeliveryStatus(state); - } - - public void setOrdersDelivered(List groups) { - for (DeliveryGroupData group : groups) { - changeStateOfGroup(group.getId(), group.getDeliveryStatus()); - } - - fireTableDataChanged(); - } - - public void addDeliveries(List groups) { - synchronized (items) { - for (DeliveryGroupData group : groups) { - final DeliveryGroup deliveryGroup = items.get(group.getId()); - if(deliveryGroup==null) { - log.error("Delivery group not found!!!"); - } - deliveryGroup.setDeliveryGroupData(group); - } - } - fireTableDataChanged(); - } - - public void createStatistics() { - int size; - int finished; - synchronized (items) { - final Collection values = items.values(); - size = values.size(); - finished = 0; - for (DeliveryGroup group : values) { - final DeliveryStatus status = group.getDeliveryGroupData().getDeliveryStatus(); - if (status == DeliveryStatus.DELIVERED || status == DeliveryStatus.DELIVERY_FAILED) { - finished++; - } - } - } - log.info("{} deliveries were ordered", size); - log.info("{} were finished", finished); - } - - public boolean hasFinished() { - int size; - int finished; - synchronized (items) { - final Collection values = items.values(); - size = values.size(); - finished = 0; - for (DeliveryGroup group : values) { - final DeliveryStatus status = group.getDeliveryGroupData().getDeliveryStatus(); - if (status == DeliveryStatus.DELIVERED || status == DeliveryStatus.DELIVERY_FAILED) { - finished++; - } - } - } - return size == finished; - } + private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class); + + private static final String STATE = "State"; + private static final String PIZZAS = "Pizzas"; + private static final String ADDRESS = "Address"; + private static final String ID = "ID"; + private static final String PIZZERIA = "Pizzeria"; + private static final String[] COLUMNS = new String[]{ID, ADDRESS, PIZZAS, STATE, PIZZERIA}; + + @Override + protected String[] getColumns() { + return COLUMNS; + } + + @Override + public void addItems(List newItems) { + log.info("addItems()"); + super.addItems(newItems); + for (DeliveryGroup g : newItems) { + if (Util.useJMS) + g.orderSomeFood(); + } + } + + @Override + public Object getValueAt(int rowIndex, int columnIndex) { + List values = new ArrayList<>(items.values()); + DeliveryGroup group = values.get(rowIndex); + String wantedColumn = COLUMNS[columnIndex]; + switch (wantedColumn) { + case ID : + int groupId = group.getId(); + return groupId; + case ADDRESS : + return group.getDeliveryGroupData().getAddress(); + case PIZZAS : + if (group.getDeliveryGroupData().getOrder() == null) + return "none"; + return Util.pizzaDisplay(group.getDeliveryGroupData().getOrder().getOrderedPizzas()); + case STATE : + return group.getDeliveryGroupData().getDeliveryStatus(); + case PIZZERIA : + return group.getDeliveryGroupData().getPizzeriaId(); + default : + throw new RuntimeException(UNHANDLEDCOLUMN); + } + } + + public void setOrdersTaken(List groups) { + for (DeliveryGroupData group : groups) { + changeStateOfGroup(group.getId(), DeliveryStatus.ORDERED); + } + + fireTableDataChanged(); + } + + private void changeStateOfGroup(int groupId, DeliveryStatus state) { + items.get(groupId).getDeliveryGroupData().setDeliveryStatus(state); + } + + public void setOrdersDelivered(List groups) { + for (DeliveryGroupData group : groups) { + changeStateOfGroup(group.getId(), group.getDeliveryStatus()); + } + + fireTableDataChanged(); + } + + public void addDeliveries(List groups) { + synchronized (items) { + for (DeliveryGroupData group : groups) { + final DeliveryGroup deliveryGroup = items.get(group.getId()); + if (deliveryGroup == null) { + log.error("Delivery group not found!!!"); + } + deliveryGroup.setDeliveryGroupData(group); + } + } + fireTableDataChanged(); + } + + public void createStatistics() { + int size; + int finished; + synchronized (items) { + final Collection values = items.values(); + size = values.size(); + finished = 0; + for (DeliveryGroup group : values) { + final DeliveryStatus status = group.getDeliveryGroupData().getDeliveryStatus(); + if (status == DeliveryStatus.DELIVERED || status == DeliveryStatus.DELIVERY_FAILED) { + finished++; + } + } + } + log.info("{} deliveries were ordered", size); + log.info("{} were finished", finished); + } + + public boolean hasFinished() { + int size; + int finished; + synchronized (items) { + final Collection values = items.values(); + size = values.size(); + finished = 0; + for (DeliveryGroup group : values) { + final DeliveryStatus status = group.getDeliveryGroupData().getDeliveryStatus(); + if (status == DeliveryStatus.DELIVERED || status == DeliveryStatus.DELIVERY_FAILED) { + finished++; + } + } + } + return size == finished; + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/DeliveryStatus.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/DeliveryStatus.java index 1d7225a..b0d3495 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/DeliveryStatus.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/DeliveryStatus.java @@ -1,8 +1,7 @@ package at.ac.tuwien.sbc.valesriegler.types; - import java.io.Serializable; public enum DeliveryStatus implements Serializable { - ORDERED, IN_PROGRESS, IS_DELIVERED, DELIVERED, PAID, DELIVERY_FAILED, START; + ORDERED, IN_PROGRESS, IS_DELIVERED, DELIVERED, PAID, DELIVERY_FAILED, START, ORDER_PENDING; } -- 2.43.0