diff options
author | Simon Rettberg | 2016-08-09 13:22:56 +0200 |
---|---|---|
committer | Simon Rettberg | 2016-08-09 13:22:56 +0200 |
commit | a1058f4d9baba8bd9b6662ba5ef2d41a5dd54cf2 (patch) | |
tree | bd6d07876908ce30bc5f18a72cd203d3c3774f18 /dozentenmodulserver/src | |
parent | Merge branch 'feature/mail-templates' into v1.1 (diff) | |
download | tutor-module-a1058f4d9baba8bd9b6662ba5ef2d41a5dd54cf2.tar.gz tutor-module-a1058f4d9baba8bd9b6662ba5ef2d41a5dd54cf2.tar.xz tutor-module-a1058f4d9baba8bd9b6662ba5ef2d41a5dd54cf2.zip |
[server] Make mail template system use enum for template names to avoid typos
Diffstat (limited to 'dozentenmodulserver/src')
8 files changed, 76 insertions, 57 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 637fd0ce..8b3076a5 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/App.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/App.java @@ -16,8 +16,10 @@ import org.apache.log4j.PatternLayout; import org.apache.thrift.TException; import org.apache.thrift.transport.TTransportException; import org.openslx.bwlp.sat.database.Updater; +import org.openslx.bwlp.sat.database.mappers.DbConfiguration; import org.openslx.bwlp.sat.database.mappers.DbUser; import org.openslx.bwlp.sat.fileserv.FileServer; +import org.openslx.bwlp.sat.mail.MailTemplatePlain.Template; import org.openslx.bwlp.sat.maintenance.DeleteOldImages; import org.openslx.bwlp.sat.maintenance.DeleteOldLectures; import org.openslx.bwlp.sat.maintenance.MailFlusher; @@ -65,6 +67,8 @@ public class App { // Update database schema if applicable try { Updater.updateDatabase(); + RuntimeConfig.get(); + DbConfiguration.getMailTemplate(Template.TEST_MAIL); } catch (SQLException e1) { LOGGER.fatal("Updating/checking the database layout failed."); return; diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/RuntimeConfig.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/RuntimeConfig.java index 987a3f90..245d2730 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/RuntimeConfig.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/RuntimeConfig.java @@ -15,12 +15,12 @@ public class RuntimeConfig { private static GenericDataCache<SatelliteConfig> cache = new GenericDataCache<SatelliteConfig>(60000) { @Override - protected SatelliteConfig update() throws Exception { - SatelliteConfig satConfig; + protected SatelliteConfig update() { + SatelliteConfig satConfig, readConfig; try { - satConfig = DbConfiguration.getSatelliteConfig(); + readConfig = satConfig = DbConfiguration.getSatelliteConfig(); } catch (SQLException e) { - satConfig = null; + readConfig = satConfig = null; } if (satConfig == null) { satConfig = new SatelliteConfig(); @@ -45,6 +45,12 @@ public class RuntimeConfig { satConfig.setMaxConnectionsPerTransfer(Constants.MAX_CONNECTIONS_PER_TRANSFER); satConfig.setMaxTransfers(Constants.MAX_UPLOADS_PER_USER); satConfig.setMaxLocationsPerLecture(4); + if (!satConfig.equals(readConfig)) { + try { + DbConfiguration.setSatelliteConfig(satConfig); + } catch (SQLException e) { + } + } return satConfig; } }; 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 53e5acc7..e8f9ce27 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 @@ -14,8 +14,6 @@ import java.security.cert.CertificateException; import java.sql.ResultSet; import java.sql.SQLException; -import javax.swing.DebugGraphics; - import org.apache.commons.io.IOUtils; import org.apache.log4j.Logger; import org.openslx.bwlp.sat.database.Database; @@ -24,6 +22,7 @@ import org.openslx.bwlp.sat.database.MysqlStatement; import org.openslx.bwlp.sat.mail.MailQueue.MailConfig; import org.openslx.bwlp.sat.mail.MailTemplate; import org.openslx.bwlp.sat.mail.MailTemplateConfiguration; +import org.openslx.bwlp.sat.mail.MailTemplatePlain.Template; import org.openslx.bwlp.thrift.iface.SatelliteConfig; import org.openslx.util.Json; @@ -123,8 +122,12 @@ public class DbConfiguration { return null; return Json.deserializeThrift(new String(conf, StandardCharsets.UTF_8), SatelliteConfig.class); } + + public static void setSatelliteConfig(SatelliteConfig config) throws SQLException { + store(KEY_LIMITS, Json.serialize(config).getBytes(StandardCharsets.UTF_8)); + } - /* + /** * access the database to read the mail templates. If the template is not * found a hard-coded configuration is used and is merged with the database. * @@ -133,7 +136,7 @@ public class DbConfiguration { * @return the mail template with the given name or NULL if no such template * could be found. */ - public static MailTemplate getMailTemplate(String name) { + public static MailTemplate getMailTemplate(Template name) { /* Try to get config from DB */ MailTemplateConfiguration templateConf = null; @@ -143,8 +146,8 @@ public class DbConfiguration { String json = new String(raw, StandardCharsets.UTF_8); templateConf = Json.deserialize(json, MailTemplateConfiguration.class); } - } catch (SQLException e) { - LOGGER.debug("sql exception while reading mail config"); + } catch (Exception e) { + LOGGER.debug("Cannot get mail templates from db", e); } /* Case 1: Nothing in DB */ @@ -153,36 +156,28 @@ public class DbConfiguration { LOGGER.debug("No template config in DB -> save the default config to DB"); templateConf = MailTemplateConfiguration.defaultTemplateConfiguration; try { - store(KEY_TEMPLATES, Json.serialize(templateConf).getBytes()); + store(KEY_TEMPLATES, Json.serialize(templateConf).getBytes(StandardCharsets.UTF_8)); } catch (SQLException e) { - LOGGER.debug("erroring while storing"); - e.printStackTrace(); } - return templateConf.getByName(name); - } /* Case 2: DB has config but not the template */ - if (templateConf != null && templateConf.getByName(name) == null) { + if (templateConf != null && templateConf.getByName(name) == null + && MailTemplateConfiguration.defaultTemplateConfiguration.getByName(name) != null) { /* merge default config with templateConf */ LOGGER.debug("DB template config does not contain a template for " + name); - MailTemplateConfiguration newConf = templateConf - .merge(MailTemplateConfiguration.defaultTemplateConfiguration); + MailTemplateConfiguration newConf = templateConf.merge(MailTemplateConfiguration.defaultTemplateConfiguration); try { - store(KEY_TEMPLATES, Json.serialize(newConf).getBytes()); + store(KEY_TEMPLATES, Json.serialize(newConf).getBytes(StandardCharsets.UTF_8)); templateConf = newConf; } catch (SQLException e) { - LOGGER.debug("sql exception while storing merged config"); - e.printStackTrace(); } - return newConf.getByName(name); } /* Case 3: DB has config and has the template */ if (templateConf != null && templateConf.getByName(name) != null) { - LOGGER.debug("DB template found in DB"); return templateConf.getByName(name); } - /* CASE 4: No DB and template not in default */ + /* CASE 4: Neither in DB nor in default */ LOGGER.debug("Template with name \"" + name + "\" could not be found"); return null; diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java index 7ffba241..191d0359 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java @@ -753,7 +753,7 @@ public class DbImage { } // Now update the latestversionid of the baseimage if applicable if (setLatestVersion(connection, imageBaseId, latestVersion)) { - MailGenerator.versionDeleted(imageBaseId, changingVersion, latestVersion); + MailGenerator.sendImageVersionDeleted(imageBaseId, changingVersion, latestVersion); } } diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/MailGenerator.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/MailGenerator.java index 0c32339e..5fd313e3 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/MailGenerator.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/MailGenerator.java @@ -10,7 +10,6 @@ import java.util.Map; import java.util.Map.Entry; import org.apache.log4j.Logger; -import org.omg.CosNaming.NamingContextPackage.NotFound; import org.openslx.bwlp.sat.database.mappers.DbConfiguration; import org.openslx.bwlp.sat.database.mappers.DbImage; import org.openslx.bwlp.sat.database.mappers.DbImagePermissions; @@ -20,6 +19,7 @@ import org.openslx.bwlp.sat.database.mappers.DbUser; import org.openslx.bwlp.sat.database.mappers.DbUser.User; import org.openslx.bwlp.sat.database.models.LocalImageVersion; import org.openslx.bwlp.sat.mail.MailQueue.MailConfig; +import org.openslx.bwlp.sat.mail.MailTemplatePlain.Template; import org.openslx.bwlp.sat.util.Formatter; import org.openslx.bwlp.sat.util.Util; import org.openslx.bwlp.thrift.iface.ImageDetailsRead; @@ -51,7 +51,7 @@ public class MailGenerator { for (LectureSummary lecture : lectures) { List<UserInfo> relevantUsers = getUserToMail(lecture); - MailTemplate template = DbConfiguration.getMailTemplate("updatedLecture"); + MailTemplate template = DbConfiguration.getMailTemplate(Template.LECTURE_UPDATED); Map<String, String> templateArgs = new HashMap<>(); templateArgs.put("lecture", lecture.lectureName); @@ -83,7 +83,7 @@ public class MailGenerator { for (LectureSummary lecture : lectures) { List<UserInfo> relevantUsers = getUserToMail(lecture); - MailTemplate template = DbConfiguration.getMailTemplate("lectureForcedUpdate"); + MailTemplate template = DbConfiguration.getMailTemplate(Template.LECTURE_FORCED_UPDATE); Map<String, String> templateArgs = new HashMap<>(); templateArgs.put("lecture", lecture.lectureName); templateArgs.put("date", Formatter.date(newVersion.createTime)); @@ -115,7 +115,7 @@ public class MailGenerator { } } - public static void versionDeleted(String imageBaseId, LocalImageVersion oldLocal, LocalImageVersion newLocal) { + public static void sendImageVersionDeleted(String imageBaseId, LocalImageVersion oldLocal, LocalImageVersion newLocal) { if (!hasMailConfig()) return; ImageDetailsRead image; @@ -144,9 +144,9 @@ public class MailGenerator { } if (newVersion == null) { - template = DbConfiguration.getMailTemplate("deleteOnlyVersion"); + template = DbConfiguration.getMailTemplate(Template.VM_DELETED_LAST_VERSION); } else { - template = DbConfiguration.getMailTemplate("deleteVersion"); + template = DbConfiguration.getMailTemplate(Template.VM_DELETED_OLD_VERSION); String uploaderName; try { User uploader = DbUser.getCached(newVersion.uploaderId); @@ -168,7 +168,7 @@ public class MailGenerator { } } - public static void sendDeletionReminder(LocalImageVersion version, int days, boolean mailForced) { + public static void sendImageDeletionReminder(LocalImageVersion version, int days, boolean mailForced) { if (!hasMailConfig()) return; ImageDetailsRead image; @@ -188,9 +188,9 @@ public class MailGenerator { templateArgs.put("created", Formatter.date(version.createTime)); if (isCurrentlyLatest) { - template = DbConfiguration.getMailTemplate("deletionReminderOfCurrentlyLatest"); + template = DbConfiguration.getMailTemplate(Template.VM_CURRENT_VERSION_EXPIRING); } else if (mailForced) { - template = DbConfiguration.getMailTemplate("deletionRminderOfOldVersion"); + template = DbConfiguration.getMailTemplate(Template.VM_OLD_VERSION_EXPIRING); } else { return; } @@ -216,7 +216,7 @@ public class MailGenerator { if (lecture.endTime < version.expireTime) { continue; } - template = DbConfiguration.getMailTemplate("deletionReminderUsers"); + template = DbConfiguration.getMailTemplate(Template.LECTURE_LINKED_VM_EXPIRING); message = wordWrap(template.format(templateArgs)); relevantUsers = getUserToMail(lecture); for (UserInfo user : relevantUsers) { @@ -225,11 +225,11 @@ public class MailGenerator { } } - public static void sendEndDateRemainder(LectureSummary lecture, int days) { + public static void sendLectureExpiringReminder(LectureSummary lecture, int days) { if (!hasMailConfig()) return; List<UserInfo> relevantUsers = getUserToMail(lecture); - MailTemplate template = DbConfiguration.getMailTemplate("endDateRemainder"); + MailTemplate template = DbConfiguration.getMailTemplate(Template.LECTURE_EXPIRING); Map<String, String> templateArgs = new HashMap<>(); templateArgs.put("lecture", lecture.lectureName); templateArgs.put("remaining_days", String.valueOf(days)); 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 4836ea39..d4363d4d 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 @@ -1,8 +1,9 @@ package org.openslx.bwlp.sat.mail; -import java.util.ArrayList; import java.util.HashMap; +import org.openslx.bwlp.sat.mail.MailTemplatePlain.Template; + /** * used for serialization to the database * @@ -14,14 +15,14 @@ public class MailTemplateConfiguration { private static final MailTemplatePlain[] defaultTemplates = { new MailTemplatePlain( - "updatedLecture", + Template.LECTURE_UPDATED, "Wird nach der Aktualisierung einer VM versendet.", "Zur Veranstaltung %lecture% gehörige VM wurde aktualisiert.", new String[]{}, new String[]{"lecture"}), new MailTemplatePlain( - "lectureDeactivated", + Template.LECTURE_DEACTIVATED, "Wird versendet wenn eine Veranstaltung deaktiviert werden musste", "Die Veranstaltung '%lecture%' musste deaktiviert werden," + " da die verknüpfte VM gelöscht oder beschädigt wurde. Bitte überprüfen" @@ -32,7 +33,7 @@ public class MailTemplateConfiguration { ), new MailTemplatePlain( - "deletionReminderOfCurrentlyLatest", + Template.VM_CURRENT_VERSION_EXPIRING, "Die Update-Erinnerung", "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.", @@ -41,7 +42,7 @@ public class MailTemplateConfiguration { ), new MailTemplatePlain( - "deletionReminderOfOldVersion", + Template.VM_OLD_VERSION_EXPIRING, "Benachrichtigung 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.", @@ -50,7 +51,7 @@ public class MailTemplateConfiguration { ), new MailTemplatePlain( - "deletionReminderUsers", + Template.LECTURE_LINKED_VM_EXPIRING, "Die Update-Erinnerung", "Hinweis zur Veranstaltung '%lecture%': Die verwendete VM '%image'" + " läuft in %remaining_days% Tag(en) ab. Bitte aktualisieren oder wechseln Sie die VM.", @@ -60,16 +61,16 @@ public class MailTemplateConfiguration { new MailTemplatePlain( - "endDateRemainder", + Template.LECTURE_EXPIRING, "Erinnerung daran, dass das Enddatum einer Veranstaltung bald erreicht ist", - "Die Veranstaltung '%lecture%' läuft in %remaining_days% Tag(en) ab.", + "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( - "deleteOnlyVersion", + Template.VM_DELETED_LAST_VERSION, "Bestätigung dass VM gelöscht wird", "Die letzte verbliebene Version der VM '%image%' wurde gelöscht; VM zur Löschung vorgemerkt.", new String[]{}, @@ -77,7 +78,7 @@ public class MailTemplateConfiguration { ), new MailTemplatePlain( - "deleteVersion", + 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%)", @@ -85,7 +86,7 @@ public class MailTemplateConfiguration { new String[]{"image"} ), new MailTemplatePlain( - "lectureForcedUpdate", + Template.LECTURE_FORCED_UPDATE, "Benachrichtigung nach erzwungenem Update", "Die verlinkte VM zur Veranstaltung '%lecture%' wurde gelöscht oder ist beschädigt," + "daher verweist sie jetzt auf die VM-Version vom %date%. " @@ -94,7 +95,7 @@ public class MailTemplateConfiguration { new String[]{"lecture"} ), new MailTemplatePlain( - "testMail", + Template.TEST_MAIL, "Die Test-Email, die bei der Dozmod-Konfiguration verschickt wird", "Test der Mailkonfiguration.\n\n%host%:%port% \nSSL: %ssl%" + "\nLogin: %username%", @@ -115,9 +116,9 @@ public class MailTemplateConfiguration { * @param name the name of the desired mail template * @return the mail template or NULL if no such template exists */ - public MailTemplate getByName(String name) { + public MailTemplate getByName(Template name) { for(int i = 0; i < templates.length; i++) { - if (templates[i].getName().equals(name)) { + if (templates[i].getName() == name) { return templates[i].toMailTemplate(); } } @@ -135,7 +136,7 @@ public class MailTemplateConfiguration { * If a template with the same name exists in both then "this" has priority */ public MailTemplateConfiguration merge(MailTemplateConfiguration conf) { - HashMap<String, MailTemplatePlain> templates = new HashMap<>(); + HashMap<Template, MailTemplatePlain> templates = new HashMap<>(); /* add all templates from conf */ for (MailTemplatePlain t : conf.templates) { templates.put(t.getName(), t); 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 470bd36d..3c26fcc5 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 @@ -4,7 +4,20 @@ import com.google.gson.annotations.SerializedName; public class MailTemplatePlain { - private String name; + public enum Template { + LECTURE_UPDATED, + LECTURE_DEACTIVATED, + VM_CURRENT_VERSION_EXPIRING, + VM_OLD_VERSION_EXPIRING, + LECTURE_LINKED_VM_EXPIRING, + LECTURE_EXPIRING, + VM_DELETED_LAST_VERSION, + VM_DELETED_OLD_VERSION, + LECTURE_FORCED_UPDATE, + TEST_MAIL + } + + private Template name; private String description; private String template; @@ -18,7 +31,7 @@ public class MailTemplatePlain { - public MailTemplatePlain(String name, String description, String template, String[] optionalVariables, + public MailTemplatePlain(Template name, String description, String template, String[] optionalVariables, String[] mandatoryVariables) { this.name = name; this.description = description; @@ -27,7 +40,7 @@ public class MailTemplatePlain { this.mandatoryVariables = mandatoryVariables; } - public String getName() { + public Template getName() { return this.name; } public MailTemplate toMailTemplate() { diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/SendExpireWarning.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/SendExpireWarning.java index badf97fe..adbf1c12 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/SendExpireWarning.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/SendExpireWarning.java @@ -74,7 +74,7 @@ public class SendExpireWarning implements Runnable { final int days = (int) ((lecture.endTime - now) / 86400); LOGGER.debug(lecture.lectureName + " expires in " + days); if ((lecture.isEnabled && (days == 14 || days == 1)) || (days == 7)) { - MailGenerator.sendEndDateRemainder(lecture, days); + MailGenerator.sendLectureExpiringReminder(lecture, days); } } } @@ -101,7 +101,7 @@ public class SendExpireWarning implements Runnable { || (!version.isValid && days == 3); boolean mailForced = version.isValid && days == 1; if (mailNormal || mailForced) { - MailGenerator.sendDeletionReminder(version, days, mailForced); + MailGenerator.sendImageDeletionReminder(version, days, mailForced); } } } |