Add method to save a track with artists and properties.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 Jul 2012 23:39:06 +0000 (01:39 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 Jul 2012 23:39:06 +0000 (01:39 +0200)
src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java

index 5f20905..6499603 100644 (file)
@@ -38,6 +38,7 @@ import net.pterodactylus.util.database.Join.JoinType;
 import net.pterodactylus.util.database.ObjectCreator;
 import net.pterodactylus.util.database.ObjectCreator.StringCreator;
 import net.pterodactylus.util.database.OrderField;
+import net.pterodactylus.util.database.Parameter.IntegerParameter;
 import net.pterodactylus.util.database.Parameter.StringParameter;
 import net.pterodactylus.util.database.Query;
 import net.pterodactylus.util.database.Query.Type;
@@ -392,6 +393,36 @@ public class DataManager {
        }
 
        /**
+        * Saves the given track. This also saves all relationships of the track.
+        *
+        * @param track
+        *            The track to save
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public void saveTrack(Track track) throws DatabaseException {
+               Query query = new Query(Type.UPDATE, "TRACKS");
+               query.addValueField(new ValueField("TRACKS.NAME", new StringParameter(track.getName())));
+               query.addValueField(new ValueField("TRACKS.REMIX", new StringParameter(track.getRemix())));
+               query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACKS.ID", new StringParameter(track.getId()))));
+               database.update(query);
+               /* store artist information. */
+               track.getArtists(); /* prefetch artists. */
+               query = new Query(Type.DELETE, "TRACK_ARTISTS");
+               query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_ARTISTS.TRACK", new StringParameter(track.getId()))));
+               database.update(query);
+               for (int index = 0; index < track.getArtists().size(); ++index) {
+                       query = new Query(Type.INSERT, "TRACK_ARTISTS");
+                       query.addValueField(new ValueField("TRACK_ARTISTS.TRACK", new StringParameter(track.getId())));
+                       query.addValueField(new ValueField("TRACK_ARTISTS.ARTIST", new StringParameter(track.getArtists().get(index).getId())));
+                       query.addValueField(new ValueField("TRACK_ARTISTS.DISPLAY_ORDER", new IntegerParameter(index + 1)));
+                       database.insert(query);
+               }
+               /* store properties. */
+               saveProperties(track.getProperties(), "TRACK_PROPERTIES", "TRACK", track.getId());
+       }
+
+       /**
         * Returns the derivative with the given ID.
         *
         * @param id
@@ -603,6 +634,7 @@ public class DataManager {
 
                /** Memoizer for the tracks by this artist. */
                private final Memoizer<Void> tracksMemoizer = new Memoizer<Void>(new Callable<Void>() {
+
                        @Override
                        public Void call() throws DatabaseException {
                                if (!hasValue("tracks")) {
@@ -814,6 +846,7 @@ public class DataManager {
                                }
                                return null;
                        }
+
                });
 
                /**