@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;
  }
  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;
  }
 @Override
 @Transactional(readOnly = true)
 public Boolean isValidAPIKey(String code, String apiKey) throws Exception {
   Collection collection = findByCode(code);
   if (collection.getOwner().getApiKey().equals(apiKey)) {
     return true;
   }
   return false;
 }
  @Override
  @Transactional(readOnly = false)
  public Collection start(Long collectionId) throws Exception {

    // We are going to start new collection. Lets stop collection which is running for owner of the
    // new collection.
    Collection dbCollection = collectionRepository.findById(collectionId);
    Long userId = dbCollection.getOwner().getId();
    Collection alreadyRunningCollection =
        collectionRepository.getRunningCollectionStatusByUser(userId);
    if (alreadyRunningCollection != null) {
      this.stop(alreadyRunningCollection.getId(), userId);
    }

    return startFetcher(prepareFetcherRequest(dbCollection), dbCollection);
  }
  @Override
  @Transactional(readOnly = false)
  public boolean updateCollection(CollectionUpdateInfo collectionUpdateInfo, Long userId) {

    String filteredTrack = "";
    try {
      Collection collection = findByCode(collectionUpdateInfo.getCode());

      if (!collection.getName().equals(collectionUpdateInfo.getName())) {
        if (!existName(collectionUpdateInfo.getName())) {
          collection.setName(collectionUpdateInfo.getName());
        } else {
          return false;
        }
      }
      // if collection exists with same name

      collection.setProvider(CollectionType.valueOf(collectionUpdateInfo.getProvider()));
      collection.setFollow(collectionUpdateInfo.getFollow());
      collection.setFetchInterval(collectionUpdateInfo.getFetchInterval());
      collection.setFetchFrom(collectionUpdateInfo.getFetchFrom());
      filteredTrack = collectionUpdateInfo.getTrack();

      if (!StringUtils.isEmpty(filteredTrack)) {
        filteredTrack = getFilteredTrack(filteredTrack);

        if (StringUtils.isEmpty(filteredTrack)) {
          return false;
        }
      }
      collection.setTrack(filteredTrack);
      collection.setGeo(collectionUpdateInfo.getGeo());
      collection.setGeoR(collectionUpdateInfo.getGeoR());
      collection.setDurationHours(Integer.parseInt(collectionUpdateInfo.getDurationHours()));
      collection.setLangFilters(collectionUpdateInfo.getLangFilters());

      Long crisisTypeId = Long.parseLong(collectionUpdateInfo.getCrisisType());
      CrisisType crisisType = crisisTypeService.getById(crisisTypeId);
      if (crisisType != null) {
        collection.setCrisisType(crisisType);
      } else {
        logger.error(
            "Crisis Type Id: "
                + crisisTypeId
                + " does not exist. Can't update the collection : "
                + collectionUpdateInfo.getCode());
        return false;
      }

      if (CollectionType.SMS.equals(collection.getProvider())) {
        collection.setTrack(null);
        collection.setLangFilters(null);
        collection.setGeo(null);
        collection.setGeoR(null);
        collection.setFollow(null);
      }
      if (collection.getProvider() == CollectionType.Twitter) {
        collection.setFollow(
            this.getFollowTwitterIDs(
                collectionUpdateInfo.getFollow(), collection.getOwner().getUserName()));
      }

      collectionRepository.update(collection);
      // first make an entry in log if collection is running
      if (CollectionStatus.RUNNING_WARNING.equals(collection.getStatus())
          || CollectionStatus.RUNNING.equals(collection.getStatus())) {

        this.stop(collection.getId(), userId);
        this.startFetcher(this.prepareFetcherRequest(collection), collection);
      }
      return true;
    } catch (Exception e) {

      logger.error("Unable to update the collection : " + collectionUpdateInfo.getCode(), e);
      return false;
    }
  }