Transactions pour EvaluationManagerBean, et tests réussis
This commit is contained in:
@@ -5,68 +5,34 @@ package org.yacos.core.evaluation;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.ejb.Stateless;
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.naming.NamingException;
|
||||
import javax.ejb.TransactionManagement;
|
||||
import javax.ejb.TransactionManagementType;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import javax.transaction.UserTransaction;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.yacos.core.article.IArticleManager;
|
||||
import org.yacos.core.conferences.IConferenceManager;
|
||||
import org.yacos.core.users.IUserManager;
|
||||
import org.yacos.core.article.Article;
|
||||
import org.yacos.core.conferences.Conference;
|
||||
import org.yacos.core.users.User;
|
||||
|
||||
/**
|
||||
* @author christiancorsano
|
||||
*
|
||||
*/
|
||||
@Stateless
|
||||
@TransactionManagement(TransactionManagementType.BEAN)
|
||||
public class EvaluationManagerBean implements IEvaluationManager {
|
||||
@PersistenceContext
|
||||
EntityManager em;
|
||||
@Resource
|
||||
UserTransaction ut;
|
||||
|
||||
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")
|
||||
public List<Criterion> getCriterions(int confId){
|
||||
//Query q = em.createQuery("from Criterion");
|
||||
@@ -90,25 +56,62 @@ public class EvaluationManagerBean implements IEvaluationManager {
|
||||
|
||||
public Criterion addCriterion(String name, Integer min_rating,
|
||||
Integer max_rating, int confId) {
|
||||
cm = getConferenceManager();
|
||||
Criterion crit = new Criterion();
|
||||
Criterion crit = null;
|
||||
try {
|
||||
ut.begin();
|
||||
crit = new Criterion();
|
||||
crit.setName(name);
|
||||
crit.setMin_rating(min_rating);
|
||||
crit.setMax_rating(max_rating);
|
||||
crit.setConference(cm.getConference(confId));
|
||||
Conference conf = em.find(Conference.class, confId);
|
||||
crit.setConference(conf);
|
||||
em.persist(crit);
|
||||
em.flush();
|
||||
ut.commit();
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
ut.rollback();
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
e.printStackTrace();
|
||||
}
|
||||
return crit;
|
||||
}
|
||||
|
||||
public void updateCriterion(Criterion criterion) {
|
||||
em.merge(criterion);
|
||||
em.flush();
|
||||
try {
|
||||
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) {
|
||||
Criterion criterion = this.getCriterion(criterionId);
|
||||
try {
|
||||
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) {
|
||||
@@ -122,31 +125,65 @@ public class EvaluationManagerBean implements IEvaluationManager {
|
||||
|
||||
public Report addReport(int articleId, String commentPCMember, String commentAuthor, List<Rating> ratings,
|
||||
String refereeLogin) {
|
||||
um = getUserManager();
|
||||
am = getArticleManager();
|
||||
|
||||
Report report = new Report();
|
||||
report.setReferee(um.getUser(refereeLogin));
|
||||
report.setArticle(am.getArticle(articleId));
|
||||
Report report = null;
|
||||
try {
|
||||
ut.begin();
|
||||
report = new Report();
|
||||
User referee = em.find(User.class, refereeLogin);
|
||||
Article article = em.find(Article.class, articleId);
|
||||
report.setReferee(referee);
|
||||
report.setArticle(article);
|
||||
report.setCommentPCMember(commentPCMember);
|
||||
report.setCommentAuthor(commentAuthor);
|
||||
report.setRatings(ratings);
|
||||
|
||||
//Report report = new Report("dsd",commentAuthor,ratings,um.getUser(refereeLogin),am.getArticle(articleId));
|
||||
em.persist(report);
|
||||
em.flush();
|
||||
ut.commit();
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
ut.rollback();
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
e.printStackTrace();
|
||||
}
|
||||
return report;
|
||||
}
|
||||
|
||||
public void updateReport(Report report) {
|
||||
em.merge(report);
|
||||
em.flush();
|
||||
try {
|
||||
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) {
|
||||
Report report = this.getReport(reportId);
|
||||
try {
|
||||
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){
|
||||
@@ -180,11 +217,22 @@ public class EvaluationManagerBean implements IEvaluationManager {
|
||||
*/
|
||||
|
||||
public Rating addRating(Integer value, Integer criterionId, Integer reportId) {
|
||||
Criterion criterion = getCriterion(criterionId);
|
||||
Report report=getReport(reportId);
|
||||
Rating rating = new Rating(value,criterion,report);
|
||||
Rating rating = null;
|
||||
try {
|
||||
ut.begin();
|
||||
Criterion criterion = em.find(Criterion.class, criterionId);
|
||||
Report report = em.find(Report.class,reportId);
|
||||
rating = new Rating(value,criterion,report);
|
||||
em.persist(rating);
|
||||
em.flush();
|
||||
ut.commit();
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
ut.rollback();
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
e.printStackTrace();
|
||||
}
|
||||
return rating;
|
||||
}
|
||||
|
||||
@@ -195,14 +243,42 @@ public class EvaluationManagerBean implements IEvaluationManager {
|
||||
return q.getResultList();
|
||||
}
|
||||
public void removeRating(Criterion criterion, Report report) {
|
||||
Rating rating = em.find(Rating.class, new RatingPK(criterion,report));
|
||||
try {
|
||||
ut.begin();
|
||||
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);
|
||||
em.flush();
|
||||
ut.commit();
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
ut.rollback();
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public Rating updateRating(Rating rating) {
|
||||
em.merge(rating);
|
||||
em.flush();
|
||||
try {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user