]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java
[XVSM] Delivery in progress, Delivery done updates in UIs, Delivery logic
[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 at.ac.tuwien.sbc.valesriegler.common.TableModel;
4 import at.ac.tuwien.sbc.valesriegler.common.Util;
5 import at.ac.tuwien.sbc.valesriegler.group.Group;
6 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
7 import at.ac.tuwien.sbc.valesriegler.types.GroupState;
8 import at.ac.tuwien.sbc.valesriegler.types.Table;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
15 /**
16  * Might not the nicest way to store global data, but this is an elemental
17  * Object, storing ALL the Group's data. Might consider splitting those two
18  * usecases apart.
19  * 
20  * @author jan
21  * 
22  */
23 public class GroupOverviewModel extends TableModel<Group> {
24         private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class);
25
26         private static final String STATE = "State";
27         private static final String PIZZAS = "Pizzas";
28         private static final String SIZE = "Size";
29         private static final String ID = "ID";
30         private static final String PIZZERIA = "Pizzeria";
31         private static final String[] COLUMNS = new String[] { ID, SIZE, PIZZAS, STATE, PIZZERIA };
32
33
34         @Override
35         protected String[] getColumns() {
36                 return COLUMNS;
37         }
38
39         public List<Group> getItems() {
40                 return new ArrayList<>(items.values());
41         }
42
43         public Group getGroupByID(int id) {
44                 return items.get(id);
45         }
46
47         @Override
48         public Object getValueAt(int rowIndex, int columnIndex) {
49                 List<Group> values = new ArrayList<>(items.values());
50                 Group group = values.get(rowIndex);
51                 String wantedColumn = COLUMNS[columnIndex];
52                 switch (wantedColumn) {
53                 case ID:
54                         int groupId = group.getGroupData().getId();
55                         return String.format(Util.NUMBER_DISPLAY_FORMAT, groupId);
56                 case SIZE:
57                         int groupSize = group.getGroupData().getSize();
58                         return groupSize;
59                 case PIZZAS:
60                         if (group.getGroupData().getOrder() == null)
61                                 return "none";
62                         return Util.pizzaDisplay(group.getGroupData().getOrder().getOrderedPizzas());
63                 case STATE:
64                         return group.getGroupData().getState();
65         case PIZZERIA:
66             return group.getGroupData().getPizzeriaId();
67                 default:
68                         throw new RuntimeException(UNHANDLEDCOLUMN);
69                 }
70         }
71
72     @Override
73         public void addItems(List<Group> newItems) {
74                 log.info("addItems()");
75                 super.addItems(newItems);
76                 for (Group g : newItems) {
77                         if(Util.useJMS) g.goGrabSomeFood();
78                 }
79         }
80         
81         /**
82          * 
83          * This is necessary as in the space version GroupData objects get written to the space. In order to 
84          * translate from Group data to the actual group object the group object has to be looked up by the id.
85          * 
86          * @param newItems the group data of the corresponding groups
87          */
88         @Deprecated
89         public void addGroupData(List<GroupData> newItems) {
90                 List<Group> groups = new ArrayList<>();
91                 for (GroupData groupData : newItems) {
92                         Group group = items.get(groupData.getId());
93                         group.setGroupData(groupData);
94                         groups.add(group);
95                 }
96                 super.addItems(groups);
97         }
98
99         public void setGroupEating(int groupId) {
100 //              if(stateIs(groupId, GroupState.GONE) || stateIs(groupId, GroupState.PAY)) return;
101                 changeStateOfGroup(groupId, GroupState.EATING);
102                 
103                 fireTableDataChanged();
104         }
105
106         public void setGroupWantsToPay(int groupId) {
107 //              if(stateIs(groupId, GroupState.GONE)) return;
108                 changeStateOfGroup(groupId, GroupState.PAY);
109                 
110                 fireTableDataChanged();
111         }
112
113         private boolean stateIs(int groupId, GroupState state) {
114                 return items.get(groupId).getGroupData().getState() == state;
115         }
116
117
118         public void setGroupHasPaid(int groupId) {
119                 changeStateOfGroup(groupId, GroupState.GONE);
120                 
121                 fireTableDataChanged();
122         }
123         
124         private void changeStateOfGroup(int groupId, GroupState state) {
125                 items.get(groupId).getGroupData().setState(state);
126         }
127
128         public void setGroupsSitting(List<Table> tables) {
129                 synchronized (items) {
130                         for (Table table : tables) {
131                                 changeStateOfGroup(table.getGroupId(), GroupState.SITTING);
132                         }
133                 }
134                 
135                 fireTableDataChanged();
136         }
137
138         public void setOrderTaken(GroupData group) {
139                 changeStateOfGroup(group.getId(), GroupState.ORDERED);
140                 
141                 fireTableDataChanged();
142         }
143
144 }