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
|
package bean.session.manager;
import java.sql.Date;
import java.util.Collection;
import java.util.List;
import interfaces.session.InterfaceTachesManager;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import bean.entity.Taches;
import bean.entity.TachesPriorite;
@Stateless(mappedName="TachesManager")
@Remote(InterfaceTachesManager.class)
public class TachesManager implements InterfaceTachesManager{
/*
* http://java.sun.com/developer/technicalArticles/J2EE/jpa/#tents
* the @PersistenceContext annotation injects an EntityManager with
* a transactional persistence context, on which the session bean has a dependency
*/
@PersistenceContext(unitName="ToDoEJB3")
private EntityManager entityManager;
/*
* METHODES DU CRUD
*
*/
/*
* CREATION
*
* Pour définir une priorité cela se fait simplement:
* 1- On défini dans l'entity à @joinColumn
* 2- Pour ce joinColumn on défini un getter et un setter
* 3- Enfin dans le manager au moment de la création d'une tache, on fait un new de notre tache puis un setRefPriorite
* 4- Ainsi on a pas besoin de définir au constructeur de tache la priorité
* on passe uniquement l'objet lié et JPA fait le reste.
*/
public void create(String nom, String prenom, String commentaire,Date date,int priorite) {
long begin = System.currentTimeMillis();
if(priorite!=0){
Taches tache = new Taches(nom,prenom,commentaire, date);
try{
tache.setRefPriorite(entityManager.find(TachesPriorite.class, priorite));
entityManager.persist(tache);
TachesPriorite tachepriorite = (TachesPriorite)entityManager.find(TachesPriorite.class, priorite);
Collection<Taches> collect = tachepriorite.getTache();
collect.add(tache);
tachepriorite.setTache(collect);
entityManager.persist(tachepriorite);
//entityManager.find(TachesPriorite.class, priorite).setTache(collect);
System.out.println("Tache créée. nom:"+nom+" prenom:"+prenom+" commentaire:"+commentaire+" date:"+date);
}
catch(Exception a){
try{
entityManager.remove(tache);
System.out.println("rollback effectuée, tache supprimée");
}
catch(Exception i){
System.out.println("priorité inexistante ou null");
System.out.println("priorité : "+priorite);
System.out.println("La tache n'a pas été crée donc pas besoin de rollback");
}
a.printStackTrace();
}
}
else{
System.out.println("Veuillez entrer une priorité");
}
long end = System.currentTimeMillis();
float time = ((float) (end-begin)) / 1000f;
System.out.println("[CREATE]Temps d execution :"+time +"ms");
}
/*
* REFRESH
* permet de réinitialiser létat dun objet par rapport à la base de données (annule toute modification)
*/
public void undoUpdate(int id){
long begin = System.currentTimeMillis();
entityManager.refresh(entityManager.find(Taches.class, id));
long end = System.currentTimeMillis();
float time = ((float) (end-begin)) / 1000f;
System.out.println("[undoUpdate]Temps d execution :"+time +"ms");
}
//UPDATE
public void updateNom(int id,String nom) {
long begin = System.currentTimeMillis();
entityManager.find(Taches.class, id).setNom(nom);
long end = System.currentTimeMillis();
float time = ((float) (end-begin)) / 1000f;
System.out.println("[updateNom]Temps d execution :"+time +"ms");
}
public void updatePrenom(int id,String prenom) {
long begin = System.currentTimeMillis();
entityManager.find(Taches.class, id).setPrenom(prenom);
long end = System.currentTimeMillis();
float time = ((float) (end-begin)) / 1000f;
System.out.println("[updatePrenom]Temps d execution :"+time +"ms");
}
public void updateCommentaire(int id,String commentaire) {
long begin = System.currentTimeMillis();
entityManager.find(Taches.class, id).setCommentaire(commentaire);
long end = System.currentTimeMillis();
float time = ((float) (end-begin)) / 1000f;
System.out.println("[updateCommentaire]Temps d execution :"+time +"ms");
}
public void updateDate(int id,Date date) {
long begin = System.currentTimeMillis();
entityManager.find(Taches.class, id).setDate(date);
long end = System.currentTimeMillis();
float time = ((float) (end-begin)) / 1000f;
System.out.println("[updateDate]Temps d execution :"+time +"ms");
}
//DELETE
public void delete(int id) {
long begin = System.currentTimeMillis();
entityManager.remove(entityManager.find(Taches.class, id));
long end = System.currentTimeMillis();
float time = ((float) (end-begin)) / 1000f;
System.out.println("[delete]Temps d execution :"+time +"ms");
}
@SuppressWarnings("unchecked")
public void deleteByName(String nom) {
long begin = System.currentTimeMillis();
List liste = rechercherTachesByNom(nom);
for(int j=0;j<liste.size();j++){
entityManager.remove(entityManager.find(Taches.class,Integer.parseInt(liste.get(j).toString())));
}
long end = System.currentTimeMillis();
float time = ((float) (end-begin)) / 1000f;
System.out.println("[delete]Temps d execution :"+time +"ms");
}
/*
* METHODES DE RECHERCHE
* @return List
*
*/
//LISTE COMPLETE DES TACHES
@SuppressWarnings("unchecked")
public List getListTaches() {
long begin = System.currentTimeMillis();
Query oQuery = entityManager.createQuery("select id from Taches");
long end = System.currentTimeMillis();
float time = ((float) (end-begin)) / 1000f;
System.out.println("[getListTaches]Temps d execution :"+time +"ms");
return oQuery.getResultList();
}
//LISTE DES TACHE PAR ID
public Taches rechercherTachesById(int id) {
long begin = System.currentTimeMillis();
Taches tache = entityManager.find(Taches.class, id);
long end = System.currentTimeMillis();
float time = ((float) (end-begin)) / 1000f;
System.out.println("[rechercherTachesById]Temps d execution :"+time +"ms");
return tache;
}
//LISTE DES TACHES PAR NOM
@SuppressWarnings("unchecked")
public List rechercherTachesByNom(String nom){
long begin = System.currentTimeMillis();
Query query = entityManager.createNamedQuery("findByNom");
query.setParameter(1, nom);
List liste = query.getResultList();
long end = System.currentTimeMillis();
float time = ((float) (end-begin)) / 1000f;
System.out.println("[rechercherTachesByNom]Temps d execution :"+time +"ms");
return liste;
}
//LISTE DES TACHES PAR PRENOM
@SuppressWarnings("unchecked")
public List rechercherTachesByPrenom(String prenom){
long begin = System.currentTimeMillis();
Query query = entityManager.createNamedQuery("findByPrenom");
query.setParameter(1, prenom);
List liste = query.getResultList();
long end = System.currentTimeMillis();
float time = ((float) (end-begin)) / 1000f;
System.out.println("[rechercherTachesByPrenom]Temps d execution :"+time +"ms");
return liste;
}
//LISTE DES TACHES PAR DATE
@SuppressWarnings("unchecked")
public List rechercherTachesByDate(Date date){
long begin = System.currentTimeMillis();
Query query = entityManager.createNamedQuery("findByDate");
query.setParameter(1, date);
List liste = query.getResultList();
long end = System.currentTimeMillis();
float time = ((float) (end-begin)) / 1000f;
System.out.println("[rechercherTachesByDate]Temps d execution :"+time +"ms");
return liste;
}
//LISTE DES TACHES PAR DATE
@SuppressWarnings("unchecked")
public List rechercherTachesByPriorite(int priorite){
long begin = System.currentTimeMillis();
Query query = entityManager.createNamedQuery("findByPriorite");
query.setParameter(1, priorite);
List liste = query.getResultList();
long end = System.currentTimeMillis();
float time = ((float) (end-begin)) / 1000f;
System.out.println("[rechercherTachesByPriorite]Temps d execution :"+time +"ms");
return liste;
}
/*
* GETTER AND SETTERS
* @return un champ
*/
public String rechercherCommentaire(int id) {
return entityManager.find(Taches.class, id).getCommentaire();
}
public Date rechercherDate(int id) {
return entityManager.find(Taches.class, id).getDate();
}
public String rechercherNom(int id) {
return entityManager.find(Taches.class, id).getNom();
}
public String rechercherPrenom(int id) {
return entityManager.find(Taches.class, id).getPrenom();
}
public String rechercherPriorite(int id) {
return entityManager.find(Taches.class, id).getRefPriorite().getType();
}
public int rechercherTotalTache() {
return getListTaches().size();
}
} |