X-Git-Url: https://git.pterodactylus.net/?p=demoscenemusic.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fdemoscenemusic%2Fdata%2FDataManager.java;h=eb5e1c7bd65128de2e088eb8bc9329fe7bb52dfb;hp=8d331cb725bf160d0a45899bdb6081c3ac63caf5;hb=0ac7c17ecc8854789ce04cc8786c508815c9b351;hpb=6bdfd09158a7d212e47ee96ed89dc91a91dad668 diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java index 8d331cb..eb5e1c7 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java @@ -21,10 +21,12 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.Collection; import java.util.EnumMap; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import java.util.UUID; import java.util.concurrent.Callable; @@ -37,6 +39,7 @@ import net.pterodactylus.util.database.Join; import net.pterodactylus.util.database.Join.JoinType; import net.pterodactylus.util.database.ObjectCreator; import net.pterodactylus.util.database.ObjectCreator.StringCreator; +import net.pterodactylus.util.database.OrWhereClause; import net.pterodactylus.util.database.OrderField; import net.pterodactylus.util.database.Parameter.IntegerParameter; import net.pterodactylus.util.database.Parameter.StringParameter; @@ -273,10 +276,7 @@ public class DataManager { * if a database error occurs */ public List loadArtistProperties(List artists) throws DatabaseException { - for (Artist artist : artists) { - loadArtistProperties(artist); - } - return artists; + return loadProperties(artists, "ARTIST_PROPERTIES", "ARTIST"); } /** @@ -421,10 +421,7 @@ public class DataManager { * if a database error occurs */ public List loadTrackProperties(List tracks) throws DatabaseException { - for (Track track : tracks) { - loadTrackProperties(track); - } - return tracks; + return loadProperties(tracks, "TRACK_PROPERTIES", "TRACK"); } /** @@ -561,10 +558,7 @@ public class DataManager { * if a database error occurs */ public List loadTrackDerivativeProperties(List trackDerivatives) throws DatabaseException { - for (TrackDerivative trackDerivative : trackDerivatives) { - loadTrackDerivativeProperties(trackDerivative); - } - return trackDerivatives; + return loadProperties(trackDerivatives, "TRACK_DERIVATIVE_PROPERTIES", "TRACK_DERIVATIVE"); } /** @@ -598,6 +592,36 @@ public class DataManager { } /** + * Returns all groups. + * + * @return All groups + * @throws DatabaseException + * if a database error occurs + */ + public Collection 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 +639,79 @@ 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 loadGroupProperties(List groups) throws DatabaseException { + return loadProperties(groups, "GROUP_PROPERTIES", "GROUP_ID"); + } + + /** + * 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 @@ -700,10 +797,7 @@ public class DataManager { * if a database error occurs */ public List loadPartyProperties(List parties) throws DatabaseException { - for (Party party : parties) { - loadPartyProperties(party); - } - return parties; + return loadProperties(parties, "PARTY_PROPERTIES", "PARTY"); } /** @@ -844,6 +938,56 @@ public class DataManager { } /** + * Loads the properties for the given objects. + * + * @param + * The type of the object + * @param objects + * The objects + * @param table + * The table to load the properties from + * @param type + * The type of the object (“ARTIST,” “TRACK,” etc.) + * @return The object with its properties loaded + * @throws DatabaseException + * if a database error occurs + */ + private List loadProperties(List objects, final String table, final String type) throws DatabaseException { + if (objects.isEmpty()) { + return objects; + } + final Map objectMap = new HashMap(); + final Set firstObjects = new HashSet(); + for (T object : objects) { + objectMap.put(object.getId(), object); + firstObjects.add(object.getId()); + } + Query query = new Query(Type.SELECT, table); + query.addField(new Field(table + "." + type)); + query.addField(new Field(table + ".PROPERTY")); + query.addField(new Field(table + ".VALUE")); + OrWhereClause whereClause = new OrWhereClause(); + for (T object : objects) { + whereClause.add(new ValueFieldWhereClause(new ValueField(type, new StringParameter(object.getId())))); + } + query.addWhereClause(whereClause); + database.process(query, new ResultProcessor() { + + @Override + public void processResult(ResultSet resultSet) throws SQLException { + String id = resultSet.getString(table + "." + type); + T object = objectMap.get(id); + if (firstObjects.remove(id)) { + object.getProperties().removeAll(); + } + object.getProperties().set(resultSet.getString(table + ".PROPERTY"), resultSet.getString(table + ".VALUE")); + } + + }); + return objects; + } + + /** * {@link Artist} implementation that retrieves some attributes (such as * {@link #getGroups()}, and {@link #getTracks()}) from the * {@link DataManager} on demand. @@ -990,7 +1134,7 @@ public class DataManager { */ @Override public Group createObject(ResultSet resultSet) throws SQLException { - return new LazyGroup(resultSet.getString("GROUPS.ID")).setName(resultSet.getString("GROUPS.NAME")).setUrl(resultSet.getString("GROUPS.URL")); + return new LazyGroup(resultSet.getString("GROUPS.ID")).setName(resultSet.getString("GROUPS.NAME")); } }