PrinterRestView.java
3.54 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
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;
}
}