/**
  * Instantiates a new job.
  *
  * @param cfg the {@link DomainConfiguration} being processed
  * @param harvest the {@link HarvestDefinition} being processed
  * @return an instance of {@link Job}
  */
 public static Job getNewJob(HarvestDefinition harvest, DomainConfiguration cfg) {
   HarvestChannelDAO harvestChannelDao = HarvestChannelDAO.getInstance();
   HarvestChannel channel = harvestChannelDao.getChannelForHarvestDefinition(harvest.getOid());
   if (channel == null) {
     log.info(
         "No channel mapping registered for harvest id "
             + harvest.getOid()
             + ", will use default.");
     channel = harvestChannelDao.getDefaultChannel(harvest.isSnapShot());
   }
   if (harvest.isSnapShot()) {
     return Job.createSnapShotJob(
         harvest.getOid(),
         channel,
         cfg,
         harvest.getMaxCountObjects(),
         harvest.getMaxBytes(),
         ((FullHarvest) harvest).getMaxJobRunningTime(),
         harvest.getNumEvents());
   }
   return Job.createJob(harvest.getOid(), channel, cfg, harvest.getNumEvents());
 }
  @Override
  public int generateJobs(HarvestDefinition harvest) {
    log.info("Generating jobs for harvestdefinition # " + harvest.getOid());
    int jobsMade = 0;
    final Iterator<DomainConfiguration> domainConfigurations = harvest.getDomainConfigurations();

    while (domainConfigurations.hasNext()) {
      List<DomainConfiguration> subset = new ArrayList<DomainConfiguration>();
      while (domainConfigurations.hasNext() && subset.size() < DOMAIN_CONFIG_SUBSET_SIZE) {
        subset.add(domainConfigurations.next());
      }

      Collections.sort(subset, getDomainConfigurationSubsetComparator(harvest));
      if (log.isTraceEnabled()) {
        log.trace(
            subset.size()
                + " domainconfigs now sorted and ready to processing "
                + "for harvest #"
                + harvest.getOid());
      }
      jobsMade += processDomainConfigurationSubset(harvest, subset.iterator());
    }
    harvest.setNumEvents(harvest.getNumEvents() + 1);

    if (!harvest.isSnapShot()) {
      PartialHarvest focused = (PartialHarvest) harvest;
      Schedule schedule = focused.getSchedule();
      int numEvents = harvest.getNumEvents();

      // Calculate next event
      Date now = new Date();
      Date nextEvent = schedule.getNextEvent(focused.getNextDate(), numEvents);

      // Refuse to schedule event in the past
      if (nextEvent != null && nextEvent.before(now)) {
        int eventsSkipped = 0;
        while (nextEvent != null && nextEvent.before(now)) {
          nextEvent = schedule.getNextEvent(nextEvent, numEvents);
          eventsSkipped++;
        }
        if (log.isWarnEnabled()) {
          log.warn(
              "Refusing to schedule harvest definition '"
                  + harvest.getName()
                  + "' in the past. Skipped "
                  + eventsSkipped
                  + " events. Old nextDate was "
                  + focused.getNextDate()
                  + " new nextDate is "
                  + nextEvent);
        }
      }

      // Set next event
      focused.setNextDate(nextEvent);
      if (log.isTraceEnabled()) {
        log.trace(
            "Next event for harvest definition "
                + harvest.getName()
                + " happens: "
                + (nextEvent == null ? "Never" : nextEvent.toString()));
      }
    }

    log.info(
        "Finished generating " + jobsMade + " jobs for harvestdefinition # " + harvest.getOid());
    return jobsMade;
  }