@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
  public MeasurementBaseline calculateAutoBaselineInNewTransaction(
      Subject subject, Integer measurementScheduleId, long startDate, long endDate, boolean save)
      throws BaselineCreationException, MeasurementNotFoundException {

    MeasurementBaseline baseline;
    MeasurementSchedule sched =
        entityManager.find(MeasurementSchedule.class, measurementScheduleId);

    if (sched != null) {
      Resource resource = sched.getResource();

      // only check permissions if the user is attempting to save a new baseline
      if (save
          && !authorizationManager.hasResourcePermission(
              subject, Permission.MANAGE_MEASUREMENTS, resource.getId())) {
        log.error(
            "Cannot calculate baseline - permission denied. "
                + "resource="
                + resource
                + "; user="******"; perm="
                + Permission.MANAGE_MEASUREMENTS);
        throw new PermissionException(
            "Cannot calculate baseline - you do not have permission on this resource");
      }
    } else {
      throw new MeasurementNotFoundException(
          "Scheduled measurement [" + measurementScheduleId + "] not found");
    }

    try {
      baseline = calculateBaseline(sched, true, startDate, endDate, save);
      if (save) {
        // We have changed the baseline information for the schedule, so remove the now outdated OOB
        // info.
        oobManager.removeOOBsForSchedule(subject, sched);
      }
    } catch (DataNotAvailableException e) {
      throw new BaselineCreationException(
          "Error fetching data for baseline calculation for measurementSchedule[id="
              + measurementScheduleId
              + "]");
    }

    return baseline;
  }
  public void enableAutoBaselineCalculation(
      Subject subject, Integer[] resourceIds, Integer[] definitionIds) {
    // bail out early if there's nothing to do
    if ((resourceIds.length < 1) || (definitionIds.length < 1)) {
      return;
    }

    List<MeasurementBaseline> bList =
        getBaselinesForResourcesAndDefinitionIds(resourceIds, definitionIds);
    for (MeasurementBaseline bl : bList) {
      if (!authorizationManager.hasResourcePermission(
          subject, Permission.MANAGE_MEASUREMENTS, bl.getSchedule().getResource().getId())) {
        throw new PermissionException(
            "Cannot enable baseline [" + bl + "] - you do not have permission");
      }

      bl.setUserEntered(false);
    }
  }