Remove javadoc comments from overriding methods.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneDownloader.java
index eaf56a9..dea7402 100644 (file)
 
 package net.pterodactylus.sone.core;
 
+import static com.google.common.base.Optional.of;
+
 import java.io.InputStream;
 import java.net.MalformedURLException;
-import java.util.ArrayList;
 import java.util.HashSet;
-import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -35,8 +36,12 @@ import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.Sone.SoneStatus;
+import net.pterodactylus.sone.data.impl.DefaultSone;
+import net.pterodactylus.sone.database.ImageBuilder.ImageCreated;
 import net.pterodactylus.sone.database.PostBuilder;
+import net.pterodactylus.sone.database.PostBuilder.PostCreated;
 import net.pterodactylus.sone.database.PostReplyBuilder;
+import net.pterodactylus.sone.database.PostReplyBuilder.PostReplyCreated;
 import net.pterodactylus.util.io.Closer;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.number.Numbers;
@@ -44,12 +49,14 @@ import net.pterodactylus.util.service.AbstractService;
 import net.pterodactylus.util.xml.SimpleXML;
 import net.pterodactylus.util.xml.XML;
 
-import org.w3c.dom.Document;
-
 import freenet.client.FetchResult;
 import freenet.keys.FreenetURI;
 import freenet.support.api.Bucket;
 
+import com.google.common.base.Optional;
+import com.google.common.collect.Maps;
+import org.w3c.dom.Document;
+
 /**
  * The Sone downloader is responsible for download Sones as they are updated.
  *
@@ -239,7 +246,7 @@ public class SoneDownloader extends AbstractService {
                        return null;
                }
 
-               Sone sone = new Sone(originalSone.getId(), originalSone.isLocal()).setIdentity(originalSone.getIdentity());
+               Sone sone = new DefaultSone(core.getDatabase(), originalSone.getId(), originalSone.isLocal()).setIdentity(originalSone.getIdentity());
 
                SimpleXML soneXml;
                try {
@@ -307,16 +314,8 @@ public class SoneDownloader extends AbstractService {
                        }
                }
 
-               String soneInsertUri = soneXml.getValue("insert-uri", null);
-               if ((soneInsertUri != null) && (sone.getInsertUri() == null)) {
-                       try {
-                               sone.setInsertUri(new FreenetURI(soneInsertUri));
-                               sone.setLatestEdition(Math.max(sone.getRequestUri().getEdition(), sone.getInsertUri().getEdition()));
-                       } catch (MalformedURLException mue1) {
-                               /* TODO - mark Sone as bad. */
-                               logger.log(Level.WARNING, String.format("Downloaded Sone %s has invalid insert URI: %s", sone, soneInsertUri), mue1);
-                               return null;
-                       }
+               if (originalSone.getInsertUri() != null) {
+                       sone.setInsertUri(originalSone.getInsertUri());
                }
 
                SimpleXML profileXml = soneXml.getNode("profile");
@@ -375,13 +374,13 @@ public class SoneDownloader extends AbstractService {
                                        return null;
                                }
                                try {
-                                       PostBuilder postBuilder = core.postBuilder();
+                                       PostBuilder postBuilder = sone.newPostBuilder();
                                        /* TODO - parse time correctly. */
-                                       postBuilder.withId(postId).from(sone.getId()).withTime(Long.parseLong(postTime)).withText(postText);
+                                       postBuilder.withId(postId).withTime(Long.parseLong(postTime)).withText(postText);
                                        if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
-                                               postBuilder.to(postRecipientId);
+                                               postBuilder.to(of(postRecipientId));
                                        }
