/**
  * Get hashTags button stats.
  *
  * @param tagName
  * @param filter
  * @param limit
  * @param request
  * @param response
  * @return
  * @throws JsonGenerationException
  * @throws JsonMappingException
  * @throws IOException
  */
 @RequestMapping(value = "/api/common/hashtags/stats/button.json", method = RequestMethod.GET)
 public @ResponseBody ModelMap getHashTagButtonStats(
     @RequestParam(value = "tagName", required = true) String tagName,
     @RequestParam(value = "filter", required = true) String filter,
     @RequestParam(value = "period", required = false) String period,
     HttpServletRequest request,
     HttpServletResponse response)
     throws JsonGenerationException, JsonMappingException, IOException {
   try {
     final Map<String, Object> jsonResponse = new HashMap<String, Object>();
     final HashTagStatsBean tagStatsBean = new HashTagStatsBean();
     final TypeSearchResult filterType = TypeSearchResult.getTypeSearchResult(filter);
     final SearchPeriods searchPeriod = SearchPeriods.getPeriodString(period);
     if (filterType == null) {
       throw new EnMeNoResultsFoundException("type not found");
     } else {
       if (filterType.equals(TypeSearchResult.HASHTAGRATED)) {
         // hits /period
         tagStatsBean.setTotalHits(
             getStatisticsService()
                 .getHashTagHitsbyName(tagName, filterType, request, searchPeriod));
         tagStatsBean.getTotalHits().setTypeSearchResult(TypeSearchResult.HITS);
         // social network use /period
         tagStatsBean.setTotalUsageBySocialNetwork(
             getStatisticsService()
                 .getSocialNetworkUseByHashTag(
                     tagName, this.INIT_RESULTS, null, request, searchPeriod));
         tagStatsBean
             .getTotalUsageBySocialNetwork()
             .setTypeSearchResult(TypeSearchResult.SOCIALNETWORK);
         // usage by / period
         tagStatsBean.setUsageByItem(
             getStatisticsService()
                 .getTotalUsageByHashTag(
                     tagName, this.INIT_RESULTS, null, filterType, request, searchPeriod));
         tagStatsBean.getUsageByItem().setTypeSearchResult(TypeSearchResult.HASHTAG);
         // votes
         tagStatsBean.setUsageByVotes(
             getStatisticsService()
                 .getHashTagUsedOnItemsVoted(
                     tagName, this.INIT_RESULTS, null, request, searchPeriod));
         tagStatsBean.getUsageByVotes().setTypeSearchResult(TypeSearchResult.VOTES);
         jsonResponse.put("hashTagButtonStats", tagStatsBean);
         setItemResponse(jsonResponse);
       } else {
         setError("filter not valid", response);
       }
     }
   } catch (Exception e) {
     e.printStackTrace();
     log.error(e);
     setError(e.getMessage(), response);
   }
   return returnData();
 }
 /**
  * @param tagName
  * @param period
  * @param filter
  * @param request
  * @param response
  * @return
  */
 @RequestMapping(
     value = "/api/common/hashtags/stats/button/range.json",
     method = RequestMethod.GET)
 public @ResponseBody ModelMap getHashTagButtonStatsByDateRange(
     @RequestParam(value = "tagName", required = true) String tagName,
     @RequestParam(value = "period", required = false) String period,
     @RequestParam(value = "filter", required = true) String filter,
     HttpServletRequest request,
     HttpServletResponse response) {
   try {
     List<HashTagDetailStats> tagStats = new ArrayList<HashTagDetailStats>();
     final TypeSearchResult filterType = TypeSearchResult.getTypeSearchResult(filter);
     // final SearchPeriods periodSearch = SearchPeriods.getPeriodString(period);
     final Map<String, Object> jsonResponse = new HashMap<String, Object>();
     final SearchPeriods searchPeriods = SearchPeriods.getPeriodString(period);
     if (searchPeriods == null) {
       setError("Period not valid", response);
     } else {
       if (filterType.equals(TypeSearchResult.HASHTAG)) {
         tagStats =
             getStatisticsService()
                 .getTotalUsagebyHashtagAndDateRangeGraph(tagName, searchPeriods, request);
       } else if (filterType.equals(TypeSearchResult.SOCIALNETWORK)) {
         tagStats =
             getStatisticsService()
                 .getTotalSocialLinksbyHashTagUsageAndDateRangeGraph(
                     tagName, searchPeriods, request);
       } else if (filterType.equals(TypeSearchResult.HITS)) {
         tagStats =
             getStatisticsService()
                 .getTotalHitsUsagebyHashTagAndDateRangeGraph(tagName, searchPeriods, request);
       } else if (filterType.equals(TypeSearchResult.VOTES)) {
         tagStats =
             getStatisticsService()
                 .getTotalVotesbyHashTagUsageAndDateRangeGraph(tagName, searchPeriods, request);
       }
       jsonResponse.put("statsByRange", tagStats);
       setItemResponse(jsonResponse);
     }
   } catch (Exception e) {
     e.printStackTrace();
     log.error(e);
     setError(e.getMessage(), response);
   }
   return returnData();
 }
  /**
   * Convert a number to correct i18n label. eg: 12 == December / ONEYEAR 6 == Saturday / SEVENDAYS
   * 24 == 24 / TWENTYFOURHOURS
   *
   * @param period the period
   * @param label the label to be translated
   * @param request {@link HttpServletRequest}.
   * @return
   */
  private String getHashTagStatsDataRangeLabel(
      final String period, final Integer label, final HttpServletRequest request) {
    String dataRangeLabel = null;
    HashTagRate tagRateLabel;
    final SearchPeriods periodSelected = SearchPeriods.getPeriodString(period);

    if (periodSelected.equals(SearchPeriods.ONEYEAR)) {
      tagRateLabel = HashTagRate.getHashTagMonthLabel(Integer.toString(label));
      dataRangeLabel =
          this.convertHashTagDataRangeLabelMessage(tagRateLabel, request, new Object[] {});
    } else if (periodSelected.equals(SearchPeriods.ALLTIME)) {
      dataRangeLabel = String.valueOf(label);
    } else if (periodSelected.equals(SearchPeriods.THIRTYDAYS)) {
      dataRangeLabel = String.valueOf(label);
    } else if (periodSelected.equals(SearchPeriods.TWENTYFOURHOURS)) {
      dataRangeLabel = String.valueOf(label);
    } else if (periodSelected.equals(SearchPeriods.SEVENDAYS)) {
      tagRateLabel = HashTagRate.getHashTagWeekDayLabel(Integer.toString(label));
      dataRangeLabel =
          this.convertHashTagDataRangeLabelMessage(tagRateLabel, request, new Object[] {});
    }
    return dataRangeLabel;
  }
  /**
   * @api {get} /api/common/geolocation/search/{typeOfSearch}.json Get Items by Geolocation
   * @apiVersion 1.0.0
   * @apiName GetGeoItemsByType
   * @apiGroup Geolocation
   * @apiPermission user
   * @apiDescription A tag cloud of hashtags with the top hashtags in post mentioning ordered by
   *     frecuency.
   * @apiParam {Number} range
   * @apiParam {Number} maxItem
   * @apiParam {String} type
   * @apiParam {Number} longitude
   * @apiParam {Number} latitude
   * @apiParam {Number} period
   * @apiParam {String} tagName
   * @apiParam {String} typeOfSearch
   * @apiSampleRequest http://www.encuestame.org/demo/api/common/geolocation/search/tweetpoll.json
   * @apiSuccessExample
   * @apiSuccess {Object} success
   * @apiSuccess {String} error
   */
  @RequestMapping(
      value = "/api/common/geolocation/search/{typeOfSearch}.json",
      method = RequestMethod.GET)
  public @ResponseBody ModelMap getItemsbyGeoLocationRange(
      @RequestParam(value = "range", required = true) Double range,
      @RequestParam(value = "maxItem", required = true) Integer maxItem,
      @RequestParam(value = "type", required = true) String type,
      @RequestParam(value = "longitude", required = true) Double longitude,
      @RequestParam(value = "latitude", required = true) Double latitude,
      @RequestParam(value = "period", required = true) String period,
      @RequestParam(value = "tag", required = false) String tagName,
      @PathVariable final String typeOfSearch,
      HttpServletRequest request,
      HttpServletResponse response)
      throws JsonGenerationException, JsonMappingException, IOException {

    try {
      log.debug("Range " + range);
      log.debug("Maximum " + maxItem);

      SearchPeriods periodValue =
          (period.isEmpty() ? SearchPeriods.ALLTIME : SearchPeriods.getPeriodString(period));
      TypeSearchResult typeValue =
          (type.isEmpty() ? TypeSearchResult.ALL : TypeSearchResult.getTypeSearchResult(type));
      TypeSearchResult searchType = TypeSearchResult.getTypeSearchResult(typeOfSearch);
      List<ItemGeoLocationBean> itemsByLocation = new ArrayList<ItemGeoLocationBean>();
      final Map<String, Object> jsonResponse = new HashMap<String, Object>();
      if (searchType.equals(TypeSearchResult.ALL)) {
        itemsByLocation =
            getLocationService()
                .retrieveItemsByGeo(range, maxItem, typeValue, longitude, latitude, periodValue);
        log.debug("Items by geo location " + itemsByLocation.size());
        jsonResponse.put("itemsByGeo", itemsByLocation);
      } else if (searchType.equals(TypeSearchResult.HASHTAG)) {
        if (tagName == null) {
          throw new EnMeSearchException("search params required.");
        } else {

          itemsByLocation =
              getLocationService()
                  .retreiveHashTagUsebyGeoLo(
                      range, maxItem, typeValue, longitude, latitude, tagName, periodValue);
          log.debug("Hashtag use by geo location " + itemsByLocation.size());
          jsonResponse.put("hashtagUsebyGeo", itemsByLocation);
        }

      } else if (searchType.equals(TypeSearchResult.SOCIALNETWORK)) {
        itemsByLocation =
            getLocationService()
                .retrieveSocialNetworksPublicationsbyGeoLocation(
                    range, maxItem, typeValue, longitude, latitude, periodValue);
        log.debug("Social publications  by geo location " + itemsByLocation.size());
        jsonResponse.put("socialGeo", itemsByLocation);
      }
      setItemResponse(jsonResponse);

    } catch (Exception e) {
      log.error(e);
      setError(e.getMessage(), response);
    }
    return returnData();
  }