From e93c91f5297213ecbe61b5f053735f07557525a3 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Sun, 5 May 2013 21:08:06 +0200 Subject: [PATCH] JMSCook now cooks Pizza out of PizzaOrder. --- .../ac/tuwien/sbc/valesriegler/cook/Cook.java | 61 -------------- .../tuwien/sbc/valesriegler/cook/JMSCook.java | 51 ++++++++++++ .../messageListeners/CookRequestedPizza.java | 54 ++++++++++++- .../valesriegler/group/gui/GroupFrame.java | 8 +- .../valesriegler/pizzeria/PizzeriaAgent.java | 58 ++++++------- .../valesriegler/pizzeria/gui/DEP_Order.java | 8 +- .../pizzeria/gui/DEP_PizzaOrder.java | 38 --------- .../pizzeria/gui/PizzeriaFrame.java | 81 +++++++++---------- .../gui/tablemodels/PizzasOfOrderModel.java | 45 ++++++----- .../tuwien/sbc/valesriegler/types/Order.java | 10 +-- .../tuwien/sbc/valesriegler/types/Pizza.java | 17 ++-- .../sbc/valesriegler/types/PizzaOrder.java | 33 ++++++++ .../valesriegler/types/PizzaOrderStatus.java | 7 ++ .../sbc/valesriegler/waiter/Waiter.java | 21 +++-- .../jms/messageListeners/WantToOrder.java | 6 +- 15 files changed, 269 insertions(+), 229 deletions(-) delete mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/cook/Cook.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/cook/JMSCook.java delete mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/DEP_PizzaOrder.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaOrder.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaOrderStatus.java diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/Cook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/Cook.java deleted file mode 100644 index 03c4161..0000000 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/Cook.java +++ /dev/null @@ -1,61 +0,0 @@ -package at.ac.tuwien.sbc.valesriegler.cook; - -import java.io.Serializable; - -import javax.jms.Connection; -import javax.jms.DeliveryMode; -import javax.jms.MessageProducer; -import javax.jms.ObjectMessage; -import javax.jms.Session; - -import org.apache.activemq.ActiveMQConnectionFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import at.ac.tuwien.sbc.valesriegler.types.Pizza; -import at.ac.tuwien.sbc.valesriegler.types.PizzaType; - -public class Cook implements Serializable { - private static final Logger log = LoggerFactory.getLogger(Cook.class); - private static int nextID = 0; - final private int id; - - public static void main(String[] args) throws Exception { - new Cook(++nextID); - } - - public Cook(int id) { - this.id = id; - log.info("I AM A COOK WITH ID {}", this.id); - produce(PizzaType.CARDINALE); - } - - public int getId() { - return id; - } - - public void produce(PizzaType pizzatype) { - try { - // Connecting to the Broker and to the output queue - ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); - Connection connection = connectionFactory.createConnection(); - connection.start(); - Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - - MessageProducer producer = session.createProducer(session.createQueue("CookedPizzas")); - producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - - // Create a messages - Pizza pizza = new Pizza(pizzatype, this); - ObjectMessage message = session.createObjectMessage(pizza); - producer.send(message); - - // Clean up - session.close(); - connection.close(); - } catch (Exception e) { - log.error("Caught: ", e); - e.printStackTrace(); - } - } -} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/JMSCook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/JMSCook.java new file mode 100644 index 0000000..6e68569 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/JMSCook.java @@ -0,0 +1,51 @@ +package at.ac.tuwien.sbc.valesriegler.cook; + +import javax.jms.Connection; +import javax.jms.JMSException; +import javax.jms.MessageConsumer; +import javax.jms.Session; + +import org.apache.activemq.ActiveMQConnectionFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners.CookRequestedPizza; + +/** + * (JMS)Cook is far too primitive to require any abstraction. It would take more + * time to it, than to code it twice. + * + * @author jan + * + */ +public class JMSCook { + private static final Logger log = LoggerFactory.getLogger(JMSCook.class); + private static int nextID = 0; + final private int id; + + public static void main(String[] args) throws Exception { + new JMSCook(++nextID); + } + + public JMSCook(int id) { + this.id = id; + log.info("I AM A JMSCook WITH ID {}", this.id); + + try { + // Connecting to the Broker and to the output queue + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); + Connection connection = connectionFactory.createConnection(); + connection.start(); + + Session sessWantToSit = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageConsumer consWantToSit = sessWantToSit.createConsumer(sessWantToSit.createQueue("OrderedPizzas")); + consWantToSit.setMessageListener(new CookRequestedPizza(this)); + } catch (JMSException e) { + log.error("EXCEPTION!", e); + } + } + + public int getId() { + return id; + } +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/CookRequestedPizza.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/CookRequestedPizza.java index 3767f0c..a0e691b 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/CookRequestedPizza.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/cook/jms/messageListeners/CookRequestedPizza.java @@ -1,22 +1,68 @@ package at.ac.tuwien.sbc.valesriegler.cook.jms.messageListeners; +import javax.jms.Connection; +import javax.jms.DeliveryMode; +import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; +import javax.jms.MessageProducer; +import javax.jms.ObjectMessage; +import javax.jms.Session; +import org.apache.activemq.ActiveMQConnectionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import at.ac.tuwien.sbc.valesriegler.cook.Cook; +import at.ac.tuwien.sbc.valesriegler.cook.JMSCook; +import at.ac.tuwien.sbc.valesriegler.types.Pizza; +import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; +import at.ac.tuwien.sbc.valesriegler.types.PizzaType; public class CookRequestedPizza implements MessageListener { private static final Logger log = LoggerFactory.getLogger(CookRequestedPizza.class); - private final Cook cook; + private final JMSCook cook; - public CookRequestedPizza(Cook cook) { + public CookRequestedPizza(JMSCook cook) { this.cook = cook; } @Override - public void onMessage(Message m) { + public void onMessage(Message msg) { + try { + msg.acknowledge(); + if (msg instanceof ObjectMessage) { + ObjectMessage objMsg = (ObjectMessage) msg; + Object obj = objMsg.getObject(); + + if (obj instanceof PizzaType) { + PizzaOrder pizzaorder = (PizzaOrder) obj; + log.debug("Received: " + pizzaorder); + + // generate random delay + Thread.sleep((long) (pizzaorder.getPizzaType().duration * 1000)); + + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); + Connection connection = connectionFactory.createConnection(); + connection.start(); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + MessageProducer prodCP = session.createProducer(session.createQueue("CookedPizzas")); + prodCP.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + + prodCP.send(session.createObjectMessage(Pizza.createPizzaFromPizzaOrder(pizzaorder, cook))); + + session.close(); + connection.close(); + } else { + log.warn("Received unknown Object: " + obj); + } + } else { + log.warn("Received unknown Message: " + msg); + } + } 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/group/gui/GroupFrame.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java index 8546657..beceb22 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java @@ -23,6 +23,8 @@ import javax.swing.border.TitledBorder; import at.ac.tuwien.sbc.valesriegler.group.Group; import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; import at.ac.tuwien.sbc.valesriegler.types.Order; +import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; +import at.ac.tuwien.sbc.valesriegler.types.PizzaType; /** * Base Frame of the Group UI @@ -138,7 +140,11 @@ public class GroupFrame extends JFrame { for (int i = 0; i < gc.numberOfGroups; i++) { Group group = new Group(); group.getGroupData().setSize(gc.size); - Order order = new Order(group, gc.pizzaTypes); + List pizzaOrders = new ArrayList<>(); + for (PizzaType pt : gc.pizzaTypes) { + pizzaOrders.add(new PizzaOrder(pt)); + } + Order order = new Order(group, pizzaOrders); group.getGroupData().setOrder(order); newGroups.add(group); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java index 027bece..4335e50 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java @@ -1,38 +1,29 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria; -import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import javax.swing.SwingUtilities; -import org.mozartspaces.capi3.AnyCoordinator; -import org.mozartspaces.capi3.Coordinator; -import org.mozartspaces.core.Capi; -import org.mozartspaces.core.ContainerReference; -import org.mozartspaces.core.DefaultMzsCore; -import org.mozartspaces.core.Entry; -import org.mozartspaces.core.MzsCore; import org.mozartspaces.core.MzsCoreException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import at.ac.tuwien.sbc.valesriegler.common.SpaceUtil; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.PizzeriaFrame; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.DEP_Table; +import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.PizzeriaFrame; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.GroupsOverviewModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.OrdersOverviewModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.PizzasOfOrderModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.TablesOverviewModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.WaitersOfOrderModel; import at.ac.tuwien.sbc.valesriegler.xvsm.XVSMConnector; + /** * The Main class of the Pizzeria compoment. *

* Start the communication and the Pizzeria GUI * * @author Gregor Riegler - * + * */ public class PizzeriaAgent { private static final Logger log = LoggerFactory.getLogger(PizzeriaAgent.class); @@ -48,19 +39,28 @@ public class PizzeriaAgent { private XVSMConnector xvsm; public static void main(String[] args) { - if(args.length != 1) { + // if (args.length != 1) { + // throw new IllegalArgumentException(USAGE); + // } else { + String mom = "JMS";// args[0]; + log.info(mom); + PizzeriaAgent pizzeriaAgent = new PizzeriaAgent(); + switch (mom) { + case "XVSM": + pizzeriaAgent.createModels(); + pizzeriaAgent.initXVSM(); + pizzeriaAgent.initGUI(); + break; + case "JMS": + pizzeriaAgent.createModels(); + pizzeriaAgent.initJMS(); + pizzeriaAgent.initGUI(); + break; + default: throw new IllegalArgumentException(USAGE); - } else { - String mom = args[0]; - log.info(mom); - PizzeriaAgent pizzeriaAgent = new PizzeriaAgent(); - switch(mom) { - case "XVSM" : pizzeriaAgent.createModels(); pizzeriaAgent.initXVSM(); pizzeriaAgent.initGUI(); break; - case "JMS" : pizzeriaAgent.createModels(); pizzeriaAgent.initJMS(); pizzeriaAgent.initGUI(); break; - default : throw new IllegalArgumentException(USAGE); - } - } + + // } } private void initJMS() { @@ -71,7 +71,7 @@ public class PizzeriaAgent { try { xvsm = new XVSMConnector(); xvsm.initSpaceCommunication(); - + } catch (MzsCoreException e) { log.error(e.getMessage()); log.error("The Pizzeria has no Space connection! Have you started the Space Server?"); @@ -83,7 +83,7 @@ public class PizzeriaAgent { PizzeriaGUI gui = new PizzeriaGUI(); SwingUtilities.invokeLater(gui); } - + class PizzeriaGUI implements Runnable { @Override public void run() { @@ -92,16 +92,16 @@ public class PizzeriaAgent { @Override public void freeTablesCreated(List tables) { - xvsm.sendFreeTablesToSpace(tables); + // xvsm.sendFreeTablesToSpace(tables); } - + }); frame.start(); frame.pack(); frame.setVisible(true); } } - + private void createModels() { ordersModel = new OrdersOverviewModel(); groupModel = new GroupsOverviewModel(); @@ -109,7 +109,7 @@ public class PizzeriaAgent { waitersModel = new WaitersOfOrderModel(); pizzasModel = new PizzasOfOrderModel(); } - + public interface TablesCreatedHandler { public void freeTablesCreated(List tables); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/DEP_Order.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/DEP_Order.java index b90fa00..a4e9433 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/DEP_Order.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/DEP_Order.java @@ -2,6 +2,8 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria.gui; import java.util.List; +import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; + @Deprecated public class DEP_Order { private int id; @@ -18,7 +20,7 @@ public class DEP_Order { private int waiterIdOfServing; private int waiterIdOfPayment; - private List pizzaOrders; + private List pizzaOrders; public int getId() { return id; @@ -84,11 +86,11 @@ public class DEP_Order { this.waiterIdOfPayment = waiterIdOfPayment; } - public List getPizzaOrders() { + public List getPizzaOrders() { return pizzaOrders; } - public void setPizzaOrders(List pizzaOrders) { + public void setPizzaOrders(List pizzaOrders) { this.pizzaOrders = pizzaOrders; } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/DEP_PizzaOrder.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/DEP_PizzaOrder.java deleted file mode 100644 index 9fead0b..0000000 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/DEP_PizzaOrder.java +++ /dev/null @@ -1,38 +0,0 @@ -package at.ac.tuwien.sbc.valesriegler.pizzeria.gui; - -import at.ac.tuwien.sbc.valesriegler.types.PizzaType; - -@Deprecated -public class DEP_PizzaOrder { - private PizzaType pizzaType; - private int cookId; - private PizzaOrderStatus status; - - public PizzaType getPizzaType() { - return pizzaType; - } - - public void setPizzaType(PizzaType pizzaType) { - this.pizzaType = pizzaType; - } - - public int getCookId() { - return cookId; - } - - public void setCookId(int cookId) { - this.cookId = cookId; - } - - public PizzaOrderStatus getStatus() { - return status; - } - - public void setStatus(PizzaOrderStatus status) { - this.status = status; - } - - public enum PizzaOrderStatus { - ORDERED, IN_PREPARATION, DONE - } -} 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 79ea518..828532a 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 @@ -14,6 +14,7 @@ import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.ListSelectionModel; +import javax.swing.ScrollPaneConstants; import javax.swing.border.TitledBorder; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; @@ -23,12 +24,13 @@ import javax.swing.event.TableModelListener; import at.ac.tuwien.sbc.valesriegler.common.TableModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent; import at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent.TablesCreatedHandler; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.DEP_PizzaOrder.PizzaOrderStatus; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.GroupsOverviewModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.OrdersOverviewModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.PizzasOfOrderModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.TablesOverviewModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.WaitersOfOrderModel; +import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; +import at.ac.tuwien.sbc.valesriegler.types.PizzaOrderStatus; import at.ac.tuwien.sbc.valesriegler.types.PizzaType; /** @@ -50,12 +52,13 @@ public class PizzeriaFrame extends JFrame { private TablesOverviewModel tablesModel; private WaitersOfOrderModel waitersModel; private PizzasOfOrderModel pizzasModel; - + private TablesCreatedHandler onTablesCreatedHandler; - public PizzeriaFrame(OrdersOverviewModel ordersModel, GroupsOverviewModel groupModel, TablesOverviewModel tablesModel, WaitersOfOrderModel waitersModel, PizzasOfOrderModel pizzasModel) { + public PizzeriaFrame(OrdersOverviewModel ordersModel, GroupsOverviewModel groupModel, + TablesOverviewModel tablesModel, WaitersOfOrderModel waitersModel, PizzasOfOrderModel pizzasModel) { super("Pizzeria"); - + this.ordersModel = ordersModel; this.groupModel = groupModel; this.tablesModel = tablesModel; @@ -88,20 +91,17 @@ public class PizzeriaFrame extends JFrame { private int getNumberOfTables() { int numberOfTables; - String numberOfTablesInput = JOptionPane - .showInputDialog(PLEASE_INPUT_THE_NUMBER_OF_TABLES_TO_CREATE); + String numberOfTablesInput = JOptionPane.showInputDialog(PLEASE_INPUT_THE_NUMBER_OF_TABLES_TO_CREATE); try { numberOfTables = Integer.parseInt(numberOfTablesInput); } catch (NumberFormatException e) { - JOptionPane.showMessageDialog(this, - YOUR_INPUT_CANNOT_BE_PARSED_AS_A_NUMBER, WRONG_INPUT, + JOptionPane.showMessageDialog(this, YOUR_INPUT_CANNOT_BE_PARSED_AS_A_NUMBER, WRONG_INPUT, JOptionPane.ERROR_MESSAGE); return getNumberOfTables(); } if (numberOfTables < 1) { - JOptionPane.showMessageDialog(this, - THE_NUMBER_MUST_BE_GREATER_OR_EQUAL_TO_1, WRONG_INPUT, + JOptionPane.showMessageDialog(this, THE_NUMBER_MUST_BE_GREATER_OR_EQUAL_TO_1, WRONG_INPUT, JOptionPane.ERROR_MESSAGE); return getNumberOfTables(); } @@ -112,7 +112,7 @@ public class PizzeriaFrame extends JFrame { private void initModels() { List orders = getOrdersTestData(); ordersModel.setItems(orders); - + // When the tables get created in the first place, the handler is informed int numberOfTables = getNumberOfTables(); List freeTablesCreated = tablesModel.createFreeTables(numberOfTables); @@ -131,30 +131,25 @@ public class PizzeriaFrame extends JFrame { createTableInTitledPanel(pizzasPanel, pizzasModel, ""); /** - * Update the displayed order in the waiter and pizza detail tables when - * an order gets selected in the orders table + * Update the displayed order in the waiter and pizza detail tables when an + * order gets selected in the orders table */ - ordersTable.getSelectionModel().addListSelectionListener( - new ListSelectionListener() { - @Override - public void valueChanged(ListSelectionEvent e) { - ListSelectionModel lsm = (ListSelectionModel) e - .getSource(); - int minIndex = lsm.getMinSelectionIndex(); - - DEP_Order currentOrder = ordersModel - .getOrderOfRow(minIndex); - waitersPanel.setBorder(new TitledBorder(String.format( - WAITER_IDS_CAPTION, currentOrder.getId()))); - pizzasPanel.setBorder(new TitledBorder(String.format( - PIZZAS_CAPTION, currentOrder.getId()))); - waitersModel.setCurrentOrder(currentOrder); - pizzasModel.setCurrentOrder(currentOrder); - - waitersPanel.setVisible(true); - pizzasPanel.setVisible(true); - } - }); + ordersTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { + @Override + public void valueChanged(ListSelectionEvent e) { + ListSelectionModel lsm = (ListSelectionModel) e.getSource(); + int minIndex = lsm.getMinSelectionIndex(); + + DEP_Order currentOrder = ordersModel.getOrderOfRow(minIndex); + waitersPanel.setBorder(new TitledBorder(String.format(WAITER_IDS_CAPTION, currentOrder.getId()))); + pizzasPanel.setBorder(new TitledBorder(String.format(PIZZAS_CAPTION, currentOrder.getId()))); + waitersModel.setCurrentOrder(currentOrder); + pizzasModel.setCurrentOrder(currentOrder); + + waitersPanel.setVisible(true); + pizzasPanel.setVisible(true); + } + }); detailWrapper.add(waitersPanel); detailWrapper.add(pizzasPanel); @@ -167,8 +162,7 @@ public class PizzeriaFrame extends JFrame { private JTable initOrdersOverview(JPanel wrapper) { JPanel tablePanel = new JPanel(); - JTable orderTable = createTableInTitledPanel(tablePanel, ordersModel, - "Orders"); + JTable orderTable = createTableInTitledPanel(tablePanel, ordersModel, "Orders"); wrapper.add(tablePanel); @@ -185,7 +179,7 @@ public class PizzeriaFrame extends JFrame { private void initTablesOverview(JPanel wrapper) { JPanel scrollPanePanel = new JPanel(new GridBagLayout()); final JLabel freeTablesLabel = new JLabel(getNumberOfFreeTables()); - scrollPanePanel.add(freeTablesLabel, new GridBagConstraints()); + scrollPanePanel.add(freeTablesLabel, new GridBagConstraints()); createTableInTitledPanel(scrollPanePanel, tablesModel, "Tables"); tablesModel.addTableModelListener(new TableModelListener() { @@ -198,8 +192,7 @@ public class PizzeriaFrame extends JFrame { wrapper.add(scrollPanePanel); } - private JTable createTableInTitledPanel(JPanel wrapperPanel, - TableModel model, String title) { + private JTable createTableInTitledPanel(JPanel wrapperPanel, TableModel model, String title) { JTable table = new JTable(model); wrapperPanel.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); @@ -211,7 +204,7 @@ public class PizzeriaFrame extends JFrame { JScrollPane scrollPane = new JScrollPane(table); wrapperPanel.setBorder(new TitledBorder(title)); - scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); + scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); wrapperPanel.add(scrollPane, c); return table; @@ -235,11 +228,9 @@ public class PizzeriaFrame extends JFrame { order1.setWaiterIdOfOrder(4); order2.setWaiterIdOfServing(2); - List pizzas = new ArrayList<>(); - DEP_PizzaOrder pizzaOrder1 = new DEP_PizzaOrder(); - pizzaOrder1.setPizzaType(PizzaType.CARDINALE); + List pizzas = new ArrayList<>(); + PizzaOrder pizzaOrder1 = new PizzaOrder(PizzaType.CARDINALE); pizzaOrder1.setStatus(PizzaOrderStatus.DONE); - pizzaOrder1.setCookId(3); pizzas.add(pizzaOrder1); order1.setPizzaOrders(pizzas); @@ -255,7 +246,7 @@ public class PizzeriaFrame extends JFrame { } public void setOnTablesCreatedHandler(PizzeriaAgent.TablesCreatedHandler tablesCreatedHandler) { - this.onTablesCreatedHandler = tablesCreatedHandler; + onTablesCreatedHandler = tablesCreatedHandler; } } 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 9e738b7..1244fc7 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 @@ -4,39 +4,44 @@ import java.util.List; import at.ac.tuwien.sbc.valesriegler.common.TableModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.DEP_Order; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.DEP_PizzaOrder; +import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; -public class PizzasOfOrderModel extends TableModel { +public class PizzasOfOrderModel extends TableModel { private static final String TYPE = "Type"; private static final String STATUS = "Status"; private static final String COOK = "Cook"; - private static final String[] COLUMNS = new String[] { - TYPE, STATUS, COOK - }; - + private static final String[] COLUMNS = new String[] { TYPE, STATUS, COOK }; + private DEP_Order currentOrder; public void setCurrentOrder(DEP_Order currentOrder) { this.currentOrder = currentOrder; - + fireTableDataChanged(); } - + @Override public Object getValueAt(int rowIndex, int columnIndex) { - if(rowIndex > 0) return null; - - List pizzaOrders = currentOrder.getPizzaOrders(); - if(pizzaOrders == null) return null; - - DEP_PizzaOrder pizzaOrder = pizzaOrders.get(rowIndex); + if (rowIndex > 0) + return null; + + List pizzaOrders = currentOrder.getPizzaOrders(); + if (pizzaOrders == null) + return null; + + PizzaOrder pizzaOrder = pizzaOrders.get(rowIndex); String wantedColumn = COLUMNS[columnIndex]; - switch(wantedColumn) { - case TYPE : return pizzaOrder.getPizzaType(); - case STATUS : return pizzaOrder.getStatus(); - case COOK : return pizzaOrder.getCookId(); - default : throw new RuntimeException(UNHANDLEDCOLUMN); + switch (wantedColumn) { + case TYPE: + return pizzaOrder.getPizzaType(); + case STATUS: + return pizzaOrder.getStatus(); + case COOK: + return new Integer(3);// pizzaOrder.getCookId(); //TODO: pizzaorder <-> + // pizza + default: + throw new RuntimeException(UNHANDLEDCOLUMN); } } @@ -44,7 +49,7 @@ public class PizzasOfOrderModel extends TableModel { protected String[] getColumns() { return COLUMNS; } - + @Override public int getRowCount() { return currentOrder == null ? 0 : 1; 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 84f9f9d..3685bb6 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 @@ -11,10 +11,10 @@ public class Order implements Serializable { private int groupId; private OrderStatus status; - private List orderedPizzas; + private List orderedPizzas; private List cookedPizzas; - public Order(int groupId, List orderedPizzas) { + public Order(int groupId, List orderedPizzas) { id = ++idNext; this.groupId = groupId; status = OrderStatus.NEW; @@ -22,7 +22,7 @@ public class Order implements Serializable { cookedPizzas = null; } - public Order(Group group, List orderedPizzas) { + public Order(Group group, List orderedPizzas) { id = ++idNext; groupId = group.getGroupData().getId(); status = OrderStatus.NEW; @@ -42,7 +42,7 @@ public class Order implements Serializable { return id; } - public List getOrderedPizzas() { + public List getOrderedPizzas() { return orderedPizzas; } @@ -58,7 +58,7 @@ public class Order implements Serializable { this.groupId = groupId; } - public void setOrderedPizzas(List orderedPizzas) { + public void setOrderedPizzas(List orderedPizzas) { this.orderedPizzas = orderedPizzas; } 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 1bfeabd..a7b8ad1 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 @@ -2,24 +2,25 @@ package at.ac.tuwien.sbc.valesriegler.types; import java.io.Serializable; -import at.ac.tuwien.sbc.valesriegler.cook.Cook; +import at.ac.tuwien.sbc.valesriegler.cook.JMSCook; import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; public class Pizza implements Serializable { - final private PizzaType type; - - // debugzeugs - private static int nextID = 0; final private int id; + final private PizzaType type; final public int cookId; public Waiter deliveryAgent; // private Person consumer; - public Pizza(PizzaType type, Cook producer) { - id = ++nextID; + private Pizza(int id, PizzaType type, int cookId) { + this.id = id; this.type = type; - cookId = producer.getId(); + this.cookId = cookId; + } + + public static Pizza createPizzaFromPizzaOrder(PizzaOrder pizzaorder, JMSCook producer) { + return new Pizza(pizzaorder.getId(), pizzaorder.getPizzaType(), producer.getId()); } @Override 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 new file mode 100644 index 0000000..56d94cd --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaOrder.java @@ -0,0 +1,33 @@ +package at.ac.tuwien.sbc.valesriegler.types; + +import java.io.Serializable; + +public class PizzaOrder implements Serializable { + private static int nextID = 0; + final private int id; + + private final PizzaType pizzaType; + private PizzaOrderStatus status; + + public PizzaOrder(PizzaType pizzaType) { + id = ++nextID; + this.pizzaType = pizzaType; + status = PizzaOrderStatus.NEW; + } + + public PizzaType getPizzaType() { + return pizzaType; + } + + public PizzaOrderStatus getStatus() { + return status; + } + + public int getId() { + return id; + } + + public void setStatus(PizzaOrderStatus status) { + this.status = status; + } +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaOrderStatus.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaOrderStatus.java new file mode 100644 index 0000000..2c8d513 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaOrderStatus.java @@ -0,0 +1,7 @@ +package at.ac.tuwien.sbc.valesriegler.types; + +import java.io.Serializable; + +public enum PizzaOrderStatus implements Serializable { + NEW, ORDERED, IN_PREPARATION, DONE +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java index 5fdd224..90788cb 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java @@ -27,19 +27,7 @@ public class Waiter implements Serializable { public Waiter(int id) { this.id = id; log.info("I AM A WAITER WITH ID {}", id); - deliver(); - } - - @Override - public String toString() { - return "Waiter [id=" + id + "]"; - } - - public int getId() { - return id; - } - public void deliver() { try { // Connecting to the Broker and to the output queue ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); @@ -57,4 +45,13 @@ public class Waiter implements Serializable { log.error("Caught: ", e); } } + + @Override + public String toString() { + return "Waiter [id=" + id + "]"; + } + + public int getId() { + return id; + } } 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 47a9e3c..41fd83d 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 @@ -16,7 +16,7 @@ import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.group.actions.OrderRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.TableRequest; import at.ac.tuwien.sbc.valesriegler.types.Order; -import at.ac.tuwien.sbc.valesriegler.types.PizzaType; +import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; public class WantToOrder implements MessageListener { @@ -52,8 +52,8 @@ public class WantToOrder implements MessageListener { // Create the queue for the cooks. Order o = orderrequest.getGroupdata().getOrder(); - for (PizzaType p : o.getOrderedPizzas()) { - ObjectMessage message = session.createObjectMessage(p); + for (PizzaOrder po : o.getOrderedPizzas()) { + ObjectMessage message = session.createObjectMessage(po); prodOP.send(message); } -- 2.43.0