From 5210ec844e68311598687151456aa60b1b10d9c1 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 16 Aug 2016 17:35:03 +0200 Subject: [server] Add mail template for header and footer --- .../src/main/java/org/openslx/bwlp/sat/App.java | 2 +- .../bwlp/sat/database/mappers/DbConfiguration.java | 47 +++++++++++---- .../org/openslx/bwlp/sat/mail/MailTemplate.java | 3 +- .../bwlp/sat/mail/MailTemplateConfiguration.java | 70 ++++++++++++++-------- .../openslx/bwlp/sat/mail/MailTemplatePlain.java | 22 ++++--- .../org/openslx/bwlp/sat/thrift/ServerHandler.java | 6 +- .../main/java/org/openslx/bwlp/sat/web/WebRpc.java | 14 +++-- .../src/test/java/bwlehrpool/MailTemplateTest.java | 2 + 8 files changed, 110 insertions(+), 56 deletions(-) diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/App.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/App.java index 8b3076a5..3589b640 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/App.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/App.java @@ -68,7 +68,7 @@ public class App { try { Updater.updateDatabase(); RuntimeConfig.get(); - DbConfiguration.getMailTemplate(Template.TEST_MAIL); + DbConfiguration.updateMailTemplates(false); } catch (SQLException e1) { LOGGER.fatal("Updating/checking the database layout failed."); return; diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbConfiguration.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbConfiguration.java index e8f9ce27..a94a7441 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbConfiguration.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbConfiguration.java @@ -126,6 +126,21 @@ public class DbConfiguration { public static void setSatelliteConfig(SatelliteConfig config) throws SQLException { store(KEY_LIMITS, Json.serialize(config).getBytes(StandardCharsets.UTF_8)); } + + private static MailTemplateConfiguration getExistingMailTemplates() + { + MailTemplateConfiguration templateConf = null; + try { + byte[] raw = retrieve(KEY_TEMPLATES); + if (raw != null) { + String json = new String(raw, StandardCharsets.UTF_8); + templateConf = Json.deserialize(json, MailTemplateConfiguration.class); + } + } catch (Exception e) { + LOGGER.debug("Cannot get mail templates from db", e); + } + return templateConf; + } /** * access the database to read the mail templates. If the template is not @@ -137,18 +152,8 @@ public class DbConfiguration { * could be found. */ public static MailTemplate getMailTemplate(Template name) { - /* Try to get config from DB */ - MailTemplateConfiguration templateConf = null; - try { - byte[] raw = retrieve(KEY_TEMPLATES); - if (raw != null) { - String json = new String(raw, StandardCharsets.UTF_8); - templateConf = Json.deserialize(json, MailTemplateConfiguration.class); - } - } catch (Exception e) { - LOGGER.debug("Cannot get mail templates from db", e); - } + MailTemplateConfiguration templateConf = getExistingMailTemplates(); /* Case 1: Nothing in DB */ if (templateConf == null) { @@ -180,6 +185,24 @@ public class DbConfiguration { /* CASE 4: Neither in DB nor in default */ LOGGER.debug("Template with name \"" + name + "\" could not be found"); return null; - } + + public static void updateMailTemplates(boolean resetExisting) + { + MailTemplateConfiguration conf = null; + if (!resetExisting) { + conf = getExistingMailTemplates(); + if (conf != null) { + conf = conf.merge(MailTemplateConfiguration.defaultTemplateConfiguration); + } + } + if (conf == null) { + conf = MailTemplateConfiguration.defaultTemplateConfiguration; + } + try { + store(KEY_TEMPLATES, Json.serialize(conf).getBytes(StandardCharsets.UTF_8)); + } catch (SQLException e) { + } + } + } diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/MailTemplate.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/MailTemplate.java index 24133781..6ed88e28 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/MailTemplate.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/MailTemplate.java @@ -50,8 +50,9 @@ public class MailTemplate { int begin = raw.indexOf("%", i); int end = raw.indexOf("%", begin + 1); - if (begin == -1) { + if (begin == -1 || end == -1) { /* no variable anymore, so just add a snippet until the end */ + /* OR: stray %, add rest as literal text */ String snippet = raw.substring(i); this.snippets.add(snippet); break; diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/MailTemplateConfiguration.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/MailTemplateConfiguration.java index 1f41bf11..4eed288a 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/MailTemplateConfiguration.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/MailTemplateConfiguration.java @@ -19,7 +19,8 @@ public class MailTemplateConfiguration { "Wird an die Verantwortlichen einer Veranstaltung gesendet, wenn die verknüpfte VM aktualisiert wurde.", "Zur Veranstaltung %lecture% gehörige VM wurde aktualisiert.", new String[]{}, - new String[]{"lecture"}), + new String[]{"lecture"} + ), new MailTemplatePlain( Template.LECTURE_DEACTIVATED, @@ -28,8 +29,8 @@ public class MailTemplateConfiguration { + " da die verknüpfte VM gelöscht oder beschädigt wurde. Bitte überprüfen" + " Sie die Veranstaltung und ändern Sie ggf. die Verlinkung," + " damit die Veranstaltung wieder verwendbar ist.", - new String[] {}, - new String[] {"lecture"} + new String[] {}, + new String[] {"lecture"} ), new MailTemplatePlain( @@ -37,8 +38,8 @@ public class MailTemplateConfiguration { "Wird versendet, wenn die aktuellste Version einer VM kurz vor dem Ablaufdatum steht.", "Die aktuellste Version der VM '%image%' läuft in %remaining_days% Tag(en) ab." + " Bitte aktualisieren Sie die VM, da verknüpfte Veranstaltungen sonst deaktiviert werden.", - new String[]{"remaining_days"}, - new String[]{"image"} + new String[]{"remaining_days"}, + new String[]{"image"} ), new MailTemplatePlain( @@ -46,8 +47,8 @@ public class MailTemplateConfiguration { "Hinweis, dass eine alte Version einer VM abläuft.", "Eine alte Version der VM '%image%' läuft in %remaining_days% Tag(en) ab (Version vom %created%)." + " Eine aktuellere Version ist vorhanden, diese Nachricht dient nur der Information.", - new String[]{"remaining_days", "created"}, - new String[]{"image"} + new String[]{"remaining_days", "created"}, + new String[]{"image"} ), new MailTemplatePlain( @@ -55,25 +56,24 @@ public class MailTemplateConfiguration { "Hinweis, dass die zu einer Veranstaltung gehörige VM bald abläuft.", "Hinweis zur Veranstaltung '%lecture%': Die verwendete VM '%image'" + " läuft in %remaining_days% Tag(en) ab. Bitte aktualisieren oder wechseln Sie die VM.", - new String[]{"remaining_days"}, - new String[]{"lecture"} + new String[]{"remaining_days"}, + new String[]{"lecture"} ), new MailTemplatePlain( Template.LECTURE_EXPIRING, "Wird versendet, wenn eine Veranstaltung kurz vor dem Enddatum steht.", - "Die Veranstaltung '%lecture%' läuft in %remaining_days% Tag(en) ab. Verlängern Sie bei Bedarf das Ablaufdatum.", - new String[]{"remaining_days"}, - new String[]{"lecture"} - - ), + "Die Veranstaltung '%lecture%' läuft in %remaining_days% Tag(en) ab. Verlängern Sie bei Bedarf das Ablaufdatum.", + new String[]{"remaining_days"}, + new String[]{"lecture"} + ), new MailTemplatePlain( Template.VM_DELETED_LAST_VERSION, "Wird versendet, wenn die letzte gültige Version einer VM gelöscht wurde." - + " Die Metadaten der VM bleiben für einige Tage erhalten, falls die Verantwortliche" - + " eine neue Version hochladen möchte, ohne die Metadaten erneut eingeben zu müssen.", + + " Die Metadaten der VM bleiben für einige Tage erhalten, falls die Verantwortliche" + + " eine neue Version hochladen möchte, ohne die Metadaten erneut eingeben zu müssen.", "Die letzte verbliebene Version der VM '%image%' wurde gelöscht; VM zur Löschung vorgemerkt.", new String[]{}, new String[]{"image"} @@ -82,28 +82,50 @@ public class MailTemplateConfiguration { new MailTemplatePlain( Template.VM_DELETED_OLD_VERSION, "Bestätigung dass eine alte Version der VM gelöscht wurde", - "Eine alte Version der VM '%image%' vom %old.created% wurde gelöscht\n" - + "Die neueste Version ist jetzt vom %new.created% (erstellt von %uploader%)", - new String[]{"old.created", "new.created"}, - new String[]{"image"} - ), + "Eine alte Version der VM '%image%' vom %old_created% wurde gelöscht\n" + + "Die neueste Version ist jetzt vom %new_created% (erstellt von %uploader%)", + new String[]{"old_created", "new_created"}, + new String[]{"image"} + ), + new MailTemplatePlain( Template.LECTURE_FORCED_UPDATE, - "Benachrichtigung nach erzwungenem Update", + "Wird versendet, wenn die VM zu einer Veranstaltung unerwartet nicht mehr verfügbar" + + " ist, aber eine neuere oder ältere Version der VM als Ausweichmöglichkeit" + + " gewählt werden konnte.", "Die verlinkte VM zur Veranstaltung '%lecture%' wurde gelöscht oder ist beschädigt," + "daher verweist sie jetzt auf die VM-Version vom %date%. " + "Bitte überprüfen Sie ggf., ob diese VM-Version für Ihren Kurs geeignet ist.", - new String[]{"date"}, + new String[]{"date"}, new String[]{"lecture"} ), + new MailTemplatePlain( Template.TEST_MAIL, "Die Test-Email, die in der Mail-Konfiguration verschickt werden kann.", "Test der Mailkonfiguration.\n\n%host%:%port% \nSSL: %ssl%" - + "\nLogin: %username%", + + "\nLogin: %username%", new String[]{"host", "port", "ssl", "username"}, new String[]{} + ), + + new MailTemplatePlain( + Template.GENERAL_WRAPPER, + "Einleitung und Grußzeile ausgehender Mails.", + "Guten Tag %first_name% %last_name%,\n\n" + + "Bitte beachten Sie folgende Hinweise zu Virtuellen Maschinen und Veranstaltungen,\n" + + "für die Sie als zuständige Person hinterlegt sind:\n\n" + + "%messages%" + + "\n\n" + + "Dies ist eine automatisch generierte Mail. Wenn Sie keine Hinweise dieser Art\n" + + "wünschen, melden Sie sich bitte mittels der bwLehrpool-Suite an diesem\n" + + "Satellitenserver an, und deaktivieren Sie in den Einstellungen die\n" + + "e-Mail-Benachrichtigungen." + + "\n\n-- \n" + "Generiert auf %sender_name%", + new String[]{"first_name", "last_name", "sender_name"}, + new String[]{"messages"} ) + }; diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/MailTemplatePlain.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/MailTemplatePlain.java index 3c26fcc5..1a20d003 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/MailTemplatePlain.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/MailTemplatePlain.java @@ -3,7 +3,7 @@ package org.openslx.bwlp.sat.mail; import com.google.gson.annotations.SerializedName; public class MailTemplatePlain { - + public enum Template { LECTURE_UPDATED, LECTURE_DEACTIVATED, @@ -14,35 +14,33 @@ public class MailTemplatePlain { VM_DELETED_LAST_VERSION, VM_DELETED_OLD_VERSION, LECTURE_FORCED_UPDATE, - TEST_MAIL + TEST_MAIL, + GENERAL_WRAPPER } - + private Template name; private String description; private String template; - + @SerializedName("optional_variables") private String[] optionalVariables; - - + @SerializedName("mandatory_variables") private String[] mandatoryVariables; - - - - + public MailTemplatePlain(Template name, String description, String template, String[] optionalVariables, - String[] mandatoryVariables) { + String[] mandatoryVariables) { this.name = name; this.description = description; this.template = template; this.optionalVariables = optionalVariables; this.mandatoryVariables = mandatoryVariables; } - + public Template getName() { return this.name; } + public MailTemplate toMailTemplate() { return new MailTemplate(template); } diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/ServerHandler.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/ServerHandler.java index 093df1a0..a76382ba 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/ServerHandler.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/ServerHandler.java @@ -579,7 +579,9 @@ public class ServerHandler implements SatelliteServer.Iface { User.canLinkToImageOrFail(user, lecture.imageVersionId); Sanitizer.handleLectureDates(lecture, null); try { - return DbLecture.create(user, lecture); + String lectureId = DbLecture.create(user, lecture); + DbLog.log(user, lectureId, Formatter.userFullName(user) + " created lecture '" + lecture.lectureName + "'"); + return lectureId; } catch (SQLException e) { throw new TInvocationException(); } @@ -644,8 +646,10 @@ public class ServerHandler implements SatelliteServer.Iface { UserInfo user = SessionManager.getOrFail(userToken); User.canDeleteLectureOrFail(user, lectureId); try { + LectureSummary lecture = DbLecture.getLectureSummary(user, lectureId); if (!DbLecture.delete(lectureId)) throw new TNotFoundException(); + DbLog.log(user, lectureId, Formatter.userFullName(user) + " deleted lecture '" + lecture.lectureName + "'"); } catch (SQLException e) { throw new TInvocationException(); } diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebRpc.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebRpc.java index 12a856d5..e6cc0d73 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebRpc.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebRpc.java @@ -8,25 +8,21 @@ import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; -import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import javax.security.auth.login.LoginException; import org.apache.commons.io.output.ByteArrayOutputStream; -import org.omg.CosNaming.NamingContextPackage.NotFound; import org.openslx.bwlp.sat.database.mappers.DbConfiguration; import org.openslx.bwlp.sat.mail.MailTemplate; -import org.openslx.bwlp.sat.mail.SmtpMailer; import org.openslx.bwlp.sat.mail.MailTemplatePlain.Template; +import org.openslx.bwlp.sat.mail.SmtpMailer; import org.openslx.bwlp.sat.mail.SmtpMailer.EncryptionMode; import org.openslx.bwlp.sat.maintenance.DeleteOldImages; import org.openslx.bwlp.sat.maintenance.ImageValidCheck; import org.openslx.util.Util; -import com.btr.proxy.util.Logger; - import fi.iki.elonen.NanoHTTPD; import fi.iki.elonen.NanoHTTPD.Response; @@ -42,9 +38,17 @@ public class WebRpc { if (uri.equals("check-image")) { return checkImage(params); } + if (uri.equals("reset-mail-templates")) { + return resetMailTemplates(); + } return WebServer.notFound(); } + private static Response resetMailTemplates() { + DbConfiguration.updateMailTemplates(true); + return new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, "text/plain; charset=utf-8", "OK"); + } + private static Response checkImage(Map params) { String versionId = params.get("versionid"); if (versionId == null) diff --git a/dozentenmodulserver/src/test/java/bwlehrpool/MailTemplateTest.java b/dozentenmodulserver/src/test/java/bwlehrpool/MailTemplateTest.java index 7171d3c2..d6b5158c 100644 --- a/dozentenmodulserver/src/test/java/bwlehrpool/MailTemplateTest.java +++ b/dozentenmodulserver/src/test/java/bwlehrpool/MailTemplateTest.java @@ -23,6 +23,8 @@ public class MailTemplateTest extends TestCase { assertEquals("x1", new MailTemplate("x%eins%").format(map)); assertEquals("1x", new MailTemplate("%eins%x").format(map)); assertEquals("123", new MailTemplate("%eins%%zwei%%drei%").format(map)); + assertEquals("123x11", new MailTemplate("%eins%%zwei%%drei%x%eins%1").format(map)); + assertEquals(".1.%zwei", new MailTemplate(".%eins%.%zwei").format(map)); } public void testMailTemplateWrongPlaceholder() { -- cgit v1.2.3-55-g7522