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;
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;
}
/**
+ * 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.