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.EnumMap;
24 import java.util.HashSet;
25 import java.util.List;
27 import java.util.concurrent.Callable;
29 import net.pterodactylus.demoscenemusic.data.Track.Relationship;
30 import net.pterodactylus.util.collection.Memoizer;
31 import net.pterodactylus.util.database.Database;
32 import net.pterodactylus.util.database.DatabaseException;
33 import net.pterodactylus.util.database.Field;
34 import net.pterodactylus.util.database.Join;
35 import net.pterodactylus.util.database.Join.JoinType;
36 import net.pterodactylus.util.database.ObjectCreator;
37 import net.pterodactylus.util.database.OrderField;
38 import net.pterodactylus.util.database.Parameter.StringParameter;
39 import net.pterodactylus.util.database.Query;
40 import net.pterodactylus.util.database.Query.Type;
41 import net.pterodactylus.util.database.ResultProcessor;
42 import net.pterodactylus.util.database.ValueField;
43 import net.pterodactylus.util.database.ValueFieldWhereClause;
46 * Interface between the database and the application.
48 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
50 public class DataManager {
52 /** The artist object creator. */
53 @SuppressWarnings("synthetic-access")
54 private final ObjectCreator<Artist> artistCreator = new ArtistCreator();
56 /** The group object creator. */
57 @SuppressWarnings("synthetic-access")
58 private final ObjectCreator<Group> groupCreator = new GroupCreator();
60 /** The track object creator. */
61 @SuppressWarnings("synthetic-access")
62 private final ObjectCreator<Track> trackCreator = new TrackCreator();
64 /** The style object creator. */
65 @SuppressWarnings("synthetic-access")
66 private final ObjectCreator<Style> styleCreator = new StyleCreator();
69 private final Database database;
72 * Creates a new data manager.
75 * The database to operate on
77 public DataManager(Database database) {
78 this.database = database;
82 * Returns all artists.
85 * @throws DatabaseException
86 * if a database error occurs
88 public Collection<Artist> getAllArtists() throws DatabaseException {
89 Query query = new Query(Type.SELECT, "ARTISTS");
90 query.addField(new Field("ARTISTS.*"));
91 return database.getMultiple(query, artistCreator);
95 * Returns the artist with the given ID.
98 * The ID of the artist
99 * @return The artist with the given ID, or {@code null} if there is no
100 * artist with the given ID
101 * @throws DatabaseException
102 * if a database error occurs
104 public Artist getArtistById(String id) throws DatabaseException {
105 Query query = new Query(Type.SELECT, "ARTISTS");
106 query.addField(new Field("ARTISTS.*"));
107 query.addWhereClause(new ValueFieldWhereClause(new ValueField("ARTISTS.ID", new StringParameter(id))));
108 return database.getSingle(query, artistCreator);
112 * Returns all artists that belong to the group with the given ID.
115 * The ID of the group
116 * @return All artists belonging to the given group
117 * @throws DatabaseException
118 * if a database error occurs
120 public Collection<Artist> getArtistsByGroup(String groupId) throws DatabaseException {
121 Query query = new Query(Type.SELECT, "ARTISTS");
122 query.addField(new Field("ARTISTS.*"));
123 query.addJoin(new Join(JoinType.INNER, "GROUP_ARTISTS", new Field("ARTISTS.ID"), new Field("GROUP_ARTISTS.ARTIST")));
124 query.addWhereClause(new ValueFieldWhereClause(new ValueField("GROUP_ARTISTS.GROUP_", new StringParameter(groupId))));
125 return database.getMultiple(query, artistCreator);
129 * Returns all artists involved in the track with the given ID.
132 * The ID of the track
133 * @return All artists involved in the track, in preferred order
134 * @throws DatabaseException
135 * if a database error occurs
137 public List<Artist> getArtistsByTrack(String trackId) throws DatabaseException {
138 Query query = new Query(Type.SELECT, "ARTISTS");
139 query.addField(new Field("ARTISTS.*"));
140 query.addJoin(new Join(JoinType.INNER, "TRACK_ARTISTS", new Field("TRACK_ARTISTS.ARTIST"), new Field("ARTISTS.ID")));
141 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_ARTISTS.TRACK", new StringParameter(trackId))));
142 query.addOrderField(new OrderField(new Field("TRACK_ARTISTS.DISPLAY_ORDER")));
143 return database.getMultiple(query, artistCreator);
147 * Returns all remix artists involved in the track with the given ID.
150 * The ID of the track
151 * @return All remix artists involved in the track, in preferred order
152 * @throws DatabaseException
153 * if a database error occurs
155 public List<Artist> getRemixArtistsByTrack(String trackId) throws DatabaseException {
156 Query query = new Query(Type.SELECT, "ARTISTS");
157 query.addField(new Field("ARTISTS.*"));
158 query.addJoin(new Join(JoinType.INNER, "TRACK_REMIX_ARTISTS", new Field("TRACK_REMIX_ARTISTS.ARTIST"), new Field("ARTISTS.ID")));
159 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_REMIX_ARTISTS.TRACK", new StringParameter(trackId))));
160 query.addOrderField(new OrderField(new Field("TRACK_REMIX_ARTISTS.DISPLAY_ORDER")));
161 return database.getMultiple(query, artistCreator);
165 * Returns all related tracks for the track with the given ID.
168 * The ID of the tracks
169 * @return A mapping from relationship to all tracks that match the relation
170 * @throws DatabaseException
171 * if a database error occurs
173 public Map<Relationship, Collection<Track>> getRelatedTracksByTrack(String trackId) throws DatabaseException {
174 Query query = new Query(Type.SELECT, "TRACKS");
175 query.addField(new Field("TRACKS.*"));
176 query.addField(new Field("TRACK_RELATIONS.*"));
177 query.addJoin(new Join(JoinType.INNER, "TRACK_RELATIONS", new Field("TRACK_RELATIONS.TRACK"), new Field("TRACK_RELATIONS.RELATED_TRACK")));
178 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_RELATIONS.TRACK", new StringParameter(trackId))));
179 final Map<Relationship, Collection<Track>> relatedTracks = new EnumMap<Relationship, Collection<Track>>(Relationship.class);
180 database.process(query, new ResultProcessor() {
183 @SuppressWarnings("synthetic-access")
184 public void processResult(ResultSet resultSet) throws SQLException {
185 Track track = trackCreator.createObject(resultSet);
186 Relationship relationship = Relationship.valueOf(resultSet.getString("TRACK_RELATIONS.RELATIONSHIP"));
187 if (!relatedTracks.containsKey(relationship)) {
188 relatedTracks.put(relationship, new HashSet<Track>());
190 relatedTracks.get(relationship).add(track);
193 return relatedTracks;
197 * Returns the track with the given ID.
200 * The ID of the track
201 * @return The track with the given ID, or {@code null} if there is no such
203 * @throws DatabaseException
204 * if a database error occurs
206 public Track getTrackById(String id) throws DatabaseException {
207 Query query = new Query(Type.SELECT, "TRACKS");
208 query.addField(new Field("TRACKS.*"));
209 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACKS.ID", new StringParameter(id))));
210 return database.getSingle(query, trackCreator);
214 * Returns all tracks by the artist with the given ID.
217 * The ID of the artist
218 * @return All tracks by the given artist
219 * @throws DatabaseException
220 * if a database error occurs
222 public Collection<Track> getTracksByArtist(String artistId) throws DatabaseException {
223 Query query = new Query(Type.SELECT, "TRACKS");
224 query.addField(new Field("TRACKS.*"));
225 query.addJoin(new Join(JoinType.INNER, "TRACK_ARTISTS", new Field("TRACKS.ID"), new Field("TRACK_ARTISTS.TRACK")));
226 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_ARTISTS.ARTIST", new StringParameter(artistId))));
227 return database.getMultiple(query, trackCreator);
231 * Returns all groups the artist with the given ID belongs to.
234 * The ID of the artist
235 * @return All groups the artist belongs to
236 * @throws DatabaseException
237 * if a database error occurs
239 public Collection<Group> getGroupsByArtist(String artistId) throws DatabaseException {
240 Query query = new Query(Type.SELECT, "GROUPS");
241 query.addField(new Field("GROUPS.*"));
242 query.addJoin(new Join(JoinType.INNER, "GROUP_ARTISTS", new Field("GROUPS.ID"), new Field("GROUP_ARTISTS.GROUP_")));
243 query.addWhereClause(new ValueFieldWhereClause(new ValueField("GROUP_ARTISTS.ARTIST", new StringParameter(artistId))));
244 return database.getMultiple(query, groupCreator);
248 * Returns all styles for the track with the given ID.
251 * The ID of the track
252 * @return All styles for the given track
253 * @throws DatabaseException
254 * if a database error occurs
256 public Collection<Style> getStylesByTrack(String trackId) throws DatabaseException {
257 Query query = new Query(Type.SELECT, "STYLES");
258 query.addField(new Field("STYLES.*"));
259 query.addJoin(new Join(JoinType.INNER, "TRACK_STYLES", new Field("STYLES.ID"), new Field("TRACK_STYLES.STYLE")));
260 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_STYLES.TRACK", new StringParameter(trackId))));
261 return database.getMultiple(query, styleCreator);
265 * {@link Artist} implementation that retrieves some attributes (such as
266 * {@link #getGroups()}, and {@link #getTracks()}) from the
267 * {@link DataManager} on demand.
269 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
271 private class LazyArtist extends DefaultArtist {
273 /** Memoizer for the tracks by this artist. */
274 private final Memoizer<Void> tracksMemoizer = new Memoizer<Void>(new Callable<Void>() {
276 public Void call() throws DatabaseException {
277 if (!hasValue("tracks")) {
278 getValue("tracks", Collection.class).set(getTracksByArtist(getId()));
284 /** Memoizer for the groups of this artist. */
285 private final Memoizer<Void> groupsMemoizer = new Memoizer<Void>(new Callable<Void>() {
288 public Void call() throws Exception {
289 if (!hasValue("groups")) {
290 getValue("groups", Collection.class).set(getGroupsByArtist(getId()));
298 * Creates a new lazy artist.
301 * The ID of the artist
303 public LazyArtist(String id) {
308 // DEFAULTARTIST METHODS
315 public Collection<Group> getGroups() {
316 groupsMemoizer.get();
317 return super.getGroups();
324 public Collection<Track> getTracks() {
325 tracksMemoizer.get();
326 return super.getTracks();
332 * {@link ObjectCreator} implementation that can create {@link Artist}
333 * objects. This specific class actually creates {@link LazyArtist}
336 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
338 private class ArtistCreator implements ObjectCreator<Artist> {
344 public Artist createObject(ResultSet resultSet) throws SQLException {
345 return new LazyArtist(resultSet.getString("ARTISTS.ID")).setName(resultSet.getString("ARTISTS.NAME"));
351 * {@link Group} implementation that retrieves some attributes (such as
352 * {@link #getArtists()}) from the {@link DataManager} on demand.
354 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
356 private class LazyGroup extends DefaultGroup {
358 /** Memoizer for the artist. */
359 private final Memoizer<Void> artistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
362 public Void call() throws Exception {
363 if (!hasValue("artists")) {
364 getValue("artists", Collection.class).set(getArtistsByGroup(getId()));
372 * Creates a new lazy group.
375 * The ID of the group
377 public LazyGroup(String id) {
382 // DEFAULTGROUP METHODS
389 public Collection<Artist> getArtists() {
390 artistsMemoizer.get();
391 return super.getArtists();
397 * {@link ObjectCreator} implementation that can create {@link Group}
398 * objects. This specific implementation creates {@link LazyGroup}
401 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
403 private class GroupCreator implements ObjectCreator<Group> {
409 public Group createObject(ResultSet resultSet) throws SQLException {
410 return new LazyGroup(resultSet.getString("GROUPS.ID")).setName(resultSet.getString("GROUPS.NAME")).setUrl(resultSet.getString("GROUPS.URL"));
416 * {@link Track} implementation that retrieves some attributes (such as
417 * {@link #getArtists()}, and {@link #getStyles()}) from the
418 * {@link DataManager} on demand.
420 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
422 private class LazyTrack extends DefaultTrack {
424 /** Memoizer for the artists. */
425 private final Memoizer<Void> artistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
428 public Void call() throws Exception {
429 if (!hasValue("artists")) {
430 getValue("artists", List.class).set(getArtistsByTrack(getId()));
437 /** Memoizer for the styles. */
438 private final Memoizer<Void> stylesMemoizer = new Memoizer<Void>(new Callable<Void>() {
441 public Void call() throws Exception {
442 if (!hasValue("styles")) {
443 getValue("styles", Collection.class).set(getStylesByTrack(getId()));
450 /** Memoizer for the remix artists. */
451 private final Memoizer<Void> remixArtistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
454 public Void call() throws Exception {
455 if (!hasValue("remixArtists")) {
456 getValue("remixArtists", List.class).set(getRemixArtistsByTrack(getId()));
463 /** Memoizer for the related tracks. */
464 private final Memoizer<Void> relatedTracksMemoizer = new Memoizer<Void>(new Callable<Void>() {
467 public Void call() throws Exception {
468 if (!hasValue("relatedTracks")) {
469 getValue("relatedTracks", Map.class).set(getRelatedTracksByTrack(getId()));
476 * Creates a new track.
479 * The ID of the track
481 public LazyTrack(String id) {
486 // DEFAULTTRACK METHODS
493 public List<Artist> getArtists() {
494 artistsMemoizer.get();
495 return super.getArtists();
502 public Collection<Style> getStyles() {
503 stylesMemoizer.get();
504 return super.getStyles();
511 public List<Artist> getRemixArtists() {
512 remixArtistsMemoizer.get();
513 return super.getRemixArtists();
520 public Map<Relationship, Collection<Track>> getRelatedTracks() {
521 relatedTracksMemoizer.get();
522 return super.getRelatedTracks();
528 * {@link ObjectCreator} implementation that can create {@link Track}
529 * objects. This specific implementation creates {@link LazyTrack}
532 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
534 private class TrackCreator implements ObjectCreator<Track> {
540 public Track createObject(ResultSet resultSet) throws SQLException {
541 return new LazyTrack(resultSet.getString("TRACKS.ID")).setName(resultSet.getString("TRACKS.NAME")).setRemix(resultSet.getString("TRACKS.REMIX"));
547 * {@link ObjectCreator} implementation that can create {@link Style}
550 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
552 private class StyleCreator implements ObjectCreator<Style> {
558 public Style createObject(ResultSet resultSet) throws SQLException {
559 return new DefaultStyle(resultSet.getString("STYLES.ID")).setName(resultSet.getString("STYLES.NAME"));