Add function that transforms a Sone into its nice name.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / DefaultAlbum.java
1 /*
2  * Sone - MemoryAlbum.java - Copyright © 2013 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.data.impl;
19
20 import static com.google.common.base.Preconditions.checkState;
21
22 import java.util.List;
23
24 import net.pterodactylus.sone.data.Album;
25 import net.pterodactylus.sone.data.Image;
26 import net.pterodactylus.sone.data.Sone;
27 import net.pterodactylus.sone.database.AlbumBuilder;
28 import net.pterodactylus.sone.database.Database;
29 import net.pterodactylus.sone.database.ImageBuilder;
30
31 import com.google.common.base.Optional;
32
33 /**
34  * TODO
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public class DefaultAlbum extends AbstractAlbum {
39
40         private final Database database;
41         private final Sone sone; /* TODO - only store sone ID. */
42
43         protected DefaultAlbum(Database database, String id, Sone sone, String parentId) {
44                 super(id, parentId);
45                 this.database = database;
46                 this.sone = sone;
47         }
48
49         @Override
50         public Sone getSone() {
51                 return sone;
52         }
53
54         @Override
55         public List<Album> getAlbums() {
56                 return database.getAlbums(this);
57         }
58
59         @Override
60         public List<Image> getImages() {
61                 return database.getImages(this);
62         }
63
64         @Override
65         public Optional<Image> getAlbumImage() {
66                 return database.getImage(albumImage);
67         }
68
69         @Override
70         public Album getParent() {
71                 return database.getAlbum(parentId).get();
72         }
73
74         @Override
75         public AlbumBuilder newAlbumBuilder() throws IllegalStateException {
76                 return new AbstractAlbumBuilder() {
77                         @Override
78                         public Album build() throws IllegalStateException {
79                                 validate();
80                                 DefaultAlbum memoryAlbum = new DefaultAlbum(database, getId(), sone, DefaultAlbum.this.id);
81                                 database.storeAlbum(memoryAlbum);
82                                 return memoryAlbum;
83                         }
84                 };
85         }
86
87         @Override
88         public ImageBuilder newImageBuilder() throws IllegalStateException {
89                 return new AbstractImageBuilder() {
90                         @Override
91                         public Image build(Optional<ImageCreated> imageCreated) throws IllegalStateException {
92                                 validate();
93                                 DefaultImage image = new DefaultImage(database, getId(), sone, DefaultAlbum.this.id, key, getCreationTime(), width, height);
94                                 database.storeImage(image);
95                                 if (imageCreated.isPresent()) {
96                                         imageCreated.get().imageCreated(image);
97                                 }
98                                 return image;
99                         }
100                 };
101         }
102
103         @Override
104         public void moveUp() {
105                 database.moveUp(this);
106         }
107
108         @Override
109         public void moveDown() {
110                 database.moveDown(this);
111         }
112
113         @Override
114         public void remove() throws IllegalStateException {
115                 checkState(!isRoot(), "can not remove root album");
116                 for (Album album : getAlbums()) {
117                         album.remove();
118                 }
119                 for (Image image : getImages()) {
120                         image.remove();
121                 }
122                 database.removeAlbum(this);
123         }
124
125 }