MapAdminView.java
8.82 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
/*
* Copyright Codecrew Ry
*
* All rights reserved.
*
* This license applies to any software containing a notice placed by the
* copyright holder. Such software is herein referred to as the Software.
* This license covers modification, distribution and use of the Software.
*
* Any distribution and use in source and binary forms, with or without
* modification is not permitted without explicit written permission from the
* copyright owner.
*
* A non-exclusive royalty-free right is granted to the copyright owner of the
* Software to use, modify and distribute all modifications to the Software in
* future versions of the Software.
*
*/
package fi.codecrew.moya.rest;
import java.io.ByteArrayInputStream;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import fi.codecrew.moya.beans.EventBeanLocal;
import fi.codecrew.moya.beans.EventMapBeanLocal;
import fi.codecrew.moya.beans.PlaceBeanLocal;
import fi.codecrew.moya.beans.PlaceGroupBeanLocal;
import fi.codecrew.moya.beans.ProductBeanLocal;
import fi.codecrew.moya.beans.UserBeanLocal;
import fi.codecrew.moya.exceptions.BortalCatchableException;
import fi.codecrew.moya.model.EventMap;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.Place;
import fi.codecrew.moya.model.Product;
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;
@RequestScoped
@Path("/placeadmin")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON })
@Api(value = "/placeadmin", description = "Administer places")
public class MapAdminView {
@EJB
private EventMapBeanLocal eventmapbean;
@EJB
private PlaceGroupBeanLocal pgbean;
@EJB
private EventBeanLocal eventbean;
@EJB
private ProductBeanLocal productbean;
@EJB
private UserBeanLocal eventuserbean;
@EJB
private PlaceBeanLocal placebean;
private static final Logger logger = LoggerFactory.getLogger(MapAdminView.class);
@GET
@Path("/background/{mapId}")
@ApiResponse(code = 200, message = "Return backround map as JPEG")
public Response getMapBg(@PathParam("mapId") Integer mapid) {
try {
EventMap map = placebean.findMap(mapid);
ByteArrayInputStream istream = new ByteArrayInputStream(map.getMapData());
return Response.ok().entity(istream).type("image/jpeg").build();
} catch (Exception e) {
logger.error("GET /placeadmin/background/{mapId} threw an exception", e);
throw e;
}
}
@GET
@Path("/maps")
@ApiOperation(value = "Get All maps", response = MapRoot.class)
@ApiResponse(code = 200, message = "Return all maps")
public MapRoot getAllMaps() {
try {
MapRoot ret = PojoUtils.parseMaps(eventbean.getCurrentEvent().getEventMaps());
logger.info("getallmaps called! {}", ret);
return ret;
} catch (Exception e) {
logger.error("GET /placeadmin/maps threw an exception", e);
throw e;
}
}
@GET
@Path("/places/{mapId}")
@ApiOperation(value = "Get places by mapId", response = PlaceRoot.class)
public PlaceRoot getPlaces(@PathParam("mapId") Integer mapid) {
try {
EventMap map = placebean.findMap(mapid);
PlaceRoot ret = PojoUtils.initPlaceRoot(map);
logger.info("returning map {} entity {}", mapid, ret);
return ret;
} catch (Exception e) {
logger.error("PUT /placeadmin/places/{mapId} threw an exception", e);
throw e;
}
}
@DELETE
@Path("/place/{id}")
@ApiOperation("Delete a place")
public Response deletePlace(@PathParam("id") Integer id) {
try {
Place place = eventmapbean.findPlace(id);
if (place == null) {
return Response.status(Status.NOT_FOUND).entity("Place not found for id " + id).build();
}
eventmapbean.deletePlace(place);
return Response.ok().build();
} catch (Exception e) {
logger.error("DELETE /placeadmin/place/{id} threw an exception", e);
return Response.serverError().entity("Got exception when removing place: " + e.getMessage()).build();
}
}
@GET
@Path("/place/")
@ApiOperation(value = "Always returns 406 - Not acceptable")
@ApiResponse(code = 406, message = "Nope. Not acceptable.")
public Response getPlaceError() {
try {
ResponseBuilder ret = Response.ok();
ret = ret.status(Status.NOT_ACCEPTABLE);
ret.entity("Method not allowed!\nGET is not supported for /place/. See api for correct use!");
return ret.build();
} catch (Exception e) {
logger.error("GET /placeadmin/place/ threw an exception", e);
throw e;
}
}
@POST
@Path("/place/")
@ApiOperation(value = "Create a place", response = PlacePojo.class)
public Response createPlace(PlacePojo create) {
try {
logger.info("Finding map with id {}", create.getMapId());
EventMap map = eventmapbean.find(create.getMapId());
if (map == null) {
logger.warn("No map found!");
return Response.serverError().entity("Error with mapId").build();
}
Place place = new Place();
place.setMap(map);
setUpdatablePlaceValues(place, create);
if (create.getProductId() != null && (place.getProduct() == null || !place.getProduct().getId().equals(create.getProductId())))
return Response.serverError().entity("Product id unknown!").build();
logger.info("Creating new place {}", place.getName());
eventmapbean.createPlace(place);
return Response.ok().entity(PojoUtils.initPlacePojo(place)).build();
} catch (Exception e) {
logger.error("POST /placeadmin/place/ threw an exception", e);
throw e;
}
}
@PUT
@Path("/place/{id}")
@ApiOperation(value = "Set a place", response = PlacePojo.class)
public Response updatePlace(@PathParam("id") Integer id, PlacePojo update) {
try {
if (update == null || id == null) {
return Response.serverError().entity("'id' field is required!").build();
}
Place place = eventmapbean.findPlace(id);
if (place == null) {
return Response.serverError().entity("place not found with id: " + id).build();
}
setUpdatablePlaceValues(place, update);
if (update.getProductId() != null && (place.getProduct() == null || !place.getProduct().getId().equals(update.getProductId())))
return Response.serverError().entity("Product id unknown!").build();
place = eventmapbean.updatePlace(place);
return Response.ok(PojoUtils.initPlacePojo(place)).build();
} catch (Exception e) {
logger.error("PUT /placeadmin/place/{id} threw an exception", e);
throw e;
}
}
@PUT
@Path("/place/{placeId}/release")
@ApiOperation("Release a class")
public Response releasePlace(@PathParam("placeId") Integer placeId) {
try {
Place place = eventmapbean.findPlace(placeId);
if (place == null) {
return Response.serverError().entity("Place not found!").build();
}
logger.info("Releasing place {}", place);
placebean.unbuyPlace(place);
return Response.ok().build();
} catch (Exception e) {
logger.error("PUT /placeadmin/place/{placeId}/release threw an exception", e);
throw e;
}
}
@PUT
@Path("/place/{placeId}/reserve/{eventuserId}")
@ApiOperation("Reserve a place")
public Response reserve(@PathParam("placeId") Integer placeId, @PathParam("eventuserId") Integer eventuserId) {
try {
Place place = eventmapbean.findPlace(placeId);
EventUser eventuser = eventuserbean.findByEventUserId(eventuserId);
if (placebean.reservePlace(place, eventuser)) {
try {
placebean.reserveSelectedPlaces(eventuser);
return Response.ok().build();
} catch (BortalCatchableException e) {
logger.warn("Wtf! Bortalerror", e);
}
}
return Response.serverError().build();
} catch (Exception e) {
logger.error("PUT /placeadmin/place/{placeId}/reserve/{eventuserId} threw an exception", e);
throw e;
}
}
private void setUpdatablePlaceValues(Place place, PlacePojo update) {
if (update.getName() != null)
place.setName(update.getName());
if (update.getMapX() != null)
place.setMapX(update.getMapX());
if (update.getMapY() != null)
place.setMapY(update.getMapY());
if (update.getWidth() != null)
place.setWidth(update.getWidth());
if (update.getHeight() != null)
place.setHeight(update.getHeight());
if (update.getBuyable() != null)
place.setBuyable(update.getBuyable());
if (update.getDisabled() != null)
place.setDisabled(update.getDisabled());
if (update.getProductId() != null) {
Product product = productbean.findById(update.getProductId());
if (product != null) {
place.setProduct(product);
}
}
}
}