+ /**
+ * Deletes the given Sone. This will remove the Sone from the
+ * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
+ * and remove the context from its identity.
+ *
+ * @param sone
+ * The Sone to delete
+ */
+ public void deleteSone(Sone sone) {
+ if (!(sone.getIdentity() instanceof OwnIdentity)) {
+ logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
+ return;
+ }
+ synchronized (localSones) {
+ if (!localSones.containsKey(sone.getId())) {
+ logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
+ return;
+ }
+ localSones.remove(sone.getId());
+ soneInserters.remove(sone).stop();
+ }
+ identityManager.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
+ identityManager.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
+ try {
+ configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
+ } catch (ConfigurationException ce1) {
+ logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
+ }
+ }
+
+ /**
+ * Loads and updates the given Sone from the configuration. If any error is
+ * encountered, loading is aborted and the given Sone is not changed.
+ *
+ * @param sone
+ * The Sone to load and update
+ */
+ public void loadSone(Sone sone) {
+ if (!isLocalSone(sone)) {
+ logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
+ return;
+ }
+
+ /* load Sone. */
+ String sonePrefix = "Sone/" + sone.getId();
+ Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
+ if (soneTime == null) {
+ logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
+ return;
+ }
+ String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
+
+ /* load profile. */
+ Profile profile = new Profile();
+ profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
+ profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
+ profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
+ profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
+ profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
+ profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
+
+ /* load posts. */
+ Set<Post> posts = new HashSet<Post>();
+ while (true) {
+ String postPrefix = sonePrefix + "/Posts/" + posts.size();
+ String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
+ if (postId == null) {
+ break;
+ }
+ String postRecipientId = configuration.getStringValue(postPrefix + "/Recipient").getValue(null);
+ long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
+ String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
+ if ((postTime == 0) || (postText == null)) {
+ logger.log(Level.WARNING, "Invalid post found, aborting load!");
+ return;
+ }
+ Post post = getPost(postId).setSone(sone).setTime(postTime).setText(postText);
+ if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
+ post.setRecipient(getSone(postRecipientId));
+ }
+ posts.add(post);
+ }
+
+ /* load replies. */
+ Set<Reply> replies = new HashSet<Reply>();
+ while (true) {
+ String replyPrefix = sonePrefix + "/Replies/" + replies.size();
+ String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
+ if (replyId == null) {
+ break;
+ }
+ String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
+ long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
+ String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
+ if ((postId == null) || (replyTime == 0) || (replyText == null)) {
+ logger.log(Level.WARNING, "Invalid reply found, aborting load!");
+ return;
+ }
+ replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
+ }
+
+ /* load post likes. */
+ Set<String> likedPostIds = new HashSet<String>();
+ while (true) {
+ String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
+ if (likedPostId == null) {
+ break;
+ }
+ likedPostIds.add(likedPostId);
+ }
+
+ /* load reply likes. */
+ Set<String> likedReplyIds = new HashSet<String>();
+ while (true) {
+ String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
+ if (likedReplyId == null) {
+ break;
+ }
+ likedReplyIds.add(likedReplyId);
+ }
+
+ /* load friends. */
+ Set<String> friends = new HashSet<String>();
+ while (true) {
+ String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
+ if (friendId == null) {
+ break;