ISBets21MAUBRY
master
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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
package dataAccess; //hello import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.ResourceBundle; import java.util.Vector; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import javax.persistence.TypedQuery; import configuration.ConfigXML; import configuration.UtilDate; import domain.Event; import domain.Forecast; import domain.Question; import domain.User; import exceptions.IncorrectPassException; import exceptions.QuestionAlreadyExist; import exceptions.UserAlreadyExistException; import exceptions.UserDoesNotExistException; /** * It implements the data access to the objectDb database */ public class DataAccess { protected static EntityManager db; protected static EntityManagerFactory emf; ConfigXML c=ConfigXML.getInstance(); public DataAccess(boolean initializeMode) { System.out.println("Creating DataAccess instance => isDatabaseLocal: "+c.isDatabaseLocal()+" getDatabBaseOpenMode: "+c.getDataBaseOpenMode()); open(initializeMode); } public DataAccess() { new DataAccess(false); } /** * This is the data access method that initializes the database with some events and questions. * This method is invoked by the business logic (constructor of BLFacadeImplementation) when the option "initialize" is declared in the tag dataBaseOpenMode of resources/config.xml file */ public void initializeDB(){ db.getTransaction().begin(); try { Calendar today = Calendar.getInstance(); int month=today.get(Calendar.MONTH); month+=1; int year=today.get(Calendar.YEAR); if (month==12) { month=0; year+=1;} Event ev1=new Event(1, "Atlético-Athletic", UtilDate.newDate(year,month,17)); Event ev2=new Event(2, "Eibar-Barcelona", UtilDate.newDate(year,month,17)); Event ev3=new Event(3, "Getafe-Celta", UtilDate.newDate(year,month,17)); Event ev4=new Event(4, "Alavés-Deportivo", UtilDate.newDate(year,month,17)); Event ev5=new Event(5, "Español-Villareal", UtilDate.newDate(year,month,17)); Event ev6=new Event(6, "Las Palmas-Sevilla", UtilDate.newDate(year,month,17)); Event ev7=new Event(7, "Malaga-Valencia", UtilDate.newDate(year,month,17)); Event ev8=new Event(8, "Girona-Leganés", UtilDate.newDate(year,month,17)); Event ev9=new Event(9, "Real Sociedad-Levante", UtilDate.newDate(year,month,17)); Event ev10=new Event(10, "Betis-Real Madrid", UtilDate.newDate(year,month,17)); Event ev11=new Event(11, "Atletico-Athletic", UtilDate.newDate(year,month,1)); Event ev12=new Event(12, "Eibar-Barcelona", UtilDate.newDate(year,month,1)); Event ev13=new Event(13, "Getafe-Celta", UtilDate.newDate(year,month,1)); Event ev14=new Event(14, "Alavés-Deportivo", UtilDate.newDate(year,month,1)); Event ev15=new Event(15, "Español-Villareal", UtilDate.newDate(year,month,1)); Event ev16=new Event(16, "Las Palmas-Sevilla", UtilDate.newDate(year,month,1)); Event ev17=new Event(17, "Málaga-Valencia", UtilDate.newDate(year,month+1,28)); Event ev18=new Event(18, "Girona-Leganés", UtilDate.newDate(year,month+1,28)); Event ev19=new Event(19, "Real Sociedad-Levante", UtilDate.newDate(year,month+1,28)); Event ev20=new Event(20, "Betis-Real Madrid", UtilDate.newDate(year,month+1,28)); Question q1; Question q2; Question q3; Question q4; Question q5; Question q6; if (Locale.getDefault().equals(new Locale("es"))) { q1=ev1.addQuestion("¿Quién ganará el partido?",1); q2=ev1.addQuestion("¿Quién meterá el primer gol?",2); q3=ev11.addQuestion("¿Quién ganará el partido?",1); q4=ev11.addQuestion("¿Cuántos goles se marcarán?",2); q5=ev17.addQuestion("¿Quién ganará el partido?",1); q6=ev17.addQuestion("¿Habrá goles en la primera parte?",2); } else if (Locale.getDefault().equals(new Locale("en"))) { q1=ev1.addQuestion("Who will win the match?",1); q2=ev1.addQuestion("Who will score first?",2); q3=ev11.addQuestion("Who will win the match?",1); q4=ev11.addQuestion("How many goals will be scored in the match?",2); q5=ev17.addQuestion("Who will win the match?",1); q6=ev17.addQuestion("Will there be goals in the first half?",2); } else { q1=ev1.addQuestion("Zeinek irabaziko du partidua?",1); q2=ev1.addQuestion("Zeinek sartuko du lehenengo gola?",2); q3=ev11.addQuestion("Zeinek irabaziko du partidua?",1); q4=ev11.addQuestion("Zenbat gol sartuko dira?",2); q5=ev17.addQuestion("Zeinek irabaziko du partidua?",1); q6=ev17.addQuestion("Golak sartuko dira lehenengo zatian?",2); } db.persist(q1); db.persist(q2); db.persist(q3); db.persist(q4); db.persist(q5); db.persist(q6); db.persist(ev1); db.persist(ev2); db.persist(ev3); db.persist(ev4); db.persist(ev5); db.persist(ev6); db.persist(ev7); db.persist(ev8); db.persist(ev9); db.persist(ev10); db.persist(ev11); db.persist(ev12); db.persist(ev13); db.persist(ev14); db.persist(ev15); db.persist(ev16); db.persist(ev17); db.persist(ev18); db.persist(ev19); db.persist(ev20); db.getTransaction().commit(); System.out.println("Db initialized"); } catch (Exception e){ e.printStackTrace(); } } /** * This method creates a question for an event, with a question text and the minimum bet * * @param event to which question is added * @param question text of the question * @param betMinimum minimum quantity of the bet * @return the created question, or null, or an exception * @throws QuestionAlreadyExist if the same question already exists for the event */ public Question createQuestion(Event event, String question, float betMinimum) throws QuestionAlreadyExist { System.out.println(">> DataAccess: createQuestion=> event= "+event+" question= "+question+" betMinimum="+betMinimum); Event ev = db.find(Event.class, event.getEventNumber()); if (ev.DoesQuestionExists(question)) throw new QuestionAlreadyExist(ResourceBundle.getBundle("Etiquetas").getString("ErrorQueryAlreadyExist")); db.getTransaction().begin(); Question q = ev.addQuestion(question, betMinimum); //db.persist(q); db.persist(ev); // db.persist(q) not required when CascadeType.PERSIST is added in questions property of Event class // @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.PERSIST) db.getTransaction().commit(); return q; } /** * This method retrieves from the database the events of a given date * * @param date in which events are retrieved * @return collection of events */ public Vector<Event> getEvents(Date date) { System.out.println(">> DataAccess: getEvents"); Vector<Event> res = new Vector<Event>(); TypedQuery<Event> query = db.createQuery("SELECT ev FROM Event ev WHERE ev.eventDate=?1",Event.class); query.setParameter(1, date); List<Event> events = query.getResultList(); for (Event ev:events){ System.out.println(ev.toString()); res.add(ev); } return res; } /** * This method retrieves from the database the dates a month for which there are events * * @param date of the month for which days with events want to be retrieved * @return collection of dates */ public Vector<Date> getEventsMonth(Date date) { System.out.println(">> DataAccess: getEventsMonth"); Vector<Date> res = new Vector<Date>(); Date firstDayMonthDate= UtilDate.firstDayMonth(date); Date lastDayMonthDate= UtilDate.lastDayMonth(date); TypedQuery<Date> query = db.createQuery("SELECT DISTINCT ev.eventDate FROM Event ev WHERE ev.eventDate BETWEEN ?1 and ?2",Date.class); query.setParameter(1, firstDayMonthDate); query.setParameter(2, lastDayMonthDate); List<Date> dates = query.getResultList(); for (Date d:dates){ System.out.println(d.toString()); res.add(d); } return res; } public void open(boolean initializeMode){ System.out.println("Opening DataAccess instance => isDatabaseLocal: "+c.isDatabaseLocal()+" getDatabBaseOpenMode: "+c.getDataBaseOpenMode()); String fileName=c.getDbFilename(); if (initializeMode) { fileName=fileName+";drop"; System.out.println("Deleting the DataBase"); } if (c.isDatabaseLocal()) { emf = Persistence.createEntityManagerFactory("objectdb:"+fileName); db = emf.createEntityManager(); } else { Map<String, String> properties = new HashMap<String, String>(); properties.put("javax.persistence.jdbc.user", c.getUser()); properties.put("javax.persistence.jdbc.password", c.getPassword()); emf = Persistence.createEntityManagerFactory("objectdb://"+c.getDatabaseNode()+":"+c.getDatabasePort()+"/"+fileName, properties); db = emf.createEntityManager(); } } public boolean existQuestion(Event event, String question) { System.out.println(">> DataAccess: existQuestion=> event= "+event+" question= "+question); Event ev = db.find(Event.class, event.getEventNumber()); return ev.DoesQuestionExists(question); } public void close(){ db.close(); System.out.println("DataBase closed"); } public User login(String username, String pass) throws UserDoesNotExistException, IncorrectPassException { User usuario = db.find(User.class, username); if (usuario == null) { throw new exceptions.UserDoesNotExistException("El usuario no existe"); } if (!pass.equals(usuario.getUserPass())) { throw new exceptions.IncorrectPassException("Contrase�a incorrecta"); } return usuario; } public boolean insertEvent(Event pEvento) { try { db.getTransaction().begin(); db.persist(pEvento); db.getTransaction().commit(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } public boolean validoUsuario(String puser) throws UserAlreadyExistException { User usuarioBD = db.find(User.class, puser); if (usuarioBD == null) { return true; } else { throw new UserAlreadyExistException("Ese usuario ya existe"); } } public User registrar(String user, String pass, String name, String lastName, String birthDate, String email, String account, Integer numb, String address) throws UserAlreadyExistException { db.getTransaction().begin(); User u = new User(user, pass, name, lastName, birthDate, email, account, numb, address); boolean b = validoUsuario(user); if (b) { db.persist(u); db.getTransaction().commit(); } return u; } public int getNumberEvents() { db.getTransaction().begin(); TypedQuery<Event> query = db.createQuery("SELECT ev FROM Event ev ",Event.class); return query.getResultList().size(); } public boolean existEvent(Event event) { System.out.println(">> DataAccess: existEvent=> event= "+event); return db.find(Event.class, event.getEventNumber()) != null ? true : false; } public boolean insertForecast(Forecast f) { try { db.getTransaction().begin(); db.persist(f); db.getTransaction().commit(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } public int getNumberForecasts() { db.getTransaction().begin(); TypedQuery<Forecast> query = db.createQuery("SELECT f FROM Forecast f ",Forecast.class); return query.getResultList().size(); } public boolean existForecast(Forecast f) { System.out.println(">> DataAccess: existForecast=> forecast= "+f); return db.find(Forecast.class, f.getForecast()) != null ? true : false; } } |
Commits for ISBets21MAUBRY/eclipse-workspace/ISBets21BRYMAUJONUNA/src/main/java/dataAccess/DataAccess.java
Revision | Author | Commited | Message |
---|---|---|---|
cbae14 ... | porkipig | Thu 04 Mar, 2021 10:33:41 +0000 | Versión del proyecto de Apuestas tras la iteración 1 |