Search in sources :

Example 1 with NotifTemplateMessageAlreadyExistsException

use of cz.metacentrum.perun.notif.exceptions.NotifTemplateMessageAlreadyExistsException 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 NotifTemplateMessageAlreadyExistsException

use of cz.metacentrum.perun.notif.exceptions.NotifTemplateMessageAlreadyExistsException in project perun by CESNET.

the class PerunNotifTemplateManagerImpl method createPerunNotifTemplateMessage.

@Override
public PerunNotifTemplateMessage createPerunNotifTemplateMessage(PerunNotifTemplateMessage message) throws InternalErrorException, NotifTemplateMessageAlreadyExistsException, TemplateMessageSyntaxErrorException {
    // if there is already template message with the same template id and locale -> throw exception
    PerunNotifTemplate template = allTemplatesById.get(message.getTemplateId());
    if (template != null) {
        for (PerunNotifTemplateMessage item : template.getPerunNotifTemplateMessages()) {
            if (item.getLocale().equals(message.getLocale())) {
                throw new NotifTemplateMessageAlreadyExistsException(message);
            }
        }
    }
    StringTemplateLoader stringTemplateLoader = (StringTemplateLoader) configuration.getTemplateLoader();
    insertPerunNotifTemplateMessageToLoader(stringTemplateLoader, message);
    validateTemplateMessage(message);
    PerunNotifTemplateMessage perunNotifTemplateMessage = perunNotifTemplateDao.createPerunNotifTemplateMessage(message);
    if (template != null) {
        template.addPerunNotifTemplateMessage(perunNotifTemplateMessage);
    }
    return perunNotifTemplateMessage;
}
Also used : StringTemplateLoader(cz.metacentrum.perun.notif.StringTemplateLoader) NotifTemplateMessageAlreadyExistsException(cz.metacentrum.perun.notif.exceptions.NotifTemplateMessageAlreadyExistsException)

Example 3 with NotifTemplateMessageAlreadyExistsException

use of cz.metacentrum.perun.notif.exceptions.NotifTemplateMessageAlreadyExistsException 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)

Aggregations

NotifTemplateMessageAlreadyExistsException (cz.metacentrum.perun.notif.exceptions.NotifTemplateMessageAlreadyExistsException)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 TemplateMessageSyntaxErrorException (cz.metacentrum.perun.notif.exceptions.TemplateMessageSyntaxErrorException)2 StringTemplateLoader (cz.metacentrum.perun.notif.StringTemplateLoader)1