/*
* Method for catching ENABLE / DISABLE changes will use switch logic off of the delta instead of calling out to
* the enable/disable functions
*/
AlertDefinition oldAlertDefinition = entityManager.find(AlertDefinition.class, alertDefinitionId);
if (checkPerms && checkPermission(subject, oldAlertDefinition) == false) {
if (oldAlertDefinition.getResourceType() != null) {
throw new PermissionException("User [" + subject.getName()
+ "] does not have permission to modify alert templates for type ["
+ oldAlertDefinition.getResourceType() + "]");
} else if (oldAlertDefinition.getGroup() != null) {
throw new PermissionException("User [" + subject.getName()
+ "] does not have permission to modify alert definitions for group ["
+ oldAlertDefinition.getGroup() + "]");
} else {
throw new PermissionException("User [" + subject.getName()
+ "] does not have permission to modify alert definitions for resource ["
+ oldAlertDefinition.getResource() + "]");
}
}
/*
* only need to check the validity of the new alert definition if the authz checks pass *and* the old definition
* is not currently deleted
*/
boolean isResourceLevel = (oldAlertDefinition.getResource() != null);
checkAlertDefinition(subject, oldAlertDefinition, alertDefinition, isResourceLevel ? oldAlertDefinition
.getResource().getId() : null, finalizeNotifications);
/*
* Should not be able to update an alert definition if the old alert definition is in an invalid state
*/
if (oldAlertDefinition.getDeleted()) {
throw new AlertDefinitionUpdateException("Can not update deleted " + oldAlertDefinition.toSimpleString());
}
AlertDefinitionUpdateType updateType = AlertDefinitionUpdateType.get(oldAlertDefinition, alertDefinition);
if (isResourceLevel
&& ((updateType == AlertDefinitionUpdateType.JUST_DISABLED) || (updateType == AlertDefinitionUpdateType.STILL_ENABLED))) {
/*
* if you were JUST_DISABLED or STILL_ENABLED, you are coming from the ENABLED state, which means you need
* to be removed from the cache as the first half of this update
*/
if (LOG.isDebugEnabled()) {
LOG.debug("Updating AlertConditionCacheManager with AlertDefinition[ id=" + oldAlertDefinition.getId()
+ " ]...DELETING");
for (AlertCondition nextCondition : oldAlertDefinition.getConditions()) {
LOG.debug("OldAlertCondition[ id=" + nextCondition.getId() + " ]");
}
}
notifyAlertConditionCacheManager(subject, "updateAlertDefinition", oldAlertDefinition,
AlertDefinitionEvent.DELETED);
}
/*
* performance optimization for the common case of single-condition alerts; it's easier for the
* out-of-band process to check whether or not ANY conditions are true rather than ALL of them
*/
if (alertDefinition.getConditions().size() == 1) {
alertDefinition.setConditionExpression(BooleanExpression.ANY);
}
oldAlertDefinition.update(alertDefinition, resetMatching);
if (LOG.isDebugEnabled()) {
LOG.debug("Updating: " + oldAlertDefinition);
for (AlertCondition nextCondition : oldAlertDefinition.getConditions()) {
LOG.debug("Condition: " + nextCondition);
}
for (AlertNotification nextNotification : oldAlertDefinition.getAlertNotifications()) {
LOG.debug("Notification: " + nextNotification);
LOG.debug("Notification-Configuration: " + nextNotification.getConfiguration().toString(true));
if (nextNotification.getExtraConfiguration() != null) {
LOG.debug("Notification-Extra-Configuration: "
+ nextNotification.getExtraConfiguration().toString(true));
}
}
}
fixRecoveryId(oldAlertDefinition);
oldAlertDefinition.setMtime(System.currentTimeMillis());
AlertDefinition newAlertDefinition = entityManager.merge(oldAlertDefinition);
if (isResourceLevel
&& ((updateType == AlertDefinitionUpdateType.JUST_ENABLED) || (updateType == AlertDefinitionUpdateType.STILL_ENABLED))) {
/*
* if you were JUST_ENABLED or STILL_ENABLED, you are moving to the ENABLED state, which means you need to
* be added to the cache as the last half of this update
*/
boolean addToCache = false;
// if this was a recovery alert, or was recently turned into one
if (newAlertDefinition.getRecoveryId() != 0) {
// only add to the cache if the to-be-recovered definition is disabled, and thus needs recovering
AlertDefinition toBeRecoveredDefinition = getAlertDefinitionById(subject,
newAlertDefinition.getRecoveryId());
if (toBeRecoveredDefinition.getEnabled() == false) {
addToCache = true;
}
} else {
addToCache = true;
}