use of org.mamute.model.ReputationEventContext in project mamute by caelum.
the class ReputationEventDAO method fetchContextData.
@SuppressWarnings("unchecked")
private List<Object[]> fetchContextData(List<Object[]> rows) {
// separate id lists by class
List<Object[]> organizedData = new ArrayList<>();
for (Object[] row : rows) {
String contextClass = (String) row[0];
Long contextId = (Long) row[1];
ReputationEventContext context = (ReputationEventContext) session.createQuery("from " + contextClass + " where id = :id").setParameter("id", contextId).uniqueResult();
// Skip invalid context entries (because the related item can have been deleted).
if (context == null) {
continue;
}
organizedData.add(new Object[] { context, row[2], row[3] });
}
return organizedData;
}
use of org.mamute.model.ReputationEventContext in project mamute by caelum.
the class VoteDAOTest method should_find_news_from_votable.
@Test
public void should_find_news_from_votable() throws Exception {
News news = new NewsBuilder().build();
ReputationEventContext context = votes.contextOf(news);
assertEquals(news, context);
}
use of org.mamute.model.ReputationEventContext in project mamute by caelum.
the class VotingMachine method unRegister.
public void unRegister(Votable votable, Vote current, Class<?> votableType) {
User voter = current.getAuthor();
User votableAuthor = votable.getAuthor();
ReputationEventContext eventContext = votes.contextOf(votable);
if (votable.getAuthor().getId().equals(voter.getId())) {
throw new IllegalArgumentException("an author can't unvote its own votable since it can't even vote on it");
}
Vote previous = votes.previousVoteFor(votable.getId(), voter, votableType);
boolean shouldCountKarma = voteChecker.shouldCountKarma(voter, votableAuthor, current);
/* O previous vai sempre existir nessa caso !! ( o ideal :] ) */
if (previous != null) {
ReputationEvent receivedVote = new ReceivedVoteEvent(previous.getType(), votable, eventContext, shouldCountKarma).reputationEvent();
votableAuthor.descreaseKarma(karmaCalculator.karmaFor(receivedVote));
ReputationEvent votedAtSomething = new VotedAtSomethingEvent(previous, eventContext).reputationEvent();
voter.descreaseKarma(karmaCalculator.karmaFor(votedAtSomething));
reputationEvents.delete(receivedVote);
reputationEvents.delete(votedAtSomething);
votable.remove(previous);
}
// ReputationEvent receivedVote = new ReceivedVoteEvent(current.getType(), votable, eventContext, shouldCountKarma).reputationEvent();
// votableAuthor.increaseKarma(karmaCalculator.karmaFor(receivedVote));
// ReputationEvent votedAtSomething = new VotedAtSomethingEvent(current, eventContext).reputationEvent();
// voter.increaseKarma(karmaCalculator.karmaFor(votedAtSomething));
// reputationEvents.save(receivedVote);
// reputationEvents.save(votedAtSomething);
// if (votable.getVoteCount() <= -5) {
// votable.getQuestion().remove();
// retrieveDownvote.retrieveKarma(votable.getVotes());
// }
}
use of org.mamute.model.ReputationEventContext in project mamute by caelum.
the class VoteDAO method contextOf.
// TODO: refactor?
public ReputationEventContext contextOf(Votable votable) {
boolean isNews = News.class.isAssignableFrom(votable.getClass());
if (isNews) {
return (ReputationEventContext) votable;
}
boolean isQuestionOrAnswer = !Comment.class.isAssignableFrom(votable.getClass());
if (isQuestionOrAnswer) {
return votable.getQuestion();
}
Question question = (Question) session.createQuery("select q from Question q join q.comments.comments c where c=:comment").setParameter("comment", votable).uniqueResult();
if (question != null) {
return question;
}
question = (Question) session.createQuery("select a.question from Answer a join a.comments.comments c where c=:comment").setParameter("comment", votable).uniqueResult();
if (question != null) {
return question;
}
ReputationEventContext ctx = (ReputationEventContext) session.createQuery("select news from News news join news.comments.comments c where c=:comment").setParameter("comment", votable).uniqueResult();
return ctx;
}
use of org.mamute.model.ReputationEventContext in project mamute by caelum.
the class VotingMachine method register.
public void register(Votable votable, Vote current, Class<?> votableType) {
User voter = current.getAuthor();
User votableAuthor = votable.getAuthor();
ReputationEventContext eventContext = votes.contextOf(votable);
if (votable.getAuthor().getId().equals(voter.getId())) {
throw new IllegalArgumentException("an author can't vote its own votable");
}
Vote previous = votes.previousVoteFor(votable.getId(), voter, votableType);
boolean shouldCountKarma = voteChecker.shouldCountKarma(voter, votableAuthor, current);
if (previous != null) {
ReputationEvent receivedVote = new ReceivedVoteEvent(previous.getType(), votable, eventContext, shouldCountKarma).reputationEvent();
votableAuthor.descreaseKarma(karmaCalculator.karmaFor(receivedVote));
ReputationEvent votedAtSomething = new VotedAtSomethingEvent(previous, eventContext).reputationEvent();
voter.descreaseKarma(karmaCalculator.karmaFor(votedAtSomething));
reputationEvents.delete(receivedVote);
reputationEvents.delete(votedAtSomething);
}
votable.substitute(previous, current);
ReputationEvent receivedVote = new ReceivedVoteEvent(current.getType(), votable, eventContext, shouldCountKarma).reputationEvent();
votableAuthor.increaseKarma(karmaCalculator.karmaFor(receivedVote));
ReputationEvent votedAtSomething = new VotedAtSomethingEvent(current, eventContext).reputationEvent();
voter.increaseKarma(karmaCalculator.karmaFor(votedAtSomething));
reputationEvents.save(receivedVote);
reputationEvents.save(votedAtSomething);
if (votable.getVoteCount() <= -5) {
votable.getQuestion().remove();
retrieveDownvote.retrieveKarma(votable.getVotes());
}
}
Aggregations