PojoUtils.java
10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package fi.codecrew.moya.rest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import fi.codecrew.moya.model.*;
import fi.codecrew.moya.rest.pojo.appconfig.v1.ApplicationInstancePojo;
import fi.codecrew.moya.rest.pojo.appconfig.v1.EventPojo;
import fi.codecrew.moya.rest.pojo.appconfig.v1.EventRoot;
import fi.codecrew.moya.rest.pojo.map.v1.MapPojo;
import fi.codecrew.moya.rest.pojo.map.v1.MapRoot;
import fi.codecrew.moya.rest.pojo.map.v1.PlacePojo;
import fi.codecrew.moya.rest.pojo.map.v1.PlaceRoot;
import fi.codecrew.moya.rest.pojo.placemap.v1.PlacemapMapRootPojo;
import fi.codecrew.moya.rest.pojo.placemap.v1.ProductRestPojo;
import fi.codecrew.moya.rest.pojo.placemap.v1.SimplePlacePojo;
import fi.codecrew.moya.rest.pojo.placemap.v1.SimplePlacelistRoot;
import fi.codecrew.moya.rest.pojo.reader.v1.ReaderEventRestPojo;
import fi.codecrew.moya.rest.pojo.reader.v1.ReaderRestPojo;
import fi.codecrew.moya.rest.pojo.userinfo.v1.CardRoot;
import fi.codecrew.moya.rest.pojo.userinfo.v1.EventUserRestPojo;
import fi.codecrew.moya.rest.pojo.userinfo.v1.PrintedCardRestPojo;
import fi.codecrew.moya.rest.pojo.userinfo.v1.SimpleEventuserRoot;
import fi.codecrew.moya.rest.pojo.userinfo.v1.UserReservationPlacePojo;
import fi.codecrew.moya.rest.pojo.util.v1.ErrorRoot;
public class PojoUtils {
public static EventUserRestPojo initEventUserRestPojo(EventUser user)
{
EventUserRestPojo ret = new EventUserRestPojo();
ret.setNick(user.getUser().getNick());
ret.setLogin(user.getUser().getLogin());
ret.setEventuserId(user.getId());
ret.setUserId(user.getUser().getId());
ret.setFirstname(user.getUser().getFirstnames());
ret.setLastname(user.getUser().getLastname());
return ret;
}
public static SimpleEventuserRoot parseEventusers(List<EventUser> users) {
ArrayList<EventUserRestPojo> list = new ArrayList<>();
for (EventUser u : users) {
list.add(initEventUserRestPojo(u));
}
return new SimpleEventuserRoot(list);
}
public static PrintedCardRestPojo initPrintedCardRestPojo(PrintedCard card)
{
PrintedCardRestPojo ret = new PrintedCardRestPojo();
ret.setEventuserId(card.getUser().getId());
ret.setId(card.getId());
if (card.getTemplate() != null)
ret.setTemplate(card.getTemplate().getName());
ret.setUsername(card.getUser().getNick());
ret.setWholeName(card.getUser().getWholeName());
ret.setState(card.getCardState().toString());
if (card.getPrintTime() != null)
ret.setPrintTime(card.getPrintTime().getTime());
return ret;
}
public static CardRoot parsePrintedCards(List<PrintedCard> cards)
{
ArrayList<PrintedCardRestPojo> ret = new ArrayList<PrintedCardRestPojo>();
for (PrintedCard c : cards) {
ret.add(initPrintedCardRestPojo(c));
}
CardRoot root = new CardRoot();
root.setCards(ret);
return root;
}
public static PlacePojo initPlacePojo(Place place) {
PlacePojo ret = new PlacePojo();
if (place.getProduct() != null)
ret.setProductId(place.getProduct().getId());
if (place.getPlaceReserver() != null && place.getPlaceReserver().getPlaceGroup() != null && place.getPlaceReserver().getPlaceGroup().getCreator() != null)
ret.setReserverId(place.getPlaceReserver().getPlaceGroup().getCreator().getId());
ret.setId(place.getId());
ret.setDescription(place.getDescription());
ret.setName(place.getName());
ret.setMapX(place.getMapX());
ret.setMapY(place.getMapY());
ret.setDetails(place.getDetails());
ret.setCode(place.getCode());
ret.setHeight(place.getHeight());
ret.setWidth(place.getWidth());
ret.setTaken(place.isTaken());
ret.setBuyable(place.isBuyable());
ret.setDisabled(place.isDisabled());
// I cannot change REST -api without making new version, so let's simulate this ReleaseTime -feature from reserveTime
Calendar relTime = Calendar.getInstance();
relTime.add(relTime.HOUR, 4);
if(place.getReserveTime() != null)
ret.setReleaseTime(relTime);
else
ret.setReleaseTime(null);
if (place.getMap() != null) {
ret.setMapId(place.getMap().getId());
}
if (place.getPlaceReserver() != null && place.getPlaceReserver().getUser() != null) {
ret.setEventuserId(place.getPlaceReserver().getUser().getId());
}
return ret;
}
public static PlaceRoot initPlaceRoot(EventMap map) {
PlaceRoot ret = new PlaceRoot();
ret.setMap(initMapPojo(map));
ret.setPlaces(parsePlaces(map.getPlaces()));
return ret;
}
private static List<PlacePojo> parsePlaces(List<Place> places) {
ArrayList<PlacePojo> ret = new ArrayList<PlacePojo>();
for (Place place : places) {
ret.add(initPlacePojo(place));
}
return ret;
}
private static MapPojo initMapPojo(EventMap map) {
MapPojo ret = new MapPojo();
ret.setId(map.getId());
ret.setName(map.getName());
ret.setActive(map.isActive());
return ret;
}
public static MapRoot parseMaps(List<EventMap> eventMaps) {
List<MapPojo> ret = new ArrayList<>();
for (EventMap m : eventMaps) {
ret.add(initMapPojo(m));
}
return new MapRoot(ret);
}
public static ReaderEventRestPojo initReaderEventRestPojo(ReaderEvent event) {
ReaderEventRestPojo ret = new ReaderEventRestPojo();
if ( event.getPrintedCard() != null) {
if (event.getPrintedCard().getUser() != null) {
ret.setEventUser(PojoUtils.initEventUserRestPojo(event.getPrintedCard().getUser()));
}
} else if (event.getUser() != null) {
ret.setEventUser(PojoUtils.initEventUserRestPojo(event.getUser()));
}
ret.setReaderEventId(event.getId());
ret.setReaderEventTime(event.getUpdatetime());
ret.setReaderId(event.getReader().getId());
if (event.getPrintedCard() != null) {
ret.setPrintedCardId(event.getPrintedCard().getId());
}
if (event.getPrintedCard() != null) {
ret.setPrintedCardState(event.getPrintedCard().getCardState().name());
}
return ret;
}
public static List<ReaderEventRestPojo> parseReaderEvents(List<ReaderEvent> readers) {
List<ReaderEventRestPojo> ret = new ArrayList<>();
for (ReaderEvent re : readers) {
ret.add(initReaderEventRestPojo(re));
}
return ret;
}
public static ReaderRestPojo initReaderRestPojo(Reader reader)
{
ReaderRestPojo ret = new ReaderRestPojo();
ret.setReaderId(reader.getId());
ret.setIdentification(reader.getIdentification());
ret.setDescription(reader.getDescription());
String rt = null;
if (reader.getType() != null) {
rt = reader.getType().name();
}
ret.setReaderType(rt);
return ret;
}
public static List<ReaderRestPojo> parseReaders(List<Reader> readers) {
ArrayList<ReaderRestPojo> ret = new ArrayList<>();
for (Reader r : readers) {
ret.add(initReaderRestPojo(r));
}
return ret;
}
public static List<ProductRestPojo> parseProducts(List<Product> prods) {
ArrayList<ProductRestPojo> ret = new ArrayList<ProductRestPojo>();
for (Product p : prods) {
ret.add(initProductRestPojo(p));
}
return ret;
}
public static ProductRestPojo initProductRestPojo(Product product)
{
ProductRestPojo ret = new ProductRestPojo();
ret.setId(product.getId());
ret.setName(product.getName());
ret.setDescription(product.getDescription());
ret.setPrice(product.getPrice().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
return ret;
}
public static SimplePlacelistRoot parseSimplePlaces(List<Place> places, EventUser user, boolean hasPermissionViewAllusers) {
return parseSimplePlaces(places, user, hasPermissionViewAllusers, false);
}
public static SimplePlacelistRoot parseSimplePlaces(List<Place> places, EventUser user, boolean hasPermissionViewAllusers, boolean onlyHilightPlaces)
{
SimplePlacelistRoot ret = new SimplePlacelistRoot();
ArrayList<SimplePlacePojo> placeList = new ArrayList<>();
ret.setPlaces(placeList);
for (Place p : places) {
placeList.add(initSimplePlacePojo(p, user, hasPermissionViewAllusers, onlyHilightPlaces));
}
return ret;
}
private static SimplePlacePojo initSimplePlacePojo(Place p, EventUser user, boolean hasPermissionViewAllusers, boolean onlyHilightPlaces) {
SimplePlacePojo ret = new SimplePlacePojo();
ret.setId(p.getId());
ret.setName(p.getName());
String state = null;
if(hasPermissionViewAllusers) {
if(p.getPlaceReserver() != null) {
if(p.getPlaceReserver().getUser() != null) {
ret.setUserDescription(p.getPlaceReserver().getUser().getUser().getShortUserDescriptor());
} else if(p.getPlaceReserver().getPlaceGroup() != null && p.getPlaceReserver().getPlaceGroup().getCreator() != null) {
ret.setUserDescription(p.getPlaceReserver().getPlaceGroup().getCreator().getUser().getShortUserDescriptor());
}
}
}
switch (p.getState(user))
{
case DISABLED:
state = (onlyHilightPlaces)?"F":"D";
break;
case FREE:
state = "F";
break;
case LOCKED:
state = (onlyHilightPlaces)?"F":"L";
break;
case MY_PLACE:
state = "P";
break;
case RESERVED:
state = (onlyHilightPlaces)?"F":"R";
break;
case TEMP_RESERVED_FORME:
state = (onlyHilightPlaces)?"F":"T";
break;
default:
break;
}
if(onlyHilightPlaces) {
}
ret.setState(state);
ret.setX(p.getMapX());
ret.setY(p.getMapY());
ret.setWidth(p.getWidth());
ret.setHeight(p.getHeight());
return ret;
}
public static PlacemapMapRootPojo.MapPojo initPlacemapMapPojo(EventMap map) {
PlacemapMapRootPojo.MapPojo ret = new PlacemapMapRootPojo.MapPojo();
ret.setId(map.getId());
ret.setName(map.getName());
ret.setWidth(map.getWidth());
ret.setHeight(map.getHeight());
return ret;
}
public static UserReservationPlacePojo initUserReservationPlace(GroupMembership g) {
UserReservationPlacePojo ur = new UserReservationPlacePojo();
ur.setPlacegiven(g.getEnteredEvent() != null);
Place place = g.getPlaceReservation();
if (place != null) {
ur.setPlaceid(place.getId());
ur.setPlacename(place.getName());
ur.setProductId(place.getProduct().getId());
ur.setProductName(place.getProduct().getName());
}
return ur;
}
public static EventRoot parseEvents(List<LanEvent> events) {
EventRoot root = new EventRoot();
ArrayList<EventPojo> eventPojos = new ArrayList<>();
for(LanEvent event : events) {
ArrayList<String> urls = new ArrayList<>();
for(LanEventDomain domain : event.getDomains()) {
urls.add(domain.getDomain());
}
EventPojo pojo = new EventPojo();
pojo.setName(event.getName());
pojo.setLanEventId(event.getId());
pojo.setUrls(urls);
pojo.setStartTime(event.getStartTime());
eventPojos.add(pojo);
}
root.setEvents(eventPojos);
return root;
}
public static ApplicationInstancePojo parseApplicationInstance(ApiApplicationInstance instance) {
ApplicationInstancePojo pojo = new ApplicationInstancePojo();
pojo.setName(instance.getName());
pojo.setAuthname(instance.getAuthname());
pojo.setSecretKey(instance.getSecretKey());
return pojo;
}
public static ErrorRoot initErrorPojo(String errorMessage) {
ErrorRoot errorRoot = new ErrorRoot();
errorRoot.setError(errorMessage);
return errorRoot;
}
}