Add related tracks to track interface.
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / data / DataManager.java
index b2a3337..6bec2a8 100644 (file)
@@ -20,9 +20,13 @@ package net.pterodactylus.demoscenemusic.data;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Collection;
+import java.util.EnumMap;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.Callable;
 
+import net.pterodactylus.demoscenemusic.data.Track.Relationship;
 import net.pterodactylus.util.collection.Memoizer;
 import net.pterodactylus.util.database.Database;
 import net.pterodactylus.util.database.DatabaseException;
@@ -34,37 +38,69 @@ import net.pterodactylus.util.database.OrderField;
 import net.pterodactylus.util.database.Parameter.StringParameter;
 import net.pterodactylus.util.database.Query;
 import net.pterodactylus.util.database.Query.Type;
+import net.pterodactylus.util.database.ResultProcessor;
 import net.pterodactylus.util.database.ValueField;
 import net.pterodactylus.util.database.ValueFieldWhereClause;
 
 /**
- * TODO
+ * Interface between the database and the application.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
 public class DataManager {
 
+       /** The artist object creator. */
        @SuppressWarnings("synthetic-access")
        private final ObjectCreator<Artist> artistCreator = new ArtistCreator();
+
+       /** The group object creator. */
        @SuppressWarnings("synthetic-access")
        private final ObjectCreator<Group> groupCreator = new GroupCreator();
+
+       /** The track object creator. */
        @SuppressWarnings("synthetic-access")
        private final ObjectCreator<Track> trackCreator = new TrackCreator();
+
+       /** The style object creator. */
        @SuppressWarnings("synthetic-access")
        private final ObjectCreator<Style> styleCreator = new StyleCreator();
 
+       /** The database. */
        private final Database database;
 
+       /**
+        * Creates a new data manager.
+        *
+        * @param database
+        *            The database to operate on
+        */
        public DataManager(Database database) {
                this.database = database;
        }
 
