Implement “like” button.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
1 /*
2  * FreenetSone - Core.java - Copyright © 2010 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.core;
19
20 import java.net.MalformedURLException;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.UUID;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
33
34 import net.pterodactylus.sone.core.SoneException.Type;
35 import net.pterodactylus.sone.data.Post;
36 import net.pterodactylus.sone.data.Profile;
37 import net.pterodactylus.sone.data.Reply;
38 import net.pterodactylus.sone.data.Sone;
39 import net.pterodactylus.util.config.Configuration;
40 import net.pterodactylus.util.config.ConfigurationException;
41 import net.pterodactylus.util.filter.Filter;
42 import net.pterodactylus.util.filter.Filters;
43 import net.pterodactylus.util.logging.Logging;
44 import net.pterodactylus.util.service.AbstractService;
45 import freenet.client.FetchResult;
46 import freenet.keys.FreenetURI;
47
48 /**
49  * The Sone core.
50  *
51  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
52  */
53 public class Core extends AbstractService {
54
55         /**
56          * Enumeration for the possible states of a {@link Sone}.
57          *
58          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
59          */
60         public enum SoneStatus {
61
62                 /** The Sone is unknown, i.e. not yet downloaded. */
63                 unknown,
64
65                 /** The Sone is idle, i.e. not being downloaded or inserted. */
66                 idle,
67
68                 /** The Sone is currently being inserted. */
69                 inserting,
70
71                 /** The Sone is currently being downloaded. */
72                 downloading,
73         }
74
75         /** The logger. */
76         private static final Logger logger = Logging.getLogger(Core.class);
77
78         /** The configuration. */
79         private Configuration configuration;
80
81         /** Interface to freenet. */
82         private FreenetInterface freenetInterface;
83
84         /** The Sone downloader. */
85         private SoneDownloader soneDownloader;
86
87         /** The local Sones. */
88         private final Set<Sone> localSones = new HashSet<Sone>();
89
90         /** Sone inserters. */
91         private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
92
93         /** The Sones’ statuses. */
94         private final Map<Sone, SoneStatus> soneStatuses = Collections.synchronizedMap(new HashMap<Sone, SoneStatus>());
95
96         /* various caches follow here. */
97
98         /** Cache for all known Sones. */
99         private final Map<String, Sone> soneCache = new HashMap<String, Sone>();
100
101         /** Cache for all known posts. */
102         private final Map<String, Post> postCache = new HashMap<String, Post>();
103
104         /** Cache for all known replies. */
105         private final Map<String, Reply> replyCache = new HashMap<String, Reply>();
106
107         /**
108          * Creates a new core.
109          */
110         public Core() {
111                 super("Sone Core", false);
112         }
113
114         //
115         // ACCESSORS
116         //
117
118         /**
119          * Sets the configuration of the core.
120          *
121          * @param configuration
122          *            The configuration of the core
123          * @return This core (for method chaining)
124          */
125         public Core configuration(Configuration configuration) {
126                 this.configuration = configuration;
127                 return this;
128         }
129
130         /**
131          * Sets the Freenet interface to use.
132          *
133          * @param freenetInterface
134          *            The Freenet interface to use
135          * @return This core (for method chaining)
136          */
137         public Core freenetInterface(FreenetInterface freenetInterface) {
138                 this.freenetInterface = freenetInterface;
139                 soneDownloader = new SoneDownloader(this, freenetInterface);
140                 soneDownloader.start();
141                 return this;
142         }
143
144         /**
145          * Returns the local Sones.
146          *
147          * @return The local Sones
148          */
149         public Set<Sone> getSones() {
150                 return Collections.unmodifiableSet(localSones);
151         }
152
153         /**
154          * Returns the Sone with the given ID, or an empty Sone that has been
155          * initialized with the given ID.
156          *
157          * @param soneId
158          *            The ID of the Sone
159          * @return The Sone
160          */
161         public Sone getSone(String soneId) {
162                 if (!soneCache.containsKey(soneId)) {
163                         Sone sone = new Sone(soneId);
164                         soneCache.put(soneId, sone);
165                         setSoneStatus(sone, SoneStatus.unknown);
166                 }
167                 return soneCache.get(soneId);
168         }
169
170         /**
171          * Returns all known sones.
172          *
173          * @return All known sones
174          */
175         public Collection<Sone> getKnownSones() {
176                 return soneCache.values();
177         }
178
179         /**
180          * Gets all known Sones that are not local Sones.
181          *
182          * @return All remote Sones
183          */
184         public Collection<Sone> getRemoteSones() {
185                 return Filters.filteredCollection(getKnownSones(), new Filter<Sone>() {
186
187                         @Override
188                         @SuppressWarnings("synthetic-access")
189                         public boolean filterObject(Sone object) {
190                                 return !localSones.contains(object);
191                         }
192                 });
193         }
194
195         /**
196          * Returns the status of the given Sone.
197          *
198          * @param sone
199          *            The Sone to get the status for
200          * @return The status of the Sone
201          */
202         public SoneStatus getSoneStatus(Sone sone) {
203                 return soneStatuses.get(sone);
204         }
205
206         /**
207          * Sets the status of the Sone.
208          *
209          * @param sone
210          *            The Sone to set the status for
211          * @param soneStatus
212          *            The status of the Sone
213          */
214         public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
215                 soneStatuses.put(sone, soneStatus);
216         }
217
218         /**
219          * Creates a new post and adds it to the given Sone.
220          *
221          * @param sone
222          *            The sone that creates the post
223          * @param text
224          *            The text of the post
225          * @return The created post
226          */
227         public Post createPost(Sone sone, String text) {
228                 return createPost(sone, System.currentTimeMillis(), text);
229         }
230
231         /**
232          * Creates a new post and adds it to the given Sone.
233          *
234          * @param sone
235          *            The Sone that creates the post
236          * @param time
237          *            The time of the post
238          * @param text
239          *            The text of the post
240          * @return The created post
241          */
242         public Post createPost(Sone sone, long time, String text) {
243                 Post post = getPost(UUID.randomUUID().toString()).setSone(sone).setTime(time).setText(text);
244                 sone.addPost(post);
245                 return post;
246         }
247
248         /**
249          * Creates a reply.
250          *
251          * @param sone
252          *            The Sone that posts the reply
253          * @param post
254          *            The post the reply refers to
255          * @param text
256          *            The text of the reply
257          * @return The created reply
258          */
259         public Reply createReply(Sone sone, Post post, String text) {
260                 return createReply(sone, post, System.currentTimeMillis(), text);
261         }
262
263         /**
264          * Creates a reply.
265          *
266          * @param sone
267          *            The Sone that posts the reply
268          * @param post
269          *            The post the reply refers to
270          * @param time
271          *            The time of the post
272          * @param text
273          *            The text of the reply
274          * @return The created reply
275          */
276         public Reply createReply(Sone sone, Post post, long time, String text) {
277                 Reply reply = getReply(UUID.randomUUID().toString()).setSone(sone).setPost(post).setTime(time).setText(text);
278                 sone.addReply(reply);
279                 return reply;
280         }
281
282         //
283         // ACTIONS
284         //
285
286         /**
287          * Adds a Sone to watch for updates. The Sone needs to be completely
288          * initialized.
289          *
290          * @param sone
291          *            The Sone to watch for updates
292          */
293         public void addSone(Sone sone) {
294                 soneCache.put(sone.getId(), sone);
295                 if (!localSones.contains(sone)) {
296                         soneDownloader.addSone(sone);
297                 }
298         }
299
300         /**
301          * Adds the given Sone.
302          *
303          * @param sone
304          *            The Sone to add
305          */
306         public void addLocalSone(Sone sone) {
307                 if (localSones.add(sone)) {
308                         setSoneStatus(sone, SoneStatus.idle);
309                         SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
310                         soneInserter.start();
311                         soneInserters.put(sone, soneInserter);
312                 }
313         }
314
315         /**
316          * Creates a new Sone at a random location.
317          *
318          * @param name
319          *            The name of the Sone
320          * @return The created Sone
321          * @throws SoneException
322          *             if a Sone error occurs
323          */
324         public Sone createSone(String name) throws SoneException {
325                 return createSone(name, "Sone-" + name, null, null);
326         }
327
328         /**
329          * Creates a new Sone at the given location. If one of {@code requestUri} or
330          * {@code insertUrI} is {@code null}, the Sone is created at a random
331          * location.
332          *
333          * @param name
334          *            The name of the Sone
335          * @param documentName
336          *            The document name in the SSK
337          * @param requestUri
338          *            The request URI of the Sone, or {@link NullPointerException}
339          *            to create a Sone at a random location
340          * @param insertUri
341          *            The insert URI of the Sone, or {@code null} to create a Sone
342          *            at a random location
343          * @return The created Sone
344          * @throws SoneException
345          *             if a Sone error occurs
346          */
347         public Sone createSone(String name, String documentName, String requestUri, String insertUri) throws SoneException {
348                 if ((name == null) || (name.trim().length() == 0)) {
349                         throw new SoneException(Type.INVALID_SONE_NAME);
350                 }
351                 String finalRequestUri;
352                 String finalInsertUri;
353                 if ((requestUri == null) || (insertUri == null)) {
354                         String[] keyPair = freenetInterface.generateKeyPair();
355                         finalRequestUri = keyPair[0];
356                         finalInsertUri = keyPair[1];
357                 } else {
358                         finalRequestUri = requestUri;
359                         finalInsertUri = insertUri;
360                 }
361                 Sone sone;
362                 try {
363                         logger.log(Level.FINEST, "Creating new Sone “%s” at %s (%s)…", new Object[] { name, finalRequestUri, finalInsertUri });
364                         sone = getSone(UUID.randomUUID().toString()).setName(name).setRequestUri(new FreenetURI(finalRequestUri).setKeyType("USK").setDocName(documentName)).setInsertUri(new FreenetURI(finalInsertUri).setKeyType("USK").setDocName(documentName));
365                         sone.setProfile(new Profile());
366                         /* set modification counter to 1 so it is inserted immediately. */
367                         sone.setModificationCounter(1);
368                         addLocalSone(sone);
369                 } catch (MalformedURLException mue1) {
370                         throw new SoneException(Type.INVALID_URI);
371                 }
372                 return sone;
373         }
374
375         /**
376          * Loads the Sone from the given request URI. The fetching of the data is
377          * performed in a new thread so this method returns immediately.
378          *
379          * @param requestUri
380          *            The request URI to load the Sone from
381          */
382         public void loadSone(final String requestUri) {
383                 loadSone(requestUri, null);
384         }
385
386         /**
387          * Loads the Sone from the given request URI. The fetching of the data is
388          * performed in a new thread so this method returns immediately. If
389          * {@code insertUri} is not {@code null} the loaded Sone is converted into a
390          * local Sone and available using as any other local Sone.
391          *
392          * @param requestUri
393          *            The request URI to load the Sone from
394          * @param insertUri
395          *            The insert URI of the Sone
396          */
397         public void loadSone(final String requestUri, final String insertUri) {
398                 new Thread(new Runnable() {
399
400                         @Override
401                         @SuppressWarnings("synthetic-access")
402                         public void run() {
403                                 try {
404                                         FreenetURI realRequestUri = new FreenetURI(requestUri).setMetaString(new String[] { "sone.xml" });
405                                         FetchResult fetchResult = freenetInterface.fetchUri(realRequestUri);
406                                         if (fetchResult == null) {
407                                                 return;
408                                         }
409                                         Sone parsedSone = soneDownloader.parseSone(null, fetchResult, realRequestUri);
410                                         if (parsedSone != null) {
411                                                 if (insertUri != null) {
412                                                         parsedSone.setInsertUri(new FreenetURI(insertUri));
413                                                         addLocalSone(parsedSone);
414                                                 } else {
415                                                         addSone(parsedSone);
416                                                 }
417                                         }
418                                 } catch (MalformedURLException mue1) {
419                                         logger.log(Level.INFO, "Could not create URI from “" + requestUri + "”.", mue1);
420                                 }
421                         }
422                 }, "Sone Downloader").start();
423         }
424
425         /**
426          * Loads and updates the given Sone.
427          *
428          * @param sone
429          *            The Sone to load
430          */
431         public void loadSone(final Sone sone) {
432                 new Thread(new Runnable() {
433
434                         @Override
435                         @SuppressWarnings("synthetic-access")
436                         public void run() {
437                                 FreenetURI realRequestUri = sone.getRequestUri().setMetaString(new String[] { "sone.xml" });
438                                 setSoneStatus(sone, SoneStatus.downloading);
439                                 try {
440                                         FetchResult fetchResult = freenetInterface.fetchUri(realRequestUri);
441                                         if (fetchResult == null) {
442                                                 /* TODO - mark Sone as bad. */
443                                                 return;
444                                         }
445                                         Sone parsedSone = soneDownloader.parseSone(sone, fetchResult, realRequestUri);
446                                         if (parsedSone != null) {
447                                                 addSone(parsedSone);
448                                         }
449                                 } finally {
450                                         setSoneStatus(sone, (sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
451                                 }
452                         }
453                 }, "Sone Downloader").start();
454         }
455
456         /**
457          * Deletes the given Sone from this plugin instance.
458          *
459          * @param sone
460          *            The sone to delete
461          */
462         public void deleteSone(Sone sone) {
463                 SoneInserter soneInserter = soneInserters.remove(sone);
464                 soneInserter.stop();
465                 localSones.remove(sone);
466         }
467
468         /**
469          * Returns the post with the given ID. If no post exists yet with the given
470          * ID, a new post is returned.
471          *
472          * @param postId
473          *            The ID of the post
474          * @return The post
475          */
476         public Post getPost(String postId) {
477                 if (!postCache.containsKey(postId)) {
478                         postCache.put(postId, new Post(postId));
479                 }
480                 return postCache.get(postId);
481         }
482
483         /**
484          * Returns the reply with the given ID. If no reply exists yet with the
485          * given ID, a new reply is returned.
486          *
487          * @param replyId
488          *            The ID of the reply
489          * @return The reply
490          */
491         public Reply getReply(String replyId) {
492                 if (!replyCache.containsKey(replyId)) {
493                         replyCache.put(replyId, new Reply(replyId));
494                 }
495                 return replyCache.get(replyId);
496         }
497
498         /**
499          * Gets all replies to the given post, sorted by date, oldest first.
500          *
501          * @param post
502          *            The post the replies refer to
503          * @return The sorted list of replies for the post
504          */
505         public List<Reply> getReplies(Post post) {
506                 List<Reply> replies = new ArrayList<Reply>();
507                 for (Reply reply : replyCache.values()) {
508                         if (reply.getPost().equals(post)) {
509                                 replies.add(reply);
510                         }
511                 }
512                 Collections.sort(replies, new Comparator<Reply>() {
513
514                         /**
515                          * {@inheritDoc}
516                          */
517                         @Override
518                         public int compare(Reply leftReply, Reply rightReply) {
519                                 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, leftReply.getTime() - rightReply.getTime()));
520                         }
521                 });
522                 return replies;
523         }
524
525         /**
526          * Gets all Sones that like the given post.
527          *
528          * @param post
529          *            The post to check for
530          * @return All Sones that like the post
531          */
532         public Set<Sone> getLikes(final Post post) {
533                 return Filters.filteredSet(getSones(), new Filter<Sone>() {
534
535                         @Override
536                         public boolean filterObject(Sone sone) {
537                                 return sone.isLikedPostId(post.getId());
538                         }
539                 });
540         }
541
542         /**
543          * Deletes the given reply. It is removed from its Sone and from the reply
544          * cache.
545          *
546          * @param reply
547          *            The reply to remove
548          */
549         public void deleteReply(Reply reply) {
550                 reply.getSone().removeReply(reply);
551                 replyCache.remove(reply.getId());
552         }
553
554         //
555         // SERVICE METHODS
556         //
557
558         /**
559          * {@inheritDoc}
560          */
561         @Override
562         protected void serviceStart() {
563                 loadConfiguration();
564         }
565
566         /**
567          * {@inheritDoc}
568          */
569         @Override
570         protected void serviceStop() {
571                 soneDownloader.stop();
572                 /* stop all Sone inserters. */
573                 for (SoneInserter soneInserter : soneInserters.values()) {
574                         soneInserter.stop();
575                 }
576                 saveConfiguration();
577         }
578
579         //
580         // PRIVATE METHODS
581         //
582
583         /**
584          * Loads the configuration.
585          */
586         private void loadConfiguration() {
587                 logger.entering(Core.class.getName(), "loadConfiguration()");
588
589                 /* parse local Sones. */
590                 logger.log(Level.INFO, "Loading Sones…");
591                 int soneId = 0;
592                 do {
593                         String sonePrefix = "Sone/Sone." + soneId++;
594                         String id = configuration.getStringValue(sonePrefix + "/ID").getValue(null);
595                         if (id == null) {
596                                 break;
597                         }
598                         String name = configuration.getStringValue(sonePrefix + "/Name").getValue(null);
599                         long time = configuration.getLongValue(sonePrefix + "/Time").getValue((long) 0);
600                         String insertUri = configuration.getStringValue(sonePrefix + "/InsertURI").getValue(null);
601                         String requestUri = configuration.getStringValue(sonePrefix + "/RequestURI").getValue(null);
602                         long modificationCounter = configuration.getLongValue(sonePrefix + "/ModificationCounter").getValue((long) 0);
603                         String firstName = configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null);
604                         String middleName = configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null);
605                         String lastName = configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null);
606                         try {
607                                 Profile profile = new Profile();
608                                 profile.setFirstName(firstName);
609                                 profile.setMiddleName(middleName);
610                                 profile.setLastName(lastName);
611                                 Sone sone = getSone(id).setName(name).setTime(time).setRequestUri(new FreenetURI(requestUri)).setInsertUri(new FreenetURI(insertUri));
612                                 sone.setProfile(profile);
613                                 int postId = 0;
614                                 do {
615                                         String postPrefix = sonePrefix + "/Post." + postId++;
616                                         id = configuration.getStringValue(postPrefix + "/ID").getValue(null);
617                                         if (id == null) {
618                                                 break;
619                                         }
620                                         time = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
621                                         String text = configuration.getStringValue(postPrefix + "/Text").getValue(null);
622                                         Post post = getPost(id).setSone(sone).setTime(time).setText(text);
623                                         sone.addPost(post);
624                                 } while (true);
625                                 int replyCounter = 0;
626                                 do {
627                                         String replyPrefix = sonePrefix + "/Reply." + replyCounter++;
628                                         String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
629                                         if (replyId == null) {
630                                                 break;
631                                         }
632                                         Post replyPost = getPost(configuration.getStringValue(replyPrefix + "/Post").getValue(null));
633                                         long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue(null);
634                                         String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
635                                         Reply reply = getReply(replyId).setSone(sone).setPost(replyPost).setTime(replyTime).setText(replyText);
636                                         sone.addReply(reply);
637                                 } while (true);
638
639                                 /* load friends. */
640                                 int friendCounter = 0;
641                                 while (true) {
642                                         String friendPrefix = sonePrefix + "/Friend." + friendCounter++;
643                                         String friendId = configuration.getStringValue(friendPrefix + "/ID").getValue(null);
644                                         if (friendId == null) {
645                                                 break;
646                                         }
647                                         Sone friendSone = getSone(friendId);
648                                         String friendKey = configuration.getStringValue(friendPrefix + "/Key").getValue(null);
649                                         String friendName = configuration.getStringValue(friendPrefix + "/Name").getValue(null);
650                                         friendSone.setRequestUri(new FreenetURI(friendKey)).setName(friendName);
651                                         sone.addFriend(friendSone);
652                                 }
653
654                                 /* load blocked Sone IDs. */
655                                 int blockedSoneCounter = 0;
656                                 while (true) {
657                                         String blockedSonePrefix = sonePrefix + "/BlockedSone." + blockedSoneCounter++;
658                                         String blockedSoneId = configuration.getStringValue(blockedSonePrefix + "/ID").getValue(null);
659                                         if (blockedSoneId == null) {
660                                                 break;
661                                         }
662                                         sone.addBlockedSoneId(blockedSoneId);
663                                 }
664
665                                 /* load liked post IDs. */
666                                 int likedPostIdCounter = 0;
667                                 while (true) {
668                                         String likedPostIdPrefix = sonePrefix + "/LikedPostId." + likedPostIdCounter++;
669                                         String likedPostId = configuration.getStringValue(likedPostIdPrefix + "/ID").getValue(null);
670                                         if (likedPostId == null) {
671                                                 break;
672                                         }
673                                         sone.addLikedPostId(likedPostId);
674                                 }
675
676                                 sone.setModificationCounter(modificationCounter);
677                                 addLocalSone(sone);
678                         } catch (MalformedURLException mue1) {
679                                 logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + requestUri + "”) and insertUri (“" + insertUri + "”)!", mue1);
680                         }
681                 } while (true);
682                 logger.log(Level.INFO, "Loaded %d Sones.", getSones().size());
683
684                 /* load all known Sones. */
685                 int knownSonesCounter = 0;
686                 while (true) {
687                         String knownSonePrefix = "KnownSone." + knownSonesCounter++;
688                         String knownSoneId = configuration.getStringValue(knownSonePrefix + "/ID").getValue(null);
689                         if (knownSoneId == null) {
690                                 break;
691                         }
692                         String knownSoneName = configuration.getStringValue(knownSonePrefix + "/Name").getValue(null);
693                         String knownSoneKey = configuration.getStringValue(knownSonePrefix + "/Key").getValue(null);
694                         try {
695                                 getSone(knownSoneId).setName(knownSoneName).setRequestUri(new FreenetURI(knownSoneKey));
696                         } catch (MalformedURLException mue1) {
697                                 logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + knownSoneKey + "”)!", mue1);
698                         }
699                 }
700
701                 /* load all remote Sones. */
702                 for (Sone remoteSone : getRemoteSones()) {
703                         loadSone(remoteSone);
704                 }
705
706                 logger.exiting(Core.class.getName(), "loadConfiguration()");
707         }
708
709         /**
710          * Saves the configuraiton.
711          */
712         private void saveConfiguration() {
713                 Set<Sone> sones = getSones();
714                 logger.log(Level.INFO, "Storing %d Sones…", sones.size());
715                 try {
716                         /* store all Sones. */
717                         int soneId = 0;
718                         for (Sone sone : localSones) {
719                                 String sonePrefix = "Sone/Sone." + soneId++;
720                                 configuration.getStringValue(sonePrefix + "/ID").setValue(sone.getId());
721                                 configuration.getStringValue(sonePrefix + "/Name").setValue(sone.getName());
722                                 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
723                                 configuration.getStringValue(sonePrefix + "/RequestURI").setValue(sone.getRequestUri().toString());
724                                 configuration.getStringValue(sonePrefix + "/InsertURI").setValue(sone.getInsertUri().toString());
725                                 configuration.getLongValue(sonePrefix + "/ModificationCounter").setValue(sone.getModificationCounter());
726                                 Profile profile = sone.getProfile();
727                                 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
728                                 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
729                                 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
730                                 int postId = 0;
731                                 for (Post post : sone.getPosts()) {
732                                         String postPrefix = sonePrefix + "/Post." + postId++;
733                                         configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
734                                         configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
735                                         configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
736                                 }
737                                 /* write null ID as terminator. */
738                                 configuration.getStringValue(sonePrefix + "/Post." + postId + "/ID").setValue(null);
739
740                                 int replyId = 0;
741                                 for (Reply reply : sone.getReplies()) {
742                                         String replyPrefix = sonePrefix + "/Reply." + replyId++;
743                                         configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
744                                         configuration.getStringValue(replyPrefix + "/Post").setValue(reply.getPost().getId());
745                                         configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
746                                         configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
747                                 }
748                                 /* write null ID as terminator. */
749                                 configuration.getStringValue(sonePrefix + "/Reply." + replyId + "/ID").setValue(null);
750
751                                 int friendId = 0;
752                                 for (Sone friend : sone.getFriends()) {
753                                         String friendPrefix = sonePrefix + "/Friend." + friendId++;
754                                         configuration.getStringValue(friendPrefix + "/ID").setValue(friend.getId());
755                                         configuration.getStringValue(friendPrefix + "/Key").setValue(friend.getRequestUri().toString());
756                                         configuration.getStringValue(friendPrefix + "/Name").setValue(friend.getName());
757                                 }
758                                 /* write null ID as terminator. */
759                                 configuration.getStringValue(sonePrefix + "/Friend." + friendId + "/ID").setValue(null);
760
761                                 /* write all blocked Sones. */
762                                 int blockedSoneCounter = 0;
763                                 for (String blockedSoneId : sone.getBlockedSoneIds()) {
764                                         String blockedSonePrefix = sonePrefix + "/BlockedSone." + blockedSoneCounter++;
765                                         configuration.getStringValue(blockedSonePrefix + "/ID").setValue(blockedSoneId);
766                                 }
767                                 configuration.getStringValue(sonePrefix + "/BlockedSone." + blockedSoneCounter + "/ID").setValue(null);
768
769                                 /* write all liked posts. */
770                                 int likedPostIdCounter = 0;
771                                 for (String soneLikedPostId : sone.getLikedPostIds()) {
772                                         String likedPostIdPrefix = sonePrefix + "/LikedPostId." + likedPostIdCounter++;
773                                         configuration.getStringValue(likedPostIdPrefix + "/ID").setValue(soneLikedPostId);
774                                 }
775                                 configuration.getStringValue(sonePrefix + "/LikedPostId." + likedPostIdCounter + "/ID").setValue(null);
776
777                         }
778                         /* write null ID as terminator. */
779                         configuration.getStringValue("Sone/Sone." + soneId + "/ID").setValue(null);
780
781                         /* write all known Sones. */
782                         int knownSonesCounter = 0;
783                         for (Sone knownSone : getRemoteSones()) {
784                                 String knownSonePrefix = "KnownSone." + knownSonesCounter++;
785                                 configuration.getStringValue(knownSonePrefix + "/ID").setValue(knownSone.getId());
786                                 configuration.getStringValue(knownSonePrefix + "/Name").setValue(knownSone.getName());
787                                 configuration.getStringValue(knownSonePrefix + "/Key").setValue(knownSone.getRequestUri().toString());
788                                 /* TODO - store all known stuff? */
789                         }
790                         configuration.getStringValue("KnownSone." + knownSonesCounter + "/ID").setValue(null);
791
792                 } catch (ConfigurationException ce1) {
793                         logger.log(Level.WARNING, "Could not store configuration!", ce1);
794                 }
795         }
796
797 }