]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java
[XVSM] Pizzeria notifies on start Group agent that it exists. So the pizzeria can...
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / common / Util.java
1 package at.ac.tuwien.sbc.valesriegler.common;
2
3 import java.awt.*;
4 import java.net.URI;
5 import java.util.*;
6 import java.util.List;
7
8 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder;
9 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
10 import org.mozartspaces.capi3.Coordinator;
11 import org.mozartspaces.core.Capi;
12 import org.mozartspaces.core.ContainerReference;
13 import org.mozartspaces.core.MzsConstants.Container;
14 import org.mozartspaces.core.MzsConstants.RequestTimeout;
15 import org.mozartspaces.core.MzsCoreException;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import javax.swing.*;
20 import javax.swing.border.TitledBorder;
21
22 public abstract class Util {
23         private static final Logger log = LoggerFactory.getLogger(Util.class);
24     public static boolean useJMS = true;
25
26         public static final String TABLE_ASSIGNED = "tables";
27         public static final String GROUP_AGENT_TABLE_ASSIGNED = "groupAgentTables";
28     public static final String ASSIGN_TABLE = "assignTable";
29     public static final String TAKE_ORDER = "takeOrder";
30     public static final String ORDER_TAKEN = "order";
31     public static final String GROUP_AGENT_ORDER_TAKEN = "groupAgentOrder";
32     public static final String DELIVER_PIZZAS = "deliverPizzas";
33     public static final String PREPARE_PIZZAS = "preparePizzas";
34     public static final String PIZZAS_IN_PROGRESS = "pizzasInProgress";
35     public static final String ORDER_COMPLETE = "orderComplete";
36     public static final String GROUP_AGENT_ORDER_COMPLETE = "groupAgentOrderComplete";
37     public static final String PAYMENT_REQUEST = "payment";
38     public static final String GROUP_AGENT_PAYMENT_REQUEST = "groupAgentPayment";
39     public static final String FREE_TABLES = "freeTables";
40     public static final String PAYMENT_DONE = "hasPaid";
41     public static final String GROUP_AGENT_PAYMENT_DONE = "groupAgentHasPaid";
42     public static final String PIZZERIA_INFO = "pizzeriaInfo";
43     public static final String GROUP_AGENT_INFO = "groupAgentInfo";
44
45         private static Random random = new Random();
46
47         public static final long SPACE_TRANSACTION_TIMEOUT = 1500;
48
49         public static final String SERVER_ADDR = "xvsm://localhost:%d";
50         public static final int GROUP_AGENT_PORT = 9876;
51
52         public static final String JMS_CONNECTSTRING = "tcp://localhost:61616?jms.prefetchPolicy.all=1";
53
54         public static String getId(int id) {
55                 return (id != 0 && id != -1) ? String.valueOf(id) : "";
56         }
57
58         public static int getIntSafe(Integer nr) {
59                 return nr == null ? 0 : nr;
60         }
61
62     public static URI createURI(int port) {
63         return URI.create(String.format(SERVER_ADDR, port));
64     }
65
66         public static <T extends HasId> Map<Integer, T> intoMapById(List<T> hasIds) {
67                 if (hasIds == null) {
68                         return Collections.emptyMap();
69                 }
70
71                 Map<Integer, T> myMap = new HashMap<>();
72                 for (T hasId : hasIds) {
73                         myMap.put(hasId.getId(), hasId);
74                 }
75                 return myMap;
76         }
77
78         public static int getRandom(int min, int max) {
79                 return random.nextInt(max - min + 1) + min;
80         }
81
82     public static Tuple<Integer> parseIdAndSpacePort(String[] args, String usage) {
83         if (args.length != 2) {
84             throw new IllegalArgumentException(usage);
85         }
86
87         int parsedId = 0;
88         int port = 0;
89         try {
90             parsedId = Integer.parseInt(args[0]);
91             port = Integer.parseInt(args[1]);
92         } catch (NumberFormatException e) {
93             throw new IllegalArgumentException(usage);
94         }
95
96         return new Tuple<Integer>(parsedId, port);
97     }
98
99     public static JTable createTableInTitledPanel(JPanel wrapperPanel, TableModel<?> model, String title) {
100         JTable table = new JTable(model);
101         wrapperPanel.setLayout(new GridBagLayout());
102         GridBagConstraints c = new GridBagConstraints();
103         c.gridx = 0;
104         c.weightx = 1;
105         c.weighty = 1;
106         c.insets = new Insets(10, 10, 10, 10);
107         c.fill = GridBagConstraints.BOTH;
108         JScrollPane scrollPane = new JScrollPane(table);
109
110         wrapperPanel.setBorder(new TitledBorder(title));
111         scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
112         wrapperPanel.add(scrollPane, c);
113
114         return table;
115     }
116
117     /**
118      *
119      * @return A display string for PizzaTypes.
120      *         <p />
121      *         e.g. Cardinale, Cardinale, Margherita is displayed as
122      *         "2xCARDINALE,1xMARGHERITA"
123      */
124     public static String pizzaDisplay(List<PizzaOrder> list) {
125         HashMap<PizzaType, Integer> pizzaCount = new HashMap<PizzaType, Integer>();
126         for (PizzaOrder pizzaOrder : list) {
127             PizzaType pizzaType = pizzaOrder.getPizzaType();
128             if (pizzaCount.containsKey(pizzaType)) {
129                 pizzaCount.put(pizzaType, pizzaCount.get(pizzaType) + 1);
130             } else
131                 pizzaCount.put(pizzaType, 1);
132         }
133         Set<PizzaType> pizzaTypes = pizzaCount.keySet();
134         StringBuilder sb = new StringBuilder();
135
136         boolean multiplePizzas = false;
137         for (PizzaType pizzaType : pizzaTypes) {
138             if (multiplePizzas)
139                 sb.append(", ");
140             else
141                 multiplePizzas = true;
142
143             sb.append(pizzaCount.get(pizzaType) + "x");
144             sb.append(pizzaType.toString());
145         }
146
147         return sb.toString();
148     }
149 }