Get parties a track was released at from the track.
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / data / DataManager.java
index 31e5def..ea040bd 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();
@@ -329,6 +333,24 @@ public class DataManager {
        }
 
        /**
+        * Returns the track that contains the derivative with the given ID.
+        *
+        * @param id
+        *            The ID of the track derivative
+        * @return The track the derivative belongs to, or {@code null} if there is
+        *         no such track
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public Track getTrackByDerivativeId(String id) throws DatabaseException {
+               Query query = new Query(Type.SELECT, "TRACKS");
+               query.addField(new Field("TRACKS.*"));
+               query.addJoin(new Join(JoinType.INNER, "TRACK_DERIVATIVES", new Field("TRACK_DERIVATIVES.TRACK"), new Field("TRACKS.ID")));
+               query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_DERIVATIVES.ID", new StringParameter(id))));
+               return loadTrackProperties(database.getSingle(query, trackCreator));
+       }
+
+       /**
         * Returns all tracks by the artist with the given ID.
         *
         * @param artistId
@@ -346,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
@@ -528,7 +567,7 @@ public class DataManager {
                query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_DERIVATIVES.ID", new StringParameter(trackDerivative.getId()))));
                database.update(query);
                /* remove the properties. */
-               saveProperties(new Properties(), "TRACK_DERIVATIVE_PROPERTIES", "TRACK_DERIVATIVE", trackDerivative.getId());
+               saveProperties(new Properties().set("dirty", "true").removeAll(), "TRACK_DERIVATIVE_PROPERTIES", "TRACK_DERIVATIVE", trackDerivative.getId());
        }
 
        /**
@@ -566,6 +605,127 @@ 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 all parties that the track with the given ID was released at.
+        *
+        * @param trackId
+        *            The ID of the track
+        * @return All parties the track was released at
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public Collection<Party> getPartiesByTrackId(String trackId) throws DatabaseException {
+               Query query = new Query(Type.SELECT, "PARTIES");
+               query.addField(new Field("PARTIES.*"));
+               query.addJoin(new Join(JoinType.INNER, "PARTY_TRACKS", new Field("PARTY_TRACKS.PARTY"), new Field("PARTIES.ID")));
+               query.addWhereClause(new ValueFieldWhereClause(new ValueField("PARTY_TRACKS.TRACK", new StringParameter(trackId))));
+               return loadPartyProperties(database.getMultiple(query, partyCreator));
+       }
+
+       /**
+        * Returns the party with the given ID.
+        *
+        * @param partyId
+        *            The ID of the party
+        * @return The party with the given ID
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public Party getPartyById(String partyId) throws DatabaseException {
+               Query query = new Query(Type.SELECT, "PARTIES");
+               query.addField(new Field("PARTIES.*"));
+               query.addWhereClause(new ValueFieldWhereClause(new ValueField("PARTIES.ID", new StringParameter(partyId))));
+               return loadPartyProperties(database.getSingle(query, partyCreator));
+       }
+
+       /**
+        * Loads the properties of the given party.
+        *
+        * @param party
+        *            The party to load the properties for
+        * @return The party with its properties loaded
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public Party loadPartyProperties(Party party) throws DatabaseException {
+               return loadProperties(party, "PARTY_PROPERTIES", "PARTY");
+       }
+
+       /**
+        * Loads the properties of the given parties.
+        *
+        * @param parties
+        *            The parties to load the properties for
+        * @return The parties with their properties loaded
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public List<Party> loadPartyProperties(List<Party> parties) throws DatabaseException {
+               for (Party party : parties) {
+                       loadPartyProperties(party);
+               }
+               return parties;
+       }
+
+       /**
+        * Saves the given party.
+        *
+        * @param party
+        *            The party to save
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public void saveParty(Party party) throws DatabaseException {
+               Query query = new Query(Type.UPDATE, "PARTIES");
+               query.addValueField(new ValueField("NAME", new StringParameter(party.getName())));
+               query.addWhereClause(new ValueFieldWhereClause(new ValueField("ID", new StringParameter(party.getId()))));
+               database.update(query);
+               savePartyProperties(party);
+       }
+
+       /**
+        * Saves the properties of the given party.
+        *
+        * @param party
+        *            The party whose properties to save
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public void savePartyProperties(Party party) throws DatabaseException {
+               saveProperties(party.getProperties(), "PARTY_PROPERTIES", "PARTY", party.getId());
+       }
+
+       /**
+        * Creates a new party with the given name.
+        *
+        * @param name
+        *            The name of the party
+        * @return The new party
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public Party createParty(String name) throws DatabaseException {
+               Query query = new Query(Type.INSERT, "PARTIES");
+               String id = UUID.randomUUID().toString();
+               query.addValueField(new ValueField("ID", new StringParameter(id)));
+               query.addValueField(new ValueField("NAME", new StringParameter(name)));
+               database.insert(query);
+               return getPartyById(id);
+       }
+
+       /**
         * Returns the user with the given name.
         *
         * @param username
@@ -938,6 +1098,21 @@ public class DataManager {
                        return super.getRelatedTracks();
                }
 
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               public Collection<Party> getParties() {
+                       if (!hasValue("parties")) {
+                               try {
+                                       getValue("parties", Collection.class).set(getPartiesByTrackId(getId()));
+                               } catch (DatabaseException de1) {
+                                       throw new RuntimeException("Could not load Parties for Track " + getId() + ".", de1);
+                               }
+                       }
+                       return super.getParties();
+               }
+
        }
 
        /**
@@ -996,6 +1171,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.
         *