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
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package controller;
import controller.exceptions.NonexistentEntityException;
import controller.exceptions.PreexistingEntityException;
import controller.exceptions.RollbackFailureException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
import model.Profil;
import model.User;
/**
*
* @author Bitaka-DEV
*/
public class UserJpaController implements Serializable {
public UserJpaController(UserTransaction utx, EntityManagerFactory emf) {
this.utx = utx;
this.emf = emf;
}
private UserTransaction utx = null;
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(User user) throws PreexistingEntityException, RollbackFailureException, Exception {
EntityManager em = null;
try {
utx.begin();
em = getEntityManager();
Profil idProfil = user.getIdProfil();
if (idProfil != null) {
idProfil = em.getReference(idProfil.getClass(), idProfil.getId());
user.setIdProfil(idProfil);
}
em.persist(user);
if (idProfil != null) {
idProfil.getUserList().add(user);
idProfil = em.merge(idProfil);
}
utx.commit();
} catch (Exception ex) {
try {
utx.rollback();
} catch (Exception re) {
throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
}
if (findUser(user.getLogin()) != null) {
throw new PreexistingEntityException("User " + user + " already exists.", ex);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(User user) throws NonexistentEntityException, RollbackFailureException, Exception {
EntityManager em = null;
try {
utx.begin();
em = getEntityManager();
User persistentUser = em.find(User.class, user.getLogin());
Profil idProfilOld = persistentUser.getIdProfil();
Profil idProfilNew = user.getIdProfil();
if (idProfilNew != null) {
idProfilNew = em.getReference(idProfilNew.getClass(), idProfilNew.getId());
user.setIdProfil(idProfilNew);
}
user = em.merge(user);
if (idProfilOld != null && !idProfilOld.equals(idProfilNew)) {
idProfilOld.getUserList().remove(user);
idProfilOld = em.merge(idProfilOld);
}
if (idProfilNew != null && !idProfilNew.equals(idProfilOld)) {
idProfilNew.getUserList().add(user);
idProfilNew = em.merge(idProfilNew);
}
utx.commit();
} catch (Exception ex) {
try {
utx.rollback();
} catch (Exception re) {
throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
}
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
String id = user.getLogin();
if (findUser(id) == null) {
throw new NonexistentEntityException("The user with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(String id) throws NonexistentEntityException, RollbackFailureException, Exception {
EntityManager em = null;
try {
utx.begin();
em = getEntityManager();
User user;
try {
user = em.getReference(User.class, id);
user.getLogin();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The user with id " + id + " no longer exists.", enfe);
}
Profil idProfil = user.getIdProfil();
if (idProfil != null) {
idProfil.getUserList().remove(user);
idProfil = em.merge(idProfil);
}
em.remove(user);
utx.commit();
} catch (Exception ex) {
try {
utx.rollback();
} catch (Exception re) {
throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public List<User> findUserEntities() {
return findUserEntities(true, -1, -1);
}
public List<User> findUserEntities(int maxResults, int firstResult) {
return findUserEntities(false, maxResults, firstResult);
}
private List<User> findUserEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(User.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public User findUser(String id) {
EntityManager em = getEntityManager();
try {
return em.find(User.class, id);
} finally {
em.close();
}
}
public int getUserCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<User> rt = cq.from(User.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
} |
Partager