Commits

Anonymous committed 85e92e0

Comments (0)

Files changed (14)

encuestame-business/src/main/java/org/encuestame/business/service/CommentService.java

 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.encuestame.core.config.EnMePlaceHolderConfigurer;
 import org.encuestame.core.service.AbstractBaseService;
 import org.encuestame.core.service.imp.ICommentService;
 import org.encuestame.core.service.imp.IPollService;
      */
     private Long VOTE_VALUE = 1L;
 
+    /** Time Range value by default. **/
+    private Integer TIME_RANGE_VALUE = 1;
+
 
     /*
      * (non-Javadoc)
 
     /*
      * (non-Javadoc)
+     * @see org.encuestame.core.service.imp.ICommentService#getTopRatedComments(org.encuestame.utils.enums.CommentsSocialOptions, java.lang.Integer, java.lang.Integer)
+     */
+    public List<CommentBean> getTopRatedComments(final CommentsSocialOptions socialCommentOption, final Integer maxResults,
+            final Integer start){
+        final Integer timeRange = EnMePlaceHolderConfigurer
+                .getIntegerProperty("comment.time.range") == null ? this.TIME_RANGE_VALUE
+                : EnMePlaceHolderConfigurer
+                        .getIntegerProperty("comment.time.range");
+
+        log.debug("ENME PROPERTY TIME RANGE ---> " + timeRange);
+        final List<CommentBean> commentBean = new ArrayList<CommentBean>();
+        final List<Comment> topCommentList = getCommentsOperations()
+                .getTopRatedComments(socialCommentOption,
+                        timeRange, maxResults, start);
+        commentBean.addAll(ConvertDomainBean
+                .convertListCommentDomainToBean(topCommentList));
+        return commentBean;
+    }
+
+    /*
+     * (non-Javadoc)
      * @see org.encuestame.core.service.imp.ICommentService#voteCommentSocialOption(java.lang.Long, org.encuestame.persistence.domain.CommentsSocialOptions)
      */
     public void voteCommentSocialOption(final Long commentId, final CommentsSocialOptions vote) throws EnMeNoResultsFoundException,

encuestame-business/src/test/java/org/encuestame/test/business/service/TestCommentService.java

 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
+import java.util.Calendar;
 import java.util.Date;
 import java.util.List;
+
 import org.encuestame.business.service.CommentService;
 import org.encuestame.core.service.imp.ICommentService;
 import org.encuestame.persistence.domain.Comment;
 import org.encuestame.persistence.domain.tweetpoll.TweetPoll;
 import org.encuestame.persistence.exception.EnMeExpcetion;
 import org.encuestame.persistence.exception.EnMeNoResultsFoundException;
-import org.encuestame.persistence.exception.EnMeTweetPollNotFoundException;
 import org.encuestame.test.business.security.AbstractSpringSecurityContext;
+import org.encuestame.utils.enums.CommentsSocialOptions;
 import org.encuestame.utils.web.CommentBean;
 import org.hibernate.HibernateException;
 import org.junit.Before;
     }
 
     /**
+     *Test get top rated comments.
+     * @throws EnMeNoResultsFoundException
+     */
+    @Test
+    public void testGetTopCommentsRated() throws EnMeNoResultsFoundException {
+        final Calendar myCal = Calendar.getInstance();
+        myCal.add(Calendar.DATE, -1);
+        createDefaultTweetPollCommentVoted("My top1 tweetPoll comment",
+                tweetPoll, getSpringSecurityLoggedUserAccount(), 150L, 420L,
+                myCal.getTime());
+        createDefaultTweetPollCommentVoted("My top2 tweetPoll comment",
+                tweetPoll, getSpringSecurityLoggedUserAccount(), 35L, 580L,
+                new Date());
+        myCal.add(Calendar.DATE, -2);
+        createDefaultTweetPollCommentVoted("My top3 tweetPoll comment",
+                tweetPoll, getSpringSecurityLoggedUserAccount(), 325L, 70L,
+                myCal.getTime());
+
+        final List<Comment> comments = getCommentsOperationsService()
+                .getCommentsbyTweetPoll(this.tweetPoll, this.MAX_RESULTS,
+                        this.START);
+        assertEquals("Should be equals", 7, comments.size());
+        final List<CommentBean> topList = getCommentsOperationsService()
+                .getTopRatedComments(CommentsSocialOptions.LIKE_VOTE,
+                        this.MAX_RESULTS, this.START);
+        assertEquals("Should be equals", 6, topList.size());
+    }
+
+    /**
      * @return the commentsOperationsService
      */
     public ICommentService getCommentsOperationsService() {

encuestame-core/src/main/java/org/encuestame/core/service/imp/ICommentService.java

      */
      void voteCommentSocialOption(final Long commentId, final CommentsSocialOptions vote) throws EnMeNoResultsFoundException,
         HibernateException, EnmeFailOperation;
+
+     /**
+      * Get top rated comments.
+      * @param socialCommentOption
+      * @param maxResults
+      * @param start
+      * @return
+      */
+     List<CommentBean> getTopRatedComments(final CommentsSocialOptions socialCommentOption, final Integer maxResults,
+             final Integer start);
 }

