IteraciĆ³n 3(VersiĆ³n sin idiomas)
[ISBets21MAUBRY] / eclipse-workspace / ISBets21MAUBRY / src / main / java / businessLogic / BLFacadeImplementation.java
1 package businessLogic;
2
3 //hola
4 import java.util.Date;
5 import java.util.ResourceBundle;
6 import java.util.Vector;
7
8 import javax.jws.WebMethod;
9 import javax.jws.WebService;
10
11 import configuration.ConfigXML;
12 import configuration.UtilDate;
13 import dataAccess.DataAccess;
14 import domain.AdminUser;
15 import domain.Bet;
16 import domain.Event;
17 import domain.Forecast;
18 import domain.Question;
19 import domain.RegularUser;
20 import domain.User;
21 import exceptions.EventFinished;
22 import exceptions.QuestionAlreadyExist;
23 import exceptions.UserAlreadyExistException;
24
25 /**
26  * It implements the business logic as a web service.
27  */
28 @WebService(endpointInterface = "businessLogic.BLFacade")
29 public class BLFacadeImplementation implements BLFacade {
30         DataAccess dbManager;
31
32         public BLFacadeImplementation() {
33                 System.out.println("Creating BLFacadeImplementation instance");
34                 ConfigXML c = ConfigXML.getInstance();
35
36                 if (c.getDataBaseOpenMode().equals("initialize")) {
37                         dbManager = new DataAccess(c.getDataBaseOpenMode().equals("initialize"));
38                         dbManager.initializeDB();
39                         dbManager.close();
40                 }
41
42         }
43
44         public BLFacadeImplementation(DataAccess da) {
45
46                 System.out.println("Creating BLFacadeImplementation instance with DataAccess parameter");
47                 ConfigXML c = ConfigXML.getInstance();
48
49                 if (c.getDataBaseOpenMode().equals("initialize")) {
50                         da.open(true);
51                         da.initializeDB();
52                         da.close();
53
54                 }
55                 dbManager = da;
56         }
57
58         /**
59          * This method creates a question for an event, with a question text and the
60          * minimum bet
61          * 
62          * @param event      to which question is added
63          * @param question   text of the question
64          * @param betMinimum minimum quantity of the bet
65          * @return the created question, or null, or an exception
66          * @throws EventFinished        if current data is after data of the event
67          * @throws QuestionAlreadyExist if the same question already exists for the
68          *                              event
69          */
70         @Override
71         @WebMethod
72         public Question createQuestion(Event event, String question, float betMinimum)
73                         throws EventFinished, QuestionAlreadyExist {
74
75                 // The minimum bed must be greater than 0
76                 dbManager.open(false);
77                 Question qry = null;
78
79                 if (new Date().compareTo(event.getEventDate()) > 0)
80                         throw new EventFinished(ResourceBundle.getBundle("Etiquetas").getString("ErrorEventHasFinished"));
81
82                 qry = dbManager.createQuestion(event, question, betMinimum);
83
84                 dbManager.close();
85
86                 return qry;
87         };
88
89         @Override
90         public Vector<Question> getAllQuestions() {
91                 dbManager.open(false);
92                 Vector<Question> questions = dbManager.getAllQuestions();
93                 dbManager.close();
94                 return questions;
95         }
96
97         @Override
98         public boolean deleteEvent(Event evento) {
99                 dbManager.open(false);
100                 boolean res = dbManager.deleteEvent(evento);
101                 dbManager.close();
102                 return res;
103         }
104
105         /**
106          * This method invokes the data access to retrieve the events of a given date
107          * 
108          * @param date in which events are retrieved
109          * @return collection of events
110          */
111         @Override
112         @WebMethod
113         public Vector<Event> getEvents(Date date) {
114                 dbManager.open(false);
115                 Vector<Event> events = dbManager.getEvents(date);
116                 dbManager.close();
117                 return events;
118         }
119
120         /**
121          * This method invokes the data access to retrieve the events of a given date
122          * 
123          * @param date in which events are retrieved
124          * @return collection of events
125          */
126         @Override
127         @WebMethod
128         public Vector<Event> getAllEvents() {
129                 dbManager.open(false);
130                 Vector<Event> events = dbManager.getAllEvents();
131                 dbManager.close();
132                 return events;
133         }
134
135         /**
136          * This method invokes the data access to retrieve the dates a month for which
137          * there are events
138          * 
139          * @param date of the month for which days with events want to be retrieved
140          * @return collection of dates
141          */
142         @Override
143         @WebMethod
144         public Vector<Date> getEventsMonth(Date date) {
145                 dbManager.open(false);
146                 Vector<Date> dates = dbManager.getEventsMonth(date);
147                 dbManager.close();
148                 return dates;
149         }
150
151         public void close() {
152                 DataAccess dB4oManager = new DataAccess(false);
153
154                 dB4oManager.close();
155
156         }
157
158         /**
159          * This method invokes the data access to initialize the database with some
160          * events and questions. It is invoked only when the option "initialize" is
161          * declared in the tag dataBaseOpenMode of resources/config.xml file
162          */
163         @Override
164         @WebMethod
165         public void initializeBD() {
166                 dbManager.open(false);
167                 dbManager.initializeDB();
168                 dbManager.close();
169         }
170
171 //      @Override
172 //      public RegularUser login(String userName, String password)
173 //                      throws UserDoesNotExistException, IncorrectPassException {
174 //              dbManager.open(false);
175 //              User login = dbManager.login(userName, password);
176 //              dbManager.close();
177 //              return login;
178 //      }
179
180 //      public boolean validoUsuario(String puser) {
181 //              dbManager.open(false);
182 //              boolean usuarioBD = dbManager.validoUsuario(puser);
183 //              dbManager.close();
184 //              return usuarioBD;
185 //      }
186
187         @Override
188         public RegularUser registrar(String user, String pass, String name, String lastName, String birthDate, String email,
189                         String account, Integer numb, String address, float balance) throws UserAlreadyExistException {
190                 dbManager.open(false);
191                 RegularUser u = dbManager.registrar(user, pass, name, lastName, birthDate, email, account, numb, address,
192                                 balance);
193                 dbManager.close();
194                 return u;
195         }
196
197         @Override
198         public boolean insertEvent(Event pEvento) {
199                 dbManager.open(false);
200                 boolean inserted = dbManager.insertEvent(pEvento);
201                 dbManager.close();
202
203                 return inserted;
204         }
205
206         @Override
207         public int getNumberEvents() {
208                 dbManager.open(false);
209                 int n = dbManager.getNumberEvents();
210                 dbManager.close();
211                 return n;
212         }
213
214         @Override
215         public boolean existEvent(Event event) {
216                 dbManager.open(false);
217                 boolean result = dbManager.existEvent(event);
218                 dbManager.close();
219                 return result;
220         }
221
222         @Override
223         public int getNumberForecasts() {
224                 dbManager.open(false);
225                 int n = dbManager.getNumberForecasts();
226                 dbManager.close();
227                 return n;
228         }
229
230         @Override
231         public boolean existForecast(Forecast f) {
232                 dbManager.open(false);
233                 boolean result = dbManager.existForecast(f);
234                 dbManager.close();
235                 return result;
236         }
237
238         @Override
239         public boolean insertForecast(Question question, String forecast, float fee) {
240                 dbManager.open(false);
241                 Forecast inserted = dbManager.insertForecast(question, forecast, fee);
242                 if (inserted == null) {
243                         return false;
244                 }
245                 dbManager.close();
246                 return true;
247         }
248
249         @Override
250         public Vector<Forecast> getForecasts() {
251                 dbManager.open(false);
252                 Vector<Forecast> result = dbManager.getForecasts();
253                 dbManager.close();
254                 return result;
255         }
256
257         @Override
258         public Vector<Forecast> getForecasts(Question pregunta) {
259                 dbManager.open(false);
260                 Vector<Forecast> result = dbManager.getForecasts(pregunta);
261                 dbManager.close();
262                 return result;
263         }
264
265         @Override
266         public boolean editarPerfilUsuario(String pContraseƱa, String pUsername, String pNombre, String pApellido,
267                         String pEmail, String pCuentaBancaria) {
268
269                 dbManager.open(false);
270
271                 boolean res = dbManager.editarPerfilUsuario(pContraseƱa, pUsername, pNombre, pApellido, pEmail,
272                                 pCuentaBancaria);
273                 dbManager.close();
274
275                 return res;
276
277         }
278
279         @Override
280         public boolean editarPerfilUsuarioSinPass(String pUsername, String pNombre, String pApellido, String pEmail,
281                         String pCuentaBancaria) {
282
283                 dbManager.open(false);
284
285                 boolean res = dbManager.editarPerfilUsuarioSinPass(pUsername, pNombre, pApellido, pEmail, pCuentaBancaria);
286                 dbManager.close();
287
288                 return res;
289
290         }
291
292         @Override
293         public Vector<User> getAllUsers() {
294
295                 dbManager.open(false);
296
297                 Vector<User> users = dbManager.getAllUsers();
298                 dbManager.close();
299                 return users;
300
301         }
302
303         @Override
304         public Integer getMaxIdInDB() {
305
306                 dbManager.open(false);
307                 Integer maxid = dbManager.getMaxIdInDB();
308                 dbManager.close();
309                 return maxid;
310
311         }
312
313         @Override
314         public boolean doLogin(String username, String pass) {
315                 dbManager.open(false);
316                 boolean bo = dbManager.doLogin(username, pass);
317                 dbManager.close();
318                 return bo;
319         }
320
321         @Override
322         public boolean isAdmin(String pusername, String ppassword) {
323                 dbManager.open(false);
324                 boolean bo = dbManager.isAdmin(pusername, ppassword);
325                 dbManager.close();
326                 return bo;
327         }
328
329         @Override
330         public RegularUser getRegularUserByUsername(String pusername) {
331                 dbManager.open(false);
332                 RegularUser ru = dbManager.getRegularUserByUsername(pusername);
333                 dbManager.close();
334                 return ru;
335         }
336
337         @Override
338         public AdminUser getAdminUserByUsername(String pusername) {
339                 dbManager.open(false);
340                 AdminUser au = dbManager.getAdminUserByUsername(pusername);
341                 dbManager.close();
342                 return au;
343
344         }
345
346         @Override
347         public int createApuesta(Forecast pselectedAnswer, RegularUser pselectedClient, Float pselectedAmount) {
348                 dbManager.open(false);
349                 int inserted = dbManager.createApuesta(pselectedAnswer, pselectedClient, pselectedAmount);
350                 return inserted;
351         }
352
353         @Override
354         public boolean closeEvent(Event e, Question q, Forecast f) {
355                 dbManager.open(false);
356                 boolean closed = dbManager.closeEvent(e, q, f);
357                 return closed;
358         }
359
360         @Override
361         public boolean anularApuesta(Bet pApuesta) {
362
363                 dbManager.open(false);
364
365                 boolean result = dbManager.anularApuesta(pApuesta);
366                 return result;
367
368         }
369
370         @Override
371         public Vector<Bet> getApuestasAbiertas(RegularUser pUser) {
372                 dbManager.open(false);
373                 Vector<Bet> result = dbManager.getApuestasAbiertas(pUser);
374                 return result;
375
376         }
377
378         @Override
379         public Vector<Bet> getApuestasCerradas(RegularUser pUser) {
380                 dbManager.open(false);
381                 Vector<Bet> result = dbManager.getApuestasCerradas(pUser);
382                 return result;
383
384         }
385
386         @Override
387         public Vector<Bet> getApuestasGanadas(RegularUser pUser) {
388                 dbManager.open(false);
389                 Vector<Bet> result = dbManager.getApuestasGanadas(pUser);
390                 return result;
391
392         }
393
394         @Override
395         public Vector<Bet> getApuestasPerdidas(RegularUser pUser) {
396                 dbManager.open(false);
397                 Vector<Bet> result = dbManager.getApuestasPerdidas(pUser);
398                 return result;
399
400         }
401
402         @Override
403         public Vector<Bet> getApuestasAnuladas(RegularUser pUser) {
404                 dbManager.open(false);
405                 Vector<Bet> result = dbManager.getApuestasAnuladas(pUser);
406                 return result;
407
408         }
409
410         @Override
411         public Vector<Bet> getApuestasByUser(RegularUser user) {
412                 dbManager.open(false);
413                 Vector<Bet> result = dbManager.getApuestasByUser(user);
414                 return result;
415
416         }
417
418         @Override
419         public boolean aplicarBonoBienvenida(String user) {
420
421                 dbManager.open(false);
422                 boolean result = dbManager.aplicarBonoBienvenida(user);
423                 return result;
424         }
425
426         @Override
427         public boolean recargarSaldo(String user, Float importe) {
428
429                 dbManager.open(false);
430                 boolean result = dbManager.recargarSaldo(user, importe);
431                 return result;
432         }
433
434         @Override
435         public boolean definirResultados(Event pselectedEvent, Question pselectedQuestion, Forecast pselectedForecast) {
436                 dbManager.open(false);
437                 boolean result = dbManager.definirResultados(pselectedEvent, pselectedQuestion, pselectedForecast);
438                 return result;
439         }
440
441         public static void main(String[] args) {
442
443                 BLFacadeImplementation controlador = new BLFacadeImplementation();
444                 RegularUser usuario = new RegularUser("usuario", "Usuario1?", "Nombre", "Apellido", "01/01/2000",
445                                 "usuario@gmail.com", "ES11 1111 1111 1111", 123456789, "", 0);
446                 Event ev1 = new Event(69, "Eibar-Eibar", UtilDate.newDate(2015, 4, 17));
447                 Question pregunta = new Question("pregunta", 2, ev1);
448                 Forecast pronostico = new Forecast("Madrid", 17, pregunta);
449                 Bet apuesta = new Bet(pronostico, usuario, 13);
450
451                 System.out.println(controlador.anularApuesta(apuesta));
452
453         }
454
455         @Override
456         public Vector<Question> getOpenedQuestions(Event ev) {
457                 dbManager.open(false);
458                 Vector<Question> ArrayListQuestions = dbManager.getOpenedQuestions(ev);
459                 return ArrayListQuestions;
460         }
461         
462         public boolean getEstadoEvento(Event ev) {
463                 
464                 dbManager.open(false);
465                 boolean result = dbManager.getEstadoEvento(ev);
466                 return result;
467                 
468         }
469         
470         public Vector<Event> getEventosMedioCerrados(){
471                 
472                 dbManager.open(false);
473
474                 Vector<Event> result = dbManager.getEventosMedioCerrados();
475                 return result;
476         }
477
478 }