Save and load known Sones.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index c910799..e944f1f 100644 (file)
@@ -38,6 +38,8 @@ import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.util.config.Configuration;
 import net.pterodactylus.util.config.ConfigurationException;
+import net.pterodactylus.util.filter.Filter;
+import net.pterodactylus.util.filter.Filters;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.service.AbstractService;
 import freenet.client.FetchResult;
@@ -151,7 +153,23 @@ public class Core extends AbstractService {
        }
 
        /**
-        * Creates a new post.
+        * Gets all known Sones that are not local Sones.
+        *
+        * @return All remote Sones
+        */
+       public Collection<Sone> getRemoteSones() {
+               return Filters.filteredCollection(getKnownSones(), new Filter<Sone>() {
+
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       public boolean filterObject(Sone object) {
+                               return !localSones.contains(object);
+                       }
+               });
+       }
+
+       /**
+        * Creates a new post and adds it to the given Sone.
         *
         * @param sone
         *            The sone that creates the post
@@ -164,7 +182,7 @@ public class Core extends AbstractService {
        }
 
        /**
-        * Creates a new post.
+        * Creates a new post and adds it to the given Sone.
         *
         * @param sone
         *            The Sone that creates the post
@@ -226,7 +244,10 @@ public class Core extends AbstractService {
         *            The Sone to watch for updates
         */
        public void addSone(Sone sone) {
-               soneDownloader.addSone(sone);
+               soneCache.put(sone.getId(), sone);
+               if (!localSones.contains(sone)) {
+                       soneDownloader.addSone(sone);
+               }
        }
 
        /**
@@ -302,18 +323,72 @@ public class Core extends AbstractService {
        }
 
        /**
-        * Loads the Sone from the given request URI.
+        * Loads the Sone from the given request URI. The fetching of the data is
+        * performed in a new thread so this method returns immediately.
         *
         * @param requestUri
         *            The request URI to load the Sone from
         */