-                                       posts.add(postBuilder.build());
+                                       posts.add(postBuilder.build(Optional.<PostCreated>absent()));
                                } catch (NumberFormatException nfe1) {
                                        /* TODO - mark Sone as bad. */
                                        logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with invalid time: %s", sone, postTime));
@@ -408,10 +407,9 @@ public class SoneDownloader extends AbstractService {
                                        return null;
                                }
                                try {
-                                       PostReplyBuilder postReplyBuilder = core.postReplyBuilder();
                                        /* TODO - parse time correctly. */
-                                       postReplyBuilder.withId(replyId).from(sone.getId()).to(replyPostId).withTime(Long.parseLong(replyTime)).withText(replyText);
-                                       replies.add(postReplyBuilder.build());
+                                       PostReplyBuilder postReplyBuilder = sone.newPostReplyBuilder(replyPostId).withId(replyId).withTime(Long.parseLong(replyTime)).withText(replyText);
+                                       replies.add(postReplyBuilder.build(Optional.<PostReplyCreated>absent()));
                                } catch (NumberFormatException nfe1) {
                                        /* TODO - mark Sone as bad. */
                                        logger.log(Level.WARNING, String.format("Downloaded reply for Sone %s with invalid time: %s", sone, replyTime));
@@ -448,7 +446,7 @@ public class SoneDownloader extends AbstractService {
 
                /* parse albums. */
                SimpleXML albumsXml = soneXml.getNode("albums");
-               List<Album> topLevelAlbums = new ArrayList<Album>();
+               Map<String, Album> albums = Maps.newHashMap();
                if (albumsXml != null) {
                        for (SimpleXML albumXml : albumsXml.getNodes("album")) {
                                String id = albumXml.getValue("id", null);
@@ -460,20 +458,16 @@ public class SoneDownloader extends AbstractService {
                                        logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid album!", sone));
                                        return null;
                                }
-                               Album parent = null;
+                               Album parent = sone.getRootAlbum();
                                if (parentId != null) {
-                                       parent = core.getAlbum(parentId, false);
+                                       parent = albums.get(parentId);
                                        if (parent == null) {
                                                logger.log(Level.WARNING, String.format("Downloaded Sone %s has album with invalid parent!", sone));
                                                return null;
                                        }
                                }
-                               Album album = core.getAlbum(id).setSone(sone).modify().setTitle(title).setDescription(description).update();
-                               if (parent != null) {
-                                       parent.addAlbum(album);
-                               } else {
-                                       topLevelAlbums.add(album);
-                               }
+                               Album album = parent.newAlbumBuilder().withId(id).build().modify().setTitle(title).setDescription(description).update();
+                               albums.put(album.getId(), album);
                                SimpleXML imagesXml = albumXml.getNode("images");
                                if (imagesXml != null) {
                                        for (SimpleXML imageXml : imagesXml.getNodes("image")) {
@@ -495,10 +489,8 @@ public class SoneDownloader extends AbstractService {
                                                        logger.log(Level.WARNING, String.format("Downloaded Sone %s contains image %s with invalid dimensions (%s, %s)!", sone, imageId, imageWidthString, imageHeightString));
                                                        return null;
                                                }
-                                               Image image = core.getImage(imageId).setSone(sone).setKey(imageKey).setCreationTime(creationTime);
-                                               image.setTitle(imageTitle).setDescription(imageDescription);
-                                               image.setWidth(imageWidth).setHeight(imageHeight);
-                                               album.addImage(image);
+                                               Image image = album.newImageBuilder().withId(imageId).at(imageKey).created(creationTime).sized(imageWidth, imageHeight).build(Optional.<ImageCreated>absent());
+                                               image = image.modify().setTitle(imageTitle).setDescription(imageDescription).update();
                                        }
                                }
                                album.modify().setAlbumImage(albumImageId).update();
@@ -507,7 +499,7 @@ public class SoneDownloader extends AbstractService {
 
                /* process avatar. */
                if (avatarId != null) {
-                       profile.setAvatar(core.getImage(avatarId, false));
+                       profile.setAvatar(core.getImage(avatarId).orNull());
                }
 
                /* okay, apparently everything was parsed correctly. Now import. */
@@ -518,9 +510,6 @@ public class SoneDownloader extends AbstractService {
                        sone.setReplies(replies);
                        sone.setLikePostIds(likedPostIds);
                        sone.setLikeReplyIds(likedReplyIds);
-                       for (Album album : topLevelAlbums) {
-                               sone.getRootAlbum().addAlbum(album);
-                       }
                }
 
                return sone;
@@ -530,9 +519,6 @@ public class SoneDownloader extends AbstractService {
        // SERVICE METHODS
        //
 
-       /**
-        * {@inheritDoc}
-        */
        @Override
        protected void serviceStop() {
                for (Sone sone : sones) {