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