Commits

Juan Carlos Picado Herrera  committed d2ab4ea Merge

Merge branch 'dianmorales' of github.com:encuestame/encuestame into development

  • Participants
  • Parent commits 6c928b9, 69dbea2

Comments (0)

Files changed (13)

File encuestame-business/src/main/java/org/encuestame/business/service/FrontEndService.java

 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
-
 import javax.servlet.http.HttpServletRequest;
-
 import org.apache.log4j.Logger;
 import org.encuestame.business.service.imp.IFrontEndService;
 import org.encuestame.core.util.ConvertDomainBean;
+import org.encuestame.core.util.EnMeUtils;
 import org.encuestame.persistence.dao.SearchPeriods;
 import org.encuestame.persistence.domain.HashTag;
 import org.encuestame.persistence.domain.HashTagHits;
         log.debug("Max Results HashTag -----> "+maxResults);
         List<HashTag> tags = new ArrayList<HashTag>();
         tags.addAll(getHashTagDao().getHashTags(maxResults, start, tagCriteria));
+        for (HashTag hashTag : tags) {
+            final Integer tagFrecuency = getHashTagFrecuency(hashTag.getHashTagId(),2);
+            final List<Object[]> maxMin = getHashTagDao().getMaxMinTagFrecuency();
+            final Integer maxFrecuency = (Integer) maxMin.get(0)[0];
+            final Integer minFrecuency = (Integer) maxMin.get(0)[1];
+            final Double logFrecuency = EnMeUtils.calculateSizeTag(tagFrecuency, maxFrecuency, minFrecuency);
+            hashTag.setSize(logFrecuency.longValue());
+        }
+
         log.debug("Hashtag total size ---> "+tags.size());
         hashBean.addAll(ConvertDomainBean.convertListHashTagsToBean(tags));
 
     }
 */
     /**
-     * Get hashTag relevance.
+     * Get hashTag counter.
      * @param hashTagId
      * @param limit
      * @return
      */
-    public Integer getHashTagRelevance(final Long hashTagId, final Integer limit){
+    public Integer getHashTagFrecuency(final Long hashTagId, final Integer limit){
         final Integer totalRelTweetPoll;
-        final Integer relevance;
+        final Integer frecuency;
         final List<TweetPoll> tweetPolls = getTweetPollDao().getTweetpollByHashTagId(hashTagId, limit, "");
         totalRelTweetPoll = tweetPolls.size();
-        relevance = totalRelTweetPoll;
+        frecuency = totalRelTweetPoll;
         //TODO:Pending count relevance hashtags for polls and surveys.
-        return relevance;
+        return frecuency;
+    }
+
+    /**
+     * Get Hit by Hash Tag
+     * @param hashTagId
+     * @return
+     */
+    public Long getHitbyHashTag(final Long hashTagId){
+        final Integer hit;
+        final HashTag tagHit = getHashTagDao().getHashTagById(hashTagId);
+        return (tagHit.getHits());
     }
 
     /**
             // TODO: handle exception
             e.printStackTrace();
         }
-
         return tagHit;
     }
 
-
-    public void registerHashTagHit(final String tagName, final String ip, final String username){
-        final Date hitd = new Date();
+    /**
+     * Register hash tag hit.
+     * @param tagName
+     * @param hitDate
+     * @param ipAddress
+     * @return
+     */
+    public Boolean registerHashTagHit(final String tagName, final String ip, final String username){
         final HashTagHits hashHit ;
-        final HashTag tag;
         Long hitCount = 1L;
+        Boolean register = false;
         try {
             if((ip!=null) || (tagName!=null) ){
-                hashHit = this.newHashTagHit(tagName, hitd, ip);
+                hashHit = this.newHashTagHit(tagName, new Date(), ip);
                 if (hashHit!=null){
-                    tag = getHashTagDao().getHashTagByName(tagName);
+                    final HashTag tag = getHashTagDao().getHashTagByName(tagName);
                     hitCount = tag.getHits()+hitCount;
                     tag.setHits(hitCount);
+                    register = true;
                 }
             }
         } catch (Exception e) {
             e.printStackTrace();
             // TODO: handle exception
         }
-
+        return register;
     }
 
     /**
         tagHitsDomain.setHitDate(hitDate);
         tagHitsDomain.setHashTagId(getHashTagDao().getHashTagByName(tagName));
         tagHitsDomain.setIpAddress(ipAddress);
-      //  tagHitsDomain.setUserAccount(getUserAccountLogged());
+        tagHitsDomain.setUserAccount(getUserAccountLogged());
         this.getFrontEndDao().saveOrUpdate(tagHitsDomain);
         return tagHitsDomain;
     }

File encuestame-business/src/main/java/org/encuestame/business/service/imp/IFrontEndService.java

  */
 package org.encuestame.business.service.imp;
 
-import java.util.Date;
 import java.util.List;
-
 import javax.servlet.http.HttpServletRequest;
-
 import org.encuestame.core.service.ServiceOperations;
-import org.encuestame.persistence.domain.HashTagHits;
 import org.encuestame.persistence.exception.EnMeNoResultsFoundException;
 import org.encuestame.persistence.exception.EnMeSearchException;
 import org.encuestame.utils.web.HashTagBean;
      * Register hashTag hits.
      * @param tagName
      * @param ipAddress
+     * @param username
+     */
+    Boolean registerHashTagHit(final String tagName, final String ip, final String username);
+
+    /**
+     * Get hashTag Counter.
+     * @param hashTagId
+     * @param limit
+     * @return
      */
-    void registerHashTagHit(final String tagName, final String ip, final String username);
+    Integer getHashTagFrecuency(final Long hashTagId, final Integer limit);
 }

