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);
}