Add method to load properties for arbitrary objects.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 Jul 2012 11:15:25 +0000 (13:15 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 Jul 2012 11:15:25 +0000 (13:15 +0200)
src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java

index 7943401..5324d28 100644 (file)
@@ -443,6 +443,44 @@ public class DataManager {
                return database.getMultiple(query, new StringCreator("USER_OPENIDS.OPENID"));
        }
 
+       //
+       // PRIVATE METHODS
+       //
+
+       /**
+        * Loads the properties for the given object.
+        *
+        * @param <T>
+        *            The type of the object
+        * @param object
+        *            The object
+        * @param table
+        *            The table to load the properties from
+        * @param type
+        *            The type of the object (“ARTIST,” “TRACK,” etc.)
+        * @return The object with its properties loaded
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       private <T extends Base> T loadProperties(final T object, final String table, String type) throws DatabaseException {
+               Query query = new Query(Type.SELECT, table);
+               query.addField(new Field(table + ".PROPERTY"));
+               query.addField(new Field(table + ".VALUE"));
+               query.addWhereClause(new ValueFieldWhereClause(new ValueField(type, new StringParameter(object.getId()))));
+               database.process(query, new ResultProcessor() {
+
+                       @Override
+                       public void processResult(ResultSet resultSet) throws SQLException {
+                               if (resultSet.isFirst()) {
+                                       object.getProperties().removeAll();
+                               }
+                               object.getProperties().set(resultSet.getString(table + ".PROPERTY"), resultSet.getString(table + ".VALUE"));
+                       }
+
+               });
+               return object;
+       }
+
        /**
         * {@link Artist} implementation that retrieves some attributes (such as
         * {@link #getGroups()}, and {@link #getTracks()}) from the