private Deployment getActiveDeployment() {
    Deployment deployment = null;

    GetMultipleDataResult<Deployment> dataResult =
        dao.search(
            Deployment.class,
            null,
            MapUtil.newHashMap(
                new String[] {"cloudId", "endDate"},
                new String[][] {new String[] {cloudId}, new String[] {null}}),
            1);
    if (dataResult.getData() != null && dataResult.getData().length > 0) {
      deployment = dataResult.getData()[0];
    }
    return deployment;
  }
  /**
   * Create a new instance of the {@link PaaSProviderPollingMonitor} to monitor the given paas
   * provider.
   *
   * @param paaSProvider The paas provider to monitor.
   */
  @SuppressWarnings("rawtypes")
  public PaaSProviderPollingMonitor(
      IGenericSearchDAO dao,
      IGenericSearchDAO monitorDAO,
      IPaaSProvider paaSProvider,
      List<IPaasEventListener> listeners,
      String cloudId) {
    this.cloudId = cloudId;
    this.dao = dao;
    this.monitorDAO = monitorDAO;
    this.paaSProvider = paaSProvider;
    this.listeners = listeners;
    Set<Class<?>> eventClasses = Sets.newHashSet();
    try {
      eventClasses = TypeScanner.scanTypes("alien4cloud.paas.model", AbstractMonitorEvent.class);
    } catch (ClassNotFoundException e) {
      log.info("No event class derived from {} found", AbstractMonitorEvent.class.getName());
    }
    Map<String, String[]> filter = Maps.newHashMap();
    filter.put("cloudId", new String[] {this.cloudId});
    // sort by filed date DESC
    SearchQueryHelperBuilder searchQueryHelperBuilder =
        monitorDAO
            .getQueryHelper()
            .buildSearchQuery("deploymentmonitorevents")
            .types(eventClasses.toArray(new Class<?>[eventClasses.size()]))
            .filters(filter)
            .fieldSort("date", true);

    // the first one is the one with the latest date
    GetMultipleDataResult lastestEventResult = monitorDAO.search(searchQueryHelperBuilder, 0, 1);
    if (lastestEventResult.getData().length > 0) {
      AbstractMonitorEvent lastEvent = (AbstractMonitorEvent) lastestEventResult.getData()[0];
      Date lastEventDate = new Date(lastEvent.getDate());
      log.info(
          "Recovering events from the last in elasticsearch {} of type {}",
          lastEventDate,
          lastEvent.getClass().getName());
      this.lastPollingDate = lastEventDate;
    } else {
      this.lastPollingDate = new Date();
      log.debug(
          "No monitor events found, the last polling date will be current date {}",
          this.lastPollingDate);
    }
    paaSEventsCallback = new PaaSEventsCallback();
  }
 /**
  * Get events for a specific deployment from an environment
  *
  * @param applicationEnvironmentId The environment we want to get events from
  * @param from The initial position of the events to get (based on time desc sorting)
  * @param size The number of events to get.
  * @return A result that contains all events.
  */
 public GetMultipleDataResult<?> getDeploymentEvents(
     String applicationEnvironmentId, int from, int size) {
   Deployment deployment = deploymentService.getActiveDeploymentOrFail(applicationEnvironmentId);
   String index = alienMonitorDao.getIndexForType(AbstractMonitorEvent.class);
   QueryHelper.SearchQueryHelperBuilder searchQueryHelperBuilder =
       queryHelper
           .buildSearchQuery(index)
           .types(
               PaaSDeploymentStatusMonitorEvent.class,
               PaaSInstanceStateMonitorEvent.class,
               PaaSMessageMonitorEvent.class,
               PaaSInstancePersistentResourceMonitorEvent.class)
           .filters(
               MapUtil.newHashMap(
                   new String[] {"deploymentId"},
                   new String[][] {new String[] {deployment.getId()}}))
           .fieldSort("_timestamp", true);
   return alienMonitorDao.search(searchQueryHelperBuilder, from, size);
 }