From e19732e4343d5a75a6539d6cf2fda897a2889f91 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 26 Jul 2012 11:52:39 +0200 Subject: [PATCH] Load artist properties with artists. --- .../demoscenemusic/data/DataManager.java | 54 ++++++++++++++++++++-- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java index 42952f9..8348b2f 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java @@ -94,7 +94,7 @@ public class DataManager { public Collection 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 loadProperties(List 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 -- 2.7.4