Add remix name and artists to track.
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / data / DataManager.java
1 /*
2  * DemosceneMusic - DataManager.java - Copyright © 2012 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.demoscenemusic.data;
19
20 import java.sql.ResultSet;
21 import java.sql.SQLException;
22 import java.util.Collection;
23 import java.util.List;
24 import java.util.concurrent.Callable;
25
26 import net.pterodactylus.util.collection.Memoizer;
27 import net.pterodactylus.util.database.Database;
28 import net.pterodactylus.util.database.DatabaseException;
29 import net.pterodactylus.util.database.Field;
30 import net.pterodactylus.util.database.Join;
31 import net.pterodactylus.util.database.Join.JoinType;
32 import net.pterodactylus.util.database.ObjectCreator;
33 import net.pterodactylus.util.database.OrderField;
34 import net.pterodactylus.util.database.Parameter.StringParameter;
35 import net.pterodactylus.util.database.Query;
36 import net.pterodactylus.util.database.Query.Type;
37 import net.pterodactylus.util.database.ValueField;
38 import net.pterodactylus.util.database.ValueFieldWhereClause;
39
40 /**
41  * Interface between the database and the application.
42  *
43  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44  */
45 public class DataManager {
46
47         /** The artist object creator. */
48         @SuppressWarnings("synthetic-access")
49         private final ObjectCreator<Artist> artistCreator = new ArtistCreator();
50
51         /** The group object creator. */
52         @SuppressWarnings("synthetic-access")
53         private final ObjectCreator<Group> groupCreator = new GroupCreator();
54
55         /** The track object creator. */
56         @SuppressWarnings("synthetic-access")
57         private final ObjectCreator<Track> trackCreator = new TrackCreator();
58
59         /** The style object creator. */
60         @SuppressWarnings("synthetic-access")
61         private final ObjectCreator<Style> styleCreator = new StyleCreator();
62
63         /** The database. */
64         private final Database database;
65
66         /**
67          * Creates a new data manager.
68          *
69          * @param database
70          *            The database to operate on
71          */
72         public DataManager(Database database) {
73                 this.database = database;
74         }
75
76         /**
77          * Returns all artists.
78          *
79          * @return All artists
80          * @throws DatabaseException
81          *             if a database error occurs
82          */
83         public Collection<Artist> getAllArtists() throws DatabaseException {
84                 Query query = new Query(Type.SELECT, "ARTISTS");
85                 query.addField(new Field("ARTISTS.*"));
86                 return database.getMultiple(query, artistCreator);
87         }
88
89         /**
90          * Returns the artist with the given ID.
91          *
92          * @param id
93          *            The ID of the artist
94          * @return The artist with the given ID, or {@code null} if there is no
95          *         artist with the given ID
96          * @throws DatabaseException
97          *             if a database error occurs
98          */
99         public Artist getArtistById(String id) throws DatabaseException {
100                 Query query = new Query(Type.SELECT, "ARTISTS");
101                 query.addField(new Field("ARTISTS.*"));
102                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("ARTISTS.ID", new StringParameter(id))));
103                 return database.getSingle(query, artistCreator);
104         }
105
106         /**
107          * Returns all artists that belong to the group with the given ID.
108          *
109          * @param groupId
110          *            The ID of the group
111          * @return All artists belonging to the given group
112          * @throws DatabaseException
113          *             if a database error occurs
114          */
115         public Collection<Artist> getArtistsByGroup(String groupId) throws DatabaseException {
116                 Query query = new Query(Type.SELECT, "ARTISTS");
117                 query.addField(new Field("ARTISTS.*"));
118                 query.addJoin(new Join(JoinType.INNER, "GROUP_ARTISTS", new Field("ARTISTS.ID"), new Field("GROUP_ARTISTS.ARTIST")));
119                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("GROUP_ARTISTS.GROUP_", new StringParameter(groupId))));
120                 return database.getMultiple(query, artistCreator);
121         }
122
123         /**
124          * Returns all artists involved in the track with the given ID.
125          *
126          * @param trackId
127          *            The ID of the track
128          * @return All artists involved in the track, in preferred order
129          * @throws DatabaseException
130          *             if a database error occurs
131          */
132         public List<Artist> getArtistsByTrack(String trackId) throws DatabaseException {
133                 Query query = new Query(Type.SELECT, "ARTISTS");
134                 query.addField(new Field("ARTISTS.*"));
135                 query.addJoin(new Join(JoinType.INNER, "TRACK_ARTISTS", new Field("TRACK_ARTISTS.ARTIST"), new Field("ARTISTS.ID")));
136                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_ARTISTS.TRACK", new StringParameter(trackId))));
137                 query.addOrderField(new OrderField(new Field("TRACK_ARTISTS.DISPLAY_ORDER")));
138                 return database.getMultiple(query, artistCreator);
139         }
140
141         /**
142          * Returns all remix artists involved in the track with the given ID.
143          *
144          * @param trackId
145          *            The ID of the track
146          * @return All remix artists involved in the track, in preferred order
147          * @throws DatabaseException
148          *             if a database error occurs
149          */
150         public List<Artist> getRemixArtistsByTrack(String trackId) throws DatabaseException {
151                 Query query = new Query(Type.SELECT, "ARTISTS");
152                 query.addField(new Field("ARTISTS.*"));
153                 query.addJoin(new Join(JoinType.INNER, "TRACK_REMIX_ARTISTS", new Field("TRACK_REMIX_ARTISTS.ARTIST"), new Field("ARTISTS.ID")));
154                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_REMIX_ARTISTS.TRACK", new StringParameter(trackId))));
155                 query.addOrderField(new OrderField(new Field("TRACK_REMIX_ARTISTS.DISPLAY_ORDER")));
156                 return database.getMultiple(query, artistCreator);
157         }
158
159         /**
160          * Returns the track with the given ID.
161          *
162          * @param id
163          *            The ID of the track
164          * @return The track with the given ID, or {@code null} if there is no such
165          *         track
166          * @throws DatabaseException
167          *             if a database error occurs
168          */
169         public Track getTrackById(String id) throws DatabaseException {
170                 Query query = new Query(Type.SELECT, "TRACKS");
171                 query.addField(new Field("TRACKS.*"));
172                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACKS.ID", new StringParameter(id))));
173                 return database.getSingle(query, trackCreator);
174         }
175
176         /**
177          * Returns all tracks by the artist with the given ID.
178          *
179          * @param artistId
180          *            The ID of the artist
181          * @return All tracks by the given artist
182          * @throws DatabaseException
183          *             if a database error occurs
184          */
185         public Collection<Track> getTracksByArtist(String artistId) throws DatabaseException {
186                 Query query = new Query(Type.SELECT, "TRACKS");
187                 query.addField(new Field("TRACKS.*"));
188                 query.addJoin(new Join(JoinType.INNER, "TRACK_ARTISTS", new Field("TRACKS.ID"), new Field("TRACK_ARTISTS.TRACK")));
189                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_ARTISTS.ARTIST", new StringParameter(artistId))));
190                 return database.getMultiple(query, trackCreator);
191         }
192
193         /**
194          * Returns all groups the artist with the given ID belongs to.
195          *
196          * @param artistId
197          *            The ID of the artist
198          * @return All groups the artist belongs to
199          * @throws DatabaseException
200          *             if a database error occurs
201          */
202         public Collection<Group> getGroupsByArtist(String artistId) throws DatabaseException {
203                 Query query = new Query(Type.SELECT, "GROUPS");
204                 query.addField(new Field("GROUPS.*"));
205                 query.addJoin(new Join(JoinType.INNER, "GROUP_ARTISTS", new Field("GROUPS.ID"), new Field("GROUP_ARTISTS.GROUP_")));
206                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("GROUP_ARTISTS.ARTIST", new StringParameter(artistId))));
207                 return database.getMultiple(query, groupCreator);
208         }
209
210         /**
211          * Returns all styles for the track with the given ID.
212          *
213          * @param trackId
214          *            The ID of the track
215          * @return All styles for the given track
216          * @throws DatabaseException
217          *             if a database error occurs
218          */
219         public Collection<Style> getStylesByTrack(String trackId) throws DatabaseException {
220                 Query query = new Query(Type.SELECT, "STYLES");
221                 query.addField(new Field("STYLES.*"));
222                 query.addJoin(new Join(JoinType.INNER, "TRACK_STYLES", new Field("STYLES.ID"), new Field("TRACK_STYLES.STYLE")));
223                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_STYLES.TRACK", new StringParameter(trackId))));
224                 return database.getMultiple(query, styleCreator);
225         }
226
227         /**
228          * {@link Artist} implementation that retrieves some attributes (such as
229          * {@link #getGroups()}, and {@link #getTracks()}) from the
230          * {@link DataManager} on demand.
231          *
232          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
233          */
234         private class LazyArtist extends DefaultArtist {
235
236                 /** Memoizer for the tracks by this artist. */
237                 private final Memoizer<Void> tracksMemoizer = new Memoizer<Void>(new Callable<Void>() {
238                         @Override
239                         public Void call() throws DatabaseException {
240                                 if (!hasValue("tracks")) {
241                                         getValue("tracks", Collection.class).set(getTracksByArtist(getId()));
242                                 }
243                                 return null;
244                         }
245                 });
246
247                 /** Memoizer for the groups of this artist. */
248                 private final Memoizer<Void> groupsMemoizer = new Memoizer<Void>(new Callable<Void>() {
249
250                         @Override
251                         public Void call() throws Exception {
252                                 if (!hasValue("groups")) {
253                                         getValue("groups", Collection.class).set(getGroupsByArtist(getId()));
254                                 }
255                                 return null;
256                         }
257
258                 });
259
260                 /**
261                  * Creates a new lazy artist.
262                  *
263                  * @param id
264                  *            The ID of the artist
265                  */
266                 public LazyArtist(String id) {
267                         super(id);
268                 }
269
270                 //
271                 // DEFAULTARTIST METHODS
272                 //
273
274                 /**
275                  * {@inheritDoc}
276                  */
277                 @Override
278                 public Collection<Group> getGroups() {
279                         groupsMemoizer.get();
280                         return super.getGroups();
281                 }
282
283                 /**
284                  * {@inheritDoc}
285                  */
286                 @Override
287                 public Collection<Track> getTracks() {
288                         tracksMemoizer.get();
289                         return super.getTracks();
290                 }
291
292         }
293
294         /**
295          * {@link ObjectCreator} implementation that can create {@link Artist}
296          * objects. This specific class actually creates {@link LazyArtist}
297          * instances.
298          *
299          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
300          */
301         private class ArtistCreator implements ObjectCreator<Artist> {
302
303                 /**
304                  * {@inheritDoc}
305                  */
306                 @Override
307                 public Artist createObject(ResultSet resultSet) throws SQLException {
308                         return new LazyArtist(resultSet.getString("ARTISTS.ID")).setName(resultSet.getString("ARTISTS.NAME"));
309                 }
310
311         }
312
313         /**
314          * {@link Group} implementation that retrieves some attributes (such as
315          * {@link #getArtists()}) from the {@link DataManager} on demand.
316          *
317          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
318          */
319         private class LazyGroup extends DefaultGroup {
320
321                 /** Memoizer for the artist. */
322                 private final Memoizer<Void> artistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
323
324                         @Override
325                         public Void call() throws Exception {
326                                 if (!hasValue("artists")) {
327                                         getValue("artists", Collection.class).set(getArtistsByGroup(getId()));
328                                 }
329                                 return null;
330                         }
331
332                 });
333
334                 /**
335                  * Creates a new lazy group.
336                  *
337                  * @param id
338                  *            The ID of the group
339                  */
340                 public LazyGroup(String id) {
341                         super(id);
342                 }
343
344                 //
345                 // DEFAULTGROUP METHODS
346                 //
347
348                 /**
349                  * {@inheritDoc}
350                  */
351                 @Override
352                 public Collection<Artist> getArtists() {
353                         artistsMemoizer.get();
354                         return super.getArtists();
355                 }
356
357         }
358
359         /**
360          * {@link ObjectCreator} implementation that can create {@link Group}
361          * objects. This specific implementation creates {@link LazyGroup}
362          * instances.
363          *
364          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
365          */
366         private class GroupCreator implements ObjectCreator<Group> {
367
368                 /**
369                  * {@inheritDoc}
370                  */
371                 @Override
372                 public Group createObject(ResultSet resultSet) throws SQLException {
373                         return new LazyGroup(resultSet.getString("GROUPS.ID")).setName(resultSet.getString("GROUPS.NAME")).setUrl(resultSet.getString("GROUPS.URL"));
374                 }
375
376         }
377
378         /**
379          * {@link Track} implementation that retrieves some attributes (such as
380          * {@link #getArtists()}, and {@link #getStyles()}) from the
381          * {@link DataManager} on demand.
382          *
383          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
384          */
385         private class LazyTrack extends DefaultTrack {
386
387                 /** Memoizer for the artists. */
388                 private final Memoizer<Void> artistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
389
390                         @Override
391                         public Void call() throws Exception {
392                                 if (!hasValue("artists")) {
393                                         getValue("artists", List.class).set(getArtistsByTrack(getId()));
394                                 }
395                                 return null;
396                         }
397
398                 });
399
400                 /** Memoizer for the styles. */
401                 private final Memoizer<Void> stylesMemoizer = new Memoizer<Void>(new Callable<Void>() {
402
403                         @Override
404                         public Void call() throws Exception {
405                                 if (!hasValue("styles")) {
406                                         getValue("styles", Collection.class).set(getStylesByTrack(getId()));
407                                 }
408                                 return null;
409                         }
410
411                 });
412
413                 /** Memoizer for the remix artists. */
414                 private final Memoizer<Void> remixArtistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
415
416                         @Override
417                         public Void call() throws Exception {
418                                 if (!hasValue("remixArtists")) {
419                                         getValue("remixArtists", List.class).set(getRemixArtistsByTrack(getId()));
420                                 }
421                                 return null;
422                         }
423
424                 });
425
426                 /**
427                  * Creates a new track.
428                  *
429                  * @param id
430                  *            The ID of the track
431                  */
432                 public LazyTrack(String id) {
433                         super(id);
434                 }
435
436                 //
437                 // DEFAULTTRACK METHODS
438                 //
439
440                 /**
441                  * {@inheritDoc}
442                  */
443                 @Override
444                 public List<Artist> getArtists() {
445                         artistsMemoizer.get();
446                         return super.getArtists();
447                 }
448
449                 /**
450                  * {@inheritDoc}
451                  */
452                 @Override
453                 public Collection<Style> getStyles() {
454                         stylesMemoizer.get();
455                         return super.getStyles();
456                 }
457
458                 /**
459                  * {@inheritDoc}
460                  */
461                 @Override
462                 public List<Artist> getRemixArtists() {
463                         remixArtistsMemoizer.get();
464                         return super.getRemixArtists();
465                 }
466
467         }
468
469         /**
470          * {@link ObjectCreator} implementation that can create {@link Track}
471          * objects. This specific implementation creates {@link LazyTrack}
472          * instances.
473          *
474          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
475          */
476         private class TrackCreator implements ObjectCreator<Track> {
477
478                 /**
479                  * {@inheritDoc}
480                  */
481                 @Override
482                 public Track createObject(ResultSet resultSet) throws SQLException {
483                         return new LazyTrack(resultSet.getString("TRACKS.ID")).setName(resultSet.getString("TRACKS.NAME")).setRemix(resultSet.getString("TRACKS.REMIX"));
484                 }
485
486         }
487
488         /**
489          * {@link ObjectCreator} implementation that can create {@link Style}
490          * objects.
491          *
492          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
493          */
494         private class StyleCreator implements ObjectCreator<Style> {
495
496                 /**
497                  * {@inheritDoc}
498                  */
499                 @Override
500                 public Style createObject(ResultSet resultSet) throws SQLException {
501                         return new DefaultStyle(resultSet.getString("STYLES.ID")).setName(resultSet.getString("STYLES.NAME"));
502                 }
503
504         }
505
506 }