Add track derivatives to tracks.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 Jul 2012 11:29:36 +0000 (13:29 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 Jul 2012 11:29:36 +0000 (13:29 +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 7744116..449965b 100644 (file)
@@ -345,6 +345,51 @@ public class DataManager {
        }
 
        /**
+        * Returns the derivatives for the given track.
+        *
+        * @param track
+        *            The track to get the derivatives for
+        * @return The derivatives for the given track
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public Collection<TrackDerivative> getTrackDerivativesByTrack(Track track) throws DatabaseException {
+               Query query = new Query(Type.SELECT, "TRACK_DERIVATIVES");
+               query.addField(new Field("TRACK_DERIVATIVES.*"));
+               query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_DERIVATIVES.TRACK", new StringParameter(track.getId()))));
+               return loadTrackDerivativeProperties(database.getMultiple(query, trackDerivativeCreator));
+       }
+
+       /**
+        * Loads the properties for the given track derivative.
+        *
+        * @param trackDerivative
+        *            The track derivative to load the properties for
+        * @return The track derivative with its properties loaded
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public TrackDerivative loadTrackDerivativeProperties(TrackDerivative trackDerivative) throws DatabaseException {
+               return loadProperties(trackDerivative, "TRACK_DERIVATIVE_PROPERTIES", "TRACK_DERIVATIVE");
+       }
+
+       /**
+        * Loads the properties for the given track derivatives.
+        *
+        * @param trackDerivatives
+        *            The track derivatives to load the properties for
+        * @return The track derivatives with their properties loaded
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public List<TrackDerivative> loadTrackDerivativeProperties(List<TrackDerivative> trackDerivatives) throws DatabaseException {
+               for (TrackDerivative trackDerivative : trackDerivatives) {
+                       loadTrackDerivativeProperties(trackDerivative);
+               }
+               return trackDerivatives;
+       }
+
+       /**
         * Returns all groups the artist with the given ID belongs to.
         *
         * @param artistId
@@ -665,6 +710,19 @@ public class DataManager {
 
                });
 
+               /** Memoizer for the track derivatives. */
+               private final Memoizer<Void> derivativesMemoizer = new Memoizer<Void>(new Callable<Void>() {
+
+                       @Override
+                       public Void call() throws Exception {
+                               if (!hasValue("derivatives")) {
+                                       getValue("derivatives", Collection.class).set(getTrackDerivativesByTrack(LazyTrack.this));
+                               }
+                               return null;
+                       }
+
+               });
+
                /** Memoizer for the related tracks. */
                private final Memoizer<Void> relatedTracksMemoizer = new Memoizer<Void>(new Callable<Void>() {
 
@@ -722,6 +780,15 @@ public class DataManager {
                 * {@inheritDoc}
                 */
                @Override
+               public Collection<TrackDerivative> getDerivatives() {
+                       derivativesMemoizer.get();
+                       return super.getDerivatives();
+               }
+
+               /**
+                * {@inheritDoc}
+                */
+               @Override
                public Map<Relationship, Collection<Track>> getRelatedTracks() {
                        relatedTracksMemoizer.get();
                        return super.getRelatedTracks();
index 9901fcb..804d710 100644 (file)
@@ -135,6 +135,24 @@ public class DefaultTrack extends DefaultBase implements Track {
         */
        @Override
        @SuppressWarnings("unchecked")
+       public Collection<TrackDerivative> getDerivatives() {
+               return getValue("derivatives", Collection.class).get();
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public Track setDerivatives(Collection<TrackDerivative> derivatives) {
+               getValue("derivatives", Collection.class).set(derivatives);
+               return this;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       @SuppressWarnings("unchecked")
        public Map<Relationship, Collection<Track>> getRelatedTracks() {
                return getValue("relatedTracks", Map.class).get();
        }
index 32ae937..332132a 100644 (file)
@@ -130,6 +130,23 @@ public interface Track extends Base {
        public Track setRemixArtists(List<Artist> remixArtists);
 
        /**
+        * Returns all derivatives of this track. A derivative of a MOD file could
+        * be a WAV or MP3 file.
+        *
+        * @return All derivatives of this track
+        */
+       public Collection<TrackDerivative> getDerivatives();
+
+       /**
+        * Sets the derivatives of this track.
+        *
+        * @param derivatives
+        *            The derivatives of this track
+        * @return This track
+        */
+       public Track setDerivatives(Collection<TrackDerivative> derivatives);
+
+       /**
         * Returns all tracks that are somehow related to this track.
         *
         * @return All tracks that are related to this track