+ public Set<String> parseFriends() {
+ Set<String> friends = new HashSet<>();
+ while (true) {
+ String friendId =
+ getString("/Friends/" + friends.size() + "/ID", null);
+ if (friendId == null) {
+ break;
+ }
+ friends.add(friendId);
+ }
+ return friends;
+ }
+
+ public List<Album> parseTopLevelAlbums(
+ AlbumBuilderFactory albumBuilderFactory) {
+ int albumCounter = 0;
+ while (true) {
+ String albumPrefix = "/Albums/" + albumCounter++;
+ String albumId = getString(albumPrefix + "/ID", null);
+ if (albumId == null) {
+ break;
+ }
+ String albumTitle = getString(albumPrefix + "/Title", null);
+ String albumDescription =
+ getString(albumPrefix + "/Description", null);
+ String albumParentId = getString(albumPrefix + "/Parent", null);
+ if ((albumTitle == null) || (albumDescription == null)) {
+ throw new InvalidAlbumFound();
+ }
+ Album album = albumBuilderFactory.newAlbumBuilder()
+ .withId(albumId)
+ .by(sone)
+ .build()
+ .modify()
+ .setTitle(albumTitle)
+ .setDescription(albumDescription)
+ .update();
+ if (albumParentId != null) {
+ Album parentAlbum = albums.get(albumParentId);
+ if (parentAlbum == null) {
+ throw new InvalidParentAlbumFound(albumParentId);
+ }
+ parentAlbum.addAlbum(album);
+ } else {
+ topLevelAlbums.add(album);
+ }
+ albums.put(albumId, album);
+ }
+ return topLevelAlbums;
+ }
+
+ public Map<String, Album> getAlbums() {
+ return unmodifiableMap(albums);
+ }
+
+ public void parseImages(ImageBuilderFactory imageBuilderFactory) {
+ int imageCounter = 0;
+ while (true) {
+ String imagePrefix = "/Images/" + imageCounter++;
+ String imageId = getString(imagePrefix + "/ID", null);
+ if (imageId == null) {
+ break;
+ }
+ String albumId = getString(imagePrefix + "/Album", null);
+ String key = getString(imagePrefix + "/Key", null);
+ String title = getString(imagePrefix + "/Title", null);
+ String description =
+ getString(imagePrefix + "/Description", null);
+ Long creationTime = getLong(imagePrefix + "/CreationTime", null);
+ Integer width = getInt(imagePrefix + "/Width", null);
+ Integer height = getInt(imagePrefix + "/Height", null);
+ if (albumAttributesAreInvalid(albumId, key, title, description,
+ creationTime,
+ width, height)) {
+ throw new InvalidImageFound();
+ }
+ Album album = albums.get(albumId);
+ if (album == null) {
+ throw new InvalidParentAlbumFound(albumId);
+ }
+ Image image = imageBuilderFactory.newImageBuilder()
+ .withId(imageId)
+ .build()
+ .modify()
+ .setSone(sone)
+ .setCreationTime(creationTime)
+ .setKey(key)
+ .setTitle(title)
+ .setDescription(description)
+ .setWidth(width)
+ .setHeight(height)
+ .update();
+ album.addImage(image);
+ images.put(image.getId(), image);
+ }
+ }
+
+ public Map<String, Image> getImages() {
+ return images;
+ }
+
+ private boolean albumAttributesAreInvalid(String albumId, String key,
+ String title, String description, Long creationTime,
+ Integer width, Integer height) {
+ return (albumId == null) || (key == null) || (title == null) || (
+ description == null) || (creationTime == null) || (width
+ == null) || (height == null);
+ }
+