File encuestame-business/src/test/java/org/encuestame/test/business/service/TestFrontEndService.java

 package org.encuestame.test.business.service;
 
 import org.encuestame.business.service.FrontEndService;
+import org.encuestame.business.service.imp.IFrontEndService;
+import org.encuestame.persistence.domain.HashTag;
+import org.encuestame.persistence.domain.HashTagHits;
+import org.encuestame.persistence.domain.security.UserAccount;
 import org.encuestame.test.business.service.config.AbstractServiceBase;
+import org.junit.Before;
 import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
 
 /**
  * Test for {@link FrontEndService}.
  */
 public class TestFrontEndService extends AbstractServiceBase{
 
+    @Autowired
+    private IFrontEndService frontEndService;
+
+    /** {@link HashTag} **/
+    private HashTag hashTag;
+
+    /** {@link HashTagHits} **/
+    private HashTagHits hashTagHit;
+
+    /** {@link UserAccount}. **/
+    private UserAccount secondary;
+
+    /** ip address. **/
+    final String ipAddress = "192.168.1.1";
+
+    final String ipAddress2 = "192.168.1.2";
+
+    @Before
+    public void initData(){
+        this.secondary = createUserAccount("paola", createAccount());
+        this.hashTag = createHashTag("software");
+        this.hashTagHit = createHashTagHit(hashTag, this.ipAddress, this.secondary);
+        System.out.println("hashTag ID --->"+ hashTag.getHashTagId());
+    }
+
+    @Test
+    public void testCheckPreviousHashTagHit(){
+        flushIndexes();
+        final Boolean previousRecord = getFrontEndService().checkPreviousHashTagHit(this.ipAddress);
+        System.out.println("Previous record exists? --> "+ previousRecord + "IP" + this.ipAddress);
+        final Boolean previousRecord2 = getFrontEndService().checkPreviousHashTagHit(this.ipAddress2);
+        System.out.println("Previous record exists 2? --> "+ previousRecord2 + "IP" + this.ipAddress2);
+    }
+
     @Test
-    public void testGetHashTagHitbyIp(){
-            System.out.println("testing");
+    public void testRegisterHashTagHit(){
+        System.out.println(" previous tag hit --> "+ this.hashTag.getHits());
+        final Boolean registerHit = getFrontEndService().registerHashTagHit(this.hashTag.getHashTag(), this.ipAddress,
+                                    this.secondary.getUsername());
+        System.out.println(" Se agrego registro nuevo? --> "+ registerHit);
+        System.out.println(" tag hit anterior 2--> "+ this.hashTag.getHits());
+        getFrontEndService().registerHashTagHit(this.hashTag.getHashTag(), this.ipAddress2,
+                             this.secondary.getUsername());
+        System.out.println(" tag hit posterior --> "+ this.hashTag.getHits());
     }
 
+    /**
+    * @return the frontEndService
+    */
+    public IFrontEndService getFrontEndService() {
+        return frontEndService;
+    }
+
+    /**
+    * @param frontEndService the frontEndService to set
+    */
+    public void setFrontEndService(IFrontEndService frontEndService) {
+        this.frontEndService = frontEndService;
+    }
 }

File encuestame-core/src/main/java/org/encuestame/core/util/ConvertDomainBean.java

         unitHashTag.setId(hashTag.getHashTagId());
         unitHashTag.setHits(hashTag.getHits());
         //TODO: ENCUESTAME-191
-        int x = (10 + (int)(Math.random() * ((40) - 5) + 10)); //TEMP.
-        log.debug("random hastag number "+x);
-        unitHashTag.setSize(x);
+        // int x = (10 + (int)(Math.random() * ((40) - 5) + 10)); //TEMP.
+        log.debug("random hastag number "+hashTag.getSize());
+        unitHashTag.setSize(hashTag.getSize().intValue());
         return unitHashTag;
     }
 

