From 81735313646a70368b24b472f8d810bc128c7829 Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Mon, 6 May 2013 17:11:43 +0200 Subject: [PATCH] XVSMConnector refactoring --- .../sbc/valesriegler/common/TableModel.java | 1 - .../sbc/valesriegler/group/GroupAgent.java | 2 +- .../group/gui/GroupOverviewModel.java | 17 +++ .../valesriegler/pizzeria/PizzeriaAgent.java | 38 +++++- .../pizzeria/gui/PizzeriaFrame.java | 2 +- .../gui/tablemodels/GroupsOverviewModel.java | 10 ++ .../gui/tablemodels/TablesOverviewModel.java | 24 +--- .../sbc/valesriegler/xvsm/XVSMConnector.java | 128 ++++++------------ 8 files changed, 114 insertions(+), 108 deletions(-) diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/TableModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/TableModel.java index f58b8ba..cc67bbb 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/TableModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/TableModel.java @@ -46,5 +46,4 @@ public abstract class TableModel extends AbstractTableModel } protected abstract String[] getColumns(); - } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java index 2502529..08b4cfc 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java @@ -75,7 +75,7 @@ public class GroupAgent { for (Group group : newGroups) { groupData.add(group.getGroupData()); } - xvsm.createGroups(groupData); + xvsm.sendGroupsToSpace(groupData); } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java index 118f3eb..6933822 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java @@ -11,6 +11,7 @@ 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.Group; +import at.ac.tuwien.sbc.valesriegler.types.GroupData; import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; import at.ac.tuwien.sbc.valesriegler.types.PizzaType; @@ -111,4 +112,20 @@ public class GroupOverviewModel extends TableModel { if(Util.useJMS) g.goGrabSomeFood(); } } + + /** + * + * This is necessary as in the space version GroupData objects get written to the space. In order to + * translate from Group data to the actual group object the group object has to be looked up by the id. + * + * @param newItems the group data of the corresponding groups + */ + public void addGroupData(List newItems) { + List groups = new ArrayList<>(); + for (GroupData groupData : newItems) { + Group group = items.get(groupData.getId()); + groups.add(group); + } + super.addItems(groups); + } } 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 5b63bb6..6732999 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,9 +1,15 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria; +import java.io.Serializable; +import java.util.ArrayList; import java.util.List; import javax.swing.SwingUtilities; +import org.mozartspaces.core.Entry; +import org.mozartspaces.notifications.Notification; +import org.mozartspaces.notifications.NotificationListener; +import org.mozartspaces.notifications.Operation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,6 +20,7 @@ import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.OrdersOverviewMode 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.GroupData; import at.ac.tuwien.sbc.valesriegler.types.Table; import at.ac.tuwien.sbc.valesriegler.xvsm.XVSMConnector; @@ -32,9 +39,6 @@ public class PizzeriaAgent { private static PizzeriaAgent pizzeriaAgent; - - - private OrdersOverviewModel ordersModel; private GroupsOverviewModel groupModel; private TablesOverviewModel tablesModel; @@ -79,7 +83,33 @@ public class PizzeriaAgent { xvsm.useTablesContainer(); xvsm.useGroupsContainer(); - xvsm.initPizzeriaNotifications(); + NotificationListener tablesListener = new NotificationListener() { + @Override + public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { + + final List tables = new ArrayList<>(); + List entryList = (List) entries; + for (Entry entry : entryList) { + tables.add((Table) entry.getValue()); + } + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + PizzeriaAgent.getInstance().getTablesModel().addItems(tables); + } + }); + } + }; + + NotificationListener groupsListener = new NotificationListener() { + @Override + public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { + List groups = (List) entries; + PizzeriaAgent.getInstance().getGroupModel().addItems(groups); + } + }; + xvsm.initTablesNotifications(tablesListener); + xvsm.initGroupNotifications(groupsListener); } private void initGUI() { 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 4ae943c..ac64ec4 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 @@ -159,7 +159,7 @@ public class PizzeriaFrame extends JFrame { private void initWaitingGroupsOverview(JPanel wrapper) { JPanel tablePanel = new JPanel(); - createTableInTitledPanel(tablePanel, PizzeriaAgent.getInstance().getGroupModel(), "Groups"); + createTableInTitledPanel(tablePanel, PizzeriaAgent.getInstance().getGroupModel(), "Waiting groups"); wrapper.add(tablePanel); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/GroupsOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/GroupsOverviewModel.java index 3e21eeb..69ea92f 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/GroupsOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/GroupsOverviewModel.java @@ -1,10 +1,12 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import at.ac.tuwien.sbc.valesriegler.common.TableModel; import at.ac.tuwien.sbc.valesriegler.types.GroupData; +import at.ac.tuwien.sbc.valesriegler.types.GroupState; public class GroupsOverviewModel extends TableModel { private static final String ID = "ID"; @@ -20,6 +22,14 @@ public class GroupsOverviewModel extends TableModel { @Override public Object getValueAt(int rowIndex, int columnIndex) { List values = new ArrayList<>(items.values()); + Iterator it = values.iterator(); + while (it.hasNext()) { + GroupData groupData = it.next(); + // Show only the waiting groups! + if(groupData.getState() != GroupState.WAITING) { + it.remove(); + } + } GroupData group = values.get(rowIndex); String wantedColumn = COLUMNS[columnIndex]; switch (wantedColumn) { 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 031b231..c67a67d 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 @@ -54,28 +54,18 @@ public class TablesOverviewModel extends TableModel
{ } public List
createFreeTables(int numberOfTables) { - synchronized (items) { - List
tables = new ArrayList
(); - for (int i = 0; i < numberOfTables; i++) { - Table table = createFreeTable(); - tables.add(table); - } + List
tables = new ArrayList
(); + for (int i = 0; i < numberOfTables; i++) { + Table table = createFreeTable(); + tables.add(table); + } - setItems(tables); + setItems(tables); - return tables; - } + return tables; } private Table createFreeTable() { return new Table(); } - - public void mergeTables(List
tables) { - for (Table table : tables) { - items.put(table.getId(), table); - } - fireTableDataChanged(); - } - } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/XVSMConnector.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/XVSMConnector.java index 6c4bab5..b79fa63 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/XVSMConnector.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/XVSMConnector.java @@ -29,6 +29,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.Util; +import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; import at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent; import at.ac.tuwien.sbc.valesriegler.types.GroupData; import at.ac.tuwien.sbc.valesriegler.types.GroupState; @@ -77,45 +78,15 @@ public class XVSMConnector { handleSpaceErrorAndTerminate(e); } } - - private List createCoordinators(Coordinator... coordinator) { - return Arrays.asList(coordinator); - } - - private void handleSpaceErrorAndTerminate(Exception e) { - log.error(e.getMessage()); - e.printStackTrace(); - System.exit(1); - } + public void sendTablesToSpace(List
tables) { - try { - List entries = new ArrayList<>(); - for (Table table : tables) { - entries.add(new Entry(table)); - } - capi.write(entries, tablesContainer); - - log.info("Wrote tables to Space!"); - - } catch (MzsCoreException e) { - log.info(e.getMessage()); - } + sendItemsToContainer(tables, tablesContainer); } - public void createGroups(List newGroups) { - try { - List entries = new ArrayList<>(); - for (GroupData group : newGroups) { - entries.add(new Entry(group)); - } - capi.write(entries, groupsContainer); - - log.info("Wrote new Groups to Space!"); - } catch (MzsCoreException e) { - log.info(e.getMessage()); - e.printStackTrace(); - } + + public void sendGroupsToSpace(List newGroups) { + sendItemsToContainer(newGroups, groupsContainer); } public void handleWaitingGroup(int id) { @@ -208,57 +179,20 @@ public class XVSMConnector { } } - public void initPizzeriaNotifications() { - // When tables are written to the space the Pizzeria gets informed and sets the new tables in the tables model! - NotificationListener tablesListener = new NotificationListener() { - @Override - public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { - - final List
tables = new ArrayList<>(); - List entryList = (List) entries; - for (Entry entry : entryList) { - tables.add((Table) entry.getValue()); - } - SwingUtilities.invokeLater(new Runnable() { - @Override - public void run() { - PizzeriaAgent.getInstance().getTablesModel().mergeTables(tables); - } - }); - } - }; - - NotificationListener groupsListener = new NotificationListener() { - @Override - public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { - List tables = (List) entries; -// TODO problem at the moment: Item type of groupoverviewmodel is Group and not GroupData - } - }; - - try { - notificationMgr.createNotification(tablesContainer, groupsListener, Operation.WRITE); - notificationMgr.createNotification(tablesContainer, tablesListener, Operation.WRITE); - } catch (Exception e) { - e.printStackTrace(); - } + public void initTablesNotifications(NotificationListener listener) { + try { + notificationMgr.createNotification(tablesContainer, listener, Operation.WRITE); + } catch (Exception e) { + handleSpaceErrorAndTerminate(e); + } } - public void initGroupNotifications() { - NotificationListener notifListener = new NotificationListener() { - @Override - public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { - List groups = (List) entries; - - // when a groupdata is changed, call setItems on the groupoverviewmodel. TODO problem at the moment: Item type of groupoverviewmodel is Group and not GroupData - } - - }; - try { - notificationMgr.createNotification(groupsContainer, notifListener, Operation.WRITE); - } catch (Exception e) { - handleSpaceErrorAndTerminate(e); - } + public void initGroupNotifications(NotificationListener listener) { + try { + notificationMgr.createNotification(groupsContainer, listener, Operation.WRITE); + } catch (Exception e) { + handleSpaceErrorAndTerminate(e); + } } public void handlePaymentRequest() { @@ -266,6 +200,32 @@ public class XVSMConnector { public void handlePizzaDistribution() { } + + + private List createCoordinators(Coordinator... coordinator) { + return Arrays.asList(coordinator); + } + + private void handleSpaceErrorAndTerminate(Exception e) { + log.error(e.getMessage()); + e.printStackTrace(); + System.exit(1); + } + + + private void sendItemsToContainer(List items, ContainerReference cref) { + try { + List entries = new ArrayList<>(); + for (Serializable item : items) { + entries.add(new Entry(item)); + } + capi.write(entries, cref); + + } catch (MzsCoreException e) { + log.info(e.getMessage()); + e.printStackTrace(); + } + } } -- 2.43.0