X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fdemoscenemusic%2Fdata%2FDataManager.java;h=ea040bd6e2611d792f83bf47d25a0503ade7dfc2;hb=1e2c5a3d82db1f6c93adddd79b77278222979eca;hp=0251d62e6071bcb4e45794007330497b307ea939;hpb=18e7d99933bc49b3092b156dbb16eef3f1957ec6;p=demoscenemusic.git diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java index 0251d62..ea040bd 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java @@ -618,6 +618,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 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 loadPartyProperties(List 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 @@ -990,6 +1098,21 @@ public class DataManager { return super.getRelatedTracks(); } + /** + * {@inheritDoc} + */ + @Override + public Collection 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(); + } + } /**