VersiĆ³n del proyecto de Apuestas tras la iteraciĆ³n 1
[ISBets21MAUBRY] / eclipse-workspace / ISBets21BRYMAUJONUNA / src / main / java / businessLogic / BLFacadeImplementation.java
1 package businessLogic;
2
3 import java.util.ArrayList;
4 //hola
5 import java.util.Date;
6 import java.util.ResourceBundle;
7 import java.util.Vector;
8
9 import javax.jws.WebMethod;
10 import javax.jws.WebService;
11
12 import configuration.ConfigXML;
13 import dataAccess.DataAccess;
14 import domain.Question;
15 import domain.User;
16 import domain.Event;
17 import domain.Forecast;
18 import exceptions.EventFinished;
19 import exceptions.IncorrectPassException;
20 import exceptions.QuestionAlreadyExist;
21 import exceptions.UserAlreadyExistException;
22 import exceptions.UserDoesNotExistException;
23
24 /**
25  * It implements the business logic as a web service.
26  */
27 @WebService(endpointInterface = "businessLogic.BLFacade")
28 public class BLFacadeImplementation implements BLFacade {
29         DataAccess dbManager;
30
31         public BLFacadeImplementation() {
32                 System.out.println("Creating BLFacadeImplementation instance");
33                 ConfigXML c = ConfigXML.getInstance();
34
35                 if (c.getDataBaseOpenMode().equals("initialize")) {
36                         dbManager = new DataAccess(c.getDataBaseOpenMode().equals("initialize"));
37                         dbManager.initializeDB();
38                         dbManager.close();
39                 }
40
41         }
42
43         public BLFacadeImplementation(DataAccess da) {
44
45                 System.out.println("Creating BLFacadeImplementation instance with DataAccess parameter");
46                 ConfigXML c = ConfigXML.getInstance();
47
48                 if (c.getDataBaseOpenMode().equals("initialize")) {
49                         da.open(true);
50                         da.initializeDB();
51                         da.close();
52
53                 }
54                 dbManager = da;
55         }
56
57         /**
58          * This method creates a question for an event, with a question text and the
59          * minimum bet
60          * 
61          * @param event      to which question is added
62          * @param question   text of the question
63          * @param betMinimum minimum quantity of the bet
64          * @return the created question, or null, or an exception
65          * @throws EventFinished        if current data is after data of the event
66          * @throws QuestionAlreadyExist if the same question already exists for the
67          *                              event
68          */
69         @WebMethod
70         public Question createQuestion(Event event, String question, float betMinimum)
71                         throws EventFinished, QuestionAlreadyExist {
72
73                 // The minimum bed must be greater than 0
74                 dbManager.open(false);
75                 Question qry = null;
76
77                 if (new Date().compareTo(event.getEventDate()) > 0)
78                         throw new EventFinished(ResourceBundle.getBundle("Etiquetas").getString("ErrorEventHasFinished"));
79
80                 qry = dbManager.createQuestion(event, question, betMinimum);
81
82                 dbManager.close();
83
84                 return qry;
85         };
86
87         /**
88          * This method invokes the data access to retrieve the events of a given date
89          * 
90          * @param date in which events are retrieved
91          * @return collection of events
92          */
93         @WebMethod
94         public Vector<Event> getEvents(Date date) {
95                 dbManager.open(false);
96                 Vector<Event> events = dbManager.getEvents(date);
97                 dbManager.close();
98                 return events;
99         }
100
101         /**
102          * This method invokes the data access to retrieve the dates a month for which
103          * there are events
104          * 
105          * @param date of the month for which days with events want to be retrieved
106          * @return collection of dates
107          */
108         @WebMethod
109         public Vector<Date> getEventsMonth(Date date) {
110                 dbManager.open(false);
111                 Vector<Date> dates = dbManager.getEventsMonth(date);
112                 dbManager.close();
113                 return dates;
114         }
115
116         public void close() {
117                 DataAccess dB4oManager = new DataAccess(false);
118
119                 dB4oManager.close();
120
121         }
122
123         /**
124          * This method invokes the data access to initialize the database with some
125          * events and questions. It is invoked only when the option "initialize" is
126          * declared in the tag dataBaseOpenMode of resources/config.xml file
127          */
128         @WebMethod
129         public void initializeBD() {
130                 dbManager.open(false);
131                 dbManager.initializeDB();
132                 dbManager.close();
133         }
134
135         public User login(String userName, String password) throws UserDoesNotExistException, IncorrectPassException {
136                 dbManager.open(false);
137                 User login = dbManager.login(userName, password);
138                 dbManager.close();
139                 return login;
140         }
141
142 //      public boolean validoUsuario(String puser) {
143 //              dbManager.open(false);
144 //              boolean usuarioBD = dbManager.validoUsuario(puser);
145 //              dbManager.close();
146 //              return usuarioBD;
147 //      }
148         
149         public User registrar(String user,String pass,String name,String lastName,String birthDate,String email,String account,Integer numb,String address) throws UserAlreadyExistException {
150                 dbManager.open(false);
151                 User u = dbManager.registrar(user, pass, name, lastName, birthDate, email, account, numb, address);
152                 dbManager.close();
153                 return u;
154         }
155         
156         public boolean insertEvent(Event pEvento) {
157                 dbManager.open(false);
158                 boolean inserted = dbManager.insertEvent(pEvento);
159                 dbManager.close();
160                 
161                 return inserted;
162         }
163         public int getNumberEvents() {
164                 dbManager.open(false);
165                 int n = dbManager.getNumberEvents();
166                 dbManager.close();
167                 return n;
168         }
169         
170         public boolean existEvent(Event event) {
171                 dbManager.open(false);
172                 boolean result = dbManager.existEvent(event);
173                 dbManager.close();
174                 return result;
175         }
176         
177         public int getNumberForecasts() {
178                 dbManager.open(false);
179                 int n = dbManager.getNumberForecasts();
180                 dbManager.close();
181                 return n;
182         }
183         
184         public boolean existForecast(Forecast f) {
185                 dbManager.open(false);
186                 boolean result = dbManager.existForecast(f);
187                 dbManager.close();
188                 return result;
189         }
190         
191         public boolean insertForecast(Forecast f) {
192                 dbManager.open(false);
193                 boolean inserted = dbManager.insertForecast(f);
194                 dbManager.close();
195                 return inserted;
196         }
197 }