Search in sources :

Example 1 with Karma

use of net.jforum.entities.Karma in project jforum2 by rafaelsteil.

the class KarmaAction method insert.

public void insert() {
    if (!SecurityRepository.canAccess(SecurityConstants.PERM_KARMA_ENABLED)) {
        this.error("Karma.featureDisabled", null);
        return;
    }
    int postId = this.request.getIntParameter("post_id");
    int fromUserId = SessionFacade.getUserSession().getUserId();
    PostDAO pm = DataAccessDriver.getInstance().newPostDAO();
    Post p = pm.selectById(postId);
    if (fromUserId == SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID)) {
        this.error("Karma.anonymousIsDenied", p);
        return;
    }
    if (p.getUserId() == fromUserId) {
        this.error("Karma.cannotSelfVote", p);
        return;
    }
    KarmaDAO km = DataAccessDriver.getInstance().newKarmaDAO();
    if (!km.userCanAddKarma(fromUserId, postId)) {
        this.error("Karma.alreadyVoted", p);
        return;
    }
    // Check range
    int points = this.request.getIntParameter("points");
    if (points < SystemGlobals.getIntValue(ConfigKeys.KARMA_MIN_POINTS) || points > SystemGlobals.getIntValue(ConfigKeys.KARMA_MAX_POINTS)) {
        this.error("Karma.invalidRange", p);
        return;
    }
    Karma karma = new Karma();
    karma.setFromUserId(fromUserId);
    karma.setPostUserId(p.getUserId());
    karma.setPostId(postId);
    karma.setTopicId(p.getTopicId());
    karma.setPoints(points);
    km.addKarma(karma);
    p.setKarma(new KarmaStatus(p.getId(), points));
    if (SystemGlobals.getBoolValue(ConfigKeys.POSTS_CACHE_ENABLED)) {
        PostRepository.update(p.getTopicId(), PostCommon.preparePostForDisplay(p));
    }
    JForumExecutionContext.setRedirect(this.urlToTopic(p));
}
Also used : PostDAO(net.jforum.dao.PostDAO) Post(net.jforum.entities.Post) KarmaDAO(net.jforum.dao.KarmaDAO) Karma(net.jforum.entities.Karma) KarmaStatus(net.jforum.entities.KarmaStatus)

Aggregations

KarmaDAO (net.jforum.dao.KarmaDAO)1 PostDAO (net.jforum.dao.PostDAO)1 Karma (net.jforum.entities.Karma)1 KarmaStatus (net.jforum.entities.KarmaStatus)1 Post (net.jforum.entities.Post)1