]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupAgentXVSM.java
Space waiters pay and overall remaining space workflow refactoring
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / xvsm / GroupAgentXVSM.java
1 package at.ac.tuwien.sbc.valesriegler.xvsm;
2
3 import java.io.Serializable;
4 import java.util.List;
5
6 import javax.swing.SwingUtilities;
7
8 import org.mozartspaces.capi3.AnyCoordinator;
9 import org.mozartspaces.capi3.LindaCoordinator;
10 import org.mozartspaces.core.MzsCoreException;
11 import org.mozartspaces.notifications.Notification;
12 import org.mozartspaces.notifications.NotificationListener;
13 import org.mozartspaces.notifications.Operation;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 import at.ac.tuwien.sbc.valesriegler.common.Util;
18 import at.ac.tuwien.sbc.valesriegler.group.GroupAgent;
19 import at.ac.tuwien.sbc.valesriegler.group.SpaceGroup;
20 import at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent;
21 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
22 import at.ac.tuwien.sbc.valesriegler.types.Order;
23
24 public class GroupAgentXVSM extends AbstractXVSMConnector {
25         private static final Logger log = LoggerFactory.getLogger(GroupAgentXVSM.class);
26         
27         public GroupAgentXVSM() {
28                 super();
29                 groupsContainer = useContainer(Util.GROUPS_CONTAINER) ;
30                 assignTableContainer = useContainer(Util.ASSIGN_TABLE) ;
31                 orderCompleteContainer = useContainer(Util.ORDER_COMPLETE) ;
32                 paymentContainer = useContainer(Util.PAYMENT) ;
33                 hasPaidContainer = useContainer(Util.HAS_PAID) ;
34         }
35         
36         public void listenForDeliveredOrders() {
37                 NotificationListener deliveredOrders = new NotificationListener() {
38             @Override
39             public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
40                 final List<GroupData> groups = castEntries(entries);
41                 
42                 log.info("{} order was delivered!", groups.size());
43                 if(groups.size() != 1) {
44                         throw new RuntimeException("A waiter should only deliver one order at once!");
45                 }
46                 final int groupId = groups.get(0).getOrder().getId();
47                            
48                 SwingUtilities.invokeLater(new Runnable() {
49                                         @Override
50                                         public void run() {
51                                                 GroupAgent.getInstance().getGroupModel().updateOrderDone(groupId);
52                                         }
53                                 });
54             }
55         };
56           try {
57                 notificationMgr.createNotification(orderCompleteContainer, deliveredOrders, Operation.WRITE);
58         } catch (Exception e) {
59            handleSpaceErrorAndTerminate(e);
60         }
61         }
62
63         public void listenForGroupDataChanges() {
64                 NotificationListener groupDataListener = new NotificationListener() {
65             @Override
66             public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
67                
68                                 log.info("{} groups have changed", entries.size());
69                                 
70                                 final List<GroupData> groups = castEntries(entries);
71                                 
72                                 SwingUtilities.invokeLater(new Runnable() {
73                                         
74                                         @Override
75                                         public void run() {
76                                                 GroupAgent.getInstance().getGroupModel().addGroupData(groups);
77                                         }
78                                 });
79             }
80         };
81         try {
82                         notificationMgr.createNotification(groupsContainer, groupDataListener, Operation.WRITE);
83                         log.info("Created groupsContainer notification for a waiter");
84         } catch (Exception e) {
85             handleSpaceErrorAndTerminate(e);
86          }
87                         
88         }
89
90         public void listenForPaymentRequest() {
91                 NotificationListener paymentRequest = new NotificationListener() {
92             @Override
93             public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
94                 log.info("PAY REQUEST by");
95                 final List<GroupData> groups = castEntries(entries);
96                 
97                 if(groups.size() != 1) {
98                         throw new RuntimeException("A group only says as a whole that it wants to pay");
99                 }
100                final GroupData group = groups.get(0);
101                log.info("PAY REQUEST by {}", group.getId());
102                            
103                 SwingUtilities.invokeLater(new Runnable() {
104                                         @Override
105                                         public void run() {
106                                                 GroupAgent.getInstance().getGroupModel().updateGroupPaymentPending(group.getId());
107                                         }
108                                 });
109                 
110             }
111         };
112           try {
113                 notificationMgr.createNotification(paymentContainer, paymentRequest, Operation.WRITE);
114         } catch (Exception e) {
115            handleSpaceErrorAndTerminate(e);
116         }
117         }
118
119         public void listenForPayment() {
120                 NotificationListener payment = new NotificationListener() {
121             @Override
122             public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
123                 
124                 final List<GroupData> groups = castEntries(entries);
125                 
126                 if(groups.size() != 1) {
127                         throw new RuntimeException("A group can only have paid as a whole!");
128                 }
129                final GroupData group = groups.get(0);
130                
131                            
132                 SwingUtilities.invokeLater(new Runnable() {
133                                         @Override
134                                         public void run() {
135                                                 GroupAgent.getInstance().getGroupModel().updateGroupHasPaid(group.getId());
136                                         }
137                                 });
138                 
139             }
140         };
141           try {
142                 notificationMgr.createNotification(hasPaidContainer, payment, Operation.WRITE);
143         } catch (Exception e) {
144            handleSpaceErrorAndTerminate(e);
145         }
146         }
147
148 }