File encuestame-core/src/main/java/org/encuestame/core/util/EnMeUtils.java

             return "0.00%";
         }
     }
+
+    /**
+     *
+     * @param frecuency Number of times the label has been used in polls, survey or tweetPolls
+     * @param frecMax : Maximum number of frequency.
+     * @param frecMin : Minimum number of frecuency.
+     * @return
+     */
+    public static Double calculateSizeTag(final Integer frecuency, final Integer frecMax, final Integer frecMin){
+
+        final Integer frecDiff = frecMax - frecMin;
+        final Integer v = 30;
+        final Long perRelative =   (long) ((frecuency - (frecMax - frecMin) / frecDiff) * v);
+        final double perLog = (Math.log(perRelative)/Math.log(2));
+        return perLog;
+    }
 }

File encuestame-mvc/src/main/java/org/encuestame/mvc/view/HashTagController.java

         final IFrontEndService service = getFrontService();
         log.debug("hashTag Name ---> "+name);
         name = filterValue(name);
-        final String IP = getIpClient();
+        final String IP = "192.168.1.3";
+            //getIpClient();
         log.info("IP" + IP);
 
         // Search HashTag hits.
         boolean hashTagVisite = service.checkPreviousHashTagHit(IP);
         // TODO: Check that previous hash Tag hit has been visited the same day.
         if (!hashTagVisite) {
-            service.registerHashTagHit(name, IP, "pao");
+           final Boolean tagHit = service.registerHashTagHit(name, IP, "paola");
+           System.out.println(" Registro hit? ---> "+tagHit);
+
         }
         try {
             final HashTagBean tag = service.getHashTagItem(name);

File encuestame-mvc/src/test/java/org/encuestame/mvc/test/view/HashTagControllerTestCase.java

+/*
+ ************************************************************************************
+ * Copyright (C) 2001-2011 encuestame: system online surveys Copyright (C) 2011
+ * encuestame Development Team.
+ * Licensed under the Apache Software License version 2.0
+ * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to  in writing,  software  distributed
+ * under the License is distributed  on  an  "AS IS"  BASIS,  WITHOUT  WARRANTIES  OR
+ * CONDITIONS OF ANY KIND, either  express  or  implied.  See  the  License  for  the
+ * specific language governing permissions and limitations under the License.
+ ************************************************************************************
+ */
+package org.encuestame.mvc.test.view;
+import static org.springframework.test.web.ModelAndViewAssert.assertViewName;
+
+import org.encuestame.mvc.controller.json.MethodJson;
+import org.encuestame.mvc.test.config.AbstractMvcUnitBeans;
+import org.encuestame.mvc.view.DashBoardController;
+import org.encuestame.mvc.view.HashTagController;
+import org.encuestame.persistence.domain.HashTag;
+import org.encuestame.persistence.domain.security.UserAccount;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.web.servlet.ModelAndView;
+
+/**
+ *  HashTag Controller TestCase.
+ * @author Morales, Diana Paola paolaATencuestame.org
+ * @since July 13, 2011
+ */
+public class HashTagControllerTestCase extends AbstractMvcUnitBeans{
+
+    @Autowired
+    private HashTagController hashTagController;
+
+    @Before
+       public void initMVc() {
+
+       }
+
+    @Test
+    public void testHashTagController() throws Exception {
+        final HashTag hashTag = createHashTag("software");
+        request = new MockHttpServletRequest(MethodJson.GET.toString(), "/tag/software");
+        final ModelAndView mav = handlerAdapter.handle(request, response,
+                hashTagController);
+        assertViewName(mav, "tag/detail");
+
+    }
+}

File encuestame-persistence/src/main/java/org/encuestame/persistence/dao/IHashTagDao.java

      * @return
      */
     List<HashTag> getHashTags( final Integer maxResults,final Integer start, final String tagCriteria);
+
+    /**
+     * Get hashTag by Id.
+     * @param hashTagId
+     * @return
+     * @throws HibernateException
+     */
+    HashTag getHashTagById(final Long hashTagId) throws HibernateException;
+
+    /**
+     * Get max-min tag frecuency.
+     * @return
+     */
+    List<Object[]>  getMaxMinTagFrecuency();
 }

File encuestame-persistence/src/main/java/org/encuestame/persistence/dao/imp/HashTagDao.java

 import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
+
 import org.apache.commons.collections.set.ListOrderedSet;
 import org.apache.lucene.analysis.SimpleAnalyzer;
 import org.encuestame.persistence.dao.IHashTagDao;
                 });
         return searchResult;
     }
