]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java
Some space refactoring. Doing away with unnecessary containers. Auto-Reloading of...
[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.GroupData;
15 import at.ac.tuwien.sbc.valesriegler.types.GroupState;
16 import at.ac.tuwien.sbc.valesriegler.types.Order;
17 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder;
18 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
19 import at.ac.tuwien.sbc.valesriegler.types.Table;
20
21 /**
22  * Might not the nicest way to store global data, but this is an elemental
23  * Object, storing ALL the Group's data. Might consider splitting those two
24  * usecases apart.
25  * 
26  * @author jan
27  * 
28  */
29 public class GroupOverviewModel extends TableModel<Group> {
30         private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class);
31
32         private static final String STATE = "State";
33         private static final String PIZZAS = "Pizzas";
34         private static final String SIZE = "Size";
35         private static final String ID = "ID";
36         private static final String[] COLUMNS = new String[] { ID, SIZE, PIZZAS, STATE };
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                         return groupId;
61                 case SIZE:
62                         int groupSize = group.getGroupData().getSize();
63                         return groupSize;
64                 case PIZZAS:
65                         if (group.getGroupData().getOrder() == null)
66                                 return "none";
67                         return pizzaDisplay(group.getGroupData().getOrder().getOrderedPizzas());
68                 case STATE:
69                         return group.getGroupData().getState();
70                 default:
71                         throw new RuntimeException(UNHANDLEDCOLUMN);
72                 }
73         }
74
75         /**
76          * 
77          * @return A display string for PizzaTypes.
78          *         <p />
79          *         e.g. Cardinale, Cardinale, Margherita is displayed as
80          *         "2xCARDINALE,1xMARGHERITA"
81          */
82         private String pizzaDisplay(List<PizzaOrder> list) {
83                 HashMap<PizzaType, Integer> pizzaCount = new HashMap<PizzaType, Integer>();
84                 for (PizzaOrder pizzaOrder : list) {
85                         PizzaType pizzaType = pizzaOrder.getPizzaType();
86                         if (pizzaCount.containsKey(pizzaType)) {
87                                 pizzaCount.put(pizzaType, pizzaCount.get(pizzaType) + 1);
88                         } else
89                                 pizzaCount.put(pizzaType, 1);
90                 }
91                 Set<PizzaType> pizzaTypes = pizzaCount.keySet();
92                 StringBuilder sb = new StringBuilder();
93
94                 boolean multiplePizzas = false;
95                 for (PizzaType pizzaType : pizzaTypes) {
96                         if (multiplePizzas)
97                                 sb.append(", ");
98                         else
99                                 multiplePizzas = true;
100
101                         sb.append(pizzaCount.get(pizzaType) + "x");
102                         sb.append(pizzaType.toString());
103                 }
104
105                 return sb.toString();
106         }
107
108         @Override
109         public void addItems(List<Group> newItems) {
110                 log.info("addItems()");
111                 super.addItems(newItems);
112                 for (Group g : newItems) {
113                         if(Util.useJMS) g.goGrabSomeFood();
114                 }
115         }
116         
117         /**
118          * 
119          * This is necessary as in the space version GroupData objects get written to the space. In order to 
120          * translate from Group data to the actual group object the group object has to be looked up by the id.
121          * 
122          * @param newItems the group data of the corresponding groups
123          */
124         @Deprecated
125         public void addGroupData(List<GroupData> newItems) {
126                 List<Group> groups = new ArrayList<>();
127                 for (GroupData groupData : newItems) {
128                         Group group = items.get(groupData.getId());
129                         group.setGroupData(groupData);
130                         groups.add(group);
131                 }
132                 super.addItems(groups);
133         }
134
135         public void setGroupEating(int groupId) {
136 //              if(stateIs(groupId, GroupState.GONE) || stateIs(groupId, GroupState.PAY)) return;
137                 changeStateOfGroup(groupId, GroupState.EATING);
138                 
139                 fireTableDataChanged();
140         }
141
142         public void setGroupWantsToPay(int groupId) {
143 //              if(stateIs(groupId, GroupState.GONE)) return;
144                 changeStateOfGroup(groupId, GroupState.PAY);
145                 
146                 fireTableDataChanged();
147         }
148
149         private boolean stateIs(int groupId, GroupState state) {
150                 return items.get(groupId).getGroupData().getState() == state;
151         }
152
153
154         public void setGroupHasPaid(int groupId) {
155                 changeStateOfGroup(groupId, GroupState.GONE);
156                 
157                 fireTableDataChanged();
158         }
159         
160         private void changeStateOfGroup(int groupId, GroupState state) {
161                 items.get(groupId).getGroupData().setState(state);
162         }
163
164         public void setGroupsSitting(List<Table> tables) {
165                 synchronized (items) {
166                         for (Table table : tables) {
167                                 changeStateOfGroup(table.getGroupId(), GroupState.SITTING);
168                         }
169                 }
170                 
171                 fireTableDataChanged();
172         }
173
174         public void setOrderTaken(GroupData group) {
175                 changeStateOfGroup(group.getId(), GroupState.ORDERED);
176                 
177                 fireTableDataChanged();
178         }
179
180 }