TestDataBean.java
8.21 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package fi.insomnia.bortal.beans;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.Calendar;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.slf4j.LoggerFactory;
import fi.insomnia.bortal.facade.BillFacade;
import fi.insomnia.bortal.facade.BillLineFacade;
import fi.insomnia.bortal.facade.CompoEntryFacade;
import fi.insomnia.bortal.facade.CompoFacade;
import fi.insomnia.bortal.facade.EventMapFacade;
import fi.insomnia.bortal.facade.PlaceFacade;
import fi.insomnia.bortal.facade.UserFacade;
import fi.insomnia.bortal.model.Bill;
import fi.insomnia.bortal.model.Compo;
import fi.insomnia.bortal.model.CompoEntry;
import fi.insomnia.bortal.model.EventMap;
import fi.insomnia.bortal.model.IUser;
import fi.insomnia.bortal.model.LanEvent;
import fi.insomnia.bortal.model.Place;
import fi.insomnia.bortal.model.User;
/**
* Session Bean implementation class TestDataBean
*/
@Stateless
// @DeclareRoles("ADMIN_BASE")
// @RolesAllowed("ADMIN_BASE")
public class TestDataBean implements TestDataBeanLocal {
@PersistenceContext
private EntityManager em;
public static final String TEST_MAP_IMAGE_NAME = "testmap.png";
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestDataBean.class);
@EJB
private EventMapFacade eventMapFacade;
@EJB
private EventBeanLocal eventBean;
@EJB
private UserFacade userFacade;
@EJB
private PlaceFacade placeFacade;
@EJB
private CompoFacade compoFacade;
@EJB
private CompoEntryFacade compoEntryFacade;
@EJB
private BillFacade billFacade;
@EJB
private BillLineFacade billLineFacade;
// @PersistenceUnit
// private EntityManagerFactory emf;
//
public void bootstrap() {
}
// @Override
// public void writeMap(byte[] bytes) {
// EventMap map = new EventMap(eventBean.getCurrentEvent());
// map.setName("test" + bytes);
// // StringBuilder sb = new StringBuilder();
// // for (int i = 0; i < bytes; ++i) {
// //
// // sb.append((char) (33 + i % 92));
// // }
// map.setMapData(bytes);
// eventMapFacade.create(map);
// }
// @Override
// public EventMap readMap(long bytes) {
//
// return eventMapFacade.findByName("test" + bytes);
//
// }
/**
* Default constructor.
*/
public TestDataBean() {
// TODO Auto-generated constructor stub
}
@Override
public IUser createUser() {
User u = new User();
u.setActive(true);
u.setAddress("Pallokorvankatu 1");
u.setNick("kavija");
Calendar bday = Calendar.getInstance();
bday.set(Calendar.YEAR, 1990);
u.setBirthday(bday.getTime());
u.setCreated(Calendar.getInstance());
u.setEmail("kalle.kavija@example.com");
u.setFirstnames("Kalle Kauko");
u.setLastname("Kävija");
u.setLogin("kavija");
u.setNick("^Kkavija^]9a8/");
u.resetPassword("kavija");
u.setPhone("123-45679854");
u.setTown("Keikyän MLK");
u.setPostalTown("Keykyä");
u.setZip("393929");
userFacade.create(u);
return u;
}
@Override
public IUser createAdmin() {
User u = new User();
u.setActive(true);
u.setAddress("Elite 1337");
u.setNick("admin");
Calendar bday = Calendar.getInstance();
bday.set(Calendar.YEAR, 1980);
u.setBirthday(bday.getTime());
u.setCreated(Calendar.getInstance());
u.setEmail("admin@inter.net");
u.setFirstnames("Asko Admin");
u.setLastname("admin");
u.setLogin("admin");
u.setNick("admin");
u.resetPassword("admin");
u.setPhone("1337");
u.setTown("Adminila");
u.setPostalTown("Adminila ");
u.setZip("6666");
u.setSuperadmin(true);
userFacade.create(u);
return u;
}
// @Override
// public Bill createBill(User u) {
// Bill b = new Bill(eventBean.getCurrentEvent());
// b.setUser(u);
// b.setAddr1("Kalle Kävijä");
// b.setAddr2("co / Pelivieroitus Ry");
// b.setAddr3("Pallokorvankatu 1");
// b.setAddr4("696969 Keikyä");
// b.setAddr5("FINLAND! \\o/");
// b.setBillNumber(123123);
// b.setDelayIntrest(11);
// b.setDeliveryTerms("Toimitetaan, ehkä...");
// Calendar duedate = Calendar.getInstance();
// duedate.add(Calendar.DATE, 14);
// b.setNotes("Some notes...");
// b.setNoticetime("14 vrk");
// b.setOurReference("Meitin viite ( Insomnia XII )");
// b.setPaymentTime(14);
// b.setSentDate(Calendar.getInstance());
// b.setTheirReference("Niitten viite ( tyhjä? )");
// billFacade.create(b);
// createBillLine(b, "Tuote1", 1239.3, "kpl", 11.0, 0.22);
// createBillLine(b, "Tuote2", 0.3, "pss", 11.0, 0.22);
// createBillLine(b, "Tuote3", 9.3, "l", 11.0, 0.22);
// createBillLine(b, "Tuote4", 9, "kWh", 99931911.0, 0);
// createBillLine(b, "Tuote5", 33, "mol", 11.0, 0.22);
// createBillLine(b, "Tuote6", 39939.23123123, "rad", 122.0, 0.18);
//
// return b;
// }
// private BillLine createBillLine(Bill b, String line, double d, String
// qname, double e, double f) {
// return createBillLine(b, line, new BigDecimal(d), qname, new
// BigDecimal(e), new BigDecimal(f));
//
// }
// private BillLine createBillLine(Bill b, String line, BigDecimal q, String
// qname, BigDecimal price, BigDecimal vat) {
// BillLine bl = new BillLine(b);
// bl.setName(line);
// bl.setQuantity(q);
// bl.setUnitName(qname);
// bl.setUnitPrice(price);
// bl.setVat(vat);
//
// billLineFacade.create(bl);
// return bl;
// }
@Override
public EventMap generateTestMap() {
LanEvent event = eventBean.getCurrentEvent();
InputStream stream = null;
try {
logger.info("Generating Test Map for event: " + event);
EventMap map = new EventMap(event);
stream = getClass().getResourceAsStream(TEST_MAP_IMAGE_NAME);
File file = new File(getClass().getResource(TEST_MAP_IMAGE_NAME).toURI());
long length = file.length();
// Create the byte array to hold the data
byte[] bytes = new byte[(int) length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead = stream.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file " + file.getName());
}
map.setMapData(bytes);
map.setName("test0");
eventMapFacade.create(map);
return map;
} catch (URISyntaxException ex) {
logger.warn("Exception while generating testmap", ex);
} catch (IOException ex) {
logger.warn("Exception while generating testmap", ex);
} finally
{
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
// ignore at this point
}
}
}
return null;
}
@Override
public void generateTestPlaces(EventMap map) {
logger.info("Adding places to map {}, event {}", map);
for (int x = 5; x < 400; x += 50) {
for (int y = 5; y < 150; y += 50) {
Place place = new Place(map);
place.setMap(map);
place.setMapX(x);
place.setMapY(y);
place.setWidth(50);
place.setHeight(50);
// map.getPlaces().add(place);
placeFacade.create(place);
}
}
}
// @Override
// public void printPlacesInfo() {
// logger.debug("Fetching places");
// List<Place> places = placeFacade.findAll();
// logger.debug("Got places: {}", places);
// logger.info("Printing info from places");
// for (Place place : places) {
// logger.info("Place id: {}. Event Map id: {}.", place.getId(),
// place.getMap().getId());
// }
// }
@Override
public void generateTestCompos() {
LanEvent event = eventBean.getCurrentEvent();
Compo compo = new Compo();
compo.setEvent(event);
compo.setName("Test compo");
compo.setHoldVoting(false);
compoFacade.create(compo);
CompoEntry compoEntry1 = new CompoEntry();
compoEntry1.setCompo(compo);
compoEntry1.setCreated(Calendar.getInstance());
compoEntry1.setTitle("Test entry for test compo");
compoEntryFacade.create(compoEntry1);
CompoEntry compoEntry2 = new CompoEntry();
compoEntry2.setCompo(compo);
compoEntry2.setCreated(Calendar.getInstance());
compoEntry2.setTitle("Another test entry for test compo");
compoEntryFacade.create(compoEntry2);
}
@Override
public void flushMaps() {
Query q = em.createQuery("delete from EventMap");
q.executeUpdate();
}
@Override
public Bill createBill(IUser u) {
// TODO Auto-generated method stub
return null;
}
}