package de.ivu.elect.business.system.control;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.ivu.elect.business.system.entity.BackgroundServiceEvent;
import de.ivu.elect.business.system.entity.SystemLogQuelle;

/**
 *  on 31.10.2016.
 */
@Stateless
public class BackgroundServiceEventStarter {

  private static final Logger LOG = LoggerFactory.getLogger(BackgroundServiceEventStarter.class);

  @Inject
  private transient Instance<BackgroundServiceEvent> backgroundServiceEvents;

  public List<SystemLogQuelle> sendeBackgroundServiceEvents() {
    List<SystemLogQuelle> result = new ArrayList<>();
    if (backgroundServiceEvents.isUnsatisfied()) {
      LOG.info("Keine Hintergrunddienste zu starten");
    } else {
      for (BackgroundServiceEvent event : backgroundServiceEvents) {
        LOG.info("Sende Event für Hintergrunddienst: " + event.getServiceName());
        event.sendEvent();
        result.add(event.getServiceName());
        LOG.info("Event für Hintergrunddienst: " + event.getServiceName() + " - gesendet");
      }
    }
    return result;
  }
}
