Merge branch 'notification-for-locked-sones' into next
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 13 Dec 2010 19:28:06 +0000 (20:28 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 13 Dec 2010 19:28:06 +0000 (20:28 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/CoreListener.java
src/main/java/net/pterodactylus/sone/core/CoreListenerManager.java
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/resources/i18n/sone.en.properties
src/main/resources/templates/notify/lockedSonesNotification.html [new file with mode: 0644]

index 9eba2b7..2ef2d57 100644 (file)
@@ -710,7 +710,9 @@ public class Core implements IdentityListener {
         */
        public void lockSone(Sone sone) {
                synchronized (lockedSones) {
-                       lockedSones.add(sone);
+                       if (lockedSones.add(sone)) {
+                               coreListenerManager.fireSoneLocked(sone);
+                       }
                }
        }
 
@@ -723,7 +725,9 @@ public class Core implements IdentityListener {
         */
        public void unlockSone(Sone sone) {
                synchronized (lockedSones) {
-                       lockedSones.remove(sone);
+                       if (lockedSones.remove(sone)) {
+                               coreListenerManager.fireSoneUnlocked(sone);
+                       }
                }
        }
 
index 950e890..5d5e715 100644 (file)
@@ -111,4 +111,20 @@ public interface CoreListener extends EventListener {
         */
        public void replyRemoved(Reply reply);
 
+       /**
+        * Notifies a listener when a Sone was locked.
+        *
+        * @param sone
+        *            The Sone that was locked
+        */
+       public void soneLocked(Sone sone);
+
+       /**
+        * Notifies a listener that a Sone was unlocked.
+        *
+        * @param sone
+        *            The Sone that was unlocked
+        */
+       public void soneUnlocked(Sone sone);
+
 }
index 7ba226b..464342c 100644 (file)
@@ -171,4 +171,30 @@ public class CoreListenerManager extends AbstractListenerManager<Core, CoreListe
                }
        }
 
+       /**
+        * Notifies all listeners that the given Sone was locked.
+        *
+        * @see CoreListener#soneLocked(Sone)
+        * @param sone
+        *            The Sone that was locked
+        */
+       void fireSoneLocked(Sone sone) {
+               for (CoreListener coreListener : getListeners()) {
+                       coreListener.soneLocked(sone);
+               }
+       }
+
+       /**
+        * Notifies all listeners that the given Sone was unlocked.
+        *
+        * @see CoreListener#soneUnlocked(Sone)
+        * @param sone
+        *            The Sone that was unlocked
+        */
+       void fireSoneUnlocked(Sone sone) {
+               for (CoreListener coreListener : getListeners()) {
+                       coreListener.soneUnlocked(sone);
+               }
+       }
+
 }
index d17c2d5..58962c4 100644 (file)
@@ -23,8 +23,11 @@ import java.io.Reader;
 import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 import java.util.logging.Level;
@@ -132,6 +135,12 @@ public class WebInterface implements CoreListener {
        /** The “Sone rescued” notification. */
        private final ListNotification<Sone> sonesRescuedNotification;
 
+       /** Sone locked notification ticker objects. */
+       private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
+
+       /** The “Sone locked” notification. */
+       private final ListNotification<Sone> lockedSonesNotification;
+
        /**
         * Creates a new web interface.
         *
@@ -177,6 +186,9 @@ public class WebInterface implements CoreListener {
 
                Template sonesRescuedTemplate = templateFactory.createTemplate(createReader("/templates/notify/sonesRescuedNotification.html"));
                sonesRescuedNotification = new ListNotification<Sone>("sones-rescued-notification", "sones", sonesRescuedTemplate);
+
+               Template lockedSonesTemplate = templateFactory.createTemplate(createReader("/templates/notify/lockedSonesNotification.html"));
+               lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
        }
 
        //
@@ -647,6 +659,36 @@ public class WebInterface implements CoreListener {
        }
 
        /**
+        * {@inheritDoc}
+        */
+       @Override
+       public void soneLocked(final Sone sone) {
+               Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
+
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       public void run() {
+                               lockedSonesNotification.add(sone);
+                               notificationManager.addNotification(lockedSonesNotification);
+                       }
+               }, "Sone Locked Notification");
+               lockedSonesTickerObjects.put(sone, tickerObject);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public void soneUnlocked(Sone sone) {
+               Object tickerObject = lockedSonesTickerObjects.remove(sone);
+               if (tickerObject == null) {
+                       return;
+               }
+               lockedSonesNotification.remove(sone);
+               Ticker.getInstance().deregisterEvent(tickerObject);
+       }
+
+       /**
         * Template provider implementation that uses
         * {@link WebInterface#createReader(String)} to load templates for
         * inclusion.
index 89af2d1..e080e4d 100644 (file)
@@ -186,4 +186,4 @@ Notification.NewReply.Text=New replies have been discovered by the following Son
 Notification.SoneIsBeingRescued.Text=The following Sones are currently being rescued:
 Notification.SoneRescued.Text=The following Sones have been rescued:
 Notification.SoneRescued.Text.RememberToUnlock=Please remember to control the posts and replies you have given and don’t forget to unlock your Sones!
-
+Notification.LockedSones.Text=The following Sones have been locked for more than 5 minutes. Please check if you really want to keep these Sones locked:
diff --git a/src/main/resources/templates/notify/lockedSonesNotification.html b/src/main/resources/templates/notify/lockedSonesNotification.html
new file mode 100644 (file)
index 0000000..b2b8b95
--- /dev/null
@@ -0,0 +1,6 @@
+<div class="text">
+       <%= Notification.LockedSones.Text|l10n|html>
+       <%foreach sones sone>
+               <a href="viewSone.html?sone=<% sone.id|html>" title="<% sone.requestUri|html>"><% sone.niceName|html></a><%notlast>,<%/notlast><%last>.<%/last>
+       <%/foreach>
+</div>