Return to storing the ID instead of getting it from the identity.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index e39644d..6cd6135 100644 (file)
@@ -32,6 +32,7 @@ import net.pterodactylus.sone.core.Options.DefaultOption;
 import net.pterodactylus.sone.core.Options.Option;
 import net.pterodactylus.sone.core.Options.OptionWatcher;
 import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.freenet.wot.Identity;
@@ -199,12 +200,10 @@ public class Core implements IdentityListener {
         *         Sone
         */
        public Sone getSone(String id) {
-               Sone sone = getRemoteSone(id);
-               if (sone != null) {
-                       return sone;
+               if (isLocalSone(id)) {
+                       return getLocalSone(id);
                }
-               sone = getLocalSone(id);
-               return sone;
+               return getRemoteSone(id);
        }
 
        /**
@@ -221,6 +220,20 @@ public class Core implements IdentityListener {
        }
 
        /**
+        * 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) {
+               synchronized (localSones) {
+                       return localSones.containsKey(id);
+               }
+       }
+
+       /**
         * Returns all local Sones.
         *
         * @return All local Sones
@@ -236,11 +249,16 @@ public class Core implements IdentityListener {
         *
         * @param id
         *            The ID of the Sone to get
-        * @return The Sone, or {@code null} if there is no Sone with the given ID
+        * @return The Sone with the given ID
         */
        public Sone getLocalSone(String id) {
                synchronized (localSones) {
-                       return localSones.get(id);
+                       Sone sone = localSones.get(id);
+                       if (sone == null) {
+                               sone = new Sone(id);
+                               localSones.put(id, sone);
+                       }
+                       return sone;
                }
        }
 
@@ -260,11 +278,16 @@ public class Core implements IdentityListener {
         *
         * @param id
         *            The ID of the remote Sone to get
-        * @return The Sone, or {@code null} if there is no such Sone
+        * @return The Sone with the given ID
         */
        public Sone getRemoteSone(String id) {
                synchronized (remoteSones) {
-                       return remoteSones.get(id);
+                       Sone sone = remoteSones.get(id);
+                       if (sone == null) {
+                               sone = new Sone(id);
+                               remoteSones.put(id, sone);
+                       }
+                       return sone;
                }
        }
 
@@ -291,7 +314,12 @@ public class Core implements IdentityListener {
         */
        public Post getPost(String postId) {
                synchronized (posts) {
-                       return posts.get(postId);
+                       Post post = posts.get(postId);
+                       if (post == null) {
+                               post = new Post(postId);
+                               posts.put(postId, post);
+                       }
+                       return post;
                }
        }
 
@@ -304,7 +332,12 @@ public class Core implements IdentityListener {
         */
        public Reply getReply(String replyId) {
                synchronized (replies) {
-                       return replies.get(replyId);
+                       Reply reply = replies.get(replyId);
+                       if (reply == null) {
+                               reply = new Reply(replyId);
+                               replies.put(replyId, reply);
+                       }
+                       return reply;
                }
        }
 
@@ -409,7 +442,7 @@ public class Core implements IdentityListener {
                        }
                        final Sone sone;
                        try {
-                               sone = new Sone(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
+                               sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
                                sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
                        } catch (MalformedURLException mue1) {
                                logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
@@ -559,8 +592,62 @@ public class Core implements IdentityListener {
                        logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
                        return;
                }
+
+               logger.log(Level.INFO, "Saving Sone: %s", sone);
                identityManager.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
-               /* TODO - implement saving. */
+               try {
+                       /* save Sone into configuration. */
+                       String sonePrefix = "Sone/" + sone.getId();
+                       configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
+
+                       /* save profile. */
+                       Profile profile = sone.getProfile();
+                       configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
+                       configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
+                       configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
+                       configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
+                       configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
+                       configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
+
+                       /* save posts. */
+                       int postCounter = 0;
+                       for (Post post : sone.getPosts()) {
+                               String postPrefix = sonePrefix + "/Posts/" + postCounter++;
+                               configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
+                               configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
+                               configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
+                       }
+                       configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
+
+                       /* save replies. */
+                       int replyCounter = 0;
+                       for (Reply reply : sone.getReplies()) {
+                               String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
+                               configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
+                               configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
+                               configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
+                               configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
+                       }
+                       configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
+
+                       /* save post likes. */
+                       int postLikeCounter = 0;
+                       for (String postId : sone.getLikedPostIds()) {
+                               configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
+                       }
+                       configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
+
+                       /* save reply likes. */
+                       int replyLikeCounter = 0;
+                       for (String replyId : sone.getLikedReplyIds()) {
+                               configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
+                       }
+                       configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
+
+                       logger.log(Level.INFO, "Sone %s saved.", sone);
+               } catch (ConfigurationException ce1) {
+                       logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
+               }
        }
 
        /**