private MeasurementBaseline calculateBaselineForGroup( int groupId, int definitionId, boolean userEntered, long startDate, long endDate, boolean save) throws DataNotAvailableException, BaselineCreationException { MeasurementAggregate agg = dataManager.getAggregate( subjectManager.getOverlord(), groupId, definitionId, startDate, endDate); Subject overlord = subjectManager.getOverlord(); List<Integer> resourceIds = resourceManager.findImplicitResourceIdsByResourceGroup(groupId); List<MeasurementSchedule> schedules = measurementScheduleManager.findSchedulesByResourceIdsAndDefinitionId( overlord, ArrayUtils.unwrapCollection(resourceIds), definitionId); MeasurementBaseline baseline = null; for (MeasurementSchedule schedule : schedules) { // attach the entity, so we can find the baseline schedule = entityManager.merge(schedule); if (save && (schedule.getBaseline() != null)) { /* * If saving, make sure we're updating the existing one, if it exists */ baseline = schedule.getBaseline(); } else { /* * Otherwise, if we're not saving or if the the schedule doesn't have a current baseline, we create a new * baseline object */ baseline = new MeasurementBaseline(); if (save) { /* * But, if we *are* in save mode, then set the relationship so when we merge the schedule below it * persists this new baseline too */ baseline.setSchedule(schedule); } } baseline.setUserEntered(userEntered); baseline.setMean(agg.getAvg()); baseline.setMin(agg.getMin()); baseline.setMax(agg.getMax()); if (save) { entityManager.persist(baseline); entityManager.merge(schedule); } } // all baselines should be the same return baseline; }
private MeasurementBaseline calculateBaseline( MeasurementSchedule schedule, boolean userEntered, long startDate, long endDate, boolean save) throws DataNotAvailableException, BaselineCreationException { /* * jmarques: 2007-10-26 * * navigation from schedule to definition is safe here because the only caller to this method is * calculateAutoBaseline( Subject, Integer, long, long, boolean ), which uses entityManager.find, so the * schedule should still be attached since the transaction is propagated to this method */ if (schedule.getDefinition().getNumericType() != NumericType.DYNAMIC) { throw new BaselineCreationException( "Baseline calculation is only valid for a dynamic measurement"); } MeasurementAggregate agg = dataManager.getAggregate( subjectManager.getOverlord(), schedule.getId(), startDate, endDate); // attach the entity, so we can find the baseline schedule = entityManager.merge(schedule); MeasurementBaseline baseline = null; if (save && (schedule.getBaseline() != null)) { /* * If saving, make sure we're updating the existing one, if it exists */ baseline = schedule.getBaseline(); } else { /* * Otherwise, if we're not saving or if the the schedule doesn't have a current baseline, we create a new * baseline object */ baseline = new MeasurementBaseline(); if (save) { /* * But, if we *are* in save mode, then set the relationship so when we merge the schedule below it * persists this new baseline too */ baseline.setSchedule(schedule); } } baseline.setUserEntered(userEntered); baseline.setMean(agg.getAvg()); baseline.setMin(agg.getMin()); baseline.setMax(agg.getMax()); if (save) { entityManager.persist(baseline); entityManager.merge(schedule); } return baseline; }