-       public void loadSone(String requestUri) {
-               try {
-                       FetchResult fetchResult = freenetInterface.fetchUri(new FreenetURI(requestUri).setMetaString(new String[] { "sone.xml" }));
-                       soneDownloader.parseSone(null, fetchResult);
-               } catch (MalformedURLException mue1) {
-                       logger.log(Level.INFO, "Could not create URI from “" + requestUri + "”.", mue1);
-               }
+       public void loadSone(final String requestUri) {
+               loadSone(requestUri, null);
+       }
+
+       /**
+        * Loads the Sone from the given request URI. The fetching of the data is
+        * performed in a new thread so this method returns immediately. If
+        * {@code insertUri} is not {@code null} the loaded Sone is converted into a
+        * local Sone and available using as any other local Sone.
+        *
+        * @param requestUri
+        *            The request URI to load the Sone from
+        * @param insertUri
+        *            The insert URI of the Sone
+        */
+       public void loadSone(final String requestUri, final String insertUri) {
+               new Thread(new Runnable() {
+
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       public void run() {
+                               try {
+                                       FreenetURI realRequestUri = new FreenetURI(requestUri).setMetaString(new String[] { "sone.xml" });
+                                       FetchResult fetchResult = freenetInterface.fetchUri(realRequestUri);
+                                       Sone parsedSone = soneDownloader.parseSone(null, fetchResult, realRequestUri);
+                                       if (parsedSone != null) {
+                                               if (insertUri != null) {
+                                                       parsedSone.setInsertUri(new FreenetURI(insertUri));
+                                                       addLocalSone(parsedSone);
+                                               } else {
+                                                       addSone(parsedSone);
+                                               }
+                                       }
+                               } catch (MalformedURLException mue1) {
+                                       logger.log(Level.INFO, "Could not create URI from “" + requestUri + "”.", mue1);
+                               }
+                       }
+               }, "Sone Downloader").start();
+       }
+
+       /**
+        * Loads and updates the given Sone.
+        *
+        * @param sone
+        *            The Sone to load
+        */
+       public void loadSone(final Sone sone) {
+               new Thread(new Runnable() {
+
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       public void run() {
+                               FreenetURI realRequestUri = sone.getRequestUri().setMetaString(new String[] { "sone.xml" });
+                               FetchResult fetchResult = freenetInterface.fetchUri(realRequestUri);
+                               Sone parsedSone = soneDownloader.parseSone(sone, fetchResult, realRequestUri);
+                               if (parsedSone != null) {
+                                       addSone(parsedSone);
+                               }
+                       }
+               }, "Sone Downloader").start();
        }
 
        /**
@@ -481,10 +556,20 @@ public class Core extends AbstractService {
                                        String friendKey = configuration.getStringValue(friendPrefix + "/Key").getValue(null);
                                        String friendName = configuration.getStringValue(friendPrefix + "/Name").getValue(null);
                                        friendSone.setRequestUri(new FreenetURI(friendKey)).setName(friendName);
-                                       soneDownloader.addSone(friendSone);
                                        sone.addFriend(friendSone);
                                }
 
+                               /* load blocked Sone IDs. */
+                               int blockedSoneCounter = 0;
+                               while (true) {
+                                       String blockedSonePrefix = sonePrefix + "/BlockedSone." + blockedSoneCounter++;
+                                       String blockedSoneId = configuration.getStringValue(blockedSonePrefix + "/ID").getValue(null);
+                                       if (blockedSoneId == null) {
+                                               break;
+                                       }
+                                       sone.addBlockedSoneId(blockedSoneId);
+                               }
+
                                sone.setModificationCounter(modificationCounter);
                                addLocalSone(sone);
                        } catch (MalformedURLException mue1) {
@@ -493,6 +578,28 @@ public class Core extends AbstractService {
                } while (true);
                logger.log(Level.INFO, "Loaded %d Sones.", getSones().size());
 
+               /* load all known Sones. */
+               int knownSonesCounter = 0;
+               while (true) {
+                       String knownSonePrefix = "KnownSone." + knownSonesCounter++;
+                       String knownSoneId = configuration.getStringValue(knownSonePrefix + "/ID").getValue(null);
+                       if (knownSoneId == null) {
+                               break;
+                       }
+                       String knownSoneName = configuration.getStringValue(knownSonePrefix + "/Name").getValue(null);
+                       String knownSoneKey = configuration.getStringValue(knownSonePrefix + "/Key").getValue(null);
+                       try {
+                               getSone(knownSoneId).setName(knownSoneName).setRequestUri(new FreenetURI(knownSoneKey));
+                       } catch (MalformedURLException mue1) {
+                               logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + knownSoneKey + "”)!", mue1);
+                       }
+               }
+
+               /* load all remote Sones. */
+               for (Sone remoteSone : getRemoteSones()) {
+                       loadSone(remoteSone);
+               }
+
                logger.exiting(Core.class.getName(), "loadConfiguration()");
        }
 
@@ -547,10 +654,29 @@ public class Core extends AbstractService {
                                /* write null ID as terminator. */
                                configuration.getStringValue(sonePrefix + "/Friend." + friendId + "/ID").setValue(null);
 
+                               /* write all blocked Sones. */
+                               int blockedSoneCounter = 0;
+                               for (String blockedSoneId : sone.getBlockedSoneIds()) {
+                                       String blockedSonePrefix = sonePrefix + "/BlockedSone." + blockedSoneCounter++;
+                                       configuration.getStringValue(blockedSonePrefix + "/ID").setValue(blockedSoneId);
+                               }
+                               configuration.getStringValue(sonePrefix + "/BlockedSone." + blockedSoneCounter + "/ID").setValue(null);
+
                        }
                        /* write null ID as terminator. */
                        configuration.getStringValue("Sone/Sone." + soneId + "/ID").setValue(null);
 
+                       /* write all known Sones. */
+                       int knownSonesCounter = 0;
+                       for (Sone knownSone : getRemoteSones()) {
+                               String knownSonePrefix = "KnownSone." + knownSonesCounter++;
+                               configuration.getStringValue(knownSonePrefix + "/ID").setValue(knownSone.getId());
+                               configuration.getStringValue(knownSonePrefix + "/Name").setValue(knownSone.getName());
+                               configuration.getStringValue(knownSonePrefix + "/Key").setValue(knownSone.getRequestUri().toString());
+                               /* TODO - store all known stuff? */
+                       }
+                       configuration.getStringValue("KnownSone." + knownSonesCounter + "/ID").setValue(null);
+
                } catch (ConfigurationException ce1) {
                        logger.log(Level.WARNING, "Could not store configuration!", ce1);
                }