Add “image created” callback to ImageBuilder.
[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         private final String parentId;
43
44         protected DefaultAlbum(Database database, String id, Sone sone, String parentId) {
45                 super(id);
46                 this.database = database;
47                 this.sone = sone;
48                 this.parentId = parentId;
49         }
50
51         @Override
52         public Sone getSone() {
53                 return sone;
54         }
55
56         @Override
57         public List<Album> getAlbums() {
58                 return database.getAlbums(this);
59         }
60
61         @Override
62         public List<Image> getImages() {
63                 return database.getImages(this);
64         }
65
66         @Override
67         public Optional<Image> getAlbumImage() {
68                 return database.getImage(albumImage);
69         }
70
71         @Override
72         public Album getParent() {
73                 return database.getAlbum(parentId).get();
74         }
75
76         @Override
77         public AlbumBuilder newAlbumBuilder() throws IllegalStateException {
78                 return new AbstractAlbumBuilder() {
79                         @Override
80                         public Album build() throws IllegalStateException {
81                                 validate();
82                                 DefaultAlbum memoryAlbum = new DefaultAlbum(database, getId(), sone, DefaultAlbum.this.id);
83                                 database.storeAlbum(memoryAlbum);
84                                 return memoryAlbum;
85                         }
86                 };
87         }
88
89         @Override
90         public ImageBuilder newImageBuilder() throws IllegalStateException {
91                 return new AbstractImageBuilder() {
92                         @Override
93                         public Image build(Optional<ImageCreated> imageCreated) throws IllegalStateException {
94                                 validate();
95                                 DefaultImage image = new DefaultImage(database, getId(), sone, DefaultAlbum.this.id, key, getCreationTime(), width, height);
96                                 database.storeImage(image);
97                                 if (imageCreated.isPresent()) {
98                                         imageCreated.get().imageCreated(image);
99                                 }
100                                 return image;
101                         }
102                 };
103         }
104
105         @Override
106         public void moveUp() {
107                 database.moveUp(this);
108         }
109
110         @Override
111         public void moveDown() {
112                 database.moveDown(this);
113         }
114
115         @Override
116         public void remove() throws IllegalStateException {
117                 checkState(!isRoot(), "can not remove root album");
118                 for (Album album : getAlbums()) {
119                         album.remove();
120                 }
121                 for (Image image : getImages()) {
122                         image.remove();
123                 }
124                 database.removeAlbum(this);
125         }
126
127 }