Add methods that create, load, and save parties.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 30 Jul 2012 08:58:20 +0000 (10:58 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 30 Jul 2012 08:58:20 +0000 (10:58 +0200)
src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java

index 0251d62..bcfbb18 100644 (file)
@@ -618,6 +618,97 @@ public class DataManager {
        }
 
        /**
+        * 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