@RequiresSession
  public List<StageEntity> findFirstStageByStatus(Collection<HostRoleStatus> statuses) {
    TypedQuery<Object[]> query = entityManagerProvider.get().createNamedQuery(
        "StageEntity.findFirstStageByStatus", Object[].class);

    query.setParameter("statuses", statuses);

    List<Object[]> results = daoUtils.selectList(query);
    List<StageEntity> stages = new ArrayList<>();

    for (Object[] result : results) {
      StageEntityPK stagePK = new StageEntityPK();
      stagePK.setRequestId((Long) result[0]);
      stagePK.setStageId((Long) result[1]);

      StageEntity stage = findByPK(stagePK);
      stages.add(stage);
    }

    return stages;
  }
Finds the first stage matching any of the specified statuses for every
request. For example, to find the first {@link HostRoleStatus#IN_PROGRESS}
stage for every request, pass in
{@link HostRoleStatus#IN_PROGRESS_STATUSES}.

@param statuses
         {@link HostRoleStatus}
@return the list of the first matching stage for the given statuses for
        every request.
ambari-server/src/main/java/org/apache/ambari/server/orm/dao/StageDAO.java