Add page that lists all parties.
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / data / DataManager.java
index 967faa5..0251d62 100644 (file)
@@ -73,6 +73,10 @@ public class DataManager {
        @SuppressWarnings("synthetic-access")
        private final ObjectCreator<Style> styleCreator = new StyleCreator();
 
+       /** The party object creator. */
+       @SuppressWarnings("synthetic-access")
+       private final ObjectCreator<Party> partyCreator = new PartyCreator();
+
        /** The {@link User} object creator. */
        @SuppressWarnings("synthetic-access")
        private final ObjectCreator<User> userCreator = new UserCreator();
@@ -364,6 +368,23 @@ public class DataManager {
        }
 
        /**
+        * Returns all tracks that were released at the party with the given ID.
+        *
+        * @param partyId
+        *            The ID of the party
+        * @return All tracks that were released at the given party
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public Collection<Track> getTracksByParty(String partyId) throws DatabaseException {
+               Query query = new Query(Type.SELECT, "TRACKS");
+               query.addField(new Field("TRACKS.*"));
+               query.addJoin(new Join(JoinType.INNER, "PARTY_TRACKS", new Field("PARTY_TRACKS.TRACK"), new Field("TRACKS.ID")));
+               query.addWhereClause(new ValueFieldWhereClause(new ValueField("PARTY_TRACKS.PARTY", new StringParameter(partyId))));
+               return loadTrackProperties(database.getMultiple(query, trackCreator));
+       }
+
+       /**
         * Loads the properties for the given track.
         *
         * @param track
@@ -584,6 +605,19 @@ public class DataManager {
        }
 
        /**
+        * Returns all parties.
+        *
+        * @return All parties
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public Collection<Party> getAllParties() throws DatabaseException {
+               Query query = new Query(Type.SELECT, "PARTIES");
+               query.addField(new Field("PARTIES.*"));
+               return loadPartyProperties(database.getMultiple(query, partyCreator));
+       }
+
+       /**
         * Returns the user with the given name.
         *
         * @param username
@@ -1014,6 +1048,63 @@ public class DataManager {
        }
 
        /**
+        * {@link Party} implementation that loads additional information only on
+        * demand.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       private class LazyParty extends DefaultParty {
+
+               /**
+                * Creates a new party.
+                *
+                * @param id
+                *            The ID of the party
+                */
+               public LazyParty(String id) {
+                       super(id);
+               }
+
+               //
+               // PARTY METHODS
+               //
+
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               public Collection<Track> getReleases() {
+                       if (!hasValue("releases")) {
+                               try {
+                                       getValue("releases", Collection.class).set(getTracksByParty(getId()));
+                               } catch (DatabaseException de1) {
+                                       throw new RuntimeException("Could not loaded Tracks for Party " + getId() + ".", de1);
+                               }
+                       }
+                       return super.getReleases();
+               }
+
+       }
+
+       /**
+        * {@link ObjectCreator} implementation that can create {@link Party}
+        * objects.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       private class PartyCreator implements ObjectCreator<Party> {
+
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               public Party createObject(ResultSet resultSet) throws SQLException {
+                       return new LazyParty(resultSet.getString("PARTIES.ID")).setName(resultSet.getString("PARTIES.NAME"));
+               }
+
+       }
+
+       /**
         * {@link User} implementation that retrieves some attributes (such as
         * {@link #getOpenIds()}) from the {@link DataManager} on demand.
         *