BarcodeBean.java
2.92 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.beans;
import java.io.IOException;
import java.io.InputStream;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.codecrew.moya.facade.PrintedCardFacade;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.PrintedCard;
import fi.codecrew.moya.model.User;
import fi.codecrew.moya.utilities.BarcodeUtils;
/**
* Session Bean implementation class BarcodeBean
*/
@Stateless
@LocalBean
public class BarcodeBean implements BarcodeBeanLocal {
private static final String PRINTED_CARD_PREFIX = "277"; //2M
private static final String EVENTUSER_PREFIX = "279"; //2O
//private static final String NEXT_PREFIX = "289"; //2Y
//private static final String NEXT_PREFIX = "265"; //2A
private static final Logger logger = LoggerFactory.getLogger(BarcodeBean.class);
@EJB
PrintedCardFacade printedCardFacade;
@EJB
UserBeanLocal userBean;
/**
* Default constructor.
*/
public BarcodeBean() {
// TODO Auto-generated constructor stub
}
public InputStream getUserBarcode(EventUser user) throws IOException {
StringBuilder sb = new StringBuilder();
sb.append(EVENTUSER_PREFIX);
String idStr = user.getId().toString();
for (int i = 12 - idStr.length() - sb.length(); i > 0; --i) {
sb.append("0");
}
sb.append(idStr);
String barcode = sb.toString();
logger.debug("Geneating barcode for user {} : {}", user, barcode);
return BarcodeUtils.getBarcodeEAN(barcode);
}
public InputStream getCardBarcode(PrintedCard printedCard) throws IOException {
StringBuilder sb = new StringBuilder();
sb.append(PRINTED_CARD_PREFIX);
String idStr = printedCard.getId().toString();
for (int i = 12 - idStr.length() - sb.length(); i > 0; --i) {
sb.append("0");
}
sb.append(idStr);
String barcode = sb.toString();
logger.debug("Geneating barcode for card {} : {}", printedCard, barcode);
return BarcodeUtils.getBarcodeEAN(barcode);
}
public PrintedCard getPrintedCard(String barcode) {
if(barcode == null || barcode.isEmpty())
return null;
// it's our special front barcode
try {
if (barcode.startsWith(PRINTED_CARD_PREFIX)) {
int id = Integer.parseInt(barcode.substring(3, barcode.length()-1));
PrintedCard card = printedCardFacade.find(id);
if(card != null)
return card;
}
} catch (NumberFormatException x) {
}
return printedCardFacade.findByBarcode(barcode);
}
public EventUser getUser(String barcode) {
if(barcode == null || barcode.isEmpty())
return null;
try {
// it's our special front barcode
if (barcode.startsWith(EVENTUSER_PREFIX)) {
int id = Integer.parseInt(barcode.substring(3, barcode.length()-1 ));
logger.debug("finding user with barcode {} and id {}", barcode, id);
EventUser user = userBean.findByEventUserId(id);
return user;
}
} catch (NumberFormatException x) {
}
return null;
}
}