From 6027ca5bf608fe4f377efe4ab9a8bd4e4095676e Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Thu, 9 May 2013 14:07:58 +0200 Subject: [PATCH] Show orders in PizzeriaGUI --- .../tuwien/sbc/valesriegler/common/Util.java | 8 ++ .../pizzeria/gui/PizzeriaFrame.java | 7 +- .../gui/tablemodels/PizzasOfOrderModel.java | 3 +- .../gui/tablemodels/TablesOverviewModel.java | 3 +- .../gui/tablemodels/WaitersOfOrderModel.java | 9 +- .../sbc/valesriegler/types/GroupData.java | 11 ++- .../tuwien/sbc/valesriegler/types/Order.java | 8 +- .../sbc/valesriegler/types/PizzaOrder.java | 21 +++- .../tuwien/sbc/valesriegler/types/Table.java | 2 +- .../xvsm/AbstractXVSMConnector.java | 6 +- .../sbc/valesriegler/xvsm/PizzeriaXVSM.java | 12 +++ .../xvsm/WaiterXVSMConnector.java | 95 ++++++++++--------- .../sbc/valesriegler/xvsm/waiter/Waiter.java | 2 +- 13 files changed, 121 insertions(+), 66 deletions(-) diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java index 28dc0d1..1aa3581 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java @@ -36,5 +36,13 @@ public abstract class Util { } return container; } + + public static String getId(int id) { + return id != 0 ? String.valueOf(id) : ""; + } + + public static int getIntSafe(Integer nr) { + return nr == null ? 0 : nr; + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzeriaFrame.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzeriaFrame.java index ac64ec4..53a4bc0 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzeriaFrame.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzeriaFrame.java @@ -127,10 +127,11 @@ public class PizzeriaFrame extends JFrame { public void valueChanged(ListSelectionEvent e) { ListSelectionModel lsm = (ListSelectionModel) e.getSource(); int minIndex = lsm.getMinSelectionIndex(); - + if(minIndex < 0) return; + GroupData currentGroup = PizzeriaAgent.getInstance().getOrdersModel().getGroupOfRow(minIndex); - waitersPanel.setBorder(new TitledBorder(String.format(WAITER_IDS_CAPTION, currentGroup.getId()))); - pizzasPanel.setBorder(new TitledBorder(String.format(PIZZAS_CAPTION, currentGroup.getId()))); + waitersPanel.setBorder(new TitledBorder(String.format(WAITER_IDS_CAPTION, currentGroup.getOrder().getId()))); + pizzasPanel.setBorder(new TitledBorder(String.format(PIZZAS_CAPTION, currentGroup.getOrder().getId()))); PizzeriaAgent.getInstance().getWaitersModel().setCurrentGroup(currentGroup); PizzeriaAgent.getInstance().getPizzasModel().setCurrentOrder(currentGroup.getOrder()); 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 d62877f..297e480 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 @@ -38,8 +38,7 @@ public class PizzasOfOrderModel extends TableModel { case STATUS: return pizzaOrder.getStatus(); case COOK: - return new Integer(3);// pizzaOrder.getCookId(); //TODO: pizzaorder <-> - // pizza + return pizzaOrder.getCookId(); default: throw new RuntimeException(UNHANDLEDCOLUMN); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/TablesOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/TablesOverviewModel.java index a53474b..ded871b 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/TablesOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/TablesOverviewModel.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; import at.ac.tuwien.sbc.valesriegler.common.TableModel; +import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.types.Table; public class TablesOverviewModel extends TableModel { @@ -29,7 +30,7 @@ public class TablesOverviewModel extends TableModel
{ case STATUS: return table.isFree(); case GROUP_ID: - return table.getGroupId(); + return Util.getId(table.getGroupId()); default: throw new RuntimeException(UNHANDLEDCOLUMN); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/WaitersOfOrderModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/WaitersOfOrderModel.java index 779cc9c..2c17b8a 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/WaitersOfOrderModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/WaitersOfOrderModel.java @@ -1,6 +1,7 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels; import at.ac.tuwien.sbc.valesriegler.common.TableModel; +import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.types.GroupData; @@ -37,13 +38,13 @@ public class WaitersOfOrderModel extends TableModel { // case SERVING : return currentOrder.getWaiterIdOfServing(); // case PAYMENT : return currentOrder.getWaiterIdOfPayment(); case TABLE_ASSIGNMENT: - return groupData.getTableWaiter(); + return Util.getId(groupData.getTableWaiter()); case ORDER: - return groupData.getOrderWaiter(); + return Util.getId(groupData.getOrderWaiter()); case SERVING: - return groupData.getServingWaiter(); + return Util.getId(groupData.getServingWaiter()); case PAYMENT: - return groupData.getPayingWaiter(); + return Util.getId(groupData.getPayingWaiter()); default: throw new RuntimeException(UNHANDLEDCOLUMN); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupData.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupData.java index 383a439..1fd577d 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupData.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupData.java @@ -9,6 +9,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.HasId; +import at.ac.tuwien.sbc.valesriegler.common.Util; /** * This class represents all of a group's data, that can be relevant to send @@ -49,7 +50,7 @@ public class GroupData implements Serializable, HasId { } public int getId() { - return id; + return Util.getIntSafe(id); } public Order getOrder() { @@ -57,11 +58,11 @@ public class GroupData implements Serializable, HasId { } public int getOrderWaiter() { - return orderWaiter; + return Util.getIntSafe(orderWaiter); } public int getPayingWaiter() { - return payingWaiter; + return Util.getIntSafe(payingWaiter); } @Deprecated @@ -75,7 +76,7 @@ public class GroupData implements Serializable, HasId { } public int getServingWaiter() { - return servingWaiter; + return Util.getIntSafe(servingWaiter); } public int getSize() { @@ -91,7 +92,7 @@ public class GroupData implements Serializable, HasId { } public int getTableWaiter() { - return tableWaiter; + return Util.getIntSafe(tableWaiter); } public void setOrder(Order order) { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java index deebc16..fa01a12 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java @@ -15,8 +15,8 @@ import at.ac.tuwien.sbc.valesriegler.group.Group; */ public class Order implements Serializable, HasId { private static int idNext = 0; - private final int id; - private int groupId; + private Integer id; + private Integer groupId; private OrderStatus status; private List orderedPizzas; @@ -46,6 +46,10 @@ public class Order implements Serializable, HasId { return groupId; } + public void setId(Integer id) { + this.id = id; + } + public int getId() { return id; } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaOrder.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaOrder.java index 80a10a1..6c3b768 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaOrder.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaOrder.java @@ -12,10 +12,11 @@ import at.ac.tuwien.sbc.valesriegler.common.HasId; */ public class PizzaOrder implements Serializable, HasId { private static int nextID = 0; - final private int id; + private Integer id; - private final PizzaType pizzaType; + private PizzaType pizzaType; private PizzaOrderStatus status; + private Integer cookId; public PizzaOrder(PizzaType pizzaType) { id = ++nextID; @@ -39,6 +40,22 @@ public class PizzaOrder implements Serializable, HasId { this.status = status; } + public void setId(Integer id) { + this.id = id; + } + + public void setPizzaType(PizzaType pizzaType) { + this.pizzaType = pizzaType; + } + + public Integer getCookId() { + return cookId; + } + + public void setCookId(Integer cookId) { + this.cookId = cookId; + } + @Override public String toString() { return "PizzaOrder [id=" + id + ", pizzaType=" + pizzaType + ", status=" + status + "]"; 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 ba31ff5..a931ea3 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 @@ -51,7 +51,7 @@ public class Table implements Serializable, HasId { public String toString() { return "Table [id=" + id + "]"; } - + @Override public int hashCode() { final int prime = 31; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/AbstractXVSMConnector.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/AbstractXVSMConnector.java index 887ee5b..52494ab 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/AbstractXVSMConnector.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/AbstractXVSMConnector.java @@ -15,6 +15,7 @@ import org.mozartspaces.core.CapiUtil; import org.mozartspaces.core.ContainerReference; import org.mozartspaces.core.DefaultMzsCore; import org.mozartspaces.core.Entry; +import org.mozartspaces.core.MzsTimeoutException; import org.mozartspaces.core.MzsConstants.RequestTimeout; import org.mozartspaces.core.MzsCore; import org.mozartspaces.core.MzsCoreException; @@ -127,7 +128,10 @@ public abstract class AbstractXVSMConnector { return (T) CapiUtil.getSingleEntry(entities); } catch (CountNotMetException e) { - log.info(errorMsg); + capi.rollbackTransaction(tx); + + throw new EntityNotFoundByTemplate(errorMsg); + } catch(MzsTimeoutException e) { capi.rollbackTransaction(tx); throw new EntityNotFoundByTemplate(errorMsg); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaXVSM.java index 4e48b4b..90a9df8 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaXVSM.java @@ -15,6 +15,7 @@ import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent; import at.ac.tuwien.sbc.valesriegler.types.GroupData; +import at.ac.tuwien.sbc.valesriegler.types.GroupState; import at.ac.tuwien.sbc.valesriegler.types.Table; public class PizzeriaXVSM extends AbstractXVSMConnector { @@ -67,6 +68,14 @@ public class PizzeriaXVSM extends AbstractXVSMConnector { @Override public void run() { PizzeriaAgent.getInstance().getGroupModel().addItems(groups); + + List groupsWhoHaveOrdered = new ArrayList<>(); + for (GroupData groupData : groups) { + if(groupData.getState() != GroupState.NEW && groupData.getState() != GroupState.WAITING) { + groupsWhoHaveOrdered.add(groupData); + } + } + PizzeriaAgent.getInstance().getOrdersModel().addItems(groupsWhoHaveOrdered); } }); @@ -79,5 +88,8 @@ public class PizzeriaXVSM extends AbstractXVSMConnector { } } + public void listenForOrders() { + } + } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSMConnector.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSMConnector.java index 02819c4..17cbbf6 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSMConnector.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSMConnector.java @@ -10,6 +10,8 @@ import java.util.List; import org.mozartspaces.capi3.CountNotMetException; import org.mozartspaces.core.MzsConstants.RequestTimeout; import org.mozartspaces.core.MzsCoreException; +import org.mozartspaces.core.MzsTimeoutException; +import org.mozartspaces.core.TransactionException; import org.mozartspaces.core.TransactionReference; import org.mozartspaces.notifications.Notification; import org.mozartspaces.notifications.NotificationListener; @@ -20,13 +22,18 @@ import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.types.GroupData; import at.ac.tuwien.sbc.valesriegler.types.GroupState; +import at.ac.tuwien.sbc.valesriegler.types.OrderStatus; import at.ac.tuwien.sbc.valesriegler.types.Table; public class WaiterXVSMConnector extends AbstractXVSMConnector { private static final Logger log = LoggerFactory.getLogger(WaiterXVSMConnector.class); + private final int waiterId; - public WaiterXVSMConnector() { + public WaiterXVSMConnector(int waiterId) { super(); + + this.waiterId = waiterId; + useFreeTablesContainer(); useAssignTableContainer(); useTablesContainer(); @@ -66,26 +73,18 @@ public class WaiterXVSMConnector extends AbstractXVSMConnector { assignGroupToTable(lockedGroup, lockedFreeTable, tx); } catch (IllegalArgumentException e) { - log.error("SHOULD NEVER HAPPEN!"); - System.exit(1); - } catch (CountNotMetException e) { - log.error("SHOULD NEVER HAPPEN!"); - System.exit(1); - log.info(e.getMessage()); - } catch (EntityNotFoundByTemplate e) { - // TODO check why another exception is thrown -// capi.rollbackTransaction(tx); - } catch (Exception e) { - log.error("SHOULD NEVER HAPPEN!"); + log.info("IllegalArgumentException"); e.printStackTrace(); + } catch (EntityNotFoundByTemplate e) { log.info(e.getMessage()); - - capi.rollbackTransaction(tx); + } catch (Exception e) { + log.error("AN INNER EXCEPTION"); + e.printStackTrace(); } + } catch (TransactionException e) { + log.info("An unimportant TransactionException has occurred"); } catch (Exception e) { - log.error("SHOULD NEVER HAPPEN!"); - - e.printStackTrace(); + log.error("OUTER EXCEPTION"); } } @@ -101,8 +100,6 @@ public class WaiterXVSMConnector extends AbstractXVSMConnector { } } - - public void listenForNewGuests() { NotificationListener newGroupsListener = new NotificationListener() { @@ -124,30 +121,23 @@ public class WaiterXVSMConnector extends AbstractXVSMConnector { GroupData lockedGroup = takeEntityByTemplateFromContainer(new GroupData(group.getId()), assignTableContainer, tx, 1000, groupNotFound); // Acquire a lock for one free table in the TablesContainer - Table lockedFreeTable = takeEntityByTemplateFromContainer(new Table(), freeTablesContainer, tx, RequestTimeout.DEFAULT, String.format("No free table for group with id %d could be found", group.getId())); + Table lockedFreeTable = takeEntityByTemplateFromContainer(new Table(null), freeTablesContainer, tx, RequestTimeout.DEFAULT, String.format("No free table for group with id %d could be found", group.getId())); assignGroupToTable(lockedGroup, lockedFreeTable, tx); } catch (IllegalArgumentException e) { - log.error("SHOULD NEVER HAPPEN!"); - System.exit(1); - } catch (CountNotMetException e) { - log.error("SHOULD NEVER HAPPEN!"); - System.exit(1); - log.info(e.getMessage()); - } catch (EntityNotFoundByTemplate e) { -// capi.rollbackTransaction(tx); - } catch (Exception e) { - log.error("SHOULD NEVER HAPPEN!"); + log.info("IllegalArgumentException"); e.printStackTrace(); + } catch (EntityNotFoundByTemplate e) { log.info(e.getMessage()); - - capi.rollbackTransaction(tx); + } catch (Exception e) { + log.error("AN INNER EXCEPTION"); + e.printStackTrace(); } + } catch (TransactionException e) { + log.info("An unimportant TransactionException has occurred"); } catch (Exception e) { - log.error("SHOULD NEVER HAPPEN!"); - - e.printStackTrace(); + log.error("OUTER EXCEPTION"); } } @@ -173,16 +163,25 @@ public class WaiterXVSMConnector extends AbstractXVSMConnector { if(groups.size() != 1) throw new RuntimeException("Multiple orders in one notification?! That should not happen!"); GroupData groupData = groups.get(0); - groupData.setState(GroupState.ORDERED); try { TransactionReference tx = capi.createTransaction(1500, URI.create(Util.SERVER_ADDR)); GroupData entity = new GroupData(groupData.getId()); entity.setState(GroupState.ORDER_PENDING); takeEntityByTemplateFromContainer(entity, takeOrderContainer, tx, RequestTimeout.DEFAULT, String.format("The order for group %d was already taken by an other waiter!", groupData.getId())); - sendItemsToContainer(Arrays.asList(groupData), ordersContainer, RequestTimeout.ZERO, tx); - sendItemsToContainer(Arrays.asList(groupData), groupsContainer, RequestTimeout.ZERO, tx); - capi.commitTransaction(tx); + + + log.info("Will now write taken order from group {} to space", groupData.getId()); + groupData.setOrderWaiter(waiterId); + groupData.setState(GroupState.ORDERED); + groupData.getOrder().setStatus(OrderStatus.ORDERED); + sendItemsToContainer(Arrays.asList(groupData), ordersContainer, RequestTimeout.ZERO, tx); + sendItemsToContainer(Arrays.asList(groupData), groupsContainer, RequestTimeout.ZERO, tx); + capi.commitTransaction(tx); + + + log.info("Waiter has taken order from group {}", groupData.getId()); + } catch (MzsCoreException e) { log.info("ERROR in listenForOrders"); e.printStackTrace(); @@ -205,18 +204,26 @@ public class WaiterXVSMConnector extends AbstractXVSMConnector { throws MzsCoreException { // The new group sits down at the table lockedFreeTable.setGroupId(lockedGroup.getId()); - lockedGroup.setTable(lockedFreeTable); + // The new group now wants to order lockedGroup.setState(GroupState.ORDER_PENDING); + lockedGroup.setTable(lockedFreeTable); + lockedGroup.setTableWaiter(waiterId); sendItemsToContainer(Arrays.asList(lockedFreeTable), tablesContainer, RequestTimeout.ZERO, tx); - sendItemsToContainer(Arrays.asList(lockedGroup), groupsContainer, RequestTimeout.ZERO, tx); sendItemsToContainer(Arrays.asList(lockedGroup), takeOrderContainer, RequestTimeout.ZERO, tx); - log.info("Assigned Table to group with groupId {}", lockedGroup.getId()); - if(tx != null) capi.commitTransaction(tx); - else log.error("TX IS NULL!"); + + sendItemsToContainer(Arrays.asList(lockedGroup), groupsContainer, RequestTimeout.ZERO, tx); + + try { + capi.commitTransaction(tx); + log.info("Assigned table to group with groupId {}", lockedGroup.getId()); + } catch (Exception e) { + log.info("Assigning a table to group with groupId {} failed", lockedGroup.getId()); + } + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/waiter/Waiter.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/waiter/Waiter.java index 069f055..090c2b2 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/waiter/Waiter.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/waiter/Waiter.java @@ -50,7 +50,7 @@ public class Waiter implements Serializable { } private void start() { - xvsm = new WaiterXVSMConnector(); + xvsm = new WaiterXVSMConnector(id); xvsm.listenForOrders(); -- 2.43.0