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;
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;
}
/**
+ * 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
});
+ /** 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.
*
return super.getRemixArtists();
}
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Map<Relationship, Collection<Track>> getRelatedTracks() {
+ relatedTracksMemoizer.get();
+ return super.getRelatedTracks();
+ }
+
}
/**
import java.util.Collection;
import java.util.List;
+import java.util.Map;
/**
* Data interface for tracks.
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
*/
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);
+
}