encuestame-core/src/main/resources/org/encuestame/core/config/encuestame-config.properties

 #-----------------------------------------------------------------------------
 # Updates link
 #-----------------------------------------------------------------------------
-update.link = http://www.encuestame.org/updates/check
+update.link = http://www.encuestame.org/updates/check
+
+comment.time.range = 2

encuestame-mvc/src/main/java/org/encuestame/mvc/controller/HomeController.java

 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import javax.swing.plaf.ListUI;
 
 import org.apache.commons.collections.ListUtils;
 import org.apache.commons.logging.Log;

encuestame-mvc/src/main/java/org/encuestame/mvc/controller/json/CommentJsonController.java

          }
          return returnData();
      }
+
+    @RequestMapping(value = "/api/common/comment/topRated.json", method = RequestMethod.POST)
+    @PreAuthorize("hasRole('ENCUESTAME_USER')")
+    public ModelMap getTopRatedComments(
+            @RequestParam(value = "commentOption", required = true) String commentOption,
+            @RequestParam(value = "max", required = false) Integer max,
+            @RequestParam(value = "start", required = false) Integer start,
+            HttpServletRequest request, HttpServletResponse response) {
+        try {
+
+            final Map<String, Object> jsonResponse = new HashMap<String, Object>();
+            final List<CommentBean> comments = getCommentService()
+                    .getTopRatedComments(
+                            CommentsSocialOptions
+                                    .getCommentsSocialOptions(commentOption),
+                            max, start);
+            jsonResponse.put("topComments", comments);
+            setItemResponse(jsonResponse);
+        } catch (Exception e) {
+            log.error(e);
+            setError(e.getMessage(), response);
+        }
+        return returnData();
+    }
 }

encuestame-mvc/src/test/java/org/encuestame/mvc/test/json/CommentJsonControllerTestCase.java

 package org.encuestame.mvc.test.json;
 
 import java.io.IOException;
+import java.util.Calendar;
 import java.util.Date;
 
 import javax.servlet.ServletException;
+
 import junit.framework.Assert;
+
 import org.encuestame.mvc.test.config.AbstractJsonMvcUnitBeans;
 import org.encuestame.persistence.domain.Comment;
 import org.encuestame.persistence.domain.question.Question;
 import org.json.simple.JSONArray;
 import org.json.simple.JSONObject;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 
 /**
          final JSONObject comment = (JSONObject) success.get("comment");
          Assert.assertEquals(comment.get("comment").toString(), "My Comment");
         }
+
+     /**
+      * Test get top rated comments.
+      * @throws ServletException
+      * @throws IOException
+      */
+     //@Test
+     public void testGetTopRatedComments() throws ServletException, IOException{
+         final Calendar myCal = Calendar.getInstance();
+         myCal.add(Calendar.DATE, -1);
+         createDefaultTweetPollCommentVoted("first comment",
+                 tweetPoll, getSpringSecurityLoggedUserAccount(), 150L, 420L,
+                 myCal.getTime());
+         createDefaultTweetPollCommentVoted("second comment",
+                 tweetPoll, getSpringSecurityLoggedUserAccount(), 35L, 580L,
+                 new Date());
+         myCal.add(Calendar.DATE, -2);
+         createDefaultTweetPollCommentVoted("third comment",
+                 tweetPoll, getSpringSecurityLoggedUserAccount(), 325L, 70L,
+                 myCal.getTime());
+
+
+         initService("/api/common/comment/topRated.json", MethodJson.GET);
+         setParameter("commentOption", "LIKE_VOTE");
+         setParameter("max", "10");
+         setParameter("start", "0");
+         final JSONObject response = callJsonService();
+         final JSONObject success = getSucess(response);
+         final JSONArray comments = (JSONArray) success.get("topComments");
+         System.out.println("COMMENTS JSON TEST-->" + comments.size());
+         //Assert.assertEquals(comments.size(), 4);
+
+     }
 }

encuestame-persistence/src/main/java/org/encuestame/persistence/dao/CommentsOperations.java

 import org.encuestame.persistence.domain.Comment;
 import org.encuestame.persistence.domain.security.UserAccount;
 import org.encuestame.persistence.domain.tweetpoll.TweetPoll;
+import org.encuestame.utils.enums.CommentsSocialOptions;
 import org.encuestame.utils.enums.TypeSearchResult;
 import org.hibernate.HibernateException;
 
      * @return
      */
     Long getTotalCommentsbyItem(final Long id, final TypeSearchResult itemType);
