Load artist properties with artists.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 26 Jul 2012 09:52:39 +0000 (11:52 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 26 Jul 2012 09:52:39 +0000 (11:52 +0200)
src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java

index 42952f9..8348b2f 100644 (file)
@@ -94,7 +94,7 @@ public class DataManager {
        public Collection<Artist> getAllArtists() throws DatabaseException {
                Query query = new Query(Type.SELECT, "ARTISTS");
                query.addField(new Field("ARTISTS.*"));
-               return database.getMultiple(query, artistCreator);
+               return loadProperties(database.getMultiple(query, artistCreator));
        }
 
        /**
@@ -111,7 +111,7 @@ public class DataManager {
                Query query = new Query(Type.SELECT, "ARTISTS");
                query.addField(new Field("ARTISTS.*"));
                query.addWhereClause(new ValueFieldWhereClause(new ValueField("ARTISTS.ID", new StringParameter(id))));
-               return database.getSingle(query, artistCreator);
+               return loadProperties(database.getSingle(query, artistCreator));
        }
 
        /**
@@ -128,7 +128,7 @@ public class DataManager {
                query.addField(new Field("ARTISTS.*"));
                query.addJoin(new Join(JoinType.INNER, "GROUP_ARTISTS", new Field("ARTISTS.ID"), new Field("GROUP_ARTISTS.ARTIST")));
                query.addWhereClause(new ValueFieldWhereClause(new ValueField("GROUP_ARTISTS.GROUP_", new StringParameter(groupId))));
-               return database.getMultiple(query, artistCreator);
+               return loadProperties(database.getMultiple(query, artistCreator));
        }
 
        /**
@@ -146,7 +146,7 @@ public class DataManager {
                query.addJoin(new Join(JoinType.INNER, "TRACK_ARTISTS", new Field("TRACK_ARTISTS.ARTIST"), new Field("ARTISTS.ID")));
                query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_ARTISTS.TRACK", new StringParameter(trackId))));
                query.addOrderField(new OrderField(new Field("TRACK_ARTISTS.DISPLAY_ORDER")));
-               return database.getMultiple(query, artistCreator);
+               return loadProperties(database.getMultiple(query, artistCreator));
        }
 
        /**
@@ -164,7 +164,7 @@ public class DataManager {
                query.addValueField(new ValueField("ID", new StringParameter(id)));
                query.addValueField(new ValueField("NAME", new StringParameter(name)));
                database.insert(query);
-               return getArtistById(id);
+               return loadProperties(getArtistById(id));
        }
 
        /**
@@ -183,6 +183,50 @@ public class DataManager {
        }
 
        /**
+        * Loads the properties for an artist.
+        *
+        * @param artist
+        *            The artist to load the properties for
+        * @return The artist
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public Artist loadProperties(final Artist artist) throws DatabaseException {
+               Query query = new Query(Type.SELECT, "ARTIST_PROPERTIES");
+               query.addField(new Field("ARTIST_PROPERTIES.PROPERTY"));
+               query.addField(new Field("ARTIST_PROPERTIES.VALUE"));
+               query.addWhereClause(new ValueFieldWhereClause(new ValueField("ARTIST", new StringParameter(artist.getId()))));
+               database.process(query, new ResultProcessor() {
+
+                       @Override
+                       public void processResult(ResultSet resultSet) throws SQLException {
+                               if (resultSet.isFirst()) {
+                                       artist.getProperties().removeAll();
+                               }
+                               artist.getProperties().set(resultSet.getString("ARTIST_PROPERTIES.PROPERTY"), resultSet.getString("ARTIST_PROPERTIES.VALUE"));
+                       }
+
+               });
+               return artist;
+       }
+
+       /**
+        * Loads the properties of all given artists.
+        *
+        * @param artists
+        *            The artists to load the properties for
+        * @return The list of artists
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public List<Artist> loadProperties(List<Artist> artists) throws DatabaseException {
+               for (Artist artist : artists) {
+                       loadProperties(artist);
+               }
+               return artists;
+       }
+
+       /**
         * Returns all remix artists involved in the track with the given ID.
         *
         * @param trackId