+       /**
+        * Returns all artists.
+        *
+        * @return All artists
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
        public Collection<Artist> getAllArtists() throws DatabaseException {
                Query query = new Query(Type.SELECT, "ARTISTS");
                query.addField(new Field("ARTISTS.*"));
                return database.getMultiple(query, artistCreator);
        }
 
+       /**
+        * Returns the artist with the given ID.
+        *
+        * @param id
+        *            The ID of the artist
+        * @return The artist with the given ID, or {@code null} if there is no
+        *         artist with the given ID
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
        public Artist getArtistById(String id) throws DatabaseException {
                Query query = new Query(Type.SELECT, "ARTISTS");
                query.addField(new Field("ARTISTS.*"));
@@ -72,6 +108,15 @@ public class DataManager {
                return database.getSingle(query, artistCreator);
        }
 
+       /**
+        * Returns all artists that belong to the group with the given ID.
+        *
+        * @param groupId
+        *            The ID of the group
+        * @return All artists belonging to the given group
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
        public Collection<Artist> getArtistsByGroup(String groupId) throws DatabaseException {
                Query query = new Query(Type.SELECT, "ARTISTS");
                query.addField(new Field("ARTISTS.*"));
@@ -80,6 +125,15 @@ public class DataManager {
                return database.getMultiple(query, artistCreator);
        }
 
+       /**
+        * Returns all artists involved in the track with the given ID.
+        *
+        * @param trackId
+        *            The ID of the track
+        * @return All artists involved in the track, in preferred order
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
        public List<Artist> getArtistsByTrack(String trackId) throws DatabaseException {
                Query query = new Query(Type.SELECT, "ARTISTS");
                query.addField(new Field("ARTISTS.*"));
@@ -89,6 +143,66 @@ public class DataManager {
                return database.getMultiple(query, artistCreator);
        }
 
+       /**
+        * Returns all remix artists involved in the track with the given ID.
+        *
+        * @param trackId
+        *            The ID of the track
+        * @return All remix artists involved in the track, in preferred order
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public List<Artist> getRemixArtistsByTrack(String trackId) throws DatabaseException {
+               Query query = new Query(Type.SELECT, "ARTISTS");
+               query.addField(new Field("ARTISTS.*"));
+               query.addJoin(new Join(JoinType.INNER, "TRACK_REMIX_ARTISTS", new Field("TRACK_REMIX_ARTISTS.ARTIST"), new Field("ARTISTS.ID")));
+               query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_REMIX_ARTISTS.TRACK", new StringParameter(trackId))));
+               query.addOrderField(new OrderField(new Field("TRACK_REMIX_ARTISTS.DISPLAY_ORDER")));
+               return database.getMultiple(query, artistCreator);
+       }
+
+       /**
+        * Returns all related tracks for the track with the given ID.
+        *
+        * @param trackId
+        *            The ID of the tracks
+        * @return A mapping from relationship to all tracks that match the relation
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public Map<Relationship, Collection<Track>> getRelatedTracksByTrack(String trackId) throws DatabaseException {
+               Query query = new Query(Type.SELECT, "TRACKS");
+               query.addField(new Field("TRACKS.*"));
+               query.addField(new Field("TRACK_RELATIONS.*"));
+               query.addJoin(new Join(JoinType.INNER, "TRACK_RELATIONS", new Field("TRACK_RELATIONS.TRACK"), new Field("TRACK_RELATIONS.RELATED_TRACK")));
+               query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_RELATIONS.TRACK", new StringParameter(trackId))));
+               final Map<Relationship, Collection<Track>> relatedTracks = new EnumMap<Relationship, Collection<Track>>(Relationship.class);
+               database.process(query, new ResultProcessor() {
+
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       public void processResult(ResultSet resultSet) throws SQLException {
+                               Track track = trackCreator.createObject(resultSet);
+                               Relationship relationship = Relationship.valueOf(resultSet.getString("TRACK_RELATIONS.RELATIONSHIP"));
+                               if (!relatedTracks.containsKey(relationship)) {
+                                       relatedTracks.put(relationship, new HashSet<Track>());
+                               }
+                               relatedTracks.get(relationship).add(track);
+                       }
+               });
+               return relatedTracks;
+       }
+
+       /**
+        * Returns the track with the given ID.
+        *
+        * @param id
+        *            The ID of the track
+        * @return The track with the given ID, or {@code null} if there is no such
+        *         track
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
        public Track getTrackById(String id) throws DatabaseException {
                Query query = new Query(Type.SELECT, "TRACKS");
                query.addField(new Field("TRACKS.*"));
@@ -96,6 +210,15 @@ public class DataManager {
                return database.getSingle(query, trackCreator);
        }
 
+       /**
+        * Returns all tracks by the artist with the given ID.
+        *
+        * @param artistId
+        *            The ID of the artist
+        * @return All tracks by the given artist
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
        public Collection<Track> getTracksByArtist(String artistId) throws DatabaseException {
                Query query = new Query(Type.SELECT, "TRACKS");
                query.addField(new Field("TRACKS.*"));
@@ -104,6 +227,15 @@ public class DataManager {
                return database.getMultiple(query, trackCreator);
        }
 
+       /**
+        * Returns all groups the artist with the given ID belongs to.
+        *
+        * @param artistId
+        *            The ID of the artist
+        * @return All groups the artist belongs to
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
        public Collection<Group> getGroupsByArtist(String artistId) throws DatabaseException {
                Query query = new Query(Type.SELECT, "GROUPS");
                query.addField(new Field("GROUPS.*"));
@@ -112,6 +244,15 @@ public class DataManager {
                return database.getMultiple(query, groupCreator);
        }
 
+       /**
+        * Returns all styles for the track with the given ID.
+        *
+        * @param trackId
+        *            The ID of the track
+        * @return All styles for the given track
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
        public Collection<Style> getStylesByTrack(String trackId) throws DatabaseException {
                Query query = new Query(Type.SELECT, "STYLES");
                query.addField(new Field("STYLES.*"));
@@ -120,8 +261,16 @@ public class DataManager {
                return database.getMultiple(query, styleCreator);
        }
 
+       /**
+        * {@link Artist} implementation that retrieves some attributes (such as
+        * {@link #getGroups()}, and {@link #getTracks()}) from the
+        * {@link DataManager} on demand.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
        private class LazyArtist extends DefaultArtist {
 
+               /** Memoizer for the tracks by this artist. */
                private final Memoizer<Void> tracksMemoizer = new Memoizer<Void>(new Callable<Void>() {
                        @Override
                        public Void call() throws DatabaseException {
@@ -132,6 +281,7 @@ public class DataManager {
                        }
                });
 
+               /** Memoizer for the groups of this artist. */
                private final Memoizer<Void> groupsMemoizer = new Memoizer<Void>(new Callable<Void>() {
 
                        @Override
@@ -144,10 +294,20 @@ public class DataManager {
 
                });
 
+               /**
+                * Creates a new lazy artist.
+                *
+                * @param id
+                *            The ID of the artist
+                */
                public LazyArtist(String id) {
                        super(id);
                }
 
+               //
+               // DEFAULTARTIST METHODS
+               //
+
                /**
                 * {@inheritDoc}
                 */
@@ -157,6 +317,9 @@ public class DataManager {
                        return super.getGroups();
                }
 
+               /**
+                * {@inheritDoc}
+                */
                @Override
                public Collection<Track> getTracks() {
                        tracksMemoizer.get();
@@ -165,6 +328,13 @@ public class DataManager {
 
        }
 
+       /**
+        * {@link ObjectCreator} implementation that can create {@link Artist}
+        * objects. This specific class actually creates {@link LazyArtist}
+        * instances.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
        private class ArtistCreator implements ObjectCreator<Artist> {
 
                /**
@@ -177,8 +347,15 @@ public class DataManager {
 
        }
 
+       /**
+        * {@link Group} implementation that retrieves some attributes (such as
+        * {@link #getArtists()}) from the {@link DataManager} on demand.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
        private class LazyGroup extends DefaultGroup {
 
+               /** Memoizer for the artist. */
                private final Memoizer<Void> artistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
 
                        @Override
@@ -191,10 +368,20 @@ public class DataManager {
 
                });
 
+               /**
+                * Creates a new lazy group.
+                *
+                * @param id
+                *            The ID of the group
+                */
                public LazyGroup(String id) {
                        super(id);
                }
 
+               //
+               // DEFAULTGROUP METHODS
+               //
+
                /**
                 * {@inheritDoc}
                 */
@@ -206,6 +393,13 @@ public class DataManager {
 
        }
 
+       /**
+        * {@link ObjectCreator} implementation that can create {@link Group}
+        * objects. This specific implementation creates {@link LazyGroup}
+        * instances.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
        private class GroupCreator implements ObjectCreator<Group> {
 
                /**
@@ -218,8 +412,16 @@ public class DataManager {
 
        }
 
+       /**
+        * {@link Track} implementation that retrieves some attributes (such as
+        * {@link #getArtists()}, and {@link #getStyles()}) from the
+        * {@link DataManager} on demand.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
        private class LazyTrack extends DefaultTrack {
 
+               /** Memoizer for the artists. */
                private final Memoizer<Void> artistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
 
                        @Override
@@ -232,6 +434,7 @@ public class DataManager {
 
                });
 
+               /** Memoizer for the styles. */
                private final Memoizer<Void> stylesMemoizer = new Memoizer<Void>(new Callable<Void>() {
 
                        @Override
@@ -244,13 +447,45 @@ public class DataManager {
 
                });
 
+               /** Memoizer for the remix artists. */
+               private final Memoizer<Void> remixArtistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
+
+                       @Override
+                       public Void call() throws Exception {
+                               if (!hasValue("remixArtists")) {
+                                       getValue("remixArtists", List.class).set(getRemixArtistsByTrack(getId()));
+                               }
+                               return null;
+                       }
+
+               });
+
+               /** Memoizer for the related tracks. */
+               private final Memoizer<Void> relatedTracksMemoizer = new Memoizer<Void>(new Callable<Void>() {
+
+                       @Override
+                       public Void call() throws Exception {
+                               if (!hasValue("relatedTracks")) {
+                                       getValue("relatedTracks", Map.class).set(getRelatedTracksByTrack(getId()));
+                               }
+                               return null;
+                       }
+               });
+
                /**
+                * Creates a new track.
+                *
                 * @param id
+                *            The ID of the track
                 */
                public LazyTrack(String id) {
                        super(id);
                }
 
