Store locality of a Sone in the Sone itself, remove related methods from Database.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index a574a59..d552f7f 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) {
@@ -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
@@ -810,6 +763,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
@@ -1014,7 +991,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 +1211,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 +1258,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;
                }
@@ -1558,7 +1535,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 +1569,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 +1675,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 +1709,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 +1784,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 +1811,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 +1830,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 +1953,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;
                }