class AlertDefinitionResourceProvider extends AbstractControllerResourceProvider {

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

    Set<String> requestPropertyIds = getRequestPropertyIds(request, predicate);

    // use a collection which preserves order since JPA sorts the results
    Set<Resource> results = new LinkedHashSet<>();

    for (Map<String, Object> propertyMap : getPropertyMaps(predicate)) {
      String clusterName = (String) propertyMap.get(ALERT_DEF_CLUSTER_NAME);

      if (null == clusterName || clusterName.isEmpty()) {
        throw new IllegalArgumentException("Invalid argument, cluster name is required");
      }

      String id = (String) propertyMap.get(ALERT_DEF_ID);
      if (null != id) {
        AlertDefinitionEntity entity = alertDefinitionDAO.findById(Long.parseLong(id));
        if (null != entity) {
          AlertResourceProviderUtils.verifyViewAuthorization(entity);
          results.add(toResource(clusterName, entity, requestPropertyIds));
        }
      } else {
        Cluster cluster = null;
        try {
          cluster = getManagementController().getClusters().getCluster(clusterName);
        } catch (AmbariException e) {
          throw new NoSuchResourceException("Parent Cluster resource doesn't exist", e);
        }

        List<AlertDefinitionEntity> entities = alertDefinitionDAO.findAll(cluster.getClusterId());

        // Any service name that is not empty or equal to "AMBARI" indicates a service-level alert
        boolean serviceLevelAuthorization = AlertResourceProviderUtils.hasViewAuthorization("_SERVICE_NAME_", cluster.getResourceId());
        boolean clusterLevelAuthorization = AlertResourceProviderUtils.hasViewAuthorization("", cluster.getResourceId());

        for (AlertDefinitionEntity entity : entities) {
          String serviceName = entity.getServiceName();

          // Include the alert in the results if the authenticated user is authorized to get it
          if((StringUtils.isEmpty(serviceName) || "AMBARI".equals(serviceName))
              ? clusterLevelAuthorization
              : serviceLevelAuthorization) {
            results.add(toResource(clusterName, entity, requestPropertyIds));
          }
        }
      }
    }

    return results;
  }

    AlertDefinitionResourceProvider(AmbariManagementController controller);

}