Add method to load all artists.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 18 Apr 2012 21:39:47 +0000 (23:39 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 18 Apr 2012 21:39:47 +0000 (23:39 +0200)
src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java
src/main/webapp/index.jsp [deleted file]

index a99b13d..ba2f61e 100644 (file)
@@ -20,6 +20,7 @@ package net.pterodactylus.demoscenemusic.data;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
 
@@ -50,6 +51,19 @@ public class DataManager {
                this.database = database;
        }
 
+       public Collection<Artist> getAllArtists() throws DatabaseException {
+               Query query = new Query(Type.SELECT, "ARTISTS");
+               query.addField(new Field("ARTISTS.*"));
+               query.addField(new Field("GROUP_ARTISTS.*"));
+               query.addField(new Field("GROUPS.*"));
+               query.addJoin(new Join(JoinType.INNER, "GROUP_ARTISTS", new Field("ARTISTS.ID"), new Field("GROUP_ARTISTS.ARTIST")));
+               query.addJoin(new Join(JoinType.INNER, "GROUPS", new Field("GROUP_ARTISTS.GROUP_"), new Field("GROUPS.ID")));
+               query.addOrderField(new OrderField(new Field("ARTISTS.ID")));
+               ArtistLoader artistLoader = new ArtistLoader();
+               database.process(query, artistLoader);
+               return artistLoader.all();
+       }
+
        public Track trackById(String id) throws DatabaseException {
                Query query = new Query(Type.SELECT, "TRACKS");
                query.addField(new Field("TRACKS.*"));
@@ -114,4 +128,43 @@ public class DataManager {
 
        }
 
+       private class ArtistLoader implements ResultProcessor {
+
+               private final List<Artist> artists = new ArrayList<Artist>();
+               private Artist artist;
+
+               public List<Artist> all() {
+                       return artists;
+               }
+
+               //
+               // RESULTPROCESSOR METHODS
+               //
+
+               /**
+                * {@inheritDoc}
+                */
+               public void processResult(ResultSet resultSet) throws SQLException {
+                       String artistId = resultSet.getString("ARTISTS.ID");
+                       if ((artist == null) || (!artist.id().equals(artistId))) {
+                               artist = new AbstractArtist(artistId) {
+
+                                       public void save() {
+                                       }
+
+                               }.name(resultSet.getString("ARTISTS.NAME")).groups(new HashSet<Group>());
+                               artists.add(artist);
+                       }
+
+                       Group group = new AbstractGroup(resultSet.getString("GROUPS.ID")) {
+
+                               public void save() {
+                               }
+
+                       }.name(resultSet.getString("GROUPS.NAME")).url(resultSet.getString("GROUPS.URL"));
+                       artist.groups().add(group);
+               }
+
+       }
+
 }
diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp
deleted file mode 100644 (file)
index 9dff832..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-<jsp:include page="include/header.jsp">
-       <jsp:param name="title" value="Welcome"/>
-</jsp:include>
-
-<h1>Demoscene Music</h1>
-
-<jsp:include page="include/footer.jsp"/>