+               //
+               // DEFAULTTRACK METHODS
+               //
+
                /**
                 * {@inheritDoc}
                 */
@@ -269,8 +504,33 @@ public class DataManager {
                        return super.getStyles();
                }
 
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               public List<Artist> getRemixArtists() {
+                       remixArtistsMemoizer.get();
+                       return super.getRemixArtists();
+               }
+
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               public Map<Relationship, Collection<Track>> getRelatedTracks() {
+                       relatedTracksMemoizer.get();
+                       return super.getRelatedTracks();
+               }
+
        }
 
+       /**
+        * {@link ObjectCreator} implementation that can create {@link Track}
+        * objects. This specific implementation creates {@link LazyTrack}
+        * instances.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
        private class TrackCreator implements ObjectCreator<Track> {
 
                /**
@@ -278,11 +538,17 @@ public class DataManager {
                 */
                @Override
                public Track createObject(ResultSet resultSet) throws SQLException {
-                       return new LazyTrack(resultSet.getString("TRACKS.ID")).setName(resultSet.getString("TRACKS.NAME"));
+                       return new LazyTrack(resultSet.getString("TRACKS.ID")).setName(resultSet.getString("TRACKS.NAME")).setRemix(resultSet.getString("TRACKS.REMIX"));
                }
 
        }
 
+       /**
+        * {@link ObjectCreator} implementation that can create {@link Style}
+        * objects.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
        private class StyleCreator implements ObjectCreator<Style> {
 
                /**