Turn Sone into an interface, add in-memory implementation of Sone.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 4df6584..922f0d1 100644 (file)
@@ -279,7 +279,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         * @return The Sone rescuer for the given Sone
         */
        public SoneRescuer getSoneRescuer(Sone sone) {
-               Validation.begin().isNotNull("Sone", sone).check().is("Local Sone", isLocalSone(sone)).check();
+               Validation.begin().isNotNull("Sone", sone).check().is("Local Sone", sone.isLocal()).check();
                synchronized (soneRescuers) {
                        SoneRescuer soneRescuer = soneRescuers.get(sone);
                        if (soneRescuer == null) {
@@ -323,7 +323,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         *         Sone
         */
        public Sone getSone(String id) {
-               return database.getSone(id, true);
+               return getSone(id, true);
        }
 
        /**
@@ -353,30 +353,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         *         otherwise
         */
        public boolean hasSone(String id) {
-               return database.isLocalSone(id) || database.isRemoteSone(id);
-       }
-
-       /**
-        * Returns whether the given Sone is a local Sone.
-        *
-        * @param sone
-        *            The Sone to check for its locality
-        * @return {@code true} if the given Sone is local, {@code false} otherwise
-        */
-       public boolean isLocalSone(Sone sone) {
-               return database.isLocalSone(sone);
-       }
-
-       /**
-        * Returns whether the given ID is the ID of a local Sone.
-        *
-        * @param id
-        *            The Sone ID to check for its locality
-        * @return {@code true} if the given ID is a local Sone, {@code false}
-        *         otherwise
-        */
-       public boolean isLocalSone(String id) {
-               return database.isLocalSone(id);
+               return database.getSone(id, false) != null;
        }
 
        /**
@@ -437,30 +414,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        }
 
        /**
-        * Returns whether the given Sone is a remote Sone.
-        *
-        * @param sone
-        *            The Sone to check
-        * @return {@code true} if the given Sone is a remote Sone, {@code false}
-        *         otherwise
-        */
-       public boolean isRemoteSone(Sone sone) {
-               return database.isRemoteSone(sone);
-       }
-
-       /**
-        * Returns whether the Sone with the given ID is a remote Sone.
-        *
-        * @param id
-        *            The ID of the Sone to check
-        * @return {@code true} if the Sone with the given ID is a remote Sone,
-        *         {@code false} otherwise
-        */
-       public boolean isRemoteSone(String id) {
-               return database.isRemoteSone(id);
-       }
-
-       /**
         * Returns whether the given Sone has been modified.
         *
         * @param sone
@@ -504,17 +457,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        }
 
        /**
-        * Returns the post with the given ID.
-        *
-        * @param postId
-        *            The ID of the post to get
-        * @return The post with the given ID, or a new post with the given ID
-        */
-       public Post getPost(String postId) {
-               return getPost(postId, true);
-       }
-
-       /**
         * Returns the post with the given ID, optionally creating a new post.
         *
         * @param postId
@@ -810,6 +752,30 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        }
 
        /**
+<<<<<<< HEAD
+=======
+        * Adds a local Sone from the given ID which has to be the ID of an own
+        * identity.
+        *
+        * @param id
+        *            The ID of an own identity to add a Sone for
+        * @return The added (or already existing) Sone
+        */
+       public Sone addLocalSone(String id) {
+               if (database.getLocalSone(id, false) != null) {
+                       logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
+                       return database.getLocalSone(id, false);
+               }
+               OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
+               if (ownIdentity == null) {
+                       logger.log(Level.INFO, "Invalid Sone ID: %s", id);
+                       return null;
+               }
+               return addLocalSone(ownIdentity);
+       }
+
+       /**
+>>>>>>> 4f36598... Store locality of a Sone in the Sone itself, remove related methods from Database.
         * Adds a local Sone from the given own identity.
         *
         * @param ownIdentity
@@ -831,7 +797,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
                sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
                sone.setKnown(true);
-               database.saveSone(sone);
                /* TODO - load posts ’n stuff */
                final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
                soneInserter.addSoneInsertListener(this);
@@ -839,6 +804,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                sone.setStatus(SoneStatus.idle);
                loadSone(sone);
                soneInserter.start();
+               database.saveSone(sone);
                return sone;
        }
 
@@ -899,7 +865,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                                }
                        }
                }
-               database.saveSone(sone);
                soneDownloader.addSone(sone);
                soneDownloaders.execute(new Runnable() {
 
@@ -910,6 +875,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                        }
 
                });
+               database.saveSone(sone);
                return sone;
        }
 
@@ -1014,7 +980,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         * @return The trust relationship
         */
        public Trust getTrust(Sone origin, Sone target) {
-               if (!isLocalSone(origin)) {
+               if (!origin.isLocal()) {
                        logger.log(Level.WARNING, String.format("Tried to get trust from remote Sone: %s", origin));
                        return null;
                }
@@ -1234,7 +1200,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                        logger.log(Level.WARNING, String.format("Tried to delete Sone of non-own identity: %s", sone));
                        return;
                }
-               if (!database.isLocalSone(sone)) {
+               if (!sone.isLocal()) {
                        logger.log(Level.WARNING, String.format("Tried to delete non-local Sone: %s", sone));
                        return;
                }
@@ -1281,7 +1247,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         *            The Sone to load and update
         */
        public void loadSone(Sone sone) {
-               if (!isLocalSone(sone)) {
+               if (!sone.isLocal()) {
                        logger.log(Level.FINE, String.format("Tried to load non-local Sone: %s", sone));
                        return;
                }
@@ -1338,7 +1304,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                                logger.log(Level.WARNING, "Invalid post found, aborting load!");
                                return;
                        }
-                       Post post = getPost(postId).setSone(sone).setTime(postTime).setText(postText);
+                       Post post = getPost(postId, true).setSone(sone).setTime(postTime).setText(postText);
                        if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
                                post.setRecipient(getSone(postRecipientId));
                        }
@@ -1360,7 +1326,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                                logger.log(Level.WARNING, "Invalid reply found, aborting load!");
                                return;
                        }
-                       replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
+                       replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId, true)).setTime(replyTime).setText(replyText));
                }
 
                /* load post likes. */
