Remove updated time setter from Sone, store update time in database.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneInserter.java
index f544ef2..8c24fc0 100644 (file)
@@ -19,6 +19,7 @@ package net.pterodactylus.sone.core;
 
 import static java.lang.String.format;
 import static java.lang.System.currentTimeMillis;
+import static java.util.logging.Logger.getLogger;
 import static net.pterodactylus.sone.data.Album.NOT_EMPTY;
 
 import java.io.InputStream;
@@ -38,14 +39,15 @@ import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
 import net.pterodactylus.sone.core.event.SoneInsertedEvent;
 import net.pterodactylus.sone.core.event.SoneInsertingEvent;
 import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.LocalSone;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.Sone.SoneStatus;
+import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.sone.freenet.StringBucket;
 import net.pterodactylus.sone.main.SonePlugin;
 import net.pterodactylus.util.io.Closer;
-import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.service.AbstractService;
 import net.pterodactylus.util.template.HtmlFilter;
 import net.pterodactylus.util.template.ReflectionAccessor;
@@ -74,7 +76,7 @@ import freenet.keys.FreenetURI;
 public class SoneInserter extends AbstractService {
 
        /** The logger. */
-       private static final Logger logger = Logging.getLogger(SoneInserter.class);
+       private static final Logger logger = getLogger("Sone.Inserter");
 
        /** The insertion delay (in seconds). */
        private static final AtomicInteger insertionDelay = new AtomicInteger(60);
@@ -99,7 +101,7 @@ public class SoneInserter extends AbstractService {
 
        /** The Freenet interface. */
        private final FreenetInterface freenetInterface;
-
+       private final Database database;
        private final SoneModificationDetector soneModificationDetector;
        private final long delay;
        private final String soneId;
@@ -116,11 +118,11 @@ public class SoneInserter extends AbstractService {
         * @param soneId
         *            The ID of the Sone to insert
         */
-       public SoneInserter(final Core core, EventBus eventBus, FreenetInterface freenetInterface, final String soneId) {
-               this(core, eventBus, freenetInterface, soneId, new SoneModificationDetector(new LockableFingerprintProvider() {
+       public SoneInserter(final Core core, EventBus eventBus, FreenetInterface freenetInterface, Database database, final String soneId) {
+               this(core, eventBus, freenetInterface, database, soneId, new SoneModificationDetector(new LockableFingerprintProvider() {
                        @Override
                        public boolean isLocked() {
-                               final Optional<Sone> sone = core.getSone(soneId);
+                               final Optional<LocalSone> sone = core.getLocalSone(soneId);
                                if (!sone.isPresent()) {
                                        return false;
                                }
@@ -139,11 +141,12 @@ public class SoneInserter extends AbstractService {
        }
 
        @VisibleForTesting
-       SoneInserter(Core core, EventBus eventBus, FreenetInterface freenetInterface, String soneId, SoneModificationDetector soneModificationDetector, long delay) {
+       SoneInserter(Core core, EventBus eventBus, FreenetInterface freenetInterface, Database database, String soneId, SoneModificationDetector soneModificationDetector, long delay) {
                super("Sone Inserter for “" + soneId + "”", false);
                this.core = core;
                this.eventBus = eventBus;
                this.freenetInterface = freenetInterface;
+               this.database = database;
                this.soneId = soneId;
                this.soneModificationDetector = soneModificationDetector;
                this.delay = delay;
@@ -214,12 +217,12 @@ public class SoneInserter extends AbstractService {
                                sleep(delay);
 
                                if (soneModificationDetector.isEligibleForInsert()) {
-                                       Optional<Sone> soneOptional = core.getSone(soneId);
+                                       Optional<LocalSone> soneOptional = core.getLocalSone(soneId);
                                        if (!soneOptional.isPresent()) {
                                                logger.log(Level.WARNING, format("Sone %s has disappeared, exiting inserter.", soneId));
                                                return;
                                        }
-                                       Sone sone = soneOptional.get();
+                                       LocalSone sone = soneOptional.get();
                                        InsertInformation insertInformation = new InsertInformation(sone);
                                        logger.log(Level.INFO, String.format("Inserting Sone “%s”…", sone.getName()));
 
@@ -229,13 +232,13 @@ public class SoneInserter extends AbstractService {
                                                long insertTime = currentTimeMillis();
                                                eventBus.post(new SoneInsertingEvent(sone));
                                                FreenetURI finalUri = freenetInterface.insertDirectory(sone.getInsertUri(), insertInformation.generateManifestEntries(), "index.html");
-                                               eventBus.post(new SoneInsertedEvent(sone, currentTimeMillis() - insertTime));
+                                               eventBus.post(new SoneInsertedEvent(sone, currentTimeMillis() - insertTime, insertInformation.getFingerprint()));
                                                /* at this point we might already be stopped. */
                                                if (shouldStop()) {
                                                        /* if so, bail out, don’t change anything. */
                                                        break;
                                                }
-                                               sone.setTime(insertTime);
+                                               database.updateSoneTime(sone, insertTime);
                                                sone.setLatestEdition(finalUri.getEdition());
                                                core.touchConfiguration();
                                                success = true;