]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java
loadbalancer and some little changes.
[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.GridBagConstraints;
4 import java.awt.GridBagLayout;
5 import java.awt.Insets;
6 import java.net.URI;
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Random;
12 import java.util.Set;
13
14 import javax.swing.JPanel;
15 import javax.swing.JScrollPane;
16 import javax.swing.JTable;
17 import javax.swing.ScrollPaneConstants;
18 import javax.swing.border.TitledBorder;
19
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder;
24 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
25
26 public abstract class Util {
27         private static final Logger log = LoggerFactory.getLogger(Util.class);
28         public static boolean useJMS = true;
29
30         public static final String TABLE_ASSIGNED = "tables";
31         public static final String ASSIGN_TABLE = "assignTable";
32         public static final String TAKE_ORDER = "takeOrder";
33         public static final String ORDER_TAKEN = "order";
34         public static final String DELIVERY_ORDER_TAKEN = "deliverOrderTaken";
35         public static final String DELIVER_PIZZAS = "deliverPizzas";
36         public static final String DELIVER_DELIVERY_PIZZAS = "deliverDeliveryPizzas";
37         public static final String DELIVER_DELIVERY_ORDER = "deliverDeliveryOrderContainer";
38         public static final String PREPARE_PIZZAS = "preparePizzas";
39         public static final String PREPARE_DELIVERY_PIZZAS = "prepareDeliveryPizzas";
40         public static final String PIZZAS_IN_PROGRESS = "pizzasInProgress";
41         public static final String ORDER_COMPLETE = "orderComplete";
42         public static final String PAYMENT_REQUEST = "payment";
43         public static final String FREE_TABLES = "freeTables";
44         public static final String PAYMENT_DONE = "hasPaid";
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         public static final String PIZZERIA_GROUP = "pizzeriaGroupContainer";
49         public static final String PIZZERIA_TABLE = "pizzeriaTableContainer";
50         public static final String PIZZERIA_DELIVERY = "pizzeriaDeliveryContainer";
51
52         private static Random random = new Random();
53
54         public static final long SPACE_TRANSACTION_TIMEOUT = 1500;
55
56         public static final String SERVER_ADDR = "xvsm://localhost:%d";
57         public static final int GROUP_AGENT_PORT = 9876;
58         public static final int DELIVERY_CUSTOMERS_PORT = 9877;
59
60         public static final String JMS_NAMING_SERVICE = "tcp://localhost:61616?jms.prefetchPolicy.all=1";
61
62         public static String getId(int id) {
63                 return (id != 0 && id != -1) ? String.valueOf(id) : "";
64         }
65
66         public static int getIntSafe(Integer nr) {
67                 return nr == null ? 0 : nr;
68         }
69
70         public static URI createURI(int port) {
71                 return URI.create(String.format(SERVER_ADDR, port));
72         }
73
74         public static <T extends HasId> Map<Integer, T> intoMapById(List<T> hasIds) {
75                 if (hasIds == null) {
76                         return Collections.emptyMap();
77                 }
78
79                 Map<Integer, T> myMap = new HashMap<>();
80                 for (T hasId : hasIds) {
81                         myMap.put(hasId.getId(), hasId);
82                 }
83                 return myMap;
84         }
85
86         public static int getRandom(int min, int max) {
87                 return random.nextInt(max - min + 1) + min;
88         }
89
90         public static Tuple<Integer> parseIdAndSpacePort(String[] args, String usage) {
91                 if (args.length != 2) {
92                         throw new IllegalArgumentException(usage);
93                 }
94
95                 int parsedId = 0;
96                 int port = 0;
97                 try {
98                         parsedId = Integer.parseInt(args[0]);
99                         port = Integer.parseInt(args[1]);
100                 } catch (NumberFormatException e) {
101                         throw new IllegalArgumentException(usage);
102                 }
103
104                 return new Tuple<Integer>(parsedId, port);
105         }
106
107         public static JTable createTableInTitledPanel(JPanel wrapperPanel, TableModel<?> model, String title) {
108                 JTable table = new JTable(model);
109                 wrapperPanel.setLayout(new GridBagLayout());
110                 GridBagConstraints c = new GridBagConstraints();
111                 c.gridx = 0;
112                 c.weightx = 1;
113                 c.weighty = 1;
114                 c.insets = new Insets(10, 10, 10, 10);
115                 c.fill = GridBagConstraints.BOTH;
116                 JScrollPane scrollPane = new JScrollPane(table);
117
118                 wrapperPanel.setBorder(new TitledBorder(title));
119                 scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
120                 wrapperPanel.add(scrollPane, c);
121                 table.setAutoCreateRowSorter(true);
122                 return table;
123         }
124
125         /**
126          * 
127          * @return A display string for PizzaTypes.
128          *         <p />
129          *         e.g. Cardinale, Cardinale, Margherita is displayed as "2xCARDINALE,1xMARGHERITA"
130          */
131         public static String pizzaDisplay(List<PizzaOrder> list) {
132                 HashMap<PizzaType, Integer> pizzaCount = new HashMap<PizzaType, Integer>();
133                 for (PizzaOrder pizzaOrder : list) {
134                         PizzaType pizzaType = pizzaOrder.getPizzaType();
135                         if (pizzaCount.containsKey(pizzaType)) {
136                                 pizzaCount.put(pizzaType, pizzaCount.get(pizzaType) + 1);
137                         } else
138                                 pizzaCount.put(pizzaType, 1);
139                 }
140                 Set<PizzaType> pizzaTypes = pizzaCount.keySet();
141                 StringBuilder sb = new StringBuilder();
142
143                 boolean multiplePizzas = false;
144                 for (PizzaType pizzaType : pizzaTypes) {
145                         if (multiplePizzas)
146                                 sb.append(", ");
147                         else
148                                 multiplePizzas = true;
149
150                         sb.append(pizzaCount.get(pizzaType) + "x");
151                         sb.append(pizzaType.toString());
152                 }
153
154                 return sb.toString();
155         }
156
157         public static void handleSpaceErrorAndTerminate(Exception e) {
158                 log.error(e.getMessage());
159                 e.printStackTrace();
160                 System.exit(1);
161         }
162 }