From 904ede237e9dd7961ff660a407b43a9191656d2e Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 26 Apr 2012 11:22:14 +0200 Subject: [PATCH] Add related tracks to track interface. --- .../demoscenemusic/data/DataManager.java | 58 ++++++++++++++++++++++ .../demoscenemusic/data/DefaultTrack.java | 19 +++++++ .../pterodactylus/demoscenemusic/data/Track.java | 35 +++++++++++++ 3 files changed, 112 insertions(+) diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java index ac146e1..6bec2a8 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java @@ -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> 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> relatedTracks = new EnumMap>(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()); + } + 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 relatedTracksMemoizer = new Memoizer(new Callable() { + + @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> getRelatedTracks() { + relatedTracksMemoizer.get(); + return super.getRelatedTracks(); + } + } /** diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultTrack.java b/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultTrack.java index fafb8a2..9901fcb 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultTrack.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultTrack.java @@ -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> getRelatedTracks() { + return getValue("relatedTracks", Map.class).get(); + } + + /** + * {@inheritDoc} + */ + @Override + public Track setRelatedTracks(Map> relatedTracks) { + getValue("relatedTracks", Map.class).set(relatedTracks); + return this; + } + } diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/Track.java b/src/main/java/net/pterodactylus/demoscenemusic/data/Track.java index a1362db..32ae937 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/data/Track.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/Track.java @@ -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 David ‘Bombe’ Roden + */ + 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 remixArtists); + /** + * Returns all tracks that are somehow related to this track. + * + * @return All tracks that are related to this track + */ + public Map> getRelatedTracks(); + + /** + * Sets all related tracks. + * + * @param relatedTracks + * All related tracks + * @return This track + */ + public Track setRelatedTracks(Map> relatedTracks); + } -- 2.7.4