+
+    /**
+     * Get top rated comments.
+     * @param socialOption
+     * @param timeRange
+     * @param maxResults
+     * @param startResults
+     * @return
+     */
+    List<Comment> getTopRatedComments(
+            final CommentsSocialOptions socialOption, final Integer timeRange,
+            final Integer maxResults, final Integer startResults);
 }

encuestame-persistence/src/main/java/org/encuestame/persistence/dao/imp/AbstractHibernateDaoSupport.java

 package org.encuestame.persistence.dao.imp;
 
 import java.util.ArrayList;
+import java.util.Calendar;
 import java.util.Date;
 import java.util.List;
 import java.util.StringTokenizer;
     }
 
     /**
+     * Return the current date.
+     * @return
+     */
+    public Date getCurrentdDateTime(){
+        DateTime currentDate = new DateTime();
+        return currentDate.toDate();
+    }
+
+    /**
+     * Get comment time range.
+     * @param range
+     * @return
+     */
+    public Date getCommentTimeRange(final Integer range){
+        final Calendar cal = Calendar.getInstance();
+        cal.add(Calendar.DATE, -range);
+        return cal.getTime();
+    }
+
+    /**
      * Fetch by multi query parser full text.
      * @param keyword keyword to search
      * @param fields list of fields.

encuestame-persistence/src/main/java/org/encuestame/persistence/dao/imp/CommentDao.java

 import org.apache.lucene.analysis.SimpleAnalyzer;
 import org.encuestame.persistence.dao.CommentsOperations;
 import org.encuestame.persistence.domain.Comment;
-import org.encuestame.persistence.domain.Hit;
 import org.encuestame.persistence.domain.security.UserAccount;
 import org.encuestame.persistence.domain.tweetpoll.TweetPoll;
+import org.encuestame.utils.enums.CommentsSocialOptions;
 import org.encuestame.utils.enums.TypeSearchResult;
 import org.hibernate.Criteria;
 import org.hibernate.HibernateException;
 
     /*
      * (non-Javadoc)
+     * @see org.encuestame.persistence.dao.CommentsOperations#getTopRatedComments(org.encuestame.utils.enums.CommentsSocialOptions, java.lang.Integer, java.lang.Integer, java.lang.Integer)
+     */
+    @SuppressWarnings("unchecked")
+    public List<Comment> getTopRatedComments(
+            final CommentsSocialOptions socialOption, final Integer timeRange,
+            final Integer maxResults, final Integer startResults) {
+        final DetachedCriteria criteria = DetachedCriteria
+                .forClass(Comment.class);
+        criteria.add(Restrictions.between("createdAt",
+                getCommentTimeRange(timeRange), getNextDayMidnightDate()));
+        if (socialOption.equals(CommentsSocialOptions.LIKE_VOTE)) {
+            criteria.addOrder(Order.desc("likeVote"));
+        } else {
+            criteria.addOrder(Order.desc("dislikeVote"));
+        }
+        return (List<Comment>) filterByMaxorStart(criteria, maxResults,
+                startResults);
+    }
+
+    /*
+     * (non-Javadoc)
      * @see org.encuestame.persistence.dao.CommentsOperations#getListCommentsByKeyword(java.lang.String, java.lang.Integer, java.lang.Long[])
      */
     @SuppressWarnings("unchecked")

encuestame-persistence/src/main/java/org/encuestame/persistence/domain/AccessRate.java

     /** **/
     private String ipAddress;
 
+    /** {@link Comment} **/
+    private Comment comments;
+
     /**
      * @return the tweetPollRateId
      */
     public void setIpAddress(String ipAddress) {
         this.ipAddress = ipAddress;
     }
+
+    /**
+     * @return the comments
+     */
+    @ManyToOne(cascade = CascadeType.MERGE)
+    public Comment getComments() {
+        return comments;
+    }
+
+    /**
+     * @param comments the comments to set
+     */
+    public void setComments(final Comment comments) {
+        this.comments = comments;
+    }
 }

encuestame-persistence/src/main/resources/properties-test/encuestame-test-config.properties

 # Security
 #-----------------------------------------------------------------------------
 account.expire.limit = 7
+
+comment.time.range = 2

encuestame-persistence/src/test/java/org/encuestame/test/config/AbstractBase.java

             final Survey survey,
             final Poll poll,
             final UserAccount user,
