Search in sources :

Example 1 with StringTemplateLoader

use of cz.metacentrum.perun.notif.StringTemplateLoader in project perun by CESNET.

the class PerunNotifTemplateManagerImpl method removePerunNotifTemplateMessage.

@Override
public void removePerunNotifTemplateMessage(int id) throws InternalErrorException {
    PerunNotifTemplateMessage templateMessage = getPerunNotifTemplateMessageById(id);
    if (templateMessage == null) {
        throw new NotExistsException("Template message with id: " + id + " not exists.");
    }
    perunNotifTemplateDao.removePerunNotifTemplateMessage(id);
    PerunNotifTemplate template = allTemplatesById.get(templateMessage.getTemplateId());
    template.getPerunNotifTemplateMessages().remove(templateMessage);
    StringTemplateLoader stringTemplateLoader = (StringTemplateLoader) configuration.getTemplateLoader();
    String templateName = createTemplateName(templateMessage);
    stringTemplateLoader.removeTemplate(templateName);
    configuration.clearTemplateCache();
}
Also used : StringTemplateLoader(cz.metacentrum.perun.notif.StringTemplateLoader) NotExistsException(cz.metacentrum.perun.notif.exceptions.NotExistsException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)

Example 2 with StringTemplateLoader

use of cz.metacentrum.perun.notif.StringTemplateLoader in project perun by CESNET.

the class PerunNotifTemplateManagerImpl method resolveSender.

private String resolveSender(String input, Map<String, Object> container) throws IOException {
    Matcher emailMatcher = Utils.emailPattern.matcher(input);
    String method = null;
    String email = null;
    if (input.contains(";")) {
        String[] parts = input.split(";", 2);
        method = parts[0];
        email = parts[1];
    } else if (!emailMatcher.find()) {
        method = input;
    } else {
        email = input;
    }
    if (method != null) {
        StringTemplateLoader stringTemplateLoader = (StringTemplateLoader) configuration.getTemplateLoader();
        stringTemplateLoader.putTemplate(EVALUATION_TEMPLATE, "${" + method + "}");
        configuration.clearTemplateCache();
        Template freeMarkerTemplate = this.configuration.getTemplate(EVALUATION_TEMPLATE);
        StringWriter stringWriter = new StringWriter(4096);
        try {
            // because for template messages the nulls are ignored, now we want to fail when null
            this.configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
            freeMarkerTemplate.process(container, stringWriter);
        } catch (TemplateException ex) {
            stringWriter = null;
            logger.info("Resolving sender for method " + method + " failed because of exception: ", ex);
        }
        if (stringWriter != null) {
            if (stringWriter.toString().trim().isEmpty()) {
                stringWriter = null;
            }
        }
        if (stringWriter == null) {
            return email;
        } else {
            return stringWriter.toString();
        }
    } else {
        return email;
    }
}
Also used : StringTemplateLoader(cz.metacentrum.perun.notif.StringTemplateLoader) StringWriter(java.io.StringWriter) Matcher(java.util.regex.Matcher) TemplateException(freemarker.template.TemplateException) Template(freemarker.template.Template)

Example 3 with StringTemplateLoader

use of cz.metacentrum.perun.notif.StringTemplateLoader 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 4 with StringTemplateLoader

use of cz.metacentrum.perun.notif.StringTemplateLoader in project perun by CESNET.

the class PerunNotifTemplateManagerImpl method testPerunNotifMessageText.

