PrinterRestView.java 3.54 KB
package fi.codecrew.moya.rest;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fi.codecrew.moya.beans.CardPrintBeanLocal;
import fi.codecrew.moya.beans.CardTemplateBeanLocal;
import fi.codecrew.moya.beans.UserBeanLocal;
import fi.codecrew.moya.enums.CardState;
import fi.codecrew.moya.model.PrintedCard;
import fi.codecrew.moya.rest.pojo.CardRoot;
import fi.codecrew.moya.rest.pojo.PrintedCardRestPojo;

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

	@EJB
	private CardTemplateBeanLocal cardbean;
	@EJB
	private CardPrintBeanLocal printbean;
	@EJB
	private UserBeanLocal userbean;
	private static final Logger logger = LoggerFactory.getLogger(PrinterRestView.class);

	@GET
	@Path("/Reserve/{id}/")
	public Response reserveCardForPrinting(@PathParam("id") int cardId, @QueryParam("key") String hash) throws Exception {

		ResponseBuilder ret = null;
		try {
			PrintedCard card = cardbean.setCardState(cardId, CardState.PRINTING_IN_PROGRESS);
			if (card.getCardState().equals(CardState.PRINTING_IN_PROGRESS)) {
				ret = Response.ok(new PrintedCardRestPojo(card));
			}
		} catch (Exception e) {
			ret = null;
			logger.warn("Caught exception while reserving card for printing" + cardId, e);
		}

		if (ret == null) {
			ret = Response.status(Response.Status.CONFLICT);
		}
		return ret.build();
	}

	@GET
	@Path("/Printed/{id}/")
	public PrintedCardRestPojo setCardPrinted(@PathParam("id") int cardId, @QueryParam("key") String hash) throws Exception {
		return new PrintedCardRestPojo(cardbean.setCardState(cardId, CardState.PRINTED));
		// PrintedCard card = cardbean.findCard(cardId);
		// card.setCardState(CardState.PRINTED);
		// card.setPrintCount(card.getPrintCount() + 1);
		// card = cardbean.saveCard(card);
		// return new PrintedCardRestPojo(card);
	}

	@GET
	@Path("/Get/{id}/")
	public PrintedCardRestPojo getCard(@PathParam("id") int cardId, @QueryParam("key") String hash) throws Exception {
		PrintedCard card = cardbean.findCard(cardId);
		return new PrintedCardRestPojo(card);
	}

	@GET
	@Path("/GetImage/{id}/")
	public Response getUserCard(@PathParam("id") int cardId, @QueryParam("key") String hash) throws Exception {
		ResponseBuilder ret = null;
		PrintedCard card = null;
		try {

			card = cardbean.findCard(cardId);
			ret = Response.ok(printbean.constructPNG(card), "image/png");
		} catch (Throwable t) {
			card.setCardState(CardState.DATA_MISSING);
			cardbean.saveCard(card);
			logger.warn("Error creating card for user cardId " + cardId, t);
			ret = Response.status(Response.Status.NOT_ACCEPTABLE);

		}
		return ret.build();
	}

	@GET
	@Path("/ListUnprinted")
	public CardRoot getUserCard(@QueryParam("key") String key) throws Exception {
		CardRoot ret = PrintedCardRestPojo.parseCards(cardbean.getCardsByState(CardState.VALIDATED));
		logger.info("Returning card pojos: {} for key {}", ret, key);
		return ret;
	}

	@GET
	@Path("/ListAll")
	public CardRoot getAllCards(@QueryParam("key") String key) throws Exception {
		CardRoot ret = PrintedCardRestPojo.parseCards(cardbean.getCardsByState());
		logger.info("Returning card pojos: {} for key {}", ret, key);
		return ret;
	}
}