-            final Long dislikeVote){
+            final Long dislikeVote,
+            final Date createdAt){
            final Comment comment = new Comment();
            comment.setComment(comm);
-           comment.setCreatedAt(new Date());
+           comment.setCreatedAt(createdAt);
            comment.setLikeVote(likeVote);
            comment.setDislikeVote(dislikeVote);
            comment.setPoll(poll);
             final String comment,
             final TweetPoll tpoll,
             final UserAccount userAcc){
-        return this.createComment(comment, 0L, tpoll, null, null, userAcc, 0L);
+        return this.createComment(comment, 0L, tpoll, null, null, userAcc, 0L , new Date());
+    }
+
+    /**
+     *
+     * @param comment
+     * @param tpoll
+     * @param userAcc
+     * @param likeVote
+     * @param dislikeVote
+     * @return
+     */
+    public Comment createDefaultTweetPollCommentVoted(
+            final String comment,
+            final TweetPoll tpoll,
+            final UserAccount userAcc,
+            final Long likeVote,
+            final Long dislikeVote,
+            final Date createdAt){
+        return this.createComment(comment, likeVote, tpoll, null, null, userAcc, dislikeVote, createdAt);
     }
 
     /**
             final String comment,
             final Poll poll,
             final UserAccount userAcc){
-        return this.createComment(comment, 0L, null, null, poll, userAcc, 0L);
+        return this.createComment(comment, 0L, null, null, poll, userAcc, 0L , new Date());
     }
 
     /**
             final String comment,
             final Survey survey,
             final UserAccount userAcc){
-        return this.createComment(comment, 0L, null, survey, null, userAcc, 0L);
+        return this.createComment(comment, 0L, null, survey, null, userAcc, 0L, new Date());
     }
 
     /**

encuestame-persistence/src/test/java/org/encuestame/test/persistence/dao/TestCommentDao.java

 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
+import java.util.Calendar;
+import java.util.Date;
 import java.util.List;
 
 import org.encuestame.persistence.dao.imp.CommentDao;
 import org.encuestame.persistence.domain.security.UserAccount;
 import org.encuestame.persistence.domain.tweetpoll.TweetPoll;
 import org.encuestame.test.config.AbstractBase;
+import org.encuestame.utils.enums.CommentsSocialOptions;
 import org.encuestame.utils.enums.TypeSearchResult;
 import org.junit.Before;
 import org.junit.Test;
         this.user = createUserAccount("isabelle", createAccount());
         this.question = createQuestion("When did you go last weekend", "");
         this.tpoll = createPublishedTweetPoll(user.getAccount(), this.question);
-        this.comment = createDefaultTweetPollComment("I was working", this.tpoll, user);
+        this.comment = createDefaultTweetPollCommentVoted("I was working", this.tpoll, user, 150L, 985L, new Date());
     }
 
     /**
                         TypeSearchResult.TWEETPOLL);
         assertEquals("Should be equals", 2, totalComment.intValue());
     }
+
+    /**
+     * Test get top rated comments.
+     */
+    @Test
+    public void testGetTopRatedComments(){
+        final Calendar myDate = Calendar.getInstance();
+        myDate.add(Calendar.DATE, -1);
+        this.comment.setCreatedAt(myDate.getTime());
+        getCommentsOperations().saveOrUpdate(this.comment);
+        //two days before
+        myDate.add(Calendar.DATE, -2);
+        createDefaultTweetPollCommentVoted("Comment 1", tpoll, this.user, 170L, 300L, myDate.getTime());
+        createDefaultTweetPollCommentVoted("Comment 2", tpoll, this.user, 540L, 580L, myDate.getTime());
+
+        //three days before
+        myDate.add(Calendar.DATE, -3);
+        createDefaultTweetPollCommentVoted("Comment 3", tpoll, this.user, 223L, 160L, myDate.getTime());
+        createDefaultTweetPollCommentVoted("Comment 4", tpoll, this.user, 389L, 60L, myDate.getTime());
+
+        //four days before
+        myDate.add(Calendar.DATE, -4);
+        createDefaultTweetPollCommentVoted("Comment 5", tpoll, this.user, 110L, 630L, myDate.getTime());
+        //five days before
+        myDate.add(Calendar.DATE, -5);
+        createDefaultTweetPollCommentVoted("Comment 6", tpoll, this.user, 15L, 155L, myDate.getTime());
+
+        // Get total tweetpoll comments published.
+        final List<Comment> totalCommentsPublished = getCommentsOperations().getCommentsbyTweetPoll(tpoll, 15, 0);
+        assertEquals("Should be equals", 7, totalCommentsPublished.size());
+
+        // Get total rated comments published.
+        final List<Comment> getLikeTopRatedComments = getCommentsOperations().getTopRatedComments(CommentsSocialOptions.LIKE_VOTE, 3, 15, 0);
+        assertEquals("Should be equals", 3, getLikeTopRatedComments.size());
+
+        final List<Comment> getDisLikeTopRatedComments = getCommentsOperations().getTopRatedComments(CommentsSocialOptions.DISLIKE_VOTE, 3, 15, 0);
+        assertEquals("Should be equals", 3, getDisLikeTopRatedComments.size());
+    }
 }