@Override
public String testPerunNotifMessageText(String template, Map<Integer, List<PerunBean>> regexIdsBeans) throws IOException, TemplateException {
    //Initialization of freemarker
    StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
    Configuration myConfiguration = createFreemarkerConfiguration(stringTemplateLoader);
    stringTemplateLoader.putTemplate("test", template);
    Template freeMarkerTemplate = myConfiguration.getTemplate(template, new Locale("en"));
    StringWriter stringWriter = new StringWriter(4096);
    Map<String, Object> container = new HashMap<String, Object>();
    Map<String, Map<String, PerunBean>> resultMapOfBeans = new HashMap<String, Map<String, PerunBean>>();
    for (Integer regexId : regexIdsBeans.keySet()) {
        Map<String, PerunBean> normalizedBeans = new HashMap<String, PerunBean>();
        List<PerunBean> perunBeans = regexIdsBeans.get(regexId);
        for (PerunBean retrievedObject : perunBeans) {
            normalizedBeans.put(normalizeName(retrievedObject.getClass().toString()), retrievedObject);
        }
        resultMapOfBeans.put(regexId.toString(), normalizedBeans);
    }
    container.put("retrievedObjects", resultMapOfBeans);
    container.put("perunSession", session);
    container.put("perun", perun);
    freeMarkerTemplate.process(container, stringWriter);
    return stringWriter.toString();
}
Also used : Configuration(freemarker.template.Configuration) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Template(freemarker.template.Template) StringTemplateLoader(cz.metacentrum.perun.notif.StringTemplateLoader) PerunBean(cz.metacentrum.perun.core.api.PerunBean) StringWriter(java.io.StringWriter) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 5 with StringTemplateLoader

use of cz.metacentrum.perun.notif.StringTemplateLoader in project perun by CESNET.

the class PerunNotifTemplateManagerImpl method updatePerunNotifTemplateMessage.

@Override
public PerunNotifTemplateMessage updatePerunNotifTemplateMessage(PerunNotifTemplateMessage message) throws InternalErrorException, TemplateMessageSyntaxErrorException {
    StringTemplateLoader stringTemplateLoader = (StringTemplateLoader) configuration.getTemplateLoader();
    insertPerunNotifTemplateMessageToLoader(stringTemplateLoader, message);
    configuration.clearTemplateCache();
    validateTemplateMessage(message);
    PerunNotifTemplateMessage oldMessage = perunNotifTemplateDao.getPerunNotifTemplateMessageById(message.getId());
    PerunNotifTemplateMessage newMessage = perunNotifTemplateDao.updatePerunNotifTemplateMessage(message);
    if (oldMessage.getTemplateId() != newMessage.getTemplateId()) {
        //Template message has changed template
        PerunNotifTemplate oldTemplate = allTemplatesById.get(oldMessage.getTemplateId());
        oldTemplate.getPerunNotifTemplateMessages().remove(oldMessage);
        PerunNotifTemplate newTemplate = allTemplatesById.get(newMessage.getTemplateId());
        newTemplate.addPerunNotifTemplateMessage(newMessage);
        for (List<PerunNotifTemplate> templateList : allTemplatesByRegexId.values()) {
            for (PerunNotifTemplate template : templateList) {
                if (template.getId() == oldTemplate.getId()) {
                    template.getPerunNotifTemplateMessages().remove(oldMessage);
                }
                if (template.getId() == newTemplate.getId()) {
                    template.addPerunNotifTemplateMessage(newMessage);
                }
            }
        }
    } else {
        //We only update templateMessage
        PerunNotifTemplate template = allTemplatesById.get(newMessage.getTemplateId());
        for (PerunNotifTemplateMessage myMessage : template.getPerunNotifTemplateMessages()) {
            if (myMessage.getId() == newMessage.getId()) {
                myMessage.update(newMessage);
            }
        }
    }
    return newMessage;
}
Also used : StringTemplateLoader(cz.metacentrum.perun.notif.StringTemplateLoader)

Aggregations

StringTemplateLoader (cz.metacentrum.perun.notif.StringTemplateLoader)6 Template (freemarker.template.Template)2 StringWriter (java.io.StringWriter)2 PerunBean (cz.metacentrum.perun.core.api.PerunBean)1 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)1 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)1 NotExistsException (cz.metacentrum.perun.notif.exceptions.NotExistsException)1 NotifTemplateMessageAlreadyExistsException (cz.metacentrum.perun.notif.exceptions.NotifTemplateMessageAlreadyExistsException)1 Configuration (freemarker.template.Configuration)1 TemplateException (freemarker.template.TemplateException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Matcher (java.util.regex.Matcher)1 PostConstruct (javax.annotation.PostConstruct)1