Add method that loads properties of several objects at once.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 31 Jul 2012 05:01:17 +0000 (07:01 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 31 Jul 2012 05:01:17 +0000 (07:01 +0200)
src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java

index f06a48b..5972bd8 100644 (file)
@@ -21,10 +21,12 @@ import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Collection;
 import java.util.EnumMap;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Set;
 import java.util.UUID;
 import java.util.concurrent.Callable;
 
@@ -37,6 +39,7 @@ import net.pterodactylus.util.database.Join;
 import net.pterodactylus.util.database.Join.JoinType;
 import net.pterodactylus.util.database.ObjectCreator;
 import net.pterodactylus.util.database.ObjectCreator.StringCreator;
+import net.pterodactylus.util.database.OrWhereClause;
 import net.pterodactylus.util.database.OrderField;
 import net.pterodactylus.util.database.Parameter.IntegerParameter;
 import net.pterodactylus.util.database.Parameter.StringParameter;
@@ -950,6 +953,53 @@ public class DataManager {
        }
 
        /**
+        * Loads the properties for the given objects.
+        *
+        * @param <T>
+        *            The type of the object
+        * @param objects
+        *            The objects
+        * @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> List<T> loadProperties(List<T> objects, final String table, final String type) throws DatabaseException {
+               final Map<String, T> objectMap = new HashMap<String, T>();
+               final Set<String> firstObjects = new HashSet<String>();
+               for (T object : objects) {
+                       objectMap.put(object.getId(), object);
+                       firstObjects.add(object.getId());
+               }
+               Query query = new Query(Type.SELECT, table);
+               query.addField(new Field(table + "." + type));
+               query.addField(new Field(table + ".PROPERTY"));
+               query.addField(new Field(table + ".VALUE"));
+               OrWhereClause whereClause = new OrWhereClause();
+               for (T object : objects) {
+                       whereClause.add(new ValueFieldWhereClause(new ValueField(type, new StringParameter(object.getId()))));
+               }
+               query.addWhereClause(whereClause);
+               database.process(query, new ResultProcessor() {
+
+                       @Override
+                       public void processResult(ResultSet resultSet) throws SQLException {
+                               String id = resultSet.getString(table + "." + type);
+                               T object = objectMap.get(id);
+                               if (firstObjects.remove(id)) {
+                                       object.getProperties().removeAll();
+                               }
+                               object.getProperties().set(resultSet.getString(table + ".PROPERTY"), resultSet.getString(table + ".VALUE"));
+                       }
+
+               });
+               return objects;
+       }
+
+       /**
         * {@link Artist} implementation that retrieves some attributes (such as
         * {@link #getGroups()}, and {@link #getTracks()}) from the
         * {@link DataManager} on demand.