From 85e627b741a00383efb781f525bfd9f471dc7665 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 18 Apr 2012 12:27:30 +0200 Subject: [PATCH] Load tracks correctly. --- .../demoscenemusic/data/DataManager.java | 93 ++++++++++------------ 1 file changed, 44 insertions(+), 49 deletions(-) diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java index 56d5cea..a99b13d 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java @@ -19,16 +19,21 @@ package net.pterodactylus.demoscenemusic.data; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; import net.pterodactylus.util.database.Database; import net.pterodactylus.util.database.DatabaseException; import net.pterodactylus.util.database.Field; import net.pterodactylus.util.database.Join; import net.pterodactylus.util.database.Join.JoinType; -import net.pterodactylus.util.database.ObjectCreator; +import net.pterodactylus.util.database.OrderField; +import net.pterodactylus.util.database.OrderField.Order; import net.pterodactylus.util.database.Parameter.StringParameter; import net.pterodactylus.util.database.Query; import net.pterodactylus.util.database.Query.Type; +import net.pterodactylus.util.database.ResultProcessor; import net.pterodactylus.util.database.ValueField; import net.pterodactylus.util.database.ValueFieldWhereClause; @@ -41,12 +46,6 @@ public class DataManager { private final Database database; - private final TrackLoader trackLoader = new TrackLoader(); - - private final ArtistLoader artistLoader = new ArtistLoader(); - - private final StyleLoader styleLoader = new StyleLoader(); - public DataManager(Database database) { this.database = database; } @@ -54,67 +53,63 @@ public class DataManager { public Track trackById(String id) throws DatabaseException { Query query = new Query(Type.SELECT, "TRACKS"); query.addField(new Field("TRACKS.*")); + query.addField(new Field("TRACK_ARTISTS.*")); query.addField(new Field("ARTISTS.*")); + query.addField(new Field("TRACK_STYLES.*")); query.addField(new Field("STYLES.*")); - query.addJoin(new Join(JoinType.INNER, "ARTISTS", new Field("TRACKS.ARTIST"), new Field("ARTISTS.ID"))); - query.addJoin(new Join(JoinType.INNER, "STYLES", new Field("TRACKS.STYLE"), new Field("STYLES.ID"))); - query.addWhereClause(new ValueFieldWhereClause(new ValueField("ID", new StringParameter(id)))); - return database.getSingle(query, trackLoader); + query.addJoin(new Join(JoinType.INNER, "TRACK_ARTISTS", new Field("TRACKS.ID"), new Field("TRACK_ARTISTS.TRACK"))); + query.addJoin(new Join(JoinType.INNER, "ARTISTS", new Field("TRACK_ARTISTS.ARTIST"), new Field("ARTISTS.ID"))); + query.addJoin(new Join(JoinType.INNER, "TRACK_STYLES", new Field("TRACKS.ID"), new Field("TRACK_STYLES.TRACK"))); + query.addJoin(new Join(JoinType.INNER, "STYLES", new Field("TRACK_STYLES.STYLE"), new Field("STYLES.ID"))); + query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACKS.ID", new StringParameter(id)))); + query.addOrderField(new OrderField(new Field("TRACKS.ID"), Order.ASCENDING)); + query.addOrderField(new OrderField(new Field("TRACK_ARTISTS.DISPLAY_ORDER"), Order.ASCENDING)); + TrackLoader trackLoader = new TrackLoader(); + database.process(query, trackLoader); + return trackLoader.get(); } - private class TrackLoader implements ObjectCreator { - - /** - * {@inheritDoc} - */ - public Track createObject(ResultSet resultSet) throws SQLException { - return new AbstractTrack(resultSet.getString("TRACKS.ID")) { - - public void save() { - if (!dirty()) { - return; - } - } - - }.name(resultSet.getString("TRACKS.NAME")).style(styleLoader.createObject(resultSet)).artist(artistLoader.createObject(resultSet)); - } - - } + private class TrackLoader implements ResultProcessor { - private class ArtistLoader implements ObjectCreator { + private final List loadedTracks = new ArrayList(); + private Track track; /** * {@inheritDoc} */ - public Artist createObject(ResultSet resultSet) throws SQLException { - return new AbstractArtist(resultSet.getString("ARTISTS.ID")) { + public void processResult(ResultSet resultSet) throws SQLException { + String trackId = resultSet.getString("TRACKS.ID"); + if ((track == null) || (!track.id().equals(trackId))) { + track = new AbstractTrack(trackId) { + + public void save() { + if (!dirty()) { + return; + } + } + }.name(resultSet.getString("TRACKS.NAME")).artists(new ArrayList()).styles(new HashSet