Search in sources :

Example 1 with ReputationEventContext

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;
}
Also used : ArrayList(java.util.ArrayList) ReputationEventContext(org.mamute.model.ReputationEventContext)

Example 2 with ReputationEventContext

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);
}
Also used : NewsBuilder(org.mamute.builder.NewsBuilder) News(org.mamute.model.News) ReputationEventContext(org.mamute.model.ReputationEventContext) Test(org.junit.Test)

Example 3 with ReputationEventContext

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());
// }
}
Also used : Vote(org.mamute.model.Vote) User(org.mamute.model.User) ReceivedVoteEvent(org.mamute.reputation.rules.ReceivedVoteEvent) VotedAtSomethingEvent(org.mamute.reputation.rules.VotedAtSomethingEvent) ReputationEventContext(org.mamute.model.ReputationEventContext) ReputationEvent(org.mamute.model.ReputationEvent)

Example 4 with ReputationEventContext

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;
}
Also used : Comment(org.mamute.model.Comment) ReputationEventContext(org.mamute.model.ReputationEventContext) Question(org.mamute.model.Question)

Example 5 with ReputationEventContext

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());
    }
}
Also used : Vote(org.mamute.model.Vote) User(org.mamute.model.User) ReceivedVoteEvent(org.mamute.reputation.rules.ReceivedVoteEvent) VotedAtSomethingEvent(org.mamute.reputation.rules.VotedAtSomethingEvent) ReputationEventContext(org.mamute.model.ReputationEventContext) ReputationEvent(org.mamute.model.ReputationEvent)

Aggregations

ReputationEventContext (org.mamute.model.ReputationEventContext)5 ReputationEvent (org.mamute.model.ReputationEvent)2 User (org.mamute.model.User)2 Vote (org.mamute.model.Vote)2 ReceivedVoteEvent (org.mamute.reputation.rules.ReceivedVoteEvent)2 VotedAtSomethingEvent (org.mamute.reputation.rules.VotedAtSomethingEvent)2 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 NewsBuilder (org.mamute.builder.NewsBuilder)1 Comment (org.mamute.model.Comment)1 News (org.mamute.model.News)1 Question (org.mamute.model.Question)1