use of cz.metacentrum.perun.notif.exceptions.TemplateMessageSyntaxErrorException in project perun by CESNET.
the class PerunNotifTemplateManagerImpl method updatePerunNotifTemplate.
@Override
public PerunNotifTemplate updatePerunNotifTemplate(PerunNotifTemplate template) throws InternalErrorException {
PerunNotifTemplate oldTemplate = getPerunNotifTemplateById(template.getId());
perunNotifTemplateDao.updatePerunNotifTemplateData(template);
// create rexeges if not exist
if (template.getMatchingRegexs() != null) {
for (PerunNotifRegex regex : template.getMatchingRegexs()) {
if ((regex.getId() == null) || (perunNotifRegexDao.getPerunNotifRegexById(regex.getId()) == null)) {
perunNotifRegexDao.saveInternals(regex);
}
if (!(perunNotifRegexDao.isRegexRelation(template.getId(), regex.getId()))) {
perunNotifRegexDao.saveTemplateRegexRelation(template.getId(), regex.getId());
}
}
}
// create receivers if not exist
if (template.getReceivers() != null) {
for (PerunNotifReceiver receiver : template.getReceivers()) {
receiver.setTemplateId(template.getId());
if ((receiver.getId() == null) || (perunNotifTemplateDao.getPerunNotifReceiverById(receiver.getId()) == null)) {
perunNotifTemplateDao.createPerunNotifReceiver(receiver);
}
}
}
// create template messages if not exist
if (template.getPerunNotifTemplateMessages() != null) {
for (PerunNotifTemplateMessage message : template.getPerunNotifTemplateMessages()) {
message.setTemplateId(template.getId());
PerunNotifTemplateMessage newMessage;
try {
newMessage = createPerunNotifTemplateMessage(message);
message.setId(newMessage.getId());
} catch (NotifTemplateMessageAlreadyExistsException ex) {
// template message already exists
} catch (TemplateMessageSyntaxErrorException ex) {
throw new InternalErrorException(ex);
}
}
}
PerunNotifTemplate updatedTemplate = getPerunNotifTemplateById(template.getId());
allTemplatesById.put(updatedTemplate.getId(), updatedTemplate);
for (PerunNotifRegex regex : updatedTemplate.getMatchingRegexs()) {
List<PerunNotifTemplate> list = allTemplatesByRegexId.get(regex.getId());
if (list == null) {
list = new ArrayList<PerunNotifTemplate>();
allTemplatesByRegexId.put(regex.getId(), list);
}
boolean updated = false;
for (PerunNotifTemplate templateToUpdate : list) {
if (templateToUpdate.getId() == updatedTemplate.getId()) {
templateToUpdate.update(updatedTemplate);
updated = true;
}
}
if (!updated) {
list.add(updatedTemplate);
}
}
return updatedTemplate;
}
use of cz.metacentrum.perun.notif.exceptions.TemplateMessageSyntaxErrorException in project perun by CESNET.
the class PerunNotifTemplateManagerImpl method createPerunNotifTemplate.
@Override
public PerunNotifTemplate createPerunNotifTemplate(PerunNotifTemplate template) throws InternalErrorException {
// save template internals
PerunNotifTemplate newTemplate = perunNotifTemplateDao.savePerunNotifTemplateInternals(template);
template.setId(newTemplate.getId());
// create rexeges if not exist
if (template.getMatchingRegexs() != null) {
for (PerunNotifRegex regex : template.getMatchingRegexs()) {
if ((regex.getId() == null) || (perunNotifRegexDao.getPerunNotifRegexById(regex.getId()) == null)) {
PerunNotifRegex newRegex = perunNotifRegexDao.saveInternals(regex);
regex.setId(newRegex.getId());
}
if (!(perunNotifRegexDao.isRegexRelation(template.getId(), regex.getId()))) {
perunNotifRegexDao.saveTemplateRegexRelation(template.getId(), regex.getId());
}
}
}
// create receivers if not exist
if (template.getReceivers() != null) {
for (PerunNotifReceiver receiver : template.getReceivers()) {
receiver.setTemplateId(template.getId());
if ((receiver.getId() == null) || (perunNotifTemplateDao.getPerunNotifReceiverById(receiver.getId()) == null)) {
PerunNotifReceiver newReceiver = perunNotifTemplateDao.createPerunNotifReceiver(receiver);
receiver.setId(newReceiver.getId());
}
}
}
// create template messages if not exist
if (template.getPerunNotifTemplateMessages() != null) {
for (PerunNotifTemplateMessage message : template.getPerunNotifTemplateMessages()) {
message.setTemplateId(template.getId());
PerunNotifTemplateMessage newMessage;
try {
newMessage = createPerunNotifTemplateMessage(message);
message.setId(newMessage.getId());
} catch (NotifTemplateMessageAlreadyExistsException ex) {
// template message already exists
} catch (TemplateMessageSyntaxErrorException ex) {
throw new InternalErrorException(ex);
}
}
}
// update cache allTemplatesById
allTemplatesById.put(template.getId(), template);
// update cache allTemplatesByRegexId
if (template.getMatchingRegexs() != null) {
for (PerunNotifRegex regex : template.getMatchingRegexs()) {
List<PerunNotifTemplate> list = allTemplatesByRegexId.get(regex.getId());
if (list == null) {
list = new ArrayList<>();
list.add(template);
allTemplatesByRegexId.put(regex.getId(), list);
} else {
list.add(template);
}
}
}
return template;
}
use of cz.metacentrum.perun.notif.exceptions.TemplateMessageSyntaxErrorException in project perun by CESNET.
the class PerunNotifTemplateManagerImpl method validateTemplateMessage.
private void validateTemplateMessage(PerunNotifTemplateMessage message) throws InternalErrorException, TemplateMessageSyntaxErrorException {
String templateName = Integer.toString(message.getTemplateId());
Locale locale = message.getLocale();
try {
Template freeMarkerTemplate = this.configuration.getTemplate(templateName + "_" + locale.getLanguage(), locale);
} catch (ParseException ex) {
throw new TemplateMessageSyntaxErrorException(message, ex);
} catch (IOException ex) {
// template not found
throw new InternalErrorException("FreeMarker Template internal error.", ex);
}
}
Aggregations