@@ -1504,50 +1470,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         *
         * @param sone
         *            The Sone that creates the post
-        * @param text
-        *            The text of the post
-        * @return The created post
-        */
-       public Post createPost(Sone sone, String text) {
-               return createPost(sone, System.currentTimeMillis(), text);
-       }
-
-       /**
-        * Creates a new post.
-        *
-        * @param sone
-        *            The Sone that creates the post
-        * @param time
-        *            The time of the post
-        * @param text
-        *            The text of the post
-        * @return The created post
-        */
-       public Post createPost(Sone sone, long time, String text) {
-               return createPost(sone, null, time, text);
-       }
-
-       /**
-        * Creates a new post.
-        *
-        * @param sone
-        *            The Sone that creates the post
-        * @param recipient
-        *            The recipient Sone, or {@code null} if this post does not have
-        *            a recipient
-        * @param text
-        *            The text of the post
-        * @return The created post
-        */
-       public Post createPost(Sone sone, Sone recipient, String text) {
-               return createPost(sone, recipient, System.currentTimeMillis(), text);
-       }
-
-       /**
-        * Creates a new post.
-        *
-        * @param sone
-        *            The Sone that creates the post
         * @param recipient
         *            The recipient Sone, or {@code null} if this post does not have
         *            a recipient
@@ -1558,7 +1480,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         * @return The created post
         */
        public Post createPost(Sone sone, Sone recipient, long time, String text) {
-               if (!isLocalSone(sone)) {
+               if (!sone.isLocal()) {
                        logger.log(Level.FINE, String.format("Tried to create post for non-local Sone: %s", sone));
                        return null;
                }
@@ -1592,7 +1514,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         *            The post to delete
         */
        public void deletePost(Post post) {
-               if (!isLocalSone(post.getSone())) {
+               if (!post.getSone().isLocal()) {
                        logger.log(Level.WARNING, String.format("Tried to delete post of non-local Sone: %s", post.getSone()));
                        return;
                }
@@ -1698,7 +1620,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         * @return The created reply
         */
        public PostReply createReply(Sone sone, Post post, long time, String text) {
-               if (!isLocalSone(sone)) {
+               if (!sone.isLocal()) {
                        logger.log(Level.FINE, String.format("Tried to create reply for non-local Sone: %s", sone));
                        return null;
                }
@@ -1732,7 +1654,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         */
        public void deleteReply(PostReply reply) {
                Sone sone = reply.getSone();
-               if (!isLocalSone(sone)) {
+               if (!sone.isLocal()) {
                        logger.log(Level.FINE, String.format("Tried to delete non-local reply: %s", reply));
                        return;
                }
@@ -1807,7 +1729,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         *            The album to remove
         */
        public void deleteAlbum(Album album) {
-               Validation.begin().isNotNull("Album", album).check().is("Local Sone", isLocalSone(album.getSone())).check();
+               Validation.begin().isNotNull("Album", album).check().is("Local Sone", album.getSone().isLocal()).check();
                if (!album.isEmpty()) {
                        return;
                }
@@ -1834,7 +1756,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         * @return The newly created image
         */
        public Image createImage(Sone sone, Album album, TemporaryImage temporaryImage) {
-               Validation.begin().isNotNull("Sone", sone).isNotNull("Album", album).isNotNull("Temporary Image", temporaryImage).check().is("Local Sone", isLocalSone(sone)).check().isEqual("Owner and Album Owner", sone, album.getSone()).check();
+               Validation.begin().isNotNull("Sone", sone).isNotNull("Album", album).isNotNull("Temporary Image", temporaryImage).check().is("Local Sone", sone.isLocal()).check().isEqual("Owner and Album Owner", sone, album.getSone()).check();
                Image image = new Image(temporaryImage.getId()).setSone(sone).setCreationTime(System.currentTimeMillis());
                album.addImage(image);
                synchronized (images) {
@@ -1853,7 +1775,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         *            The image to delete
         */
        public void deleteImage(Image image) {
-               Validation.begin().isNotNull("Image", image).check().is("Local Sone", isLocalSone(image.getSone())).check();
+               Validation.begin().isNotNull("Image", image).check().is("Local Sone", image.getSone().isLocal()).check();
                deleteTemporaryImage(image.getId());
                image.getAlbum().removeImage(image);
                synchronized (images) {
@@ -1976,7 +1898,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         *            The Sone to save
         */
        private synchronized void saveSone(Sone sone) {
-               if (!isLocalSone(sone)) {
+               if (!sone.isLocal()) {
                        logger.log(Level.FINE, String.format("Tried to save non-local Sone: %s", sone));
                        return;
                }