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
|
package com.calculateur.warhammer.dao.dao;
import java.util.List;
import java.util.Optional;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.Query;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import com.calculateur.warhammer.base.dao.IDAO;
import com.calculateur.warhammer.base.entity.IEntity;
import com.calculateur.warhammer.base.exception.DAOException;
import com.calculateur.warhammer.base.exception.FunctionnalExeption;
import com.calculateur.warhammer.dao.verification.VerificationUtils;
/**
* Template Method pour les DAO avec JPA
* @author phili
*
* @param <I> Type de l'id
* @param <E> Type de l'entité
*/
public abstract class AbstractDAO<I,E extends IEntity<I>> implements IDAO<I, E>{
@PersistenceContext
protected final EntityManager em;
protected final PlatformTransactionManager transactionManager;
protected AbstractDAO(EntityManager em, PlatformTransactionManager transactionManager) {
this.em = em;
this.transactionManager = transactionManager;
}
/**
*
* @return La classe sous forme de String pour les Query JPQL
*/
protected abstract String getEntityClass();
/**
*
* @return Le libellé de l'entité pour les query JPQL
*/
protected abstract String getEntityLibelle();
/**
*
* @return Le resource Bundle d'erreur pour les erreurs fonctionnelles
*/
protected abstract String getErrorBundle();
/**
*
* @return L'entité sous forme de classe
*/
protected abstract Class<E> getClassEntity();
@Override
public E save(E entity) throws DAOException, FunctionnalExeption {
verifieEntity(entity);
TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
try {
em.persist(entity);
transactionManager.commit(status);
return entity;
}catch (Exception e) {
transactionManager.rollback(status);
throw new DAOException(e);
}
}
@Override
public E update(E entity) throws DAOException, FunctionnalExeption {
verifieEntity(entity);
TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
try {
E rEntity = em.merge(entity);
transactionManager.commit(status);
return rEntity;
}catch (Exception e) {
transactionManager.rollback(status);
throw new DAOException(e);
}
}
@Override
public Optional<E> getById(I id) throws DAOException {
try {
return Optional.ofNullable(em.find(getClassEntity(), id));
}catch(Exception e) {
throw new DAOException(e);
}
}
@SuppressWarnings("unchecked")
@Override
public List<E> getAll() throws DAOException {
try {
Query query = em.createQuery(getJpqlAll());
return query.getResultList();
}catch(Exception e) {
throw new DAOException(e);
}
}
/**
*
* @return Le JPQL pour avoir toutes les entités
*/
private String getJpqlAll() {
StringBuilder sb = new StringBuilder("SELECT ");
sb.append(getEntityLibelle());
sb.append(" FROM ");
sb.append(getEntityClass());
sb.append(" ");
sb.append(getEntityLibelle());
return sb.toString();
}
@Override
public void delete(I id) throws DAOException {
TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
try {
Query queryDelete = getDeleteQuery(id);
queryDelete.executeUpdate();
transactionManager.commit(status);
}catch(Exception e) {
transactionManager.rollback(status);
throw new DAOException(e);
}
}
/**
* On laisse la possibilité de surcharger cette méthode au cas où l'id soit complexe (clé sur 2 colonnes par exemple).
* @param id L'id de l'entité à supprimer en BDD.
* @return La query pour effacer l'entité (le paramétrage est fait.
*/
protected Query getDeleteQuery(I id) {
Query query = em.createQuery(getDeleteJpql());
query.setParameter("id", id);
return query;
}
/**
* On laisse la possibilité de surcharger cette méthode au cas où l'id soit complexe (clé sur 2 colonnes par exemple).
* @return JPQL pour suprimer une entité
*/
protected String getDeleteJpql() {
StringBuilder sb = new StringBuilder("DELETE ");
sb.append(" FROM ");
sb.append(getEntityClass());
sb.append(" ");
sb.append(getEntityLibelle());
sb.append(" WHERE ");
sb.append(getEntityLibelle());
sb.append(".id = : id");
return sb.toString();
}
@Override
public Long count() throws DAOException {
try {
Query query = em.createQuery(getJpqlCount());
return (Long) query.getSingleResult();
}catch (Exception e) {
throw new DAOException(e);
}
}
/**
*
* @return Le JPQL pour compter ce qui est en BDD.
*/
private String getJpqlCount() {
StringBuilder sb = new StringBuilder("SELECT DISTINCT(COUNT(");
sb.append(getEntityLibelle());
sb.append(")) FROM ");
sb.append(getEntityClass());
sb.append(" ");
sb.append(getEntityLibelle());
return sb.toString();
}
@Override
public void verifieEntity(E entity) throws DAOException, FunctionnalExeption {
VerificationUtils.verifie(entity, getErrorBundle());
}
} |
Partager