Search in sources :

Example 1 with TemplateMessageSyntaxErrorException

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;
}
Also used : NotifTemplateMessageAlreadyExistsException(cz.metacentrum.perun.notif.exceptions.NotifTemplateMessageAlreadyExistsException) TemplateMessageSyntaxErrorException(cz.metacentrum.perun.notif.exceptions.TemplateMessageSyntaxErrorException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 2 with TemplateMessageSyntaxErrorException

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;
}
Also used : NotifTemplateMessageAlreadyExistsException(cz.metacentrum.perun.notif.exceptions.NotifTemplateMessageAlreadyExistsException) TemplateMessageSyntaxErrorException(cz.metacentrum.perun.notif.exceptions.TemplateMessageSyntaxErrorException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 3 with TemplateMessageSyntaxErrorException

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);
    }
}
Also used : TemplateMessageSyntaxErrorException(cz.metacentrum.perun.notif.exceptions.TemplateMessageSyntaxErrorException) ParseException(freemarker.core.ParseException) IOException(java.io.IOException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Template(freemarker.template.Template)

Aggregations

InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)3 TemplateMessageSyntaxErrorException (cz.metacentrum.perun.notif.exceptions.TemplateMessageSyntaxErrorException)3 NotifTemplateMessageAlreadyExistsException (cz.metacentrum.perun.notif.exceptions.NotifTemplateMessageAlreadyExistsException)2 ParseException (freemarker.core.ParseException)1 Template (freemarker.template.Template)1 IOException (java.io.IOException)1