/*
   * (non-Javadoc)
   *
   * @see
   * org.opengeoportal.harvester.api.service.IngestService#unscheduleByRepository
   * (Long repositoryId)
   */
  @Override
  @Transactional
  public int unscheduleByRepository(Long repositoryId) {
    List<Ingest> scheduledIngests =
        ingestRepository.findByRepositoryIdAndScheduledTrue(repositoryId);
    for (Ingest ingest : scheduledIngests) {
      try {
        scheduler.unschedule(ingest);
      } catch (SchedulerException e) {
        if (logger.isWarnEnabled()) {
          logger.warn("Cannot unschedule ingest with id " + ingest.getId(), e);
        }
      }
    }

    return ingestRepository.setScheduledForRepositoryId(repositoryId);
  }
 public Ingest saveAndUpdateServerQuery(Ingest ingest) {
   try {
     if (ingest instanceof IngestOGP) {
       IngestOGP ingestOGP = (IngestOGP) ingest;
       SolrSearchParams params = new SolrSearchParams(ingestOGP);
       SolrQuery query = params.toSolrQuery();
       String queryString = query.toString();
       if (!StringUtils.equals(queryString, ingestOGP.getServerQuery())) {
         ingestOGP.setServerQuery(queryString);
       }
     }
   } catch (Exception e) {
     logger.error("Error generating server query parameters before saving ingest", e);
   }
   return ingestRepository.save(ingest);
 }
  /*
   * (non-Javadoc)
   *
   * @see org.opengeoportal.harvester.api.service.IngestService#save(
   * org.opengeoportal .harvester.api.domain.Ingest, java.lang.Long,
   * org.opengeoportal.harvester.api.domain.InstanceType)
   */
  @Override
  @Transactional(readOnly = false)
  public Ingest save(Ingest ingest, Long customRepositoryId, InstanceType customRepoInstanceType) {
    CustomRepository cRepository = customRepositoryService.findById(customRepositoryId);
    if (cRepository == null || cRepository.getServiceType() != customRepoInstanceType) {
      throw new InstanceNotFoundException(
          "There is not any CustomRepository with id = "
              + customRepositoryId
              + " and serviceType = "
              + customRepoInstanceType.name());
    }

    ingest.setRepository(cRepository);
    Ingest result;
    if (ingest instanceof IngestOGP) {
      result = saveAndUpdateServerQuery(ingest);
    } else {
      result = ingestRepository.save(ingest);
    }

    return result;
  }
 @Override
 @Transactional
 public void delete(Long id) {
   ingestRepository.delete(id);
 }
 @Override
 @Transactional
 public Ingest save(Ingest ingest) {
   return ingestRepository.save(ingest);
 }
 /*
  * (non-Javadoc)
  *
  * @see org.opengeoportal.harvester.api.service.IngestService#
  * countScheduledIngestsByRepo(java.lang.Long)
  */
 @Override
 @Transactional
 public Long countScheduledIngestsByRepo(Long repoId) {
   return ingestRepository.countByRepositoryIdAndScheduledTrue(repoId);
 }
 /*
  * (non-Javadoc)
  *
  * @see
  * org.opengeoportal.harvester.api.service.IngestService#findById(java.lang
  * .Long)
  */
 @Override
 public Ingest findById(Long id) {
   return ingestRepository.findOne(id);
 }
 @Override
 @Transactional(readOnly = true)
 public Page<Ingest> findAll(Pageable pageable) {
   Page<Ingest> page = ingestRepository.findAll(pageable);
   return page;
 }