]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupOverviewModel.java
Create DeliveryGroup, DeliveryGroupData, DeliveryStatus. Add Deliveries overview...
[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.List;
5
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8
9 import at.ac.tuwien.sbc.valesriegler.common.TableModel;
10 import at.ac.tuwien.sbc.valesriegler.common.Util;
11 import at.ac.tuwien.sbc.valesriegler.group.Group;
12 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
13 import at.ac.tuwien.sbc.valesriegler.types.GroupState;
14 import at.ac.tuwien.sbc.valesriegler.types.Table;
15
16 /**
17  * Might not the nicest way to store global data, but this is an elemental
18  * Object, storing ALL the Group's data. Might consider splitting those two
19  * usecases apart.
20  * 
21  * @author jan
22  * 
23  */
24 public class GroupOverviewModel extends TableModel<Group> {
25         private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class);
26
27         private static final String STATE = "State";
28         private static final String PIZZAS = "Pizzas";
29         private static final String SIZE = "Size";
30         private static final String ID = "ID";
31         private static final String[] COLUMNS = new String[] { ID, SIZE, PIZZAS, STATE };
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 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                 default:
66                         throw new RuntimeException(UNHANDLEDCOLUMN);
67                 }
68         }
69
70     @Override
71         public void addItems(List<Group> newItems) {
72                 log.info("addItems()");
73                 super.addItems(newItems);
74                 for (Group g : newItems) {
75                         if(Util.useJMS) g.goGrabSomeFood();
76                 }
77         }
78         
79         /**
80          * 
81          * This is necessary as in the space version GroupData objects get written to the space. In order to 
82          * translate from Group data to the actual group object the group object has to be looked up by the id.
83          * 
84          * @param newItems the group data of the corresponding groups
85          */
86         @Deprecated
87         public void addGroupData(List<GroupData> newItems) {
88                 List<Group> groups = new ArrayList<>();
89                 for (GroupData groupData : newItems) {
90                         Group group = items.get(groupData.getId());
91                         group.setGroupData(groupData);
92                         groups.add(group);
93                 }
94                 super.addItems(groups);
95         }
96
97         public void setGroupEating(int groupId) {
98 //              if(stateIs(groupId, GroupState.GONE) || stateIs(groupId, GroupState.PAY)) return;
99                 changeStateOfGroup(groupId, GroupState.EATING);
100                 
101                 fireTableDataChanged();
102         }
103
104         public void setGroupWantsToPay(int groupId) {
105 //              if(stateIs(groupId, GroupState.GONE)) return;
106                 changeStateOfGroup(groupId, GroupState.PAY);
107                 
108                 fireTableDataChanged();
109         }
110
111         private boolean stateIs(int groupId, GroupState state) {
112                 return items.get(groupId).getGroupData().getState() == state;
113         }
114
115
116         public void setGroupHasPaid(int groupId) {
117                 changeStateOfGroup(groupId, GroupState.GONE);
118                 
119                 fireTableDataChanged();
120         }
121         
122         private void changeStateOfGroup(int groupId, GroupState state) {
123                 items.get(groupId).getGroupData().setState(state);
124         }
125
126         public void setGroupsSitting(List<Table> tables) {
127                 synchronized (items) {
128                         for (Table table : tables) {
129                                 changeStateOfGroup(table.getGroupId(), GroupState.SITTING);
130                         }
131                 }
132                 
133                 fireTableDataChanged();
134         }
135
136         public void setOrderTaken(GroupData group) {
137                 changeStateOfGroup(group.getId(), GroupState.ORDERED);
138                 
139                 fireTableDataChanged();
140         }
141
142 }