2 * DemosceneMusic - DataManager.java - Copyright © 2012 David Roden
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.
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.
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/>.
18 package net.pterodactylus.demoscenemusic.data;
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;
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;
41 * Interface between the database and the application.
43 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public class DataManager {
47 /** The artist object creator. */
48 @SuppressWarnings("synthetic-access")
49 private final ObjectCreator<Artist> artistCreator = new ArtistCreator();
51 /** The group object creator. */
52 @SuppressWarnings("synthetic-access")
53 private final ObjectCreator<Group> groupCreator = new GroupCreator();
55 /** The track object creator. */
56 @SuppressWarnings("synthetic-access")
57 private final ObjectCreator<Track> trackCreator = new TrackCreator();
59 /** The style object creator. */
60 @SuppressWarnings("synthetic-access")
61 private final ObjectCreator<Style> styleCreator = new StyleCreator();
64 private final Database database;
67 * Creates a new data manager.
70 * The database to operate on
72 public DataManager(Database database) {
73 this.database = database;
77 * Returns all artists.
80 * @throws DatabaseException
81 * if a database error occurs
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);
90 * Returns the artist with the given 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
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);
107 * Returns all artists that belong to the group with the given ID.
110 * The ID of the group
111 * @return All artists belonging to the given group
112 * @throws DatabaseException
113 * if a database error occurs
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);
124 * Returns all artists involved in the track with the given ID.
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
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);
142 * Returns all remix artists involved in the track with the given ID.
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
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);
160 * Returns the track with the given ID.
163 * The ID of the track
164 * @return The track with the given ID, or {@code null} if there is no such
166 * @throws DatabaseException
167 * if a database error occurs
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);
177 * Returns all tracks by the artist with the given ID.
180 * The ID of the artist
181 * @return All tracks by the given artist
182 * @throws DatabaseException
183 * if a database error occurs
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);
194 * Returns all groups the artist with the given ID belongs to.
197 * The ID of the artist
198 * @return All groups the artist belongs to
199 * @throws DatabaseException
200 * if a database error occurs
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);
211 * Returns all styles for the track with the given ID.
214 * The ID of the track
215 * @return All styles for the given track
216 * @throws DatabaseException
217 * if a database error occurs
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);
228 * {@link Artist} implementation that retrieves some attributes (such as
229 * {@link #getGroups()}, and {@link #getTracks()}) from the
230 * {@link DataManager} on demand.
232 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
234 private class LazyArtist extends DefaultArtist {
236 /** Memoizer for the tracks by this artist. */
237 private final Memoizer<Void> tracksMemoizer = new Memoizer<Void>(new Callable<Void>() {
239 public Void call() throws DatabaseException {
240 if (!hasValue("tracks")) {
241 getValue("tracks", Collection.class).set(getTracksByArtist(getId()));
247 /** Memoizer for the groups of this artist. */
248 private final Memoizer<Void> groupsMemoizer = new Memoizer<Void>(new Callable<Void>() {
251 public Void call() throws Exception {
252 if (!hasValue("groups")) {
253 getValue("groups", Collection.class).set(getGroupsByArtist(getId()));
261 * Creates a new lazy artist.
264 * The ID of the artist
266 public LazyArtist(String id) {
271 // DEFAULTARTIST METHODS
278 public Collection<Group> getGroups() {
279 groupsMemoizer.get();
280 return super.getGroups();
287 public Collection<Track> getTracks() {
288 tracksMemoizer.get();
289 return super.getTracks();
295 * {@link ObjectCreator} implementation that can create {@link Artist}
296 * objects. This specific class actually creates {@link LazyArtist}
299 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
301 private class ArtistCreator implements ObjectCreator<Artist> {
307 public Artist createObject(ResultSet resultSet) throws SQLException {
308 return new LazyArtist(resultSet.getString("ARTISTS.ID")).setName(resultSet.getString("ARTISTS.NAME"));
314 * {@link Group} implementation that retrieves some attributes (such as
315 * {@link #getArtists()}) from the {@link DataManager} on demand.
317 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
319 private class LazyGroup extends DefaultGroup {
321 /** Memoizer for the artist. */
322 private final Memoizer<Void> artistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
325 public Void call() throws Exception {
326 if (!hasValue("artists")) {
327 getValue("artists", Collection.class).set(getArtistsByGroup(getId()));
335 * Creates a new lazy group.
338 * The ID of the group
340 public LazyGroup(String id) {
345 // DEFAULTGROUP METHODS
352 public Collection<Artist> getArtists() {
353 artistsMemoizer.get();
354 return super.getArtists();
360 * {@link ObjectCreator} implementation that can create {@link Group}
361 * objects. This specific implementation creates {@link LazyGroup}
364 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
366 private class GroupCreator implements ObjectCreator<Group> {
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"));
379 * {@link Track} implementation that retrieves some attributes (such as
380 * {@link #getArtists()}, and {@link #getStyles()}) from the
381 * {@link DataManager} on demand.
383 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
385 private class LazyTrack extends DefaultTrack {
387 /** Memoizer for the artists. */
388 private final Memoizer<Void> artistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
391 public Void call() throws Exception {
392 if (!hasValue("artists")) {
393 getValue("artists", List.class).set(getArtistsByTrack(getId()));
400 /** Memoizer for the styles. */
401 private final Memoizer<Void> stylesMemoizer = new Memoizer<Void>(new Callable<Void>() {
404 public Void call() throws Exception {
405 if (!hasValue("styles")) {
406 getValue("styles", Collection.class).set(getStylesByTrack(getId()));
413 /** Memoizer for the remix artists. */
414 private final Memoizer<Void> remixArtistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
417 public Void call() throws Exception {
418 if (!hasValue("remixArtists")) {
419 getValue("remixArtists", List.class).set(getRemixArtistsByTrack(getId()));
427 * Creates a new track.
430 * The ID of the track
432 public LazyTrack(String id) {
437 // DEFAULTTRACK METHODS
444 public List<Artist> getArtists() {
445 artistsMemoizer.get();
446 return super.getArtists();
453 public Collection<Style> getStyles() {
454 stylesMemoizer.get();
455 return super.getStyles();
462 public List<Artist> getRemixArtists() {
463 remixArtistsMemoizer.get();
464 return super.getRemixArtists();
470 * {@link ObjectCreator} implementation that can create {@link Track}
471 * objects. This specific implementation creates {@link LazyTrack}
474 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
476 private class TrackCreator implements ObjectCreator<Track> {
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"));
489 * {@link ObjectCreator} implementation that can create {@link Style}
492 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
494 private class StyleCreator implements ObjectCreator<Style> {
500 public Style createObject(ResultSet resultSet) throws SQLException {
501 return new DefaultStyle(resultSet.getString("STYLES.ID")).setName(resultSet.getString("STYLES.NAME"));