2 * DemosceneMusic - DataManager.java - Copyright © 2012 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.demoscenemusic.data;
20 import java.sql.ResultSet;
21 import java.sql.SQLException;
22 import java.util.Collection;
23 import java.util.EnumMap;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.List;
28 import java.util.Map.Entry;
30 import java.util.UUID;
31 import java.util.concurrent.Callable;
33 import net.pterodactylus.demoscenemusic.data.Track.Relationship;
34 import net.pterodactylus.util.collection.Memoizer;
35 import net.pterodactylus.util.database.Database;
36 import net.pterodactylus.util.database.DatabaseException;
37 import net.pterodactylus.util.database.Field;
38 import net.pterodactylus.util.database.Join;
39 import net.pterodactylus.util.database.Join.JoinType;
40 import net.pterodactylus.util.database.ObjectCreator;
41 import net.pterodactylus.util.database.ObjectCreator.StringCreator;
42 import net.pterodactylus.util.database.OrWhereClause;
43 import net.pterodactylus.util.database.OrderField;
44 import net.pterodactylus.util.database.Parameter.IntegerParameter;
45 import net.pterodactylus.util.database.Parameter.StringParameter;
46 import net.pterodactylus.util.database.Query;
47 import net.pterodactylus.util.database.Query.Type;
48 import net.pterodactylus.util.database.ResultProcessor;
49 import net.pterodactylus.util.database.ValueField;
50 import net.pterodactylus.util.database.ValueFieldWhereClause;
53 * Interface between the database and the application.
55 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
57 public class DataManager {
59 /** The artist object creator. */
60 @SuppressWarnings("synthetic-access")
61 private final ObjectCreator<Artist> artistCreator = new ArtistCreator();
63 /** The group object creator. */
64 @SuppressWarnings("synthetic-access")
65 private final ObjectCreator<Group> groupCreator = new GroupCreator();
67 /** The track object creator. */
68 @SuppressWarnings("synthetic-access")
69 private final ObjectCreator<Track> trackCreator = new TrackCreator();
71 /** The track object creator. */
72 @SuppressWarnings("synthetic-access")
73 private final ObjectCreator<TrackDerivative> trackDerivativeCreator = new TrackDerivativeCreator();
75 /** The style object creator. */
76 @SuppressWarnings("synthetic-access")
77 private final ObjectCreator<Style> styleCreator = new StyleCreator();
79 /** The party object creator. */
80 @SuppressWarnings("synthetic-access")
81 private final ObjectCreator<Party> partyCreator = new PartyCreator();
83 /** The {@link User} object creator. */
84 @SuppressWarnings("synthetic-access")
85 private final ObjectCreator<User> userCreator = new UserCreator();
88 private final Database database;
91 * Creates a new data manager.
94 * The database to operate on
96 public DataManager(Database database) {
97 this.database = database;
101 * Returns all artists.
103 * @return All artists
104 * @throws DatabaseException
105 * if a database error occurs
107 public Collection<Artist> getAllArtists() throws DatabaseException {
108 Query query = new Query(Type.SELECT, "ARTISTS");
109 query.addField(new Field("ARTISTS.*"));
110 return loadArtistProperties(database.getMultiple(query, artistCreator));
114 * Returns the artist with the given ID.
117 * The ID of the artist
118 * @return The artist with the given ID, or {@code null} if there is no
119 * artist with the given ID
120 * @throws DatabaseException
121 * if a database error occurs
123 public Artist getArtistById(String id) throws DatabaseException {
124 Query query = new Query(Type.SELECT, "ARTISTS");
125 query.addField(new Field("ARTISTS.*"));
126 query.addWhereClause(new ValueFieldWhereClause(new ValueField("ARTISTS.ID", new StringParameter(id))));
127 return loadArtistProperties(database.getSingle(query, artistCreator));
131 * Returns all artists that belong to the group with the given ID.
134 * The ID of the group
135 * @return All artists belonging to the given group
136 * @throws DatabaseException
137 * if a database error occurs
139 public Collection<Artist> getArtistsByGroup(String groupId) throws DatabaseException {
140 Query query = new Query(Type.SELECT, "ARTISTS");
141 query.addField(new Field("ARTISTS.*"));
142 query.addJoin(new Join(JoinType.INNER, "GROUP_ARTISTS", new Field("ARTISTS.ID"), new Field("GROUP_ARTISTS.ARTIST")));
143 query.addWhereClause(new ValueFieldWhereClause(new ValueField("GROUP_ARTISTS.GROUP_", new StringParameter(groupId))));
144 return loadArtistProperties(database.getMultiple(query, artistCreator));
148 * Returns all artists involved in the track with the given ID.
151 * The ID of the track
152 * @return All artists involved in the track, in preferred order
153 * @throws DatabaseException
154 * if a database error occurs
156 public List<Artist> getArtistsByTrack(String trackId) throws DatabaseException {
157 Query query = new Query(Type.SELECT, "ARTISTS");
158 query.addField(new Field("ARTISTS.*"));
159 query.addJoin(new Join(JoinType.INNER, "TRACK_ARTISTS", new Field("TRACK_ARTISTS.ARTIST"), new Field("ARTISTS.ID")));
160 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_ARTISTS.TRACK", new StringParameter(trackId))));
161 query.addOrderField(new OrderField(new Field("TRACK_ARTISTS.DISPLAY_ORDER")));
162 return loadArtistProperties(database.getMultiple(query, artistCreator));
166 * Creates a new artist with the given name.
169 * The name of the artist
170 * @return The created artist
171 * @throws DatabaseException
172 * if a database error occurs
174 public Artist createArtist(String name) throws DatabaseException {
175 Query query = new Query(Type.INSERT, "ARTISTS");
176 String id = UUID.randomUUID().toString();
177 query.addValueField(new ValueField("ID", new StringParameter(id)));
178 query.addValueField(new ValueField("NAME", new StringParameter(name)));
179 database.insert(query);
180 return loadArtistProperties(getArtistById(id));
184 * Saves the given artist.
188 * @throws DatabaseException
189 * if a database error occurs
191 public void saveArtist(Artist artist) throws DatabaseException {
192 Query query = new Query(Type.UPDATE, "ARTISTS");
193 query.addValueField(new ValueField("NAME", new StringParameter(artist.getName())));
194 query.addWhereClause(new ValueFieldWhereClause(new ValueField("ID", new StringParameter(artist.getId()))));
195 database.update(query);
198 Collection<Group> groups = artist.getGroups();
199 query = new Query(Type.DELETE, "GROUP_ARTISTS");
200 query.addWhereClause(new ValueFieldWhereClause(new ValueField("GROUP_ARTISTS.ARTIST", new StringParameter(artist.getId()))));
201 database.update(query);
202 for (Group group : groups) {
203 query = new Query(Type.INSERT, "GROUP_ARTISTS");
204 query.addValueField(new ValueField("GROUP_", new StringParameter(group.getId())));
205 query.addValueField(new ValueField("ARTIST", new StringParameter(artist.getId())));
206 database.insert(query);
209 /* save properties. */
210 saveArtistProperties(artist);
214 * Saves the properties of the given artist.
217 * The artist whose properties to save
218 * @throws DatabaseException
219 * if a database error occurs
221 public void saveArtistProperties(Artist artist) throws DatabaseException {
222 saveProperties(artist.getProperties(), "ARTIST_PROPERTIES", "ARTIST", artist.getId());
226 * Saves the given properties to the given table for the given principal.
229 * The properties to save
231 * The table in which to save the properties
233 * The type of the principal (e. g. “ARTIST” or “TRACK”)
235 * The ID of the principial
236 * @throws DatabaseException
237 * if a database error occurs
239 public void saveProperties(Properties properties, String table, String type, String id) throws DatabaseException {
240 if (!properties.isDirty()) {
243 Query query = new Query(Type.DELETE, table);
244 query.addWhereClause(new ValueFieldWhereClause(new ValueField(type, new StringParameter(id))));
245 database.update(query);
246 for (Entry<String, String> property : properties) {
247 Query insertQuery = new Query(Type.INSERT, table);
248 insertQuery.addValueField(new ValueField(type, new StringParameter(id)));
249 insertQuery.addValueField(new ValueField("PROPERTY", new StringParameter(property.getKey())));
250 insertQuery.addValueField(new ValueField("VALUE", new StringParameter(property.getValue())));
251 database.insert(insertQuery);
253 properties.resetDirty();
257 * Loads the properties for an artist.
260 * The artist to load the properties for
262 * @throws DatabaseException
263 * if a database error occurs
265 public Artist loadArtistProperties(final Artist artist) throws DatabaseException {
266 return loadProperties(artist, "ARTIST_PROPERTIES", "ARTIST");
270 * Loads the properties of all given artists.
273 * The artists to load the properties for
274 * @return The list of artists
275 * @throws DatabaseException
276 * if a database error occurs
278 public List<Artist> loadArtistProperties(List<Artist> artists) throws DatabaseException {
279 return loadProperties(artists, "ARTIST_PROPERTIES", "ARTIST");
283 * Returns all remix artists involved in the track with the given ID.
286 * The ID of the track
287 * @return All remix artists involved in the track, in preferred order
288 * @throws DatabaseException
289 * if a database error occurs
291 public List<Artist> getRemixArtistsByTrack(String trackId) throws DatabaseException {
292 Query query = new Query(Type.SELECT, "ARTISTS");
293 query.addField(new Field("ARTISTS.*"));
294 query.addJoin(new Join(JoinType.INNER, "TRACK_REMIX_ARTISTS", new Field("TRACK_REMIX_ARTISTS.ARTIST"), new Field("ARTISTS.ID")));
295 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_REMIX_ARTISTS.TRACK", new StringParameter(trackId))));
296 query.addOrderField(new OrderField(new Field("TRACK_REMIX_ARTISTS.DISPLAY_ORDER")));
297 return database.getMultiple(query, artistCreator);
301 * Returns all related tracks for the track with the given ID.
304 * The ID of the tracks
305 * @return A mapping from relationship to all tracks that match the relation
306 * @throws DatabaseException
307 * if a database error occurs
309 public Map<Relationship, Collection<Track>> getRelatedTracksByTrack(String trackId) throws DatabaseException {
310 Query query = new Query(Type.SELECT, "TRACKS");
311 query.addField(new Field("TRACKS.*"));
312 query.addField(new Field("TRACK_RELATIONS.*"));
313 query.addJoin(new Join(JoinType.INNER, "TRACK_RELATIONS", new Field("TRACK_RELATIONS.TRACK"), new Field("TRACK_RELATIONS.RELATED_TRACK")));
314 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_RELATIONS.TRACK", new StringParameter(trackId))));
315 final Map<Relationship, Collection<Track>> relatedTracks = new EnumMap<Relationship, Collection<Track>>(Relationship.class);
316 database.process(query, new ResultProcessor() {
319 @SuppressWarnings("synthetic-access")
320 public void processResult(ResultSet resultSet) throws SQLException {
321 Track track = trackCreator.createObject(resultSet);
322 Relationship relationship = Relationship.valueOf(resultSet.getString("TRACK_RELATIONS.RELATIONSHIP"));
323 if (!relatedTracks.containsKey(relationship)) {
324 relatedTracks.put(relationship, new HashSet<Track>());
326 relatedTracks.get(relationship).add(track);
329 return relatedTracks;
333 * Returns the track with the given ID.
336 * The ID of the track
337 * @return The track with the given ID, or {@code null} if there is no such
339 * @throws DatabaseException
340 * if a database error occurs
342 public Track getTrackById(String id) throws DatabaseException {
343 Query query = new Query(Type.SELECT, "TRACKS");
344 query.addField(new Field("TRACKS.*"));
345 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACKS.ID", new StringParameter(id))));
346 return loadTrackProperties(database.getSingle(query, trackCreator));
350 * Returns the track that contains the derivative with the given ID.
353 * The ID of the track derivative
354 * @return The track the derivative belongs to, or {@code null} if there is
356 * @throws DatabaseException
357 * if a database error occurs
359 public Track getTrackByDerivativeId(String id) throws DatabaseException {
360 Query query = new Query(Type.SELECT, "TRACKS");
361 query.addField(new Field("TRACKS.*"));
362 query.addJoin(new Join(JoinType.INNER, "TRACK_DERIVATIVES", new Field("TRACK_DERIVATIVES.TRACK"), new Field("TRACKS.ID")));
363 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_DERIVATIVES.ID", new StringParameter(id))));
364 return loadTrackProperties(database.getSingle(query, trackCreator));
368 * Returns all tracks by the artist with the given ID.
371 * The ID of the artist
372 * @return All tracks by the given artist
373 * @throws DatabaseException
374 * if a database error occurs
376 public Collection<Track> getTracksByArtist(String artistId) throws DatabaseException {
377 Query query = new Query(Type.SELECT, "TRACKS");
378 query.addField(new Field("TRACKS.*"));
379 query.addJoin(new Join(JoinType.INNER, "TRACK_ARTISTS", new Field("TRACKS.ID"), new Field("TRACK_ARTISTS.TRACK")));
380 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_ARTISTS.ARTIST", new StringParameter(artistId))));
381 return loadTrackProperties(database.getMultiple(query, trackCreator));
385 * Returns all tracks that were released at the party with the given ID.
388 * The ID of the party
389 * @return All tracks that were released at the given party
390 * @throws DatabaseException
391 * if a database error occurs
393 public Collection<Track> getTracksByParty(String partyId) throws DatabaseException {
394 Query query = new Query(Type.SELECT, "TRACKS");
395 query.addField(new Field("TRACKS.*"));
396 query.addJoin(new Join(JoinType.INNER, "PARTY_TRACKS", new Field("PARTY_TRACKS.TRACK"), new Field("TRACKS.ID")));
397 query.addWhereClause(new ValueFieldWhereClause(new ValueField("PARTY_TRACKS.PARTY", new StringParameter(partyId))));
398 return loadTrackProperties(database.getMultiple(query, trackCreator));
402 * Loads the properties for the given track.
405 * The track for which to load the properties
406 * @return The track with the properties loaded
407 * @throws DatabaseException
408 * if a database error occurs
410 public Track loadTrackProperties(Track track) throws DatabaseException {
411 return loadProperties(track, "TRACK_PROPERTIES", "TRACK");
415 * Loads the properties for the given tracks.
418 * The tracks for which to load the properties
419 * @return The tracks with the properties loaded
420 * @throws DatabaseException
421 * if a database error occurs
423 public List<Track> loadTrackProperties(List<Track> tracks) throws DatabaseException {
424 return loadProperties(tracks, "TRACK_PROPERTIES", "TRACK");
428 * Creates a new track with the given name.
431 * The name of the track
432 * @return The created track
433 * @throws DatabaseException
434 * if a database error occurs
436 public Track createTrack(String name) throws DatabaseException {
437 Query query = new Query(Type.INSERT, "TRACKS");
438 String id = UUID.randomUUID().toString();
439 query.addValueField(new ValueField("ID", new StringParameter(id)));
440 query.addValueField(new ValueField("NAME", new StringParameter(name)));
441 database.insert(query);
442 return getTrackById(id);
446 * Saves the given track. This also saves all relationships of the track.
450 * @throws DatabaseException
451 * if a database error occurs
453 public void saveTrack(Track track) throws DatabaseException {
454 Query query = new Query(Type.UPDATE, "TRACKS");
455 query.addValueField(new ValueField("TRACKS.NAME", new StringParameter(track.getName())));
456 query.addValueField(new ValueField("TRACKS.REMIX", new StringParameter(track.getRemix())));
457 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACKS.ID", new StringParameter(track.getId()))));
458 database.update(query);
459 /* store artist information. */
460 track.getArtists(); /* prefetch artists. */
461 query = new Query(Type.DELETE, "TRACK_ARTISTS");
462 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_ARTISTS.TRACK", new StringParameter(track.getId()))));
463 database.update(query);
464 for (int index = 0; index < track.getArtists().size(); ++index) {
465 query = new Query(Type.INSERT, "TRACK_ARTISTS");
466 query.addValueField(new ValueField("TRACK_ARTISTS.TRACK", new StringParameter(track.getId())));
467 query.addValueField(new ValueField("TRACK_ARTISTS.ARTIST", new StringParameter(track.getArtists().get(index).getId())));
468 query.addValueField(new ValueField("TRACK_ARTISTS.DISPLAY_ORDER", new IntegerParameter(index + 1)));
469 database.insert(query);
472 /* store party links. */
473 Collection<Party> parties = track.getParties(); /* prefetch parties. */
474 query = new Query(Type.DELETE, "PARTY_TRACKS");
475 query.addWhereClause(new ValueFieldWhereClause(new ValueField("PARTY_TRACKS.TRACK", new StringParameter(track.getId()))));
476 database.update(query);
477 for (Party party : parties) {
478 query = new Query(Type.INSERT, "PARTY_TRACKS");
479 query.addValueField(new ValueField("PARTY_TRACKS.TRACK", new StringParameter(track.getId())));
480 query.addValueField(new ValueField("PARTY_TRACKS.PARTY", new StringParameter(party.getId())));
481 database.insert(query);
484 /* store properties. */
485 saveProperties(track.getProperties(), "TRACK_PROPERTIES", "TRACK", track.getId());
489 * Returns the derivative with the given ID.
492 * The ID of the derivatives to load
493 * @return The derivative with the given ID
494 * @throws DatabaseException
495 * if a database error occurs
497 public TrackDerivative getTrackDerivativeById(String id) throws DatabaseException {
498 Query query = new Query(Type.SELECT, "TRACK_DERIVATIVES");
499 query.addField(new Field("TRACK_DERIVATIVES.*"));
500 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_DERIVATIVES.ID", new StringParameter(id))));
501 return loadTrackDerivativeProperties(database.getSingle(query, trackDerivativeCreator));
505 * Returns the derivatives for the given track.
508 * The track ID to get the derivatives for
509 * @return The derivatives for the given track
510 * @throws DatabaseException
511 * if a database error occurs
513 public Collection<TrackDerivative> getTrackDerivativesByTrack(String trackId) throws DatabaseException {
514 Query query = new Query(Type.SELECT, "TRACK_DERIVATIVES");
515 query.addField(new Field("TRACK_DERIVATIVES.*"));
516 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_DERIVATIVES.TRACK", new StringParameter(trackId))));
517 return loadTrackDerivativeProperties(database.getMultiple(query, trackDerivativeCreator));
521 * Creates a new derivative for the given track.
524 * The track to create the derivative for
525 * @return The created derivative
526 * @throws DatabaseException
527 * if a database error occurs
529 public TrackDerivative createTrackDerivative(Track track) throws DatabaseException {
530 Query query = new Query(Type.INSERT, "TRACK_DERIVATIVES");
531 String id = UUID.randomUUID().toString();
532 query.addValueField(new ValueField("TRACK_DERIVATIVES.ID", new StringParameter(id)));
533 query.addValueField(new ValueField("TRACK_DERIVATIVES.TRACK", new StringParameter(track.getId())));
534 database.insert(query);
535 return getTrackDerivativeById(id);
539 * Loads the properties for the given track derivative.
541 * @param trackDerivative
542 * The track derivative to load the properties for
543 * @return The track derivative with its properties loaded
544 * @throws DatabaseException
545 * if a database error occurs
547 public TrackDerivative loadTrackDerivativeProperties(TrackDerivative trackDerivative) throws DatabaseException {
548 return loadProperties(trackDerivative, "TRACK_DERIVATIVE_PROPERTIES", "TRACK_DERIVATIVE");
552 * Loads the properties for the given track derivatives.
554 * @param trackDerivatives
555 * The track derivatives to load the properties for
556 * @return The track derivatives with their properties loaded
557 * @throws DatabaseException
558 * if a database error occurs
560 public List<TrackDerivative> loadTrackDerivativeProperties(List<TrackDerivative> trackDerivatives) throws DatabaseException {
561 return loadProperties(trackDerivatives, "TRACK_DERIVATIVE_PROPERTIES", "TRACK_DERIVATIVE");
565 * Saves the given track derivative. As a track derivative does not have any
566 * attributes of its own only its properties are saved.
568 * @param trackDerivative
569 * The track derivative to save
570 * @throws DatabaseException
571 * if a database error occurs
573 public void saveTrackDerivate(TrackDerivative trackDerivative) throws DatabaseException {
574 saveProperties(trackDerivative.getProperties(), "TRACK_DERIVATIVE_PROPERTIES", "TRACK_DERIVATIVE", trackDerivative.getId());
578 * Removes the given track derivative and all its properties from the
581 * @param trackDerivative
582 * The track derivative to remove
583 * @throws DatabaseException
584 * if a database error occurs
586 public void removeTrackDerivative(TrackDerivative trackDerivative) throws DatabaseException {
587 Query query = new Query(Type.DELETE, "TRACK_DERIVATIVES");
588 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_DERIVATIVES.ID", new StringParameter(trackDerivative.getId()))));
589 database.update(query);
590 /* remove the properties. */
591 saveProperties(new Properties().set("dirty", "true").removeAll(), "TRACK_DERIVATIVE_PROPERTIES", "TRACK_DERIVATIVE", trackDerivative.getId());
595 * Returns all groups.
598 * @throws DatabaseException
599 * if a database error occurs
601 public Collection<Group> getAllGroups() throws DatabaseException {
602 Query query = new Query(Type.SELECT, "GROUPS");
603 query.addField(new Field("GROUPS.*"));
604 return loadGroupProperties(database.getMultiple(query, groupCreator));
608 * Returns the group with the given ID.
611 * The ID of the group
612 * @return The group with the given ID, or {@code null} if no such group
614 * @throws DatabaseException
615 * if a database error occurs
617 public Group getGroupById(String groupId) throws DatabaseException {
618 Query query = new Query(Type.SELECT, "GROUPS");
619 query.addField(new Field("GROUPS.*"));
620 query.addWhereClause(new ValueFieldWhereClause(new ValueField("GROUPS.ID", new StringParameter(groupId))));
621 return loadGroupProperties(database.getSingle(query, groupCreator));
625 * Returns all groups the artist with the given ID belongs to.
628 * The ID of the artist
629 * @return All groups the artist belongs to
630 * @throws DatabaseException
631 * if a database error occurs
633 public Collection<Group> getGroupsByArtist(String artistId) throws DatabaseException {
634 Query query = new Query(Type.SELECT, "GROUPS");
635 query.addField(new Field("GROUPS.*"));
636 query.addJoin(new Join(JoinType.INNER, "GROUP_ARTISTS", new Field("GROUPS.ID"), new Field("GROUP_ARTISTS.GROUP_")));
637 query.addWhereClause(new ValueFieldWhereClause(new ValueField("GROUP_ARTISTS.ARTIST", new StringParameter(artistId))));
638 return database.getMultiple(query, groupCreator);
642 * Loads the properties of the given group.
645 * The group to load the properties for
646 * @return The group with its properties loaded
647 * @throws DatabaseException
648 * if a database error occurs
650 public Group loadGroupProperties(Group group) throws DatabaseException {
651 return loadProperties(group, "GROUP_PROPERTIES", "GROUP_ID");
655 * Loads the properties of the given groups.
658 * The groups to load the properties for
659 * @return The groups with their properties loaded
660 * @throws DatabaseException
661 * if a database error occurs
663 public Collection<Group> loadGroupProperties(List<Group> groups) throws DatabaseException {
664 return loadProperties(groups, "GROUP_PROPERTIES", "GROUP_ID");
668 * Creates a group with the given name.
671 * The name of the new group
672 * @return The new group
673 * @throws DatabaseException
674 * if a database error occurs
676 public Group createGroup(String name) throws DatabaseException {
677 Query query = new Query(Type.INSERT, "GROUPS");
678 String id = UUID.randomUUID().toString();
679 query.addValueField(new ValueField("ID", new StringParameter(id)));
680 query.addValueField(new ValueField("NAME", new StringParameter(name)));
681 database.insert(query);
682 return getGroupById(id);
686 * Saves the given group.
690 * @throws DatabaseException
691 * if a database error occurs
693 public void saveGroup(Group group) throws DatabaseException {
694 Query query = new Query(Type.UPDATE, "GROUPS");
695 query.addValueField(new ValueField("NAME", new StringParameter(group.getName())));
696 query.addWhereClause(new ValueFieldWhereClause(new ValueField("ID", new StringParameter(group.getId()))));
697 database.update(query);
698 /* save properties. */
699 saveGroupProperties(group);
703 * Saves the properties of the given group.
706 * The group whose properties to save
707 * @throws DatabaseException
708 * if a database error occurs
710 public void saveGroupProperties(Group group) throws DatabaseException {
711 saveProperties(group.getProperties(), "GROUP_PROPERTIES", "GROUP_ID", group.getId());
715 * Returns all styles for the track with the given ID.
718 * The ID of the track
719 * @return All styles for the given track
720 * @throws DatabaseException
721 * if a database error occurs
723 public Collection<Style> getStylesByTrack(String trackId) throws DatabaseException {
724 Query query = new Query(Type.SELECT, "STYLES");
725 query.addField(new Field("STYLES.*"));
726 query.addJoin(new Join(JoinType.INNER, "TRACK_STYLES", new Field("STYLES.ID"), new Field("TRACK_STYLES.STYLE")));
727 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_STYLES.TRACK", new StringParameter(trackId))));
728 return database.getMultiple(query, styleCreator);
732 * Returns all parties.
734 * @return All parties
735 * @throws DatabaseException
736 * if a database error occurs
738 public Collection<Party> getAllParties() throws DatabaseException {
739 Query query = new Query(Type.SELECT, "PARTIES");
740 query.addField(new Field("PARTIES.*"));
741 return loadPartyProperties(database.getMultiple(query, partyCreator));
745 * Returns all parties that the track with the given ID was released at.
748 * The ID of the track
749 * @return All parties the track was released at
750 * @throws DatabaseException
751 * if a database error occurs
753 public Collection<Party> getPartiesByTrackId(String trackId) throws DatabaseException {
754 Query query = new Query(Type.SELECT, "PARTIES");
755 query.addField(new Field("PARTIES.*"));
756 query.addJoin(new Join(JoinType.INNER, "PARTY_TRACKS", new Field("PARTY_TRACKS.PARTY"), new Field("PARTIES.ID")));
757 query.addWhereClause(new ValueFieldWhereClause(new ValueField("PARTY_TRACKS.TRACK", new StringParameter(trackId))));
758 return loadPartyProperties(database.getMultiple(query, partyCreator));
762 * Returns the party with the given ID.
765 * The ID of the party
766 * @return The party with the given ID
767 * @throws DatabaseException
768 * if a database error occurs
770 public Party getPartyById(String partyId) throws DatabaseException {
771 Query query = new Query(Type.SELECT, "PARTIES");
772 query.addField(new Field("PARTIES.*"));
773 query.addWhereClause(new ValueFieldWhereClause(new ValueField("PARTIES.ID", new StringParameter(partyId))));
774 return loadPartyProperties(database.getSingle(query, partyCreator));
778 * Loads the properties of the given party.
781 * The party to load the properties for
782 * @return The party with its properties loaded
783 * @throws DatabaseException
784 * if a database error occurs
786 public Party loadPartyProperties(Party party) throws DatabaseException {
787 return loadProperties(party, "PARTY_PROPERTIES", "PARTY");
791 * Loads the properties of the given parties.
794 * The parties to load the properties for
795 * @return The parties with their properties loaded
796 * @throws DatabaseException
797 * if a database error occurs
799 public List<Party> loadPartyProperties(List<Party> parties) throws DatabaseException {
800 return loadProperties(parties, "PARTY_PROPERTIES", "PARTY");
804 * Saves the given party.
808 * @throws DatabaseException
809 * if a database error occurs
811 public void saveParty(Party party) throws DatabaseException {
812 Query query = new Query(Type.UPDATE, "PARTIES");
813 query.addValueField(new ValueField("NAME", new StringParameter(party.getName())));
814 query.addWhereClause(new ValueFieldWhereClause(new ValueField("ID", new StringParameter(party.getId()))));
815 database.update(query);
816 savePartyProperties(party);
820 * Saves the properties of the given party.
823 * The party whose properties to save
824 * @throws DatabaseException
825 * if a database error occurs
827 public void savePartyProperties(Party party) throws DatabaseException {
828 saveProperties(party.getProperties(), "PARTY_PROPERTIES", "PARTY", party.getId());
832 * Creates a new party with the given name.
835 * The name of the party
836 * @return The new party
837 * @throws DatabaseException
838 * if a database error occurs
840 public Party createParty(String name) throws DatabaseException {
841 Query query = new Query(Type.INSERT, "PARTIES");
842 String id = UUID.randomUUID().toString();
843 query.addValueField(new ValueField("ID", new StringParameter(id)));
844 query.addValueField(new ValueField("NAME", new StringParameter(name)));
845 database.insert(query);
846 return getPartyById(id);
850 * Returns the user with the given name.
853 * The name of the user
854 * @return The user, or {@code null} if the user does not exist
855 * @throws DatabaseException
856 * if a database error occurs
858 public User getUserByName(String username) throws DatabaseException {
859 Query query = new Query(Type.SELECT, "USERS");
860 query.addField(new Field("USERS.*"));
861 query.addWhereClause(new ValueFieldWhereClause(new ValueField("USERS.NAME", new StringParameter(username))));
862 return database.getSingle(query, userCreator);
866 * Returns the user connected with the given OpenID.
869 * The OpenID to find the user for
870 * @return The user connected with the given OpenID, or {@code null} if
871 * there is no such user
872 * @throws DatabaseException
873 * if a database error occurs
875 public User getUserByOpenId(String openId) throws DatabaseException {
876 Query query = new Query(Type.SELECT, "USERS");
877 query.addField(new Field("USERS.*"));
878 query.addJoin(new Join(JoinType.INNER, "USER_OPENIDS", new Field("USER_OPENIDS.USER"), new Field("USERS.ID")));
879 query.addWhereClause(new ValueFieldWhereClause(new ValueField("USER_OPENIDS.OPENID", new StringParameter(openId))));
880 return database.getSingle(query, userCreator);
884 * Returns all OpenIDs connected with the user with the given ID.
888 * @return All OpenIDs connected with the given user
889 * @throws DatabaseException
890 * if a database error occurs
892 public Collection<String> getOpenIdsByUser(String userId) throws DatabaseException {
893 Query query = new Query(Type.SELECT, "USER_OPENIDS");
894 query.addField(new Field("USER_OPENIDS.*"));
895 query.addWhereClause(new ValueFieldWhereClause(new ValueField("USER_OPENIDS.USER", new StringParameter(userId))));
896 return database.getMultiple(query, new StringCreator("USER_OPENIDS.OPENID"));
904 * Loads the properties for the given object.
907 * The type of the object
911 * The table to load the properties from
913 * The type of the object (“ARTIST,” “TRACK,” etc.)
914 * @return The object with its properties loaded
915 * @throws DatabaseException
916 * if a database error occurs
918 private <T extends Base> T loadProperties(final T object, final String table, String type) throws DatabaseException {
919 if (object == null) {
922 Query query = new Query(Type.SELECT, table);
923 query.addField(new Field(table + ".PROPERTY"));
924 query.addField(new Field(table + ".VALUE"));
925 query.addWhereClause(new ValueFieldWhereClause(new ValueField(type, new StringParameter(object.getId()))));
926 database.process(query, new ResultProcessor() {
929 public void processResult(ResultSet resultSet) throws SQLException {
930 if (resultSet.isFirst()) {
931 object.getProperties().removeAll();
933 object.getProperties().set(resultSet.getString(table + ".PROPERTY"), resultSet.getString(table + ".VALUE"));
941 * Loads the properties for the given objects.
944 * The type of the object
948 * The table to load the properties from
950 * The type of the object (“ARTIST,” “TRACK,” etc.)
951 * @return The object with its properties loaded
952 * @throws DatabaseException
953 * if a database error occurs
955 private <T extends Base> List<T> loadProperties(List<T> objects, final String table, final String type) throws DatabaseException {
956 final Map<String, T> objectMap = new HashMap<String, T>();
957 final Set<String> firstObjects = new HashSet<String>();
958 for (T object : objects) {
959 objectMap.put(object.getId(), object);
960 firstObjects.add(object.getId());
962 Query query = new Query(Type.SELECT, table);
963 query.addField(new Field(table + "." + type));
964 query.addField(new Field(table + ".PROPERTY"));
965 query.addField(new Field(table + ".VALUE"));
966 OrWhereClause whereClause = new OrWhereClause();
967 for (T object : objects) {
968 whereClause.add(new ValueFieldWhereClause(new ValueField(type, new StringParameter(object.getId()))));
970 query.addWhereClause(whereClause);
971 database.process(query, new ResultProcessor() {
974 public void processResult(ResultSet resultSet) throws SQLException {
975 String id = resultSet.getString(table + "." + type);
976 T object = objectMap.get(id);
977 if (firstObjects.remove(id)) {
978 object.getProperties().removeAll();
980 object.getProperties().set(resultSet.getString(table + ".PROPERTY"), resultSet.getString(table + ".VALUE"));
988 * {@link Artist} implementation that retrieves some attributes (such as
989 * {@link #getGroups()}, and {@link #getTracks()}) from the
990 * {@link DataManager} on demand.
992 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
994 private class LazyArtist extends DefaultArtist {
996 /** Memoizer for the tracks by this artist. */
997 private final Memoizer<Void> tracksMemoizer = new Memoizer<Void>(new Callable<Void>() {
1000 public Void call() throws DatabaseException {
1001 if (!hasValue("tracks")) {
1002 getValue("tracks", Collection.class).set(getTracksByArtist(getId()));
1008 /** Memoizer for the groups of this artist. */
1009 private final Memoizer<Void> groupsMemoizer = new Memoizer<Void>(new Callable<Void>() {
1012 public Void call() throws Exception {
1013 if (!hasValue("groups")) {
1014 getValue("groups", Collection.class).set(getGroupsByArtist(getId()));
1022 * Creates a new lazy artist.
1025 * The ID of the artist
1027 public LazyArtist(String id) {
1032 // DEFAULTARTIST METHODS
1039 public Collection<Group> getGroups() {
1040 groupsMemoizer.get();
1041 return super.getGroups();
1048 public Collection<Track> getTracks() {
1049 tracksMemoizer.get();
1050 return super.getTracks();
1056 * {@link ObjectCreator} implementation that can create {@link Artist}
1057 * objects. This specific class actually creates {@link LazyArtist}
1060 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1062 private class ArtistCreator implements ObjectCreator<Artist> {
1068 public Artist createObject(ResultSet resultSet) throws SQLException {
1069 return new LazyArtist(resultSet.getString("ARTISTS.ID")).setName(resultSet.getString("ARTISTS.NAME"));
1075 * {@link Group} implementation that retrieves some attributes (such as
1076 * {@link #getArtists()}) from the {@link DataManager} on demand.
1078 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1080 private class LazyGroup extends DefaultGroup {
1082 /** Memoizer for the artist. */
1083 private final Memoizer<Void> artistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
1086 public Void call() throws Exception {
1087 if (!hasValue("artists")) {
1088 getValue("artists", Collection.class).set(getArtistsByGroup(getId()));
1096 * Creates a new lazy group.
1099 * The ID of the group
1101 public LazyGroup(String id) {
1106 // DEFAULTGROUP METHODS
1113 public Collection<Artist> getArtists() {
1114 artistsMemoizer.get();
1115 return super.getArtists();
1121 * {@link ObjectCreator} implementation that can create {@link Group}
1122 * objects. This specific implementation creates {@link LazyGroup}
1125 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1127 private class GroupCreator implements ObjectCreator<Group> {
1133 public Group createObject(ResultSet resultSet) throws SQLException {
1134 return new LazyGroup(resultSet.getString("GROUPS.ID")).setName(resultSet.getString("GROUPS.NAME"));
1140 * {@link Track} implementation that retrieves some attributes (such as
1141 * {@link #getArtists()}, and {@link #getStyles()}) from the
1142 * {@link DataManager} on demand.
1144 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1146 private class LazyTrack extends DefaultTrack {
1148 /** Memoizer for the artists. */
1149 private final Memoizer<Void> artistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
1152 public Void call() throws Exception {
1153 if (!hasValue("artists")) {
1154 getValue("artists", List.class).set(getArtistsByTrack(getId()));
1161 /** Memoizer for the styles. */
1162 private final Memoizer<Void> stylesMemoizer = new Memoizer<Void>(new Callable<Void>() {
1165 public Void call() throws Exception {
1166 if (!hasValue("styles")) {
1167 getValue("styles", Collection.class).set(getStylesByTrack(getId()));
1174 /** Memoizer for the remix artists. */
1175 private final Memoizer<Void> remixArtistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
1178 public Void call() throws Exception {
1179 if (!hasValue("remixArtists")) {
1180 getValue("remixArtists", List.class).set(getRemixArtistsByTrack(getId()));
1187 /** Memoizer for the track derivatives. */
1188 private final Memoizer<Void> derivativesMemoizer = new Memoizer<Void>(new Callable<Void>() {
1191 public Void call() throws Exception {
1192 if (!hasValue("derivatives")) {
1193 getValue("derivatives", Collection.class).set(getTrackDerivativesByTrack(getId()));
1200 /** Memoizer for the related tracks. */
1201 private final Memoizer<Void> relatedTracksMemoizer = new Memoizer<Void>(new Callable<Void>() {
1204 public Void call() throws Exception {
1205 if (!hasValue("relatedTracks")) {
1206 getValue("relatedTracks", Map.class).set(getRelatedTracksByTrack(getId()));
1214 * Creates a new track.
1217 * The ID of the track
1219 public LazyTrack(String id) {
1224 // DEFAULTTRACK METHODS
1231 public List<Artist> getArtists() {
1232 artistsMemoizer.get();
1233 return super.getArtists();
1240 public Collection<Style> getStyles() {
1241 stylesMemoizer.get();
1242 return super.getStyles();
1249 public List<Artist> getRemixArtists() {
1250 remixArtistsMemoizer.get();
1251 return super.getRemixArtists();
1258 public Collection<TrackDerivative> getDerivatives() {
1259 derivativesMemoizer.get();
1260 return super.getDerivatives();
1267 public Map<Relationship, Collection<Track>> getRelatedTracks() {
1268 relatedTracksMemoizer.get();
1269 return super.getRelatedTracks();
1276 public Collection<Party> getParties() {
1277 if (!hasValue("parties")) {
1279 getValue("parties", Collection.class).set(getPartiesByTrackId(getId()));
1280 } catch (DatabaseException de1) {
1281 throw new RuntimeException("Could not load Parties for Track " + getId() + ".", de1);
1284 return super.getParties();
1290 * {@link ObjectCreator} implementation that can create {@link Track}
1291 * objects. This specific implementation creates {@link LazyTrack}
1294 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1296 private class TrackCreator implements ObjectCreator<Track> {
1302 public Track createObject(ResultSet resultSet) throws SQLException {
1303 return new LazyTrack(resultSet.getString("TRACKS.ID")).setName(resultSet.getString("TRACKS.NAME")).setRemix(resultSet.getString("TRACKS.REMIX"));
1309 * {@link ObjectCreator} implementation that can create
1310 * {@link TrackDerivative} objects.
1312 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1314 private class TrackDerivativeCreator implements ObjectCreator<TrackDerivative> {
1320 public TrackDerivative createObject(ResultSet resultSet) throws SQLException {
1321 return new DefaultTrackDerivative(resultSet.getString("TRACK_DERIVATIVES.ID"));
1327 * {@link ObjectCreator} implementation that can create {@link Style}
1330 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1332 private class StyleCreator implements ObjectCreator<Style> {
1338 public Style createObject(ResultSet resultSet) throws SQLException {
1339 return new DefaultStyle(resultSet.getString("STYLES.ID")).setName(resultSet.getString("STYLES.NAME"));
1345 * {@link Party} implementation that loads additional information only on
1348 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1350 private class LazyParty extends DefaultParty {
1353 * Creates a new party.
1356 * The ID of the party
1358 public LazyParty(String id) {
1370 public Collection<Track> getReleases() {
1371 if (!hasValue("releases")) {
1373 getValue("releases", Collection.class).set(getTracksByParty(getId()));
1374 } catch (DatabaseException de1) {
1375 throw new RuntimeException("Could not loaded Tracks for Party " + getId() + ".", de1);
1378 return super.getReleases();
1384 * {@link ObjectCreator} implementation that can create {@link Party}
1387 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1389 private class PartyCreator implements ObjectCreator<Party> {
1395 public Party createObject(ResultSet resultSet) throws SQLException {
1396 return new LazyParty(resultSet.getString("PARTIES.ID")).setName(resultSet.getString("PARTIES.NAME"));
1402 * {@link User} implementation that retrieves some attributes (such as
1403 * {@link #getOpenIds()}) from the {@link DataManager} on demand.
1405 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1407 private class LazyUser extends DefaultUser {
1409 /** Memoizer for a user’s OpenIDs. */
1410 private final Memoizer<Void> openIdMemoizer = new Memoizer<Void>(new Callable<Void>() {
1413 public Void call() throws Exception {
1414 if (!hasValue("openIds")) {
1415 getValue("openIds", Collection.class).set(getOpenIdsByUser(getId()));
1422 * Creates a new user.
1425 * The ID of the user
1427 public LazyUser(String id) {
1435 public Collection<String> getOpenIds() {
1436 openIdMemoizer.get();
1437 return super.getOpenIds();
1443 * {@link ObjectCreator} implementation that can create {@link User}
1444 * objects. This specific implementation creates {@link LazyUser} instances.
1446 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1448 private class UserCreator implements ObjectCreator<User> {
1454 public User createObject(ResultSet resultSet) throws SQLException {
1455 return new LazyUser(resultSet.getString("USERS.ID")).setName(resultSet.getString("USERS.NAME")).setPasswordHash(resultSet.getString("USERS.PASSWORD")).setLevel(resultSet.getInt("USERS.LEVEL"));