]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java
Some UI Fixes in PizzeriaGUI inter-table communication.
[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 DELIVERY_ORDER_TAKEN = "deliverOrderTaken";
32     public static final String GROUP_AGENT_ORDER_TAKEN = "groupAgentOrder";
33     public static final String GROUP_AGENT_DELIVERY_ORDER_TAKEN = "groupAgentDeliveryOrder";
34     public static final String DELIVER_PIZZAS = "deliverPizzas";
35     public static final String DELIVER_DELIVERY_PIZZAS = "deliverDeliveryPizzas";
36     public static final String PREPARE_PIZZAS = "preparePizzas";
37     public static final String PIZZAS_IN_PROGRESS = "pizzasInProgress";
38     public static final String ORDER_COMPLETE = "orderComplete";
39     public static final String GROUP_AGENT_ORDER_COMPLETE = "groupAgentOrderComplete";
40     public static final String PAYMENT_REQUEST = "payment";
41     public static final String GROUP_AGENT_PAYMENT_REQUEST = "groupAgentPayment";
42     public static final String FREE_TABLES = "freeTables";
43     public static final String PAYMENT_DONE = "hasPaid";
44     public static final String GROUP_AGENT_PAYMENT_DONE = "groupAgentHasPaid";
45     public static final String PIZZERIA_INFO = "pizzeriaInfo";
46     public static final String PHONE_CALLS = "phoneCalls";
47     public static final String GROUP_AGENT_INFO = "groupAgentInfo";
48
49         private static Random random = new Random();
50
51         public static final long SPACE_TRANSACTION_TIMEOUT = 1500;
52
53         public static final String SERVER_ADDR = "xvsm://localhost:%d";
54         public static final int GROUP_AGENT_PORT = 9876;
55         public static final int DELIVERY_CUSTOMERS_PORT = 9877;
56
57         public static final String JMS_CONNECTSTRING = "tcp://localhost:61616?jms.prefetchPolicy.all=1";
58
59         public static String getId(int id) {
60                 return (id != 0 && id != -1) ? String.valueOf(id) : "";
61         }
62
63         public static int getIntSafe(Integer nr) {
64                 return nr == null ? 0 : nr;
65         }
66
67     public static URI createURI(int port) {
68         return URI.create(String.format(SERVER_ADDR, port));
69     }
70
71         public static <T extends HasId> Map<Integer, T> intoMapById(List<T> hasIds) {
72                 if (hasIds == null) {
73                         return Collections.emptyMap();
74                 }
75
76                 Map<Integer, T> myMap = new HashMap<>();
77                 for (T hasId : hasIds) {
78                         myMap.put(hasId.getId(), hasId);
79                 }
80                 return myMap;
81         }
82
83         public static int getRandom(int min, int max) {
84                 return random.nextInt(max - min + 1) + min;
85         }
86
87     public static Tuple<Integer> parseIdAndSpacePort(String[] args, String usage) {
88         if (args.length != 2) {
89             throw new IllegalArgumentException(usage);
90         }
91
92         int parsedId = 0;
93         int port = 0;
94         try {
95             parsedId = Integer.parseInt(args[0]);
96             port = Integer.parseInt(args[1]);
97         } catch (NumberFormatException e) {
98             throw new IllegalArgumentException(usage);
99         }
100
101         return new Tuple<Integer>(parsedId, port);
102     }
103
104     public static JTable createTableInTitledPanel(JPanel wrapperPanel, TableModel<?> model, String title) {
105         JTable table = new JTable(model);
106         wrapperPanel.setLayout(new GridBagLayout());
107         GridBagConstraints c = new GridBagConstraints();
108         c.gridx = 0;
109         c.weightx = 1;
110         c.weighty = 1;
111         c.insets = new Insets(10, 10, 10, 10);
112         c.fill = GridBagConstraints.BOTH;
113         JScrollPane scrollPane = new JScrollPane(table);
114
115         wrapperPanel.setBorder(new TitledBorder(title));
116         scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
117         wrapperPanel.add(scrollPane, c);
118
119         return table;
120     }
121
122     /**
123      *
124      * @return A display string for PizzaTypes.
125      *         <p />
126      *         e.g. Cardinale, Cardinale, Margherita is displayed as
127      *         "2xCARDINALE,1xMARGHERITA"
128      */
129     public static String pizzaDisplay(List<PizzaOrder> list) {
130         HashMap<PizzaType, Integer> pizzaCount = new HashMap<PizzaType, Integer>();
131         for (PizzaOrder pizzaOrder : list) {
132             PizzaType pizzaType = pizzaOrder.getPizzaType();
133             if (pizzaCount.containsKey(pizzaType)) {
134                 pizzaCount.put(pizzaType, pizzaCount.get(pizzaType) + 1);
135             } else
136                 pizzaCount.put(pizzaType, 1);
137         }
138         Set<PizzaType> pizzaTypes = pizzaCount.keySet();
139         StringBuilder sb = new StringBuilder();
140
141         boolean multiplePizzas = false;
142         for (PizzaType pizzaType : pizzaTypes) {
143             if (multiplePizzas)
144                 sb.append(", ");
145             else
146                 multiplePizzas = true;
147
148             sb.append(pizzaCount.get(pizzaType) + "x");
149             sb.append(pizzaType.toString());
150         }
151
152         return sb.toString();
153     }
154 }