@Override
  @Transactional(readOnly = false)
  public Collection stopAidrFetcher(Collection collection, Long userId) {
    try {
      /** Rest call to Fetcher */
      Client client = ClientBuilder.newBuilder().register(JacksonFeature.class).build();
      String path = "";
      if (CollectionType.SMS.equals(collection.getProvider())) {
        path = "/sms/stop?collection_code=";
      } else {
        path = "/" + collection.getProvider().toString().toLowerCase() + "/stop?id=";
      }

      WebTarget webResource =
          client.target(fetchMainUrl + path + URLEncoder.encode(collection.getCode(), "UTF-8"));

      Response clientResponse = webResource.request(MediaType.APPLICATION_JSON).get();

      String jsonString = clientResponse.readEntity(String.class);
      JSONParser parser = new JSONParser();
      JSONObject jsonResponse = (JSONObject) parser.parse(jsonString);
      collection =
          updateStatusCollection(jsonResponse.get("entity").toString(), collection, userId);

      /** Change Database Status */
      return this.collectionRepository.stop(collection.getId());
    } catch (Exception e) {
      logger.error("Error while stopping Remote FetchMain Collection", e);
    }
    return null;
  }
  // @SuppressWarnings("deprecation")
  @Transactional
  @Override
  public Collection statusByCollection(Collection collection, Long accountId) throws Exception {
    if (collection != null) {
      try {
        /** Make a call to fetcher Status Rest API */
        Client client = ClientBuilder.newBuilder().register(JacksonFeature.class).build();

        String path = "";
        if (CollectionType.Twitter.equals(collection.getProvider())
            || CollectionType.Facebook.equals(collection.getProvider())) {
          path = "/" + collection.getProvider().toString().toLowerCase() + "/status?id=";
        } else if (CollectionType.SMS.equals(collection.getProvider())) {
          path = "/sms/status?collection_code=";
        }

        WebTarget webResource =
            client.target(fetchMainUrl + path + URLEncoder.encode(collection.getCode(), "UTF-8"));
        Response clientResponse = webResource.request(MediaType.APPLICATION_JSON).get();

        String jsonString = clientResponse.readEntity(String.class);
        JSONParser parser = new JSONParser();
        JSONObject jsonResponse = (JSONObject) parser.parse(jsonString);
        collection =
            updateStatusCollection(jsonResponse.get("entity").toString(), collection, accountId);
        return collection;
      } catch (Exception e) {
        String msg = "Error while getting status for collection from Remote FetchMain Collection";
        logger.error(msg, e);
        throw new Exception(msg);
      }
    }
    return null;
  }
  @Override
  @Transactional(readOnly = true)
  public FetcherRequestDTO prepareFetcherRequest(Collection dbCollection) {
    FetcherRequestDTO dto = new FetcherRequestDTO();

    UserConnection userconnection =
        userConnectionService.fetchByCombinedUserName(dbCollection.getOwner().getUserName());
    dto.setAccessToken(userconnection.getAccessToken());
    dto.setAccessTokenSecret(userconnection.getSecret());
    dto.setCollectionName(dbCollection.getName());
    dto.setCollectionCode(dbCollection.getCode());
    if (dbCollection.getProvider() == CollectionType.Facebook) {
      dto.setToFollow(dbCollection.getFollow());
    } else {
      dto.setToFollow(
          getFollowTwitterIDs(dbCollection.getFollow(), dbCollection.getOwner().getUserName()));
    }
    dto.setToTrack(dbCollection.getTrack());
    dto.setGeoLocation(dbCollection.getGeo());
    dto.setGeoR(dbCollection.getGeoR());
    dto.setLanguageFilter(dbCollection.getLangFilters());
    dto.setSaveMediaEnabled(dbCollection.isSaveMediaEnabled());
    dto.setFetchInterval(dbCollection.getFetchInterval());
    dto.setProvider(dbCollection.getProvider().toString());
    dto.setFetchInterval(dbCollection.getFetchInterval());
    dto.setFetchFrom(dbCollection.getFetchFrom());
    dto.setLastExecutionTime(dbCollection.getLastExecutionTime());
    // Added by koushik
    accessTokenStr = dto.getAccessToken();
    accessTokenSecretStr = dto.getAccessTokenSecret();

    return dto;
  }
 @Override
 @Transactional(readOnly = false)
 public Collection updateAndGetRunningCollectionStatusByUser(Long userId) throws Exception {
   Collection collection = collectionRepository.getRunningCollectionStatusByUser(userId);
   if (collection != null) {
     logger.info(
         "User with ID: '"
             + userId
             + "' has a running collection with code: '"
             + collection.getCode());
     return statusByCollection(collection, userId);
   } else {
     // logger.info("User with ID: '" + userId + "' don't have any running collections. Nothing to
     // update." );
     //            If there is no running collection there is still can be collection with status
     // 'Initializing'.
     //            This happens because we update collection information from fetcher before
     // collection was started.
     //            So we need to update from Fetcher this kind of collections as well.
     collection = collectionRepository.getInitializingCollectionStatusByUser(userId);
     if (collection != null) {
       return statusByCollection(collection, userId);
     }
   }
   return null;
 }
  private CollectionSummaryInfo adaptCollectionToCollectionSummaryInfo(Collection collection) {

    CollectionSummaryInfo summaryInfo = new CollectionSummaryInfo();
    summaryInfo.setCode(collection.getCode());
    summaryInfo.setName(collection.getName());
    summaryInfo.setCurator(collection.getOwner().getUserName());
    summaryInfo.setStartDate(collection.getStartDate());
    summaryInfo.setEndDate(collection.getEndDate());
    summaryInfo.setCollectionCreationDate(collection.getCreatedAt());
    // TODO to fetch from collection log
    try {
      summaryInfo.setTotalCount(
          collectionLogService.countTotalDownloadedItemsForCollection(collection.getId()));
    } catch (Exception e) {
      logger.warn("Error in fetch count from collection log.", e);
      summaryInfo.setTotalCount(collection.getCount());
    }
    summaryInfo.setStatus(collection.getStatus().getStatus());

    // TODO summaryInfo.setCreatedAt(collection.getCreatedAt());
    summaryInfo.setLanguage(collection.getLangFilters());
    summaryInfo.setKeywords(collection.getTrack());
    summaryInfo.setGeo(collection.getGeo());
    summaryInfo.setLabelCount(taggerService.getLabelCount(collection.getId()));
    summaryInfo.setPubliclyListed(collection.isPubliclyListed());
    summaryInfo.setProvider(collection.getProvider().toString());
    return summaryInfo;
  }
  private CollectionBriefInfo adaptCollectionToCollectionBriefInfo(Collection collection) {

    CollectionBriefInfo briefInfo = new CollectionBriefInfo();
    briefInfo.setCollectionId(collection.getId());
    briefInfo.setCollectionCode(collection.getCode());
    briefInfo.setCollectionName(collection.getName());
    briefInfo.setMicromappersEnabled(collection.isMicromappersEnabled());
    briefInfo.setProvider(collection.getProvider().name());
    return briefInfo;
  }
  @Override
  @Transactional(readOnly = false)
  public Collection startFetcher(FetcherRequestDTO fetcherRequest, Collection collection) {
    try {
      /** Rest call to Fetcher */
      Client client = ClientBuilder.newBuilder().register(JacksonFeature.class).build();

      if (CollectionType.Twitter.equals(collection.getProvider())
          || CollectionType.Facebook.equals(collection.getProvider())) {
        WebTarget webResource =
            client.target(
                fetchMainUrl + "/" + collection.getProvider().toString().toLowerCase() + "/start");

        ObjectMapper objectMapper = JacksonWrapper.getObjectMapper();

        Response clientResponse =
            webResource
                .request(MediaType.APPLICATION_JSON)
                .post(Entity.json(objectMapper.writeValueAsString(fetcherRequest)), Response.class);

        // logger.info("ObjectMapper: " + objectMapper.writeValueAsString(fetcherRequest));
        String jsonString = clientResponse.readEntity(String.class);
        JSONParser parser = new JSONParser();
        JSONObject jsonResponse = (JSONObject) parser.parse(jsonString);
        // logger.info("NEW STRING: " + jsonResponse);
        FetcheResponseDTO response =
            objectMapper.readValue(jsonResponse.get("entity").toString(), FetcheResponseDTO.class);
        logger.info("start Response from fetchMain " + objectMapper.writeValueAsString(response));
        collection.setStatus(CollectionStatus.getByStatus(response.getStatusCode()));
      } else if (CollectionType.SMS.equals(collection.getProvider())) {
        WebTarget webResource =
            client.target(
                fetchMainUrl
                    + "/sms/start?collection_code="
                    + URLEncoder.encode(collection.getCode(), "UTF-8"));
        Response response = webResource.request(MediaType.APPLICATION_JSON).get();
        if (response.getStatus() == 200) collection.setStatus(CollectionStatus.INITIALIZING);
      }
      /** Update Status To database */
      collectionRepository.update(collection);
      return collection;
    } catch (Exception e) {
      logger.error("Error while starting Remote FetchMain Collection", e);
    }
    return null;
  }