]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java
Some debugging commenting/uncommenting
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / group / gui / GroupOverviewModel.java
1 package at.ac.tuwien.sbc.valesriegler.group.gui;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Set;
7
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11 import at.ac.tuwien.sbc.valesriegler.common.TableModel;
12 import at.ac.tuwien.sbc.valesriegler.common.Util;
13 import at.ac.tuwien.sbc.valesriegler.group.Group;
14 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder;
15 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
16
17 /**
18  * Might not the nicest way to store global data, but this is an elemental
19  * Object, storing ALL the Group's data. Might consider splitting those two
20  * usecases apart.
21  * 
22  * @author jan
23  * 
24  */
25 public class GroupOverviewModel extends TableModel<Group> {
26         private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class);
27
28         private static final String STATE = "State";
29         private static final String PIZZAS = "Pizzas";
30         private static final String SIZE = "Size";
31         private static final String ID = "ID";
32         private static final String[] COLUMNS = new String[] { ID, SIZE, PIZZAS, STATE };
33
34
35         @Override
36         protected String[] getColumns() {
37                 return COLUMNS;
38         }
39
40         public List<Group> getItems() {
41                 return new ArrayList<>(items.values());
42         }
43
44         public Group getGroupByID(int id) {
45                 return items.get(id);
46         }
47
48         @Override
49         public Object getValueAt(int rowIndex, int columnIndex) {
50                 List<Group> values = new ArrayList<>(items.values());
51                 Group group = values.get(rowIndex);
52                 String wantedColumn = COLUMNS[columnIndex];
53                 switch (wantedColumn) {
54                 case ID:
55                         int groupId = group.getGroupData().getId();
56                         log.info("This is the group id : {}", groupId);
57                         return groupId;
58                 case SIZE:
59                         int groupSize = group.getGroupData().getSize();
60                         log.info("This is the size : {}", groupSize);
61                         return groupSize;
62                 case PIZZAS:
63                         if (group.getGroupData().getOrder() == null)
64                                 return "none";
65                         return pizzaDisplay(group.getGroupData().getOrder().getOrderedPizzas());
66                 case STATE:
67                         return group.getGroupData().getState();
68                 default:
69                         throw new RuntimeException(UNHANDLEDCOLUMN);
70                 }
71         }
72
73         /**
74          * 
75          * @return A display string for PizzaTypes.
76          *         <p />
77          *         e.g. Cardinale, Cardinale, Margherita is displayed as
78          *         "2xCARDINALE,1xMARGHERITA"
79          */
80         private String pizzaDisplay(List<PizzaOrder> list) {
81                 HashMap<PizzaType, Integer> pizzaCount = new HashMap<PizzaType, Integer>();
82                 for (PizzaOrder pizzaOrder : list) {
83                         PizzaType pizzaType = pizzaOrder.getPizzaType();
84                         if (pizzaCount.containsKey(pizzaType)) {
85                                 pizzaCount.put(pizzaType, pizzaCount.get(pizzaType) + 1);
86                         } else
87                                 pizzaCount.put(pizzaType, 1);
88                 }
89                 Set<PizzaType> pizzaTypes = pizzaCount.keySet();
90                 StringBuilder sb = new StringBuilder();
91
92                 boolean multiplePizzas = false;
93                 for (PizzaType pizzaType : pizzaTypes) {
94                         if (multiplePizzas)
95                                 sb.append(", ");
96                         else
97                                 multiplePizzas = true;
98
99                         sb.append(pizzaCount.get(pizzaType) + "x");
100                         sb.append(pizzaType.toString());
101                 }
102
103                 return sb.toString();
104         }
105
106         @Override
107         public void addItems(List<Group> newItems) {
108                 log.info("addItems()");
109                 super.addItems(newItems);
110                 for (Group g : newItems) {
111                         if(Util.useJMS) g.goGrabSomeFood();
112                 }
113         }
114 }