+
+    /**
+     * Get hashTag by Id.
+     * @param hashTagId
+     * @return
+     * @throws HibernateException
+     */
+    public HashTag getHashTagById(final Long hashTagId) throws HibernateException {
+        return (HashTag) getHibernateTemplate().get(HashTag.class, hashTagId);
+    }
+
+    /**
+     * Get max-min tag frecuency.
+     * @param tag
+     * @param filter
+     * @return
+     */
+    @SuppressWarnings("unchecked")
+    public List<Object[]> getMaxMinTagFrecuency(){
+        final String maxHit = "Select max(hits) as maximum, min(hits) as minimum from HashTag";
+        return getHibernateTemplate().find(maxHit);
+    }
 }

File encuestame-persistence/src/main/java/org/encuestame/persistence/domain/HashTag.java

 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
-import javax.persistence.JoinColumn;
-import javax.persistence.JoinTable;
 import javax.persistence.ManyToMany;
 import javax.persistence.Table;
-
 import org.encuestame.persistence.domain.tweetpoll.TweetPoll;
 import org.hibernate.search.annotations.DocumentId;
 import org.hibernate.search.annotations.Field;

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

      * @param ipAddress
      * @return
      */
-    public HashTagHits createHashTagHit(final HashTag hashTag, final String ipAddress){
+    public HashTagHits createHashTagHit(final HashTag hashTag, final String ipAddress, final UserAccount userAcc){
        final Date hitDate = new Date();
        final HashTagHits tagHits = new HashTagHits();
        tagHits.setHitDate(hitDate);
        tagHits.setIpAddress(ipAddress);
        tagHits.setHashTagId(hashTag);
+       tagHits.setUserAccount(userAcc);
        getHashTagDao().saveOrUpdate(tagHits);
        return tagHits;
     }

File encuestame-persistence/src/test/java/org/encuestame/test/persistence/dao/TestFrontEndDao.java

 import org.encuestame.persistence.dao.imp.FrontEndDao;
 import org.encuestame.persistence.domain.HashTag;
 import org.encuestame.persistence.domain.HashTagHits;
+import org.encuestame.persistence.domain.security.UserAccount;
 import org.encuestame.test.config.AbstractBase;
 import org.junit.Before;
 import org.junit.Test;
     /** {@link HashTagHits} **/
     private HashTagHits hashTagHit;
 
+    /** {@link UserAccount}. **/
+    private UserAccount secondary;
+
     final String ipAddress = "192.168.1.1";
 
     @Before
     public void initData(){
+        this.secondary = createUserAccount("paola", createAccount());
         this.hashTag = createHashTag("software");
-        this.hashTagHit = createHashTagHit(hashTag, ipAddress);
+        this.hashTagHit = createHashTagHit(hashTag, ipAddress, this.secondary);
       /*  System.out.println("hashTag ID --->"+ hashTag.getHashTagId());
         System.out.println("hashTagHit IP --->"+ hashTagHit.getIpAddress());
         System.out.println("hashTagHit ID --->"+ hashTagHit.getHitId());
         final List<HashTagHits> hitsbyIp = getFrontEndDao().getHashTagsHitByIp(this.ipAddress);
         System.out.print("SIZE HASHTAG hit---> "+ hitsbyIp.size());
         assertNotNull(hitsbyIp);
-        //System.out.println("HashTagHits data ---> "+ hitsbyIp.get(0).getIpAddress());
-       // assertEquals("Should be equals", hitsbyIp.get(0).getIpAddress(), this.ipAddress);
+        assertEquals("Should be equals", hitsbyIp.get(0).getIpAddress(), this.ipAddress);
+        assertEquals("Should be equals", hitsbyIp.size(),1);
     }
 }

File encuestame-persistence/src/test/java/org/encuestame/test/persistence/dao/TestHashTagDao.java

 
     @Before
     public void initData(){
-        this.hashTag = createHashTag("software");
+        this.hashTag = createHashTag("software",10L);
     }
 
     /**
         assertEquals("Should be equals", hashTag3.getHashTag(), expHas4.getHashTag());
     }
 
+    /**
+     * Test get max-min tag frecuency.
+     */
+    @Test
+    public void testGetMaxMinTagFrecuency(){
+        createHashTag("America", 20L);
+        createHashTag("Amazonas", 90L);
+        createHashTag("Carazo",  50L);
+        final List<Object[]>  frecuency = getHashTagDao().getMaxMinTagFrecuency();
+        //System.out.println("MAX 1-------->"+ frecuency.get(0)[0]);
+        //System.out.println(" MIN 1-------->"+ frecuency.get(0)[1]);
+         for (Object[] objects : frecuency) {
+           // System.out.println("---- MAX ----****"+ objects[0]);
+           // System.out.println("---- MIN ----****"+ objects[1]);
+        }
+    }
 
 }