Add compatibility mode for old post IDs
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index da9bd83..90fc314 100644 (file)
@@ -27,6 +27,7 @@ import static java.util.logging.Logger.getLogger;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.EnumSet;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -154,6 +155,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        /** The trust updater. */
        private final WebOfTrustUpdater webOfTrustUpdater;
 
+       private final Set<CompatibilityMode> compatibilityModes = EnumSet.noneOf(CompatibilityMode.class);
+
        /** The times Sones were followed. */
        private final Map<String, Long> soneFollowingTimes = new HashMap<String, Long>();
 
@@ -273,6 +276,18 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                return updateChecker;
        }
 
+       public boolean isCompatibilityMode(CompatibilityMode compatibilityMode) {
+               return compatibilityModes.contains(compatibilityMode);
+       }
+
+       public void setCompatibilityMode(CompatibilityMode compatibilityMode) {
+               compatibilityModes.add(compatibilityMode);
+       }
+
+       public void clearCompatibilityMod(CompatibilityMode compatibilityMode) {
+               compatibilityModes.remove(compatibilityMode);
+       }
+
        /**
         * Returns the Sone rescuer for the given local Sone.
         *
@@ -651,6 +666,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        soneInserters.put(sone, soneInserter);
                }
                loadSone(sone);
+               database.storeSone(sone);
                sone.setStatus(SoneStatus.idle);
                soneInserter.start();
                return sone;
@@ -687,8 +703,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        logger.log(Level.WARNING, "Given Identity is null!");
                        return null;
                }
-               final Long latestEdition = tryParse(fromNullable(
-                               identity.getProperty("Sone.LatestEdition")).or("0"));
+               String property = fromNullable(identity.getProperty("Sone.LatestEdition")).or("0");
+               long latestEdition = fromNullable(tryParse(property)).or(0L);
                Optional<Sone> existingSone = getSone(identity.getId());
                if (existingSone.isPresent() && existingSone.get().isLocal()) {
                        return existingSone.get();
@@ -1085,7 +1101,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        for (Album album : topLevelAlbums) {
                                sone.getRootAlbum().addAlbum(album);
                        }
-                       database.storeSone(sone);
                        synchronized (soneInserters) {
                                soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
                        }
@@ -1476,7 +1491,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        int postCounter = 0;
                        for (Post post : sone.getPosts()) {
                                String postPrefix = sonePrefix + "/Posts/" + postCounter++;
-                               configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
+                               configuration.getStringValue(postPrefix + "/ID").setValue(post.getInternalId());
                                configuration.getStringValue(postPrefix + "/Recipient").setValue(post.getRecipientId().orNull());
                                configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
                                configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
@@ -1514,7 +1529,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        int albumCounter = 0;
                        for (Album album : albums) {
                                String albumPrefix = sonePrefix + "/Albums/" + albumCounter++;
-                               configuration.getStringValue(albumPrefix + "/ID").setValue(album.getId());
+                               configuration.getStringValue(albumPrefix + "/ID").setValue(album.getInternalId());
                                configuration.getStringValue(albumPrefix + "/Title").setValue(album.getTitle());
                                configuration.getStringValue(albumPrefix + "/Description").setValue(album.getDescription());
                                configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent().equals(sone.getRootAlbum()) ? null : album.getParent().getId());
@@ -1530,8 +1545,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                                continue;
                                        }
                                        String imagePrefix = sonePrefix + "/Images/" + imageCounter++;
-                                       configuration.getStringValue(imagePrefix + "/ID").setValue(image.getId());
-                                       configuration.getStringValue(imagePrefix + "/Album").setValue(album.getId());
+                                       configuration.getStringValue(imagePrefix + "/ID").setValue(image.getInternalId());
+                                       configuration.getStringValue(imagePrefix + "/Album").setValue(album.getInternalId());
                                        configuration.getStringValue(imagePrefix + "/Key").setValue(image.getKey());
                                        configuration.getStringValue(imagePrefix + "/Title").setValue(image.getTitle());
                                        configuration.getStringValue(imagePrefix + "/Description").setValue(image.getDescription());