@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;
  }
Ejemplo n.º 2
0
  @SuppressWarnings("unchecked")
  public PageList<CallTimeDataComposite> findCallTimeDataForResource(
      Subject subject, int scheduleId, long beginTime, long endTime, PageControl pageControl) {
    pageControl.initDefaultOrderingField(
        "SUM(value.total)/SUM(value.count)",
        PageOrdering.DESC); // only set if no ordering yet specified
    pageControl.addDefaultOrderingField(
        "key.callDestination", PageOrdering.ASC); // add this to sort, if not already specified

    MeasurementSchedule schedule = entityManager.find(MeasurementSchedule.class, scheduleId);
    int resourceId = schedule.getResource().getId();
    if (authorizationManager.canViewResource(subject, resourceId) == false) {
      throw new PermissionException(
          "User ["
              + subject
              + "] does not have permission to view call time data for measurementSchedule[id="
              + scheduleId
              + "] and resource[id="
              + resourceId
              + "]");
    }

    String query = CallTimeDataValue.QUERY_FIND_COMPOSITES_FOR_RESOURCE;

    Query queryWithOrderBy =
        PersistenceUtility.createQueryWithOrderBy(entityManager, query, pageControl);
    Query queryCount = PersistenceUtility.createCountQuery(this.entityManager, query);

    queryWithOrderBy.setParameter("scheduleId", scheduleId);
    queryWithOrderBy.setParameter("beginTime", beginTime);
    queryWithOrderBy.setParameter("endTime", endTime);

    List<CallTimeDataComposite> results = queryWithOrderBy.getResultList();

    queryCount.setParameter("scheduleId", scheduleId);
    queryCount.setParameter("beginTime", beginTime);
    queryCount.setParameter("endTime", endTime);

    // Because of the use of the GROUP BY clause, the result list count will be returned as
    // the number of rows, rather than as a single number.
    long count = queryCount.getResultList().size();

    return new PageList<CallTimeDataComposite>(results, (int) count, pageControl);
  }