Add Sone insert events to core listener.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 1 Jul 2011 07:03:44 +0000 (09:03 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 1 Jul 2011 07:04:36 +0000 (09:04 +0200)
src/main/java/net/pterodactylus/sone/core/CoreListener.java
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/resources/i18n/sone.en.properties
src/main/resources/templates/notify/soneInsertNotification.html [new file with mode: 0644]

index d34ac36..0e497e8 100644 (file)
@@ -137,6 +137,38 @@ public interface CoreListener extends EventListener {
        public void soneUnlocked(Sone sone);
 
        /**
+        * Notifies a listener that the insert of the given Sone has started.
+        *
+        * @see SoneInsertListener#insertStarted(Sone)
+        * @param sone
+        *            The Sone that is being inserted
+        */
+       public void soneInserting(Sone sone);
+
+       /**
+        * Notifies a listener that the insert of the given Sone has finished
+        * successfully.
+        *
+        * @see SoneInsertListener#insertFinished(Sone, long)
+        * @param sone
+        *            The Sone that has been inserted
+        * @param insertDuration
+        *            The insert duration (in milliseconds)
+        */
+       public void soneInserted(Sone sone, long insertDuration);
+
+       /**
+        * Notifies a listener that the insert of the given Sone was aborted.
+        *
+        * @see SoneInsertListener#insertAborted(Sone, Throwable)
+        * @param sone
+        *            The Sone that was inserted
+        * @param cause
+        *            The cause for the abortion (may be {@code null})
+        */
+       public void soneInsertAborted(Sone sone, Throwable cause);
+
+       /**
         * Notifies a listener that a new version has been found.
         *
         * @param version
index 6807b35..06da982 100644 (file)
@@ -184,6 +184,9 @@ public class WebInterface implements CoreListener {
        /** The “Sone rescued” notification. */
        private final ListNotification<Sone> sonesRescuedNotification;
 
+       /** Notifications for sone inserts. */
+       private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<Sone, TemplateNotification>();
+
        /** Sone locked notification ticker objects. */
        private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
 
@@ -707,6 +710,27 @@ public class WebInterface implements CoreListener {
                return Filters.filteredSet(mentionedSones, Sone.LOCAL_SONE_FILTER);
        }
 
+       /**
+        * Returns the Sone insert notification for the given Sone. If no
+        * notification for the given Sone exists, a new notification is created and
+        * cached.
+        *
+        * @param sone
+        *            The Sone to get the insert notification for
+        * @return The Sone insert notification
+        */
+       private TemplateNotification getSoneInsertNotification(Sone sone) {
+               synchronized (soneInsertNotifications) {
+                       TemplateNotification templateNotification = soneInsertNotifications.get(sone);
+                       if (templateNotification == null) {
+                               templateNotification = new TemplateNotification(TemplateParser.parse(createReader("/templates/notify/soneInsertNotification.html")));
+                               templateNotification.set("sone", sone);
+                               soneInsertNotifications.put(sone, templateNotification);
+                       }
+                       return templateNotification;
+               }
+       }
+
        //
        // CORELISTENER METHODS
        //
@@ -870,6 +894,35 @@ public class WebInterface implements CoreListener {
         * {@inheritDoc}
         */
        @Override
+       public void soneInserting(Sone sone) {
+               TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
+               soneInsertNotification.set("soneStatus", "inserting");
+               notificationManager.addNotification(soneInsertNotification);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public void soneInserted(Sone sone, long insertDuration) {
+               TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
+               soneInsertNotification.set("soneStatus", "inserted");
+               soneInsertNotification.set("insertDuration", insertDuration / 1000);
+               notificationManager.addNotification(soneInsertNotification);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public void soneInsertAborted(Sone sone, Throwable cause) {
+               notificationManager.addNotification(getSoneInsertNotification(sone).set("soneStatus", "insert-aborted").set("insert-error", cause));
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
        public void updateFound(Version version, long releaseTime, long latestEdition) {
                newVersionNotification.getTemplateContext().set("latestVersion", version);
                newVersionNotification.getTemplateContext().set("latestEdition", latestEdition);
index 2277936..43bff42 100644 (file)
@@ -332,3 +332,4 @@ Notification.LockedSones.Text=The following Sones have been locked for more than
 Notification.NewVersion.Text=Version {version} of the Sone plugin was found. Download it from USK@nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI,DuQSUZiI~agF8c-6tjsFFGuZ8eICrzWCILB60nT8KKo,AQACAAE/sone/{edition}​!
 Notification.Mention.ShortText=You have been mentioned.
 Notification.Mention.Text=You have been mentioned in the following posts:
+Notification.SoneInsert.Duration={0,number} {0,choice,0#seconds|1#second|1<seconds}
diff --git a/src/main/resources/templates/notify/soneInsertNotification.html b/src/main/resources/templates/notify/soneInsertNotification.html
new file mode 100644 (file)
index 0000000..8d3260d
--- /dev/null
@@ -0,0 +1,7 @@
+<%if soneStatus|match value="inserting">
+       Your Sone <a href="viewSone.html?sone=<%sone.id|html>"><%sone.niceName|html></a> is now being inserted.
+<%elseif soneStatus|match value="inserted">
+       Your Sone <a href="viewSone.html?sone=<%sone.id|html>"><%sone.niceName|html></a> has been inserted in <%= Notification.SoneInsert.Duration|l10n 0=insertDuration>.
+<%elseif soneStatus|match value="insert-aborted">
+       Inserting your Sone <a href="viewSone.html?sone=<%sone.id|html>"><%sone.niceName|html></a> has failed.
+<%/if>
\ No newline at end of file