Add method to get a track by a derivative ID.
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / data / DataManager.java
index 6499603..967faa5 100644 (file)
@@ -329,6 +329,24 @@ public class DataManager {
        }
 
        /**
+        * Returns the track that contains the derivative with the given ID.
+        *
+        * @param id
+        *            The ID of the track derivative
+        * @return The track the derivative belongs to, or {@code null} if there is
+        *         no such track
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public Track getTrackByDerivativeId(String id) throws DatabaseException {
+               Query query = new Query(Type.SELECT, "TRACKS");
+               query.addField(new Field("TRACKS.*"));
+               query.addJoin(new Join(JoinType.INNER, "TRACK_DERIVATIVES", new Field("TRACK_DERIVATIVES.TRACK"), new Field("TRACKS.ID")));
+               query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_DERIVATIVES.ID", new StringParameter(id))));
+               return loadTrackProperties(database.getSingle(query, trackCreator));
+       }
+
+       /**
         * Returns all tracks by the artist with the given ID.
         *
         * @param artistId
@@ -502,6 +520,36 @@ public class DataManager {
        }
 
        /**
+        * Saves the given track derivative. As a track derivative does not have any
+        * attributes of its own only its properties are saved.
+        *
+        * @param trackDerivative
+        *            The track derivative to save
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public void saveTrackDerivate(TrackDerivative trackDerivative) throws DatabaseException {
+               saveProperties(trackDerivative.getProperties(), "TRACK_DERIVATIVE_PROPERTIES", "TRACK_DERIVATIVE", trackDerivative.getId());
+       }
+
+       /**
+        * Removes the given track derivative and all its properties from the
+        * database.
+        *
+        * @param trackDerivative
+        *            The track derivative to remove
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public void removeTrackDerivative(TrackDerivative trackDerivative) throws DatabaseException {
+               Query query = new Query(Type.DELETE, "TRACK_DERIVATIVES");
+               query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_DERIVATIVES.ID", new StringParameter(trackDerivative.getId()))));
+               database.update(query);
+               /* remove the properties. */
+               saveProperties(new Properties().set("dirty", "true").removeAll(), "TRACK_DERIVATIVE_PROPERTIES", "TRACK_DERIVATIVE", trackDerivative.getId());
+       }
+
+       /**
         * Returns all groups the artist with the given ID belongs to.
         *
         * @param artistId