Add group management functions.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 31 Jul 2012 04:43:47 +0000 (06:43 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 31 Jul 2012 04:43:47 +0000 (06:43 +0200)
src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java

index 8d331cb..4d2a318 100644 (file)
@@ -598,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
@@ -615,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