MapAdminView.java 5.78 KB
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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.PlaceMapBeanLocal;
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.MapRoot;
import fi.codecrew.moya.rest.pojo.PlaceInputPojo;
import fi.codecrew.moya.rest.pojo.PlaceOutPojo;
import fi.codecrew.moya.rest.pojo.PlaceRoot;

@RequestScoped
@Path("/placeadmin")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON })
public class MapAdminView {

	@EJB
	private EventMapBeanLocal eventmapbean;
	@EJB
	private PlaceMapBeanLocal placemapbean;
	@EJB
	private PlaceGroupBeanLocal pgbean;
	@EJB
	private EventBeanLocal eventbean;
	@EJB
	private ProductBeanLocal productbean;
	@EJB
	private UserBeanLocal eventuserbean;
	@EJB
	private transient PlaceBeanLocal placebean;

	private static final Logger logger = LoggerFactory.getLogger(MapAdminView.class);

	@GET
	@Path("/background/{mapId}")
	public Response getMapBg(@PathParam("mapId") Integer mapid)
	{
		EventMap map = placemapbean.findMap(mapid);
		ByteArrayInputStream istream = new ByteArrayInputStream(map.getMapData());
		return Response.ok().entity(istream).type("image/jpeg").build();
	}

	@GET
	@Path("/maps")
	public MapRoot getAllMaps() {
		MapRoot ret = MapRoot.parseMaps(eventbean.getCurrentEvent().getEventMaps());
		logger.info("getallmaps called! {}", ret);
		return ret;
	}

	@GET
	@Path("/places/{mapId}")
	public PlaceRoot getPlaces(@PathParam("mapId") Integer mapid) {
		EventMap map = placemapbean.findMap(mapid);
		PlaceRoot ret = new PlaceRoot(map);
		logger.info("returning map {} entity {}", mapid, ret);
		return ret;
	}

	@DELETE
	@Path("/place/{id}")
	public Response deletePlace(@PathParam("id") Integer id) {
		Place place = eventmapbean.findPlace(id);
		eventmapbean.deletePlace(place);
		return Response.ok().build();
	}

	@POST
	@Path("/place/")
	public Response createPlace(PlaceInputPojo create) {
		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);
		setPlaceValues(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(new PlaceOutPojo(place)).build();
	}

	@PUT
	@Path("/place/{id}")
	public Response updatePlace(@PathParam("id") Integer id, PlaceInputPojo update) {
		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();
		}
		setPlaceValues(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(new PlaceOutPojo(place)).build();
	}

	@PUT
	@Path("/place/{placeId}/release")
	public Response releasePlace(@PathParam("placeId") Integer placeId) {
		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();
	}

	@PUT
	@Path("/place/{placeId}/reserve/{eventuserId}")
	public Response reserve(@PathParam("placeId") Integer placeId, @PathParam("eventuserId") Integer eventuserId) {
		Place place = eventmapbean.findPlace(placeId);
		EventUser eventuser = eventuserbean.findByEventUserId(eventuserId);
		if (placebean.reservePlace(place, eventuser))
		{
			try {
				placebean.buySelectedPlaces(eventuser);
				return Response.ok().build();
			} catch (BortalCatchableException e) {
				logger.warn("Wtf! Bortalerror");
			}
		}
		return Response.serverError().build();
	}

	private void setPlaceValues(Place place, PlaceInputPojo 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);
			}
		}
	}

}