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();
}
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;
}
}
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;
}
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();
}
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;
}
Aggregations