mirror of
https://github.com/kmitresse/Cards-Rush.git
synced 2026-05-16 09:05:28 +00:00
feat(DevWeb): Setup du Projet avec Servlet et JPA
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* AbstractDAOFactory.java, 09/02/2024
|
||||
* UPPA M1 TI 2023-2024
|
||||
* Pas de copyright, aucun droits
|
||||
*/
|
||||
|
||||
package uppa.project.dao;
|
||||
|
||||
import uppa.project.dao.jpa.Game_JPA_DAO_Factory;
|
||||
|
||||
/**
|
||||
* Factory renvoyant une factory de DAO en fonction du support de persistance choisi
|
||||
*/
|
||||
public class AbstractDAOFactory {
|
||||
|
||||
public static GameDAOFactory getDAOFactory(PersistenceKind type) {
|
||||
if (type.equals(PersistenceKind.JPA)) return new Game_JPA_DAO_Factory();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* DAO.java, 21/02/2024
|
||||
* UPPA M1 TI 2023-2024
|
||||
* Pas de copyright, aucun droits
|
||||
*/
|
||||
|
||||
package uppa.project.dao;
|
||||
|
||||
/**
|
||||
* DAO abstrait et générique pour tout type de données
|
||||
*
|
||||
* @param <D> la classe paramétrant le DAO
|
||||
*/
|
||||
public abstract class DAO<D> {
|
||||
|
||||
public void DAO() throws DAOException {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne à partir du support de persistance un objet en fonction de son identifiant
|
||||
*
|
||||
* @param id identifiant de l'objet
|
||||
* @return l'instance de l'objet
|
||||
* @throws DAOException en cas de problème
|
||||
*/
|
||||
public abstract D findById(int id) throws DAOException;
|
||||
|
||||
public abstract D[] findAll() throws DAOException;
|
||||
|
||||
/**
|
||||
* Rend persistant un objet qui n'avait pas encore de réprésentation sur le support de persistance
|
||||
*
|
||||
* @param data l'objet à rendre persistant
|
||||
* @throws DAOException en cas de problème
|
||||
*/
|
||||
public abstract void create(D data) throws DAOException;
|
||||
|
||||
/**
|
||||
* Met à jour le contenu correspondant à l'objet sur le support persistant (l'objet
|
||||
* avait déjà une représentation sur le support persistant)
|
||||
*
|
||||
* @param data l'objet modifié dont le contenu est à mettre à jour
|
||||
* @throws DAOException en cas de problème
|
||||
*/
|
||||
public abstract void update(D data) throws DAOException;
|
||||
|
||||
/**
|
||||
* Efface du support persistant le contenu équivalent à l'objet
|
||||
*
|
||||
* @param data l'objet à supprimer
|
||||
* @throws DAOException en cas de problème
|
||||
*/
|
||||
public abstract void delete(D data) throws DAOException;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* DAOException.java, 09/02/2024
|
||||
* UPPA M1 TI 2023-2024
|
||||
* Pas de copyright, aucun droits
|
||||
*/
|
||||
|
||||
package uppa.project.dao;
|
||||
|
||||
/**
|
||||
* Exception spécifique aux problèmes d'accès aux données via un DAO
|
||||
*/
|
||||
public class DAOException extends Exception {
|
||||
|
||||
public DAOException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public DAOException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* SportsDAOFactory.java, 09/02/2024
|
||||
* UPPA M1 TI 2023-2024
|
||||
* Pas de copyright, aucun droits
|
||||
*/
|
||||
|
||||
package uppa.project.dao;
|
||||
|
||||
import uppa.project.pojo.*;
|
||||
|
||||
/**
|
||||
* Fabrique abstraite de DAO pour le schéma sports
|
||||
*/
|
||||
public abstract class GameDAOFactory {
|
||||
|
||||
/**
|
||||
* @return le DAO pour la classe/table User
|
||||
* @throws DAOException en cas de problème
|
||||
*/
|
||||
public abstract DAO<User> getDAOUser() throws DAOException;
|
||||
|
||||
/**
|
||||
* @return le DAO pour la classe/table Game
|
||||
* @throws DAOException en cas de problème
|
||||
*/
|
||||
public abstract DAO<Game> getDAOGame() throws DAOException;
|
||||
|
||||
/**
|
||||
* @return le DAO pour la classe/table Player
|
||||
* @throws DAOException en cas de problème
|
||||
*/
|
||||
public abstract DAO<Player> getDAOPlayer() throws DAOException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* PersistenceKind.java, 09/02/2024
|
||||
* UPPA M1 TI 2023-2024
|
||||
* Pas de copyright, aucun droits
|
||||
*/
|
||||
|
||||
package uppa.project.dao;
|
||||
|
||||
/**
|
||||
* Type de support de persistance pour les données
|
||||
*/
|
||||
public enum PersistenceKind {
|
||||
JDBC, JPA, XML
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package uppa.project.dao.jpa;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.TypedQuery;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import uppa.project.EntityManagerProvider;
|
||||
import uppa.project.dao.DAO;
|
||||
import uppa.project.dao.DAOException;
|
||||
import uppa.project.pojo.Game;
|
||||
|
||||
public class DAO_JPA_Game extends DAO<Game> {
|
||||
|
||||
private final EntityManager entityManager;
|
||||
|
||||
public DAO_JPA_Game() throws DAOException {
|
||||
this.entityManager = EntityManagerProvider.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Game findById(int id) throws DAOException {
|
||||
Game result = entityManager.find(Game.class, new BigDecimal(id));
|
||||
entityManager.flush();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Game[] findAll() throws DAOException {
|
||||
TypedQuery<Game> query = entityManager.createQuery("SELECT g FROM Game g", Game.class);
|
||||
List<Game> results = query.getResultList();
|
||||
return results.toArray(new Game[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void create(Game data) throws DAOException {
|
||||
update(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Game data) throws DAOException {
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.merge(data);
|
||||
entityManager.getTransaction().commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Game data) throws DAOException {
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.remove(data);
|
||||
entityManager.getTransaction().commit();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package uppa.project.dao.jpa;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.TypedQuery;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import uppa.project.EntityManagerProvider;
|
||||
import uppa.project.dao.DAO;
|
||||
import uppa.project.dao.DAOException;
|
||||
import uppa.project.pojo.Player;
|
||||
|
||||
public class DAO_JPA_Player extends DAO<Player> {
|
||||
|
||||
private final EntityManager entityManager;
|
||||
|
||||
public DAO_JPA_Player() throws DAOException {
|
||||
this.entityManager = EntityManagerProvider.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player findById(int id) throws DAOException {
|
||||
Player result = entityManager.find(Player.class, new BigDecimal(id));
|
||||
entityManager.flush();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player[] findAll() throws DAOException {
|
||||
TypedQuery<Player> query = entityManager.createQuery("SELECT p FROM Player p", Player.class);
|
||||
List<Player> results = query.getResultList();
|
||||
return results.toArray(new Player[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void create(Player data) throws DAOException {
|
||||
update(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Player data) throws DAOException {
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.merge(data);
|
||||
entityManager.getTransaction().commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Player data) throws DAOException {
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.remove(data);
|
||||
entityManager.getTransaction().commit();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package uppa.project.dao.jpa;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.TypedQuery;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import uppa.project.dao.DAO;
|
||||
import uppa.project.dao.DAOException;
|
||||
import uppa.project.EntityManagerProvider;
|
||||
import uppa.project.pojo.User;
|
||||
|
||||
public class DAO_JPA_User extends DAO<User> {
|
||||
|
||||
private final EntityManager entityManager;
|
||||
|
||||
public DAO_JPA_User() throws DAOException {
|
||||
this.entityManager = EntityManagerProvider.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public User findById(int id) throws DAOException {
|
||||
User result = entityManager.find(User.class, new BigDecimal(id));
|
||||
entityManager.flush();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User[] findAll() throws DAOException {
|
||||
TypedQuery<User> query = entityManager.createQuery("SELECT u FROM User u", User.class);
|
||||
List<User> results = query.getResultList();
|
||||
return results.toArray(new User[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void create(User data) throws DAOException {
|
||||
update(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(User data) throws DAOException {
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.merge(data);
|
||||
entityManager.getTransaction().commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(User data) throws DAOException {
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.remove(data);
|
||||
entityManager.getTransaction().commit();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Sports_JDBC_DAOFactory.java, 09/02/2024
|
||||
* UPPA M1 TI 2023-2024
|
||||
* Pas de copyright, aucun droits
|
||||
*/
|
||||
|
||||
package uppa.project.dao.jpa;
|
||||
|
||||
import uppa.project.dao.DAO;
|
||||
import uppa.project.pojo.*;
|
||||
import uppa.project.dao.DAOException;
|
||||
import uppa.project.dao.GameDAOFactory;
|
||||
|
||||
/**
|
||||
* Fabrique concrète de DAO pour le schéma relationnel sports avec une implémentation en JDBC.
|
||||
*/
|
||||
public class Game_JPA_DAO_Factory extends GameDAOFactory {
|
||||
|
||||
private DAO_JPA_User daoUser = null;
|
||||
|
||||
private DAO_JPA_Game daoGame = null;
|
||||
|
||||
private DAO_JPA_Player daoPlayer = null;
|
||||
|
||||
@Override
|
||||
public DAO<User> getDAOUser() throws DAOException {
|
||||
if (daoUser == null) daoUser = new DAO_JPA_User();
|
||||
return daoUser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DAO<Game> getDAOGame() throws DAOException {
|
||||
if (daoGame == null) daoGame = new DAO_JPA_Game();
|
||||
return daoGame;
|
||||
}
|
||||
@Override
|
||||
public DAO<Player> getDAOPlayer() throws DAOException {
|
||||
if (daoPlayer == null) daoPlayer = new DAO_JPA_Player();
|
||||
return daoPlayer;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user