]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java
Merge branch 'master' of https://bitbucket.org/rgregor/sbc-ss-2013
[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         public GroupOverviewModel() {
35                 super();
36 //              super.items.add(new Group()); // TODO: remove, debuggroup!
37         }
38
39         @Override
40         protected String[] getColumns() {
41                 return COLUMNS;
42         }
43
44         public List<Group> getItems() {
45                 return new ArrayList<>(items.values());
46         }
47
48         public Group getGroupByID(int id) {
49                 return items.get(id);
50         }
51
52         @Override
53         public Object getValueAt(int rowIndex, int columnIndex) {
54                 List<Group> values = new ArrayList<>(items.values());
55                 Group group = values.get(rowIndex);
56                 String wantedColumn = COLUMNS[columnIndex];
57                 switch (wantedColumn) {
58                 case ID:
59                         int groupId = group.getGroupData().getId();
60                         log.info("This is the group id : {}", groupId);
61                         return groupId;
62                 case SIZE:
63                         int groupSize = group.getGroupData().getSize();
64                         log.info("This is the size : {}", groupSize);
65                         return groupSize;
66                 case PIZZAS:
67                         if (group.getGroupData().getOrder() == null)
68                                 return "none";
69                         return pizzaDisplay(group.getGroupData().getOrder().getOrderedPizzas());
70                 case STATE:
71                         return group.getGroupData().getState();
72                 default:
73                         throw new RuntimeException(UNHANDLEDCOLUMN);
74                 }
75         }
76
77         /**
78          * 
79          * @return A display string for PizzaTypes.
80          *         <p />
81          *         e.g. Cardinale, Cardinale, Margherita is displayed as
82          *         "2xCARDINALE,1xMARGHERITA"
83          */
84         private String pizzaDisplay(List<PizzaOrder> list) {
85                 HashMap<PizzaType, Integer> pizzaCount = new HashMap<PizzaType, Integer>();
86                 for (PizzaOrder pizzaOrder : list) {
87                         PizzaType pizzaType = pizzaOrder.getPizzaType();
88                         if (pizzaCount.containsKey(pizzaType)) {
89                                 pizzaCount.put(pizzaType, pizzaCount.get(pizzaType) + 1);
90                         } else
91                                 pizzaCount.put(pizzaType, 1);
92                 }
93                 Set<PizzaType> pizzaTypes = pizzaCount.keySet();
94                 StringBuilder sb = new StringBuilder();
95
96                 boolean multiplePizzas = false;
97                 for (PizzaType pizzaType : pizzaTypes) {
98                         if (multiplePizzas)
99                                 sb.append(", ");
100                         else
101                                 multiplePizzas = true;
102
103                         sb.append(pizzaCount.get(pizzaType) + "x");
104                         sb.append(pizzaType.toString());
105                 }
106
107                 return sb.toString();
108         }
109
110         @Override
111         public void addItems(List<Group> newItems) {
112                 log.info("addItems()");
113                 super.addItems(newItems);
114                 for (Group g : newItems) {
115                         if(Util.useJMS) g.goGrabSomeFood();
116                 }
117         }
118 }