Use unique IDs for replies, too
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index d0b31d0..68cf4b2 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;
@@ -98,6 +99,7 @@ import net.pterodactylus.util.thread.NamedThreadFactory;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Function;
 import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
 import com.google.common.collect.FluentIterable;
 import com.google.common.collect.HashMultimap;
 import com.google.common.collect.Multimap;
@@ -154,6 +156,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 +277,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.
         *
@@ -420,8 +436,17 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         * {@inheritDoc}
         */
        @Override
-       public Optional<Post> getPost(String postId) {
-               return database.getPost(postId);
+       public Optional<Post> getPost(final String postId) {
+               Optional<Post> post = database.getPost(postId);
+               if (post.isPresent() || !isCompatibilityMode(CompatibilityMode.oldElementIds)) {
+                       return post;
+               }
+               return FluentIterable.from(getSones()).transformAndConcat(Sone.toAllPosts).filter(new Predicate<Post>() {
+                       @Override
+                       public boolean apply(Post input) {
+                               return (input != null) && input.getInternalId().equals(postId);
+                       }
+               }).first();
        }
 
        /**
@@ -1476,7 +1501,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());
@@ -1487,7 +1512,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        int replyCounter = 0;
                        for (PostReply reply : sone.getReplies()) {
                                String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
-                               configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
+                               configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getInternalId());
                                configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPostId());
                                configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
                                configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());