From 278bcc6a8c05363a6d157076e0a018f004803746 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 28 Jul 2012 01:39:06 +0200 Subject: [PATCH] Add method to save a track with artists and properties. --- .../demoscenemusic/data/DataManager.java | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java index 5f20905..6499603 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java @@ -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 tracksMemoizer = new Memoizer(new Callable() { + @Override public Void call() throws DatabaseException { if (!hasValue("tracks")) { @@ -814,6 +846,7 @@ public class DataManager { } return null; } + }); /** -- 2.7.4