From: David ‘Bombe’ Roden Date: Thu, 26 Jul 2012 11:44:35 +0000 (+0200) Subject: Save properties of an artist when saving the artist. X-Git-Url: https://git.pterodactylus.net/?p=demoscenemusic.git;a=commitdiff_plain;h=e9c3d0da99c23ce8cb9c390a03a51b5bb8e649e4 Save properties of an artist when saving the artist. --- diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java index 8348b2f..373f80c 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java @@ -24,6 +24,7 @@ import java.util.EnumMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.UUID; import java.util.concurrent.Callable; @@ -180,6 +181,50 @@ public class DataManager { query.addValueField(new ValueField("NAME", new StringParameter(artist.getName()))); query.addWhereClause(new ValueFieldWhereClause(new ValueField("ID", new StringParameter(artist.getId())))); database.update(query); + saveArtistProperties(artist); + } + + /** + * Saves the properties of the given artist. + * + * @param artist + * The artist whose properties to save + * @throws DatabaseException + * if a database error occurs + */ + public void saveArtistProperties(Artist artist) throws DatabaseException { + saveProperties(artist.getProperties(), "ARTIST_PROPERTIES", "ARTIST", artist.getId()); + } + + /** + * Saves the given properties to the given table for the given principal. + * + * @param properties + * The properties to save + * @param table + * The table in which to save the properties + * @param type + * The type of the principal (e. g. “ARTIST” or “TRACK”) + * @param id + * The ID of the principial + * @throws DatabaseException + * if a database error occurs + */ + public void saveProperties(Properties properties, String table, String type, String id) throws DatabaseException { + if (!properties.isDirty()) { + return; + } + Query query = new Query(Type.DELETE, table); + query.addWhereClause(new ValueFieldWhereClause(new ValueField(type, new StringParameter(id)))); + database.update(query); + for (Entry property : properties) { + Query insertQuery = new Query(Type.INSERT, table); + insertQuery.addValueField(new ValueField(type, new StringParameter(id))); + insertQuery.addValueField(new ValueField("PROPERTY", new StringParameter(property.getKey()))); + insertQuery.addValueField(new ValueField("VALUE", new StringParameter(property.getValue()))); + database.insert(insertQuery); + } + properties.resetDirty(); } /**