Save properties of an artist when saving the artist.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 26 Jul 2012 11:44:35 +0000 (13:44 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 26 Jul 2012 11:44:44 +0000 (13:44 +0200)
src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java

index 8348b2f..373f80c 100644 (file)
@@ -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<String, String> 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();
        }
 
        /**