Transactions pour EvaluationManagerBean, et tests réussis
This commit is contained in:
@@ -5,67 +5,33 @@ package org.yacos.core.evaluation;
|
|||||||
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import javax.annotation.Resource;
|
||||||
import javax.ejb.Stateless;
|
import javax.ejb.Stateless;
|
||||||
import javax.naming.Context;
|
import javax.ejb.TransactionManagement;
|
||||||
import javax.naming.InitialContext;
|
import javax.ejb.TransactionManagementType;
|
||||||
import javax.naming.NamingException;
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.PersistenceContext;
|
import javax.persistence.PersistenceContext;
|
||||||
import javax.persistence.Query;
|
import javax.persistence.Query;
|
||||||
|
import javax.transaction.UserTransaction;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.yacos.core.article.IArticleManager;
|
import org.yacos.core.article.Article;
|
||||||
import org.yacos.core.conferences.IConferenceManager;
|
import org.yacos.core.conferences.Conference;
|
||||||
import org.yacos.core.users.IUserManager;
|
import org.yacos.core.users.User;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author christiancorsano
|
* @author christiancorsano
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@Stateless
|
@Stateless
|
||||||
|
@TransactionManagement(TransactionManagementType.BEAN)
|
||||||
public class EvaluationManagerBean implements IEvaluationManager {
|
public class EvaluationManagerBean implements IEvaluationManager {
|
||||||
@PersistenceContext
|
@PersistenceContext
|
||||||
EntityManager em;
|
EntityManager em;
|
||||||
|
@Resource
|
||||||
|
UserTransaction ut;
|
||||||
|
|
||||||
protected final Log logger = LogFactory.getLog(getClass());
|
protected final Log logger = LogFactory.getLog(getClass());
|
||||||
|
|
||||||
private IConferenceManager cm;
|
|
||||||
private IArticleManager am;
|
|
||||||
private IUserManager um;
|
|
||||||
|
|
||||||
private IConferenceManager getConferenceManager(){
|
|
||||||
Context context;
|
|
||||||
try {
|
|
||||||
context = new InitialContext();
|
|
||||||
cm = (IConferenceManager)context.lookup("ConferenceManagerBean/remote");
|
|
||||||
} catch (NamingException e) {
|
|
||||||
logger.error(e.getMessage());
|
|
||||||
}
|
|
||||||
return cm;
|
|
||||||
}
|
|
||||||
private IArticleManager getArticleManager(){
|
|
||||||
Context context;
|
|
||||||
try {
|
|
||||||
context = new InitialContext();
|
|
||||||
am = (IArticleManager)context.lookup("ArticleManagerBean/remote");
|
|
||||||
} catch (NamingException e) {
|
|
||||||
logger.error(e.getMessage());
|
|
||||||
}
|
|
||||||
return am;
|
|
||||||
}
|
|
||||||
private IUserManager getUserManager(){
|
|
||||||
Context context;
|
|
||||||
try {
|
|
||||||
context = new InitialContext();
|
|
||||||
um = (IUserManager)context.lookup("UserManagerBean/remote");
|
|
||||||
} catch (NamingException e) {
|
|
||||||
logger.error(e.getMessage());
|
|
||||||
}
|
|
||||||
return um;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public List<Criterion> getCriterions(int confId){
|
public List<Criterion> getCriterions(int confId){
|
||||||
@@ -89,26 +55,63 @@ public class EvaluationManagerBean implements IEvaluationManager {
|
|||||||
|
|
||||||
|
|
||||||
public Criterion addCriterion(String name, Integer min_rating,
|
public Criterion addCriterion(String name, Integer min_rating,
|
||||||
Integer max_rating, int confId) {
|
Integer max_rating, int confId) {
|
||||||
cm = getConferenceManager();
|
Criterion crit = null;
|
||||||
Criterion crit = new Criterion();
|
try {
|
||||||
crit.setName(name);
|
ut.begin();
|
||||||
crit.setMin_rating(min_rating);
|
crit = new Criterion();
|
||||||
crit.setMax_rating(max_rating);
|
crit.setName(name);
|
||||||
crit.setConference(cm.getConference(confId));
|
crit.setMin_rating(min_rating);
|
||||||
em.persist(crit);
|
crit.setMax_rating(max_rating);
|
||||||
em.flush();
|
Conference conf = em.find(Conference.class, confId);
|
||||||
|
crit.setConference(conf);
|
||||||
|
em.persist(crit);
|
||||||
|
ut.commit();
|
||||||
|
} catch (Exception e) {
|
||||||
|
try {
|
||||||
|
ut.rollback();
|
||||||
|
} catch (Exception e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
return crit;
|
return crit;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateCriterion(Criterion criterion) {
|
public void updateCriterion(Criterion criterion) {
|
||||||
em.merge(criterion);
|
try {
|
||||||
em.flush();
|
ut.begin();
|
||||||
|
Criterion persistedCriterion = em.find(Criterion.class, criterion.getId());
|
||||||
|
Conference conference = em.find(Conference.class, criterion.getConference().getId());
|
||||||
|
persistedCriterion.setName(criterion.getName());
|
||||||
|
persistedCriterion.setMin_rating(criterion.getMin_rating());
|
||||||
|
persistedCriterion.setMax_rating(criterion.getMax_rating());
|
||||||
|
persistedCriterion.setConference(conference);
|
||||||
|
ut.commit();
|
||||||
|
} catch (Exception e) {
|
||||||
|
try {
|
||||||
|
ut.rollback();
|
||||||
|
} catch (Exception e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeCriterion(int criterionId) {
|
public void removeCriterion(int criterionId) {
|
||||||
Criterion criterion = this.getCriterion(criterionId);
|
try {
|
||||||
em.remove(criterion);
|
ut.begin();
|
||||||
|
Criterion criterion = em.find(Criterion.class, criterionId);
|
||||||
|
em.remove(criterion);
|
||||||
|
ut.commit();
|
||||||
|
} catch (Exception e) {
|
||||||
|
try {
|
||||||
|
ut.rollback();
|
||||||
|
} catch (Exception e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Criterion getCriterion(int criterionId) {
|
public Criterion getCriterion(int criterionId) {
|
||||||
@@ -122,31 +125,65 @@ public class EvaluationManagerBean implements IEvaluationManager {
|
|||||||
|
|
||||||
public Report addReport(int articleId, String commentPCMember, String commentAuthor, List<Rating> ratings,
|
public Report addReport(int articleId, String commentPCMember, String commentAuthor, List<Rating> ratings,
|
||||||
String refereeLogin) {
|
String refereeLogin) {
|
||||||
um = getUserManager();
|
Report report = null;
|
||||||
am = getArticleManager();
|
try {
|
||||||
|
ut.begin();
|
||||||
Report report = new Report();
|
report = new Report();
|
||||||
report.setReferee(um.getUser(refereeLogin));
|
User referee = em.find(User.class, refereeLogin);
|
||||||
report.setArticle(am.getArticle(articleId));
|
Article article = em.find(Article.class, articleId);
|
||||||
report.setCommentPCMember(commentPCMember);
|
report.setReferee(referee);
|
||||||
report.setCommentAuthor(commentAuthor);
|
report.setArticle(article);
|
||||||
report.setRatings(ratings);
|
report.setCommentPCMember(commentPCMember);
|
||||||
|
report.setCommentAuthor(commentAuthor);
|
||||||
//Report report = new Report("dsd",commentAuthor,ratings,um.getUser(refereeLogin),am.getArticle(articleId));
|
report.setRatings(ratings);
|
||||||
em.persist(report);
|
em.persist(report);
|
||||||
em.flush();
|
ut.commit();
|
||||||
|
} catch (Exception e) {
|
||||||
|
try {
|
||||||
|
ut.rollback();
|
||||||
|
} catch (Exception e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
return report;
|
return report;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateReport(Report report) {
|
public void updateReport(Report report) {
|
||||||
em.merge(report);
|
try {
|
||||||
em.flush();
|
ut.begin();
|
||||||
|
Report persistedReport = em.find(Report.class, report.getId());
|
||||||
|
Article article = em.find(Article.class, report.getArticle().getId());
|
||||||
|
User referee = em.find(User.class, report.getReferee().getLogin());
|
||||||
|
persistedReport.setCommentAuthor(report.getCommentAuthor());
|
||||||
|
persistedReport.setCommentPCMember(report.getCommentPCMember());
|
||||||
|
persistedReport.setArticle(article);
|
||||||
|
persistedReport.setReferee(referee);
|
||||||
|
ut.commit();
|
||||||
|
} catch (Exception e) {
|
||||||
|
try {
|
||||||
|
ut.rollback();
|
||||||
|
} catch (Exception e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeReport(int reportId) {
|
public void removeReport(int reportId) {
|
||||||
Report report = this.getReport(reportId);
|
try {
|
||||||
em.remove(report);
|
ut.begin();
|
||||||
|
Report report = em.find(Report.class, reportId);
|
||||||
|
em.remove(report);
|
||||||
|
ut.commit();
|
||||||
|
} catch (Exception e) {
|
||||||
|
try {
|
||||||
|
ut.rollback();
|
||||||
|
} catch (Exception e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Report getReport(int reportId){
|
public Report getReport(int reportId){
|
||||||
@@ -180,11 +217,22 @@ public class EvaluationManagerBean implements IEvaluationManager {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public Rating addRating(Integer value, Integer criterionId, Integer reportId) {
|
public Rating addRating(Integer value, Integer criterionId, Integer reportId) {
|
||||||
Criterion criterion = getCriterion(criterionId);
|
Rating rating = null;
|
||||||
Report report=getReport(reportId);
|
try {
|
||||||
Rating rating = new Rating(value,criterion,report);
|
ut.begin();
|
||||||
em.persist(rating);
|
Criterion criterion = em.find(Criterion.class, criterionId);
|
||||||
em.flush();
|
Report report = em.find(Report.class,reportId);
|
||||||
|
rating = new Rating(value,criterion,report);
|
||||||
|
em.persist(rating);
|
||||||
|
ut.commit();
|
||||||
|
} catch (Exception e) {
|
||||||
|
try {
|
||||||
|
ut.rollback();
|
||||||
|
} catch (Exception e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
return rating;
|
return rating;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,14 +243,42 @@ public class EvaluationManagerBean implements IEvaluationManager {
|
|||||||
return q.getResultList();
|
return q.getResultList();
|
||||||
}
|
}
|
||||||
public void removeRating(Criterion criterion, Report report) {
|
public void removeRating(Criterion criterion, Report report) {
|
||||||
Rating rating = em.find(Rating.class, new RatingPK(criterion,report));
|
try {
|
||||||
em.remove(rating);
|
ut.begin();
|
||||||
em.flush();
|
Criterion persistedCriterion = em.find(Criterion.class, criterion.getId());
|
||||||
|
Report persistedReport = em.find(Report.class,report.getId());
|
||||||
|
Rating rating = em.find(Rating.class, new RatingPK(persistedCriterion,persistedReport));
|
||||||
|
em.remove(rating);
|
||||||
|
ut.commit();
|
||||||
|
} catch (Exception e) {
|
||||||
|
try {
|
||||||
|
ut.rollback();
|
||||||
|
} catch (Exception e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Rating updateRating(Rating rating) {
|
public Rating updateRating(Rating rating) {
|
||||||
em.merge(rating);
|
try {
|
||||||
em.flush();
|
ut.begin();
|
||||||
|
Criterion criterion = em.find(Criterion.class, rating.getCriterion().getId());
|
||||||
|
Report report = em.find(Report.class,rating.getReport().getId());
|
||||||
|
RatingPK ratingId = new RatingPK(criterion,report);
|
||||||
|
Rating persistedRating = em.find(Rating.class, ratingId);
|
||||||
|
// We suppose that the criterion and report didn't change,
|
||||||
|
// as such a change would result in modifying the Rating id
|
||||||
|
persistedRating.setValue(rating.getValue());
|
||||||
|
ut.commit();
|
||||||
|
} catch (Exception e) {
|
||||||
|
try {
|
||||||
|
ut.rollback();
|
||||||
|
} catch (Exception e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
return rating;
|
return rating;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user