class StageResourceProvider extends AbstractControllerResourceProvider implements ExtendedResourceProvider {

  @Override
  public Set<Resource> getResources(Request request, Predicate predicate)
      throws SystemException, UnsupportedPropertyException,
      NoSuchResourceException, NoSuchParentResourceException {

    Set<Resource> results     = new LinkedHashSet<>();
    Set<String>   propertyIds = getRequestPropertyIds(request, predicate);

    // !!! poor mans cache.  toResource() shouldn't be calling the db
    // every time, when the request id is likely the same for each stageEntity
    Map<Long, Map<Long, HostRoleCommandStatusSummaryDTO>> cache = new HashMap<>();

    List<StageEntity> entities = dao.findAll(request, predicate);
    for (StageEntity entity : entities) {
      results.add(StageResourceProvider.toResource(cache, entity, propertyIds));
    }

    cache.clear();

    // !!! check the id passed to see if it's a LogicalRequest.  This safeguards against
    // iterating all stages for all requests.  That is a problem when the request
    // is for an Upgrade, but was pulling the data anyway.
    Map<String, Object> map = PredicateHelper.getProperties(predicate);

    if (map.containsKey(STAGE_REQUEST_ID)) {
      Long requestId = NumberUtils.toLong(map.get(STAGE_REQUEST_ID).toString());
      LogicalRequest lr = topologyManager.getRequest(requestId);

      if (null != lr) {
        Collection<StageEntity> topologyManagerStages = lr.getStageEntities();
        // preload summaries as it contains summaries for all stages within this request
        Map<Long, HostRoleCommandStatusSummaryDTO> summary = topologyManager.getStageSummaries(requestId);
        cache.put(requestId, summary);
        for (StageEntity entity : topologyManagerStages) {
          Resource stageResource = toResource(cache, entity, propertyIds);
          if (predicate.evaluate(stageResource)) {
            results.add(stageResource);
          }
        }
      }
    } else {
      Collection<StageEntity> topologyManagerStages = topologyManager.getStages();
      for (StageEntity entity : topologyManagerStages) {
        if (!cache.containsKey(entity.getRequestId())) {
          Map<Long, HostRoleCommandStatusSummaryDTO> summary = topologyManager.getStageSummaries(entity.getRequestId());
          cache.put(entity.getRequestId(), summary);
        }
        Resource stageResource = toResource(cache, entity, propertyIds);
        if (predicate.evaluate(stageResource)) {
          results.add(stageResource);
        }
      }
    }

    return results;
  }

    StageResourceProvider(AmbariManagementController managementController);

}