Add related tracks to track interface.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 26 Apr 2012 09:22:14 +0000 (11:22 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 26 Apr 2012 09:22:14 +0000 (11:22 +0200)
src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java
src/main/java/net/pterodactylus/demoscenemusic/data/DefaultTrack.java
src/main/java/net/pterodactylus/demoscenemusic/data/Track.java

index ac146e1..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,6 +38,7 @@ 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;
 
@@ -157,6 +162,38 @@ public class DataManager {
        }
 
        /**
+        * 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
@@ -423,6 +460,18 @@ public class DataManager {
 
                });
 
+               /** 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.
                 *
@@ -464,6 +513,15 @@ public class DataManager {
                        return super.getRemixArtists();
                }
 
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               public Map<Relationship, Collection<Track>> getRelatedTracks() {
+                       relatedTracksMemoizer.get();
+                       return super.getRelatedTracks();
+               }
+
        }
 
        /**
index fafb8a2..9901fcb 100644 (file)
@@ -19,6 +19,7 @@ package net.pterodactylus.demoscenemusic.data;
 
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 
 /**
  * Default implementation of a track data container.
@@ -129,4 +130,22 @@ public class DefaultTrack extends DefaultBase implements Track {
                return this;
        }
 
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       @SuppressWarnings("unchecked")
+       public Map<Relationship, Collection<Track>> getRelatedTracks() {
+               return getValue("relatedTracks", Map.class).get();
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public Track setRelatedTracks(Map<Relationship, Collection<Track>> relatedTracks) {
+               getValue("relatedTracks", Map.class).set(relatedTracks);
+               return this;
+       }
+
 }
index a1362db..32ae937 100644 (file)
@@ -19,6 +19,7 @@ package net.pterodactylus.demoscenemusic.data;
 
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 
 /**
  * Data interface for tracks.
@@ -28,6 +29,24 @@ import java.util.List;
 public interface Track extends Base {
 
        /**
+        * Defines relationships between tracks.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       public enum Relationship {
+
+               /** The track is the original of a related track. */
+               original,
+
+               /** The track is a remix of a related track. */
+               remix,
+
+               /** The track is a cover version of a related track. */
+               cover,
+
+       }
+
+       /**
         * Returns the name of this track.
         *
         * @return The name of this track
@@ -110,4 +129,20 @@ public interface Track extends Base {
         */
        public Track setRemixArtists(List<Artist> remixArtists);
 
+       /**
+        * Returns all tracks that are somehow related to this track.
+        *
+        * @return All tracks that are related to this track
+        */
+       public Map<Relationship, Collection<Track>> getRelatedTracks();
+
+       /**
+        * Sets all related tracks.
+        *
+        * @param relatedTracks
+        *            All related tracks
+        * @return This track
+        */
+       public Track setRelatedTracks(Map<Relationship, Collection<Track>> relatedTracks);
+
 }