Add group management functions.
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / data / DataManager.java
index 0251d62..4d2a318 100644 (file)
@@ -190,6 +190,20 @@ public class DataManager {
                query.addValueField(new ValueField("NAME", new StringParameter(artist.getName())));
                query.addWhereClause(new ValueFieldWhereClause(new ValueField("ID", new StringParameter(artist.getId()))));
                database.update(query);
+
+               /* save groups. */
+               Collection<Group> groups = artist.getGroups();
+               query = new Query(Type.DELETE, "GROUP_ARTISTS");
+               query.addWhereClause(new ValueFieldWhereClause(new ValueField("GROUP_ARTISTS.ARTIST", new StringParameter(artist.getId()))));
+               database.update(query);
+               for (Group group : groups) {
+                       query = new Query(Type.INSERT, "GROUP_ARTISTS");
+                       query.addValueField(new ValueField("GROUP_", new StringParameter(group.getId())));
+                       query.addValueField(new ValueField("ARTIST", new StringParameter(artist.getId())));
+                       database.insert(query);
+               }
+
+               /* save properties. */
                saveArtistProperties(artist);
        }
 
@@ -457,6 +471,19 @@ public class DataManager {
                        query.addValueField(new ValueField("TRACK_ARTISTS.DISPLAY_ORDER", new IntegerParameter(index + 1)));
                        database.insert(query);
                }
+
+               /* store party links. */
+               Collection<Party> parties = track.getParties(); /* prefetch parties. */
+               query = new Query(Type.DELETE, "PARTY_TRACKS");
+               query.addWhereClause(new ValueFieldWhereClause(new ValueField("PARTY_TRACKS.TRACK", new StringParameter(track.getId()))));
+               database.update(query);
+               for (Party party : parties) {
+                       query = new Query(Type.INSERT, "PARTY_TRACKS");
+                       query.addValueField(new ValueField("PARTY_TRACKS.TRACK", new StringParameter(track.getId())));
+                       query.addValueField(new ValueField("PARTY_TRACKS.PARTY", new StringParameter(party.getId())));
+                       database.insert(query);
+               }
+
                /* store properties. */
                saveProperties(track.getProperties(), "TRACK_PROPERTIES", "TRACK", track.getId());
        }
@@ -571,6 +598,36 @@ public class DataManager {
        }
 
        /**
+        * Returns all groups.
+        *
+        * @return All groups
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public Collection<Group> getAllGroups() throws DatabaseException {
+               Query query = new Query(Type.SELECT, "GROUPS");
+               query.addField(new Field("GROUPS.*"));
+               return loadGroupProperties(database.getMultiple(query, groupCreator));
+       }
+
+       /**
+        * Returns the group with the given ID.
+        *
+        * @param groupId
+        *            The ID of the group
+        * @return The group with the given ID, or {@code null} if no such group
+        *         exists
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public Group getGroupById(String groupId) throws DatabaseException {
+               Query query = new Query(Type.SELECT, "GROUPS");
+               query.addField(new Field("GROUPS.*"));
+               query.addWhereClause(new ValueFieldWhereClause(new ValueField("GROUPS.ID", new StringParameter(groupId))));
+               return loadGroupProperties(database.getSingle(query, groupCreator));
+       }
+
+       /**
         * Returns all groups the artist with the given ID belongs to.
         *
         * @param artistId
@@ -588,6 +645,82 @@ public class DataManager {
        }
 
        /**
+        * Loads the properties of the given group.
+        *
+        * @param group
+        *            The group to load the properties for
+        * @return The group with its properties loaded
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public Group loadGroupProperties(Group group) throws DatabaseException {
+               return loadProperties(group, "GROUP_PROPERTIES", "GROUP_ID");
+       }
+
+       /**
+        * Loads the properties of the given groups.
+        *
+        * @param groups
+        *            The groups to load the properties for
+        * @return The groups with their properties loaded
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public Collection<Group> loadGroupProperties(Collection<Group> groups) throws DatabaseException {
+               for (Group group : groups) {
+                       loadGroupProperties(group);
+               }
+               return groups;
+       }
+
+       /**
+        * Creates a group with the given name.
+        *
+        * @param name
+        *            The name of the new group
+        * @return The new group
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public Group createGroup(String name) throws DatabaseException {
+               Query query = new Query(Type.INSERT, "GROUPS");
+               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 getGroupById(id);
+       }
+
+       /**
+        * Saves the given group.
+        *
+        * @param group
+        *            The group to save
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public void saveGroup(Group group) throws DatabaseException {
+               Query query = new Query(Type.UPDATE, "GROUPS");
+               query.addValueField(new ValueField("NAME", new StringParameter(group.getName())));
+               query.addWhereClause(new ValueFieldWhereClause(new ValueField("ID", new StringParameter(group.getId()))));
+               database.update(query);
+               /* save properties. */
+               saveGroupProperties(group);
+       }
+
+       /**
+        * Saves the properties of the given group.
+        *
+        * @param group
+        *            The group whose properties to save
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public void saveGroupProperties(Group group) throws DatabaseException {
+               saveProperties(group.getProperties(), "GROUP_PROPERTIES", "GROUP_ID", group.getId());
+       }
+
+       /**
         * Returns all styles for the track with the given ID.
         *
         * @param trackId
@@ -618,6 +751,114 @@ public class DataManager {
        }
 
        /**
+        * 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
@@ -687,6 +928,9 @@ public class DataManager {
         *             if a database error occurs
         */
        private <T extends Base> T loadProperties(final T object, final String table, String type) throws DatabaseException {
+               if (object == null) {
+                       return null;
+               }
                Query query = new Query(Type.SELECT, table);
                query.addField(new Field(table + ".PROPERTY"));
                query.addField(new Field(table + ".VALUE"));
@@ -990,6 +1234,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();
+               }
+
        }
 
        /**