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