@SuppressWarnings("unchecked")
List<Element> parameters = params.getChildren();
for(Element parameter : parameters) {
String identifier = parameter.getName().substring(parameter.getName().lastIndexOf('-') + 1);
if(notificationTargets.containsKey(identifier)) {
NotificationTarget notificationTarget = notificationTargets.get(identifier);
notificationTarget = parameterToNotificationTarget(parameter, notificationTarget);
}
else {
NotificationTarget notificationTarget = new NotificationTarget();
notificationTarget.setId(identifier);
notificationTarget = parameterToNotificationTarget(parameter, notificationTarget);
notificationTargets.put(identifier, notificationTarget);
}
}
final MetadataNotificationRepository notificationRepository = context.getBean(MetadataNotificationRepository.class);
final MetadataNotifierRepository notifierRepository = context.getBean(MetadataNotifierRepository.class);
for(NotificationTarget notificationTarget : notificationTargets.values()) {
final MetadataNotifier metadataNotifier = new MetadataNotifier();
metadataNotifier.setName(notificationTarget.getName());
metadataNotifier.setUsername(notificationTarget.getUsername());
metadataNotifier.setPassword(notificationTarget.getPassword());
metadataNotifier.setUrl(notificationTarget.getUrl());
metadataNotifier.setEnabled(notificationTarget.isEnabled());
// insert
if (!notificationTarget.isPreExisting() && StringUtils.isNotBlank(notificationTarget.getName())
&& StringUtils.isNotBlank(notificationTarget.getUrl())) {
notifierRepository.save(metadataNotifier);
} else if(notificationTarget.isPreExisting()) {
// pre-existing
String id = notificationTarget.getId();
// delete
if(notificationTarget.getName() == null) {
int iid = Integer.parseInt(id);
notificationRepository.deleteAllWithNotifierId(iid);
notifierRepository.delete(iid);
} else {
// update