+ /**
+ * Adds a Sone to watch for updates. The Sone needs to be completely
+ * initialized.
+ *
+ * @param sone
+ * The Sone to watch for updates
+ */
+ public void addSone(Sone sone) {
+ soneCache.put(sone.getId(), sone);
+ if (!localSones.contains(sone)) {
+ soneDownloader.addSone(sone);
+ }
+ }
+
+ /**
+ * Adds the given Sone.
+ *
+ * @param sone
+ * The Sone to add
+ */
+ public void addLocalSone(Sone sone) {
+ if (localSones.add(sone)) {
+ setSoneStatus(sone, SoneStatus.idle);
+ SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
+ soneInserter.start();
+ soneInserters.put(sone, soneInserter);
+ }
+ }
+
+ /**
+ * Creates a new Sone at a random location.
+ *
+ * @param name
+ * The name of the Sone
+ * @return The created Sone
+ * @throws SoneException
+ * if a Sone error occurs
+ */
+ public Sone createSone(String name) throws SoneException {
+ return createSone(name, "Sone-" + name, null, null);
+ }
+
+ /**
+ * Creates a new Sone at the given location. If one of {@code requestUri} or
+ * {@code insertUrI} is {@code null}, the Sone is created at a random
+ * location.
+ *
+ * @param name
+ * The name of the Sone
+ * @param documentName
+ * The document name in the SSK
+ * @param requestUri
+ * The request URI of the Sone, or {@link NullPointerException}
+ * to create a Sone at a random location
+ * @param insertUri
+ * The insert URI of the Sone, or {@code null} to create a Sone
+ * at a random location
+ * @return The created Sone
+ * @throws SoneException
+ * if a Sone error occurs
+ */
+ public Sone createSone(String name, String documentName, String requestUri, String insertUri) throws SoneException {
+ if ((name == null) || (name.trim().length() == 0)) {
+ throw new SoneException(Type.INVALID_SONE_NAME);
+ }
+ String finalRequestUri;
+ String finalInsertUri;
+ if ((requestUri == null) || (insertUri == null)) {
+ String[] keyPair = freenetInterface.generateKeyPair();
+ finalRequestUri = keyPair[0];
+ finalInsertUri = keyPair[1];
+ } else {
+ finalRequestUri = requestUri;
+ finalInsertUri = insertUri;
+ }
+ Sone sone;
+ try {
+ logger.log(Level.FINEST, "Creating new Sone “%s” at %s (%s)…", new Object[] { name, finalRequestUri, finalInsertUri });
+ sone = getSone(UUID.randomUUID().toString()).setName(name).setRequestUri(new FreenetURI(finalRequestUri).setKeyType("USK").setDocName(documentName)).setInsertUri(new FreenetURI(finalInsertUri).setKeyType("USK").setDocName(documentName));
+ sone.setProfile(new Profile());
+ /* set modification counter to 1 so it is inserted immediately. */
+ sone.setModificationCounter(1);
+ addLocalSone(sone);
+ } catch (MalformedURLException mue1) {
+ throw new SoneException(Type.INVALID_URI);
+ }
+ return sone;
+ }
+
+ /**
+ * 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(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);
+ if (fetchResult == null) {
+ return;
+ }
+ 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" });
+ setSoneStatus(sone, SoneStatus.downloading);
+ try {
+ FetchResult fetchResult = freenetInterface.fetchUri(realRequestUri);
+ if (fetchResult == null) {
+ /* TODO - mark Sone as bad. */
+ return;
+ }
+ Sone parsedSone = soneDownloader.parseSone(sone, fetchResult, realRequestUri);
+ if (parsedSone != null) {
+ addSone(parsedSone);
+ }
+ } finally {
+ setSoneStatus(sone, (sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
+ }
+ }
+ }, "Sone Downloader").start();
+ }
+
+ /**
+ * Deletes the given Sone from this plugin instance.
+ *
+ * @param sone
+ * The sone to delete
+ */
+ public void deleteSone(Sone sone) {
+ SoneInserter soneInserter = soneInserters.remove(sone);
+ soneInserter.stop();
+ localSones.remove(sone);
+ }
+
+ /**
+ * Returns the post with the given ID. If no post exists yet with the given
+ * ID, a new post is returned.
+ *
+ * @param postId
+ * The ID of the post
+ * @return The post
+ */
+ public Post getPost(String postId) {
+ if (!postCache.containsKey(postId)) {
+ postCache.put(postId, new Post(postId));
+ }
+ return postCache.get(postId);
+ }
+
+ /**
+ * Returns the reply with the given ID. If no reply exists yet with the
+ * given ID, a new reply is returned.
+ *
+ * @param replyId
+ * The ID of the reply
+ * @return The reply
+ */
+ public Reply getReply(String replyId) {
+ if (!replyCache.containsKey(replyId)) {
+ replyCache.put(replyId, new Reply(replyId));
+ }
+ return replyCache.get(replyId);
+ }
+
+ /**
+ * Gets all replies to the given post, sorted by date, oldest first.
+ *
+ * @param post
+ * The post the replies refer to
+ * @return The sorted list of replies for the post
+ */
+ public List<Reply> getReplies(Post post) {
+ List<Reply> replies = new ArrayList<Reply>();
+ for (Reply reply : replyCache.values()) {
+ if (reply.getPost().equals(post)) {
+ replies.add(reply);
+ }
+ }
+ Collections.sort(replies, new Comparator<Reply>() {
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int compare(Reply leftReply, Reply rightReply) {
+ return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, leftReply.getTime() - rightReply.getTime()));
+ }
+ });
+ return replies;
+ }
+
+ /**
+ * Gets all Sones that like the given post.
+ *
+ * @param post
+ * The post to check for
+ * @return All Sones that like the post
+ */
+ public Set<Sone> getLikes(final Post post) {
+ return Filters.filteredSet(getSones(), new Filter<Sone>() {
+
+ @Override
+ public boolean filterObject(Sone sone) {
+ return sone.isLikedPostId(post.getId());
+ }
+ });
+ }
+
+ /**
+ * Gets all Sones that like the given reply.
+ *
+ * @param reply
+ * The reply to check for
+ * @return All Sones that like the reply
+ */
+ public Set<Sone> getLikes(final Reply reply) {
+ return Filters.filteredSet(getSones(), new Filter<Sone>() {
+
+ @Override
+ public boolean filterObject(Sone sone) {
+ return sone.isLikedReplyId(reply.getId());
+ }
+ });
+ }
+
+ /**
+ * Deletes the given reply. It is removed from its Sone and from the reply
+ * cache.
+ *
+ * @param reply
+ * The reply to remove
+ */
+ public void deleteReply(Reply reply) {
+ reply.getSone().removeReply(reply);
+ replyCache.remove(reply.getId());
+ }
+