BarcodeBean.java
5.32 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
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.PlaceFacade;
import fi.codecrew.moya.facade.PrintedCardFacade;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.Place;
import fi.codecrew.moya.model.PrintedCard;
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 PLACE_PREFIX = "289"; //2Y
//private static final String NEXT_PREFIX = "265"; //2A
private static final Logger logger = LoggerFactory.getLogger(BarcodeBean.class);
@EJB
PrintedCardFacade printedCardFacade;
@EJB
PlaceFacade placeFacade;
@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 String getPlaceHexcode(Place place) {
StringBuilder sb = new StringBuilder();
sb.append(PLACE_PREFIX);
String idStr = place.getId().toString();
for (int i = 12 - idStr.length() - sb.length(); i > 0; --i) {
sb.append("0");
}
sb.append(idStr);
String barcode = sb.toString();
String hexcode = Integer.toHexString(Integer.parseInt(barcode));
logger.debug("Geneating hexcode for place {} : {}", place.getId(), hexcode);
return hexcode;
}
public String getVrAuthCodeForCard(PrintedCard printedCard) {
StringBuilder sb = new StringBuilder();
sb.append(PRINTED_CARD_PREFIX);
if(printedCard == null)
return "";
String idStr = printedCard.getId().toString();
for (int i = 8 - idStr.length() - sb.length(); i > 0; --i) {
sb.append("0");
}
sb.append(idStr);
String code = sb.toString();
String hexcode = Integer.toHexString(Integer.parseInt(code));
String checksum = Integer.toHexString(hexcode.hashCode());
checksum = checksum.substring(checksum.length() -3);
hexcode += checksum;
//hexcode = Integer.toString(hexcode.hashCode());
logger.debug("Geneating VrAuthcode for card {} : {}", printedCard.getId(), hexcode);
return hexcode;
}
public String checkVrAuthCode(String code) {
String checksumNew = code.substring(code.length() -3);
code = code.substring(0, (code.length()-3));
String checksumOld = Integer.toHexString(code.hashCode());
checksumOld = checksumOld.substring(checksumOld.length() -3);
if(checksumNew == checksumOld) {
int decimal = Integer.decode(code);
code = Integer.toString(decimal);
String prefix = code.substring(0, 2);
int id = Integer.parseInt(code.substring(3, code.length() -1));
if(prefix == PRINTED_CARD_PREFIX && id != 0) {
PrintedCard printedCard = printedCardFacade.find(id);
if(printedCard != null)
return printedCard.getUser().getNick();
else
return "Invalid";
}
} else {
return "Invalid";
}
return "Invalid";
}
@Override
public Place getPlaceFromHexcode(String hexcode) {
String barcode = ""+Integer.parseInt(hexcode, 16);
try {
if (barcode.startsWith(PLACE_PREFIX)) {
int id = Integer.parseInt(barcode.substring(3, barcode.length()-1));
Place place = placeFacade.find(id);
return place;
}
} catch (NumberFormatException x) {
}
return null;
}
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;
}
}