From dfce3d0b3c077ae2c15ebb0ea38884c5ff0037df Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Sun, 12 May 2013 15:20:40 +0200 Subject: [PATCH] JMS: pizzeriaGui now displays everything correctly. --- .../tuwien/sbc/valesriegler/group/Group.java | 1 + .../group/actions/OrderResponse.java | 2 +- .../group/jms/JMSGroupConnector.java | 11 ++-- .../gui/tablemodels/OrdersOverviewModel.java | 54 ++++++++------- .../gui/tablemodels/PizzasOfOrderModel.java | 9 ++- .../pizzeria/jms/JMSPizzeriaConnector.java | 66 +++++++++++++++++-- .../tuwien/sbc/valesriegler/types/Pizza.java | 1 + .../tuwien/sbc/valesriegler/types/Table.java | 8 +-- .../jms/messageListeners/CookedOrders.java | 2 + .../jms/messageListeners/WantToOrder.java | 5 +- .../jms/messageListeners/WantToPay.java | 6 +- 11 files changed, 118 insertions(+), 47 deletions(-) diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java index 585fc8b..9a948f5 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java @@ -61,6 +61,7 @@ public class Group implements Runnable, HasId { case PAY:// leave table GroupAgent.getInstance().getGroupcomm().send(new TableFree(groupData)); groupData.setState(GroupState.GONE); + groupData.setTable(null); break; case GONE: break; // do nothing. diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/OrderResponse.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/OrderResponse.java index 27d4619..52e6d07 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/OrderResponse.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/OrderResponse.java @@ -25,7 +25,7 @@ public class OrderResponse extends AbstractAction implements Serializable { @Override public String toString() { - return "OrderResponse [waiterId=" + waiterId + "]"; + return "OrderResponse [waiterId=" + waiterId + ", getGroupdata()=" + getGroupdata() + "]"; } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/JMSGroupConnector.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/JMSGroupConnector.java index a062fec..2e1c7b7 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/JMSGroupConnector.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/JMSGroupConnector.java @@ -101,6 +101,10 @@ public class JMSGroupConnector extends AbstractGroupConnector implements Message MessageProducer wantToSitAtTable = session.createProducer(session.createQueue("WantToSitAtTable")); wantToSitAtTable.setDeliveryMode(DeliveryMode.NON_PERSISTENT); wantToSitAtTable.send(session.createObjectMessage(request)); + + MessageProducer informPizzariaGui = session.createProducer(session.createQueue("PizzeriaConnector")); + informPizzariaGui.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + informPizzariaGui.send(session.createObjectMessage(request)); return; } else if (request instanceof OrderRequest) { @@ -115,11 +119,6 @@ public class JMSGroupConnector extends AbstractGroupConnector implements Message MessageProducer wantToPay = session.createProducer(session.createQueue("WantToPay")); wantToPay.setDeliveryMode(DeliveryMode.NON_PERSISTENT); wantToPay.send(session.createObjectMessage(request)); - - MessageProducer informPizzariaGui = session.createProducer(session.createQueue("PizzeriaConnector")); - informPizzariaGui.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - informPizzariaGui.send(session.createObjectMessage(request)); - session.close(); return; } else if (request instanceof TableFree) { @@ -128,8 +127,6 @@ public class JMSGroupConnector extends AbstractGroupConnector implements Message wantToSitAtTable.setDeliveryMode(DeliveryMode.NON_PERSISTENT); wantToSitAtTable.send(session.createObjectMessage(request.getGroupdata().getTable())); - request.getGroupdata().setTable(null); - MessageProducer wantToLeave = session.createProducer(session.createQueue("WantToLeave")); wantToLeave.setDeliveryMode(DeliveryMode.NON_PERSISTENT); wantToLeave.send(session.createObjectMessage(request)); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/OrdersOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/OrdersOverviewModel.java index ed55d3a..5f88643 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/OrdersOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/OrdersOverviewModel.java @@ -18,7 +18,7 @@ import at.ac.tuwien.sbc.valesriegler.types.PizzaOrderStatus; public class OrdersOverviewModel extends TableModel { private static final Logger log = LoggerFactory.getLogger(OrdersOverviewModel.class); - + private static final String ID = "Order ID"; private static final String TABLE_ID = "Table ID"; private static final String GROUP_ID = "Group ID"; @@ -35,8 +35,8 @@ public class OrdersOverviewModel extends TableModel { switch (wantedColumn) { case ID: return order.getId(); - case TABLE_ID: - return group.getTable().getId() ; + case TABLE_ID: + return group.getTable().getId(); case GROUP_ID: return order.getGroupId(); case STATUS: @@ -46,7 +46,6 @@ public class OrdersOverviewModel extends TableModel { } } - public GroupData getGroupOfRow(int rowIndex) { synchronized (items) { return new ArrayList<>(items.values()).get(rowIndex); @@ -58,21 +57,31 @@ public class OrdersOverviewModel extends TableModel { return COLUMNS; } + public void updateGroupData(GroupData groupdata) { + synchronized (items) { + items.put(groupdata.getId(), groupdata); + } + + fireTableDataChanged(); + } + /** - * Traverse the GroupDatas and search for an order of which the orderId matches the orderId of one of the pizzas. Then set the status and the cookId of the respective pizza. + * Traverse the GroupDatas and search for an order of which the orderId + * matches the orderId of one of the pizzas. Then set the status and the + * cookId of the respective pizza. */ public void updatePizzasInPreparation(List pizzas) { - synchronized(items) { + synchronized (items) { for (Pizza pizza : pizzas) { List groups = new ArrayList(items.values()); for (GroupData groupData : groups) { Order order = groupData.getOrder(); - if(order.getId() == pizza.getOrderId()) { - + if (order.getId() == pizza.getOrderId()) { + List orderedPizzas = order.getOrderedPizzas(); Map orderedPizzasById = Util.intoMapById(orderedPizzas); PizzaOrder pizzaOrder = orderedPizzasById.get(pizza.getId()); - if(pizzaOrder == null) { + if (pizzaOrder == null) { throw new RuntimeException("A pizza which has never been ordered is in preparation... damn!"); } pizzaOrder.setStatus(PizzaOrderStatus.IN_PREPARATION); @@ -81,58 +90,56 @@ public class OrdersOverviewModel extends TableModel { } } } - + fireTableDataChanged(); } public void updateStatusOfOrder(GroupData group) { - synchronized(items) { + synchronized (items) { Order order = group.getOrder(); List groups = new ArrayList(items.values()); for (GroupData groupData : groups) { Order orderOfGroup = groupData.getOrder(); - if(order.getId() == orderOfGroup.getId()) { + if (order.getId() == orderOfGroup.getId()) { orderOfGroup.setStatus(OrderStatus.DELIVERED); groupData.setServingWaiter(group.getServingWaiter()); } } } - + fireTableDataChanged(); } - public void updateStatusOfPizzasDone(Pizza pizza) { int orderId = pizza.getOrderId(); - synchronized(items) { + synchronized (items) { Order order = getOrderById(orderId); List orderedPizzas = order.getOrderedPizzas(); for (PizzaOrder pizzaOrder : orderedPizzas) { - if(pizzaOrder.getId() == pizza.getId()) { + if (pizzaOrder.getId() == pizza.getId()) { pizzaOrder.setStatus(PizzaOrderStatus.DONE); } } } - + fireTableDataChanged(); - } - + } + private Order getOrderById(int orderId) { List groups = new ArrayList(items.values()); for (GroupData groupData : groups) { - if(groupData.getOrder().getId() == orderId) { + if (groupData.getOrder().getId() == orderId) { return groupData.getOrder(); } } throw new RuntimeException(String.format("Order with orderId %d not found!", orderId)); } - public void updatePaymentWaiter(GroupData group) { - synchronized(items) { + synchronized (items) { List groups = new ArrayList(items.values()); for (GroupData groupData : groups) { - if(groupData.getId() == group.getId()) { + if (groupData.getId() == group.getId()) { groupData.setPayingWaiter(group.getPayingWaiter()); } } @@ -140,4 +147,3 @@ public class OrdersOverviewModel extends TableModel { fireTableDataChanged(); } } - diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/PizzasOfOrderModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/PizzasOfOrderModel.java index 161c0e2..bec5d9e 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/PizzasOfOrderModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/PizzasOfOrderModel.java @@ -1,13 +1,13 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels; import java.util.List; -import java.util.Map; import at.ac.tuwien.sbc.valesriegler.common.TableModel; import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.types.Order; import at.ac.tuwien.sbc.valesriegler.types.Pizza; import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; +import at.ac.tuwien.sbc.valesriegler.types.PizzaOrderStatus; public class PizzasOfOrderModel extends TableModel { private static final String TYPE = "Type"; @@ -27,6 +27,7 @@ public class PizzasOfOrderModel extends TableModel { @Override public Object getValueAt(int rowIndex, int columnIndex) { List pizzaOrders = currentOrder.getOrderedPizzas(); + List pizzas = currentOrder.getCookedPizzas(); PizzaOrder pizzaOrder = pizzaOrders.get(rowIndex); String wantedColumn = COLUMNS[columnIndex]; @@ -36,7 +37,11 @@ public class PizzasOfOrderModel extends TableModel { case STATUS: return pizzaOrder.getStatus(); case COOK: - return Util.getId(pizzaOrder.getCookId()); + if (pizzaOrder.getStatus() == PizzaOrderStatus.DONE) { + Pizza pizza = pizzas.get(rowIndex); + return Util.getId(pizza.getCookId()); + } + return ""; default: throw new RuntimeException(UNHANDLEDCOLUMN); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/JMSPizzeriaConnector.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/JMSPizzeriaConnector.java index e7d91ce..e90a7da 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/JMSPizzeriaConnector.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/JMSPizzeriaConnector.java @@ -1,5 +1,7 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria.jms; +import java.util.ArrayList; + import javax.jms.Connection; import javax.jms.DeliveryMode; import javax.jms.JMSException; @@ -15,11 +17,18 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.AbstractAction; +import at.ac.tuwien.sbc.valesriegler.cook.actions.OrderInfo; +import at.ac.tuwien.sbc.valesriegler.group.actions.OrderResponse; +import at.ac.tuwien.sbc.valesriegler.group.actions.PayResponse; +import at.ac.tuwien.sbc.valesriegler.group.actions.TableFree; +import at.ac.tuwien.sbc.valesriegler.group.actions.TableRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.TableResponse; import at.ac.tuwien.sbc.valesriegler.pizzeria.AbstractPizzeriaConnector; import at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent; import at.ac.tuwien.sbc.valesriegler.pizzeria.actions.TableNew; +import at.ac.tuwien.sbc.valesriegler.types.GroupData; import at.ac.tuwien.sbc.valesriegler.types.Table; +import at.ac.tuwien.sbc.valesriegler.waiter.actions.DeliverOrder; /** * This class handles the communication with other processes using JMS. @@ -53,16 +62,64 @@ public class JMSPizzeriaConnector extends AbstractPizzeriaConnector implements M ObjectMessage objMsg = (ObjectMessage) msg; Object obj = objMsg.getObject(); - if (obj instanceof TableResponse) { + if (obj instanceof TableRequest) { + TableRequest tablerrquest = (TableRequest) obj; + log.debug("Received: " + tablerrquest); + ArrayList ngroup = new ArrayList(); + ngroup.add(tablerrquest.getGroupdata()); + PizzeriaAgent.getInstance().getGroupModel().addItems(ngroup); + PizzeriaAgent.getInstance().getGroupModel().fireTableDataChanged(); + return; + + } else if (obj instanceof TableResponse) { TableResponse tablerresponse = (TableResponse) obj; log.debug("Received: " + tablerresponse); Table t = PizzeriaAgent.getInstance().getTablesModel().getTableById(tablerresponse.getTable().getId()); t.setGroupId(tablerresponse.getGroupdata().getId()); + PizzeriaAgent.getInstance().getTablesModel().fireTableDataChanged(); + PizzeriaAgent.getInstance().getGroupModel().removeGroup(tablerresponse.getGroupdata().getId()); + PizzeriaAgent.getInstance().getGroupModel().fireTableDataChanged(); + return; + + } else if (obj instanceof OrderResponse) { + OrderResponse orderResponse = (OrderResponse) obj; + log.debug("Received: " + orderResponse); + orderResponse.getGroupdata().setOrderWaiter(orderResponse.getWaiterId()); + log.info("order: " + orderResponse); + PizzeriaAgent.getInstance().getOrdersModel().updateGroupData(orderResponse.getGroupdata()); + PizzeriaAgent.getInstance().getOrdersModel().fireTableDataChanged(); + return; + + } else if (obj instanceof OrderInfo) { + OrderInfo orderInfo = (OrderInfo) obj; + log.debug("Received: " + orderInfo); + PizzeriaAgent.getInstance().getOrdersModel().updateGroupData(orderInfo.getGroupdata()); + PizzeriaAgent.getInstance().getOrdersModel().fireTableDataChanged(); + return; + + } else if (obj instanceof DeliverOrder) { + DeliverOrder deliverOrder = (DeliverOrder) obj; + log.debug("Received: " + deliverOrder); + deliverOrder.getGroupdata().setServingWaiter(deliverOrder.getWaiterId()); + PizzeriaAgent.getInstance().getOrdersModel().updateGroupData(deliverOrder.getGroupdata()); + return; + + } else if (obj instanceof PayResponse) { + PayResponse payResponse = (PayResponse) obj; + log.debug("Received: " + payResponse); + payResponse.getGroupdata().setPayingWaiter(payResponse.getWaiterId()); + PizzeriaAgent.getInstance().getOrdersModel().updateGroupData(payResponse.getGroupdata()); + return; + + } else if (obj instanceof TableFree) { + TableFree tablefree = (TableFree) obj; + log.debug("Received: " + tablefree); + int tid = tablefree.getGroupdata().getTable().getId(); + Table t = PizzeriaAgent.getInstance().getTablesModel().getTableById(tid); + t.setGroupId(-1); + PizzeriaAgent.getInstance().getTablesModel().fireTableDataChanged(); return; } - // else if (obj instanceof OrderResponse) { - // - // } log.warn("Unknown message received!" + obj); } @@ -81,7 +138,6 @@ public class JMSPizzeriaConnector extends AbstractPizzeriaConnector implements M MessageProducer wantToSitAtTable = session.createProducer(session.createQueue("TablesFree")); wantToSitAtTable.setDeliveryMode(DeliveryMode.NON_PERSISTENT); wantToSitAtTable.send(session.createObjectMessage(((TableNew) request).getTable())); - session.close(); return; } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Pizza.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Pizza.java index 7da7d46..add3e5e 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Pizza.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Pizza.java @@ -29,6 +29,7 @@ public class Pizza extends PizzaOrder implements Serializable { private Pizza(int id, PizzaType type, int cookId, int orderId) { super(id); + super.setCookId(cookId); this.id = id; pizzaType = type; this.cookId = cookId; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Table.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Table.java index 7d357f4..d398f18 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Table.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Table.java @@ -6,7 +6,6 @@ import org.mozartspaces.capi3.Queryable; import at.ac.tuwien.sbc.valesriegler.common.HasId; - /** * A table is a thing, where people can put things at. Possibly a laptop, to * have free hands to hold a pizzabox on the knees. @@ -14,7 +13,7 @@ import at.ac.tuwien.sbc.valesriegler.common.HasId; * @author jan * */ -@Queryable(autoindex=true) +@Queryable(autoindex = true) public class Table implements Serializable, HasId { private static int idNext = 0; private final Integer id; @@ -29,6 +28,7 @@ public class Table implements Serializable, HasId { id = ++idNext; } + @Override public int getId() { return id; } @@ -47,8 +47,6 @@ public class Table implements Serializable, HasId { return false; } - - @Override public String toString() { return "Table [id=" + id + ", groupId=" + groupId + "]"; @@ -78,7 +76,5 @@ public class Table implements Serializable, HasId { return false; return true; } - - } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/CookedOrders.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/CookedOrders.java index a183836..7c06318 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/CookedOrders.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/CookedOrders.java @@ -14,6 +14,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.cook.actions.OrderInfo; +import at.ac.tuwien.sbc.valesriegler.types.OrderStatus; import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; import at.ac.tuwien.sbc.valesriegler.waiter.actions.DeliverOrder; @@ -56,6 +57,7 @@ public class CookedOrders implements MessageListener { MessageProducer informGroup = session.createProducer(session.createQueue("GroupConnector")); informGroup.setDeliveryMode(DeliveryMode.NON_PERSISTENT); DeliverOrder deliverorder = new DeliverOrder(orderinfo.getGroupdata(), waiter.getId()); + deliverorder.getGroupdata().getOrder().setStatus(OrderStatus.DELIVERED); informGroup.send(session.createObjectMessage(deliverorder)); MessageProducer informPizzeria = session.createProducer(session.createQueue("PizzeriaConnector")); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToOrder.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToOrder.java index bb6dc45..a49a0d0 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToOrder.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToOrder.java @@ -51,16 +51,18 @@ public class WantToOrder implements MessageListener { Connection connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + OrderResponse or = new OrderResponse(orderrequest.getGroupdata(), waiter.getId()); // Make cooks do their work MessageProducer prodOP = session.createProducer(session.createQueue("OrdersToCook")); prodOP.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + // TODO: hack + orderrequest.getGroupdata().setOrderWaiter(waiter.getId()); prodOP.send(session.createObjectMessage(orderrequest)); // inform Group + Pizzeria MessageProducer informGroup = session.createProducer(session.createQueue("GroupConnector")); informGroup.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - OrderResponse or = new OrderResponse(orderrequest.getGroupdata(), waiter.getId()); informGroup.send(session.createObjectMessage(or)); MessageProducer informPizzeria = session.createProducer(session.createQueue("PizzeriaConnector")); @@ -78,6 +80,7 @@ public class WantToOrder implements MessageListener { } } catch (JMSException e) { log.error("EXCEPTION!", e); + } catch (InterruptedException e) { log.error("EXCEPTION!", e); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToPay.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToPay.java index d4a3113..0658280 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToPay.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToPay.java @@ -51,12 +51,16 @@ public class WantToPay implements MessageListener { Connection connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + PayResponse pr = new PayResponse(payrequest.getGroupdata(), waiter.getId()); MessageProducer informGroup = session.createProducer(session.createQueue("GroupConnector")); informGroup.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - PayResponse pr = new PayResponse(payrequest.getGroupdata(), waiter.getId()); informGroup.send(session.createObjectMessage(pr)); + MessageProducer informPizzera = session.createProducer(session.createQueue("PizzeriaConnector")); + informPizzera.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + informPizzera.send(session.createObjectMessage(pr)); + session.close(); connection.close(); } else { -- 2.43.0