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.HashSet;
25 import java.util.List;
27 import java.util.Map.Entry;
28 import java.util.UUID;
29 import java.util.concurrent.Callable;
31 import net.pterodactylus.demoscenemusic.data.Track.Relationship;
32 import net.pterodactylus.util.collection.Memoizer;
33 import net.pterodactylus.util.database.Database;
34 import net.pterodactylus.util.database.DatabaseException;
35 import net.pterodactylus.util.database.Field;
36 import net.pterodactylus.util.database.Join;
37 import net.pterodactylus.util.database.Join.JoinType;
38 import net.pterodactylus.util.database.ObjectCreator;
39 import net.pterodactylus.util.database.ObjectCreator.StringCreator;
40 import net.pterodactylus.util.database.OrderField;
41 import net.pterodactylus.util.database.Parameter.IntegerParameter;
42 import net.pterodactylus.util.database.Parameter.StringParameter;
43 import net.pterodactylus.util.database.Query;
44 import net.pterodactylus.util.database.Query.Type;
45 import net.pterodactylus.util.database.ResultProcessor;
46 import net.pterodactylus.util.database.ValueField;
47 import net.pterodactylus.util.database.ValueFieldWhereClause;
50 * Interface between the database and the application.
52 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
54 public class DataManager {
56 /** The artist object creator. */
57 @SuppressWarnings("synthetic-access")
58 private final ObjectCreator<Artist> artistCreator = new ArtistCreator();
60 /** The group object creator. */
61 @SuppressWarnings("synthetic-access")
62 private final ObjectCreator<Group> groupCreator = new GroupCreator();
64 /** The track object creator. */
65 @SuppressWarnings("synthetic-access")
66 private final ObjectCreator<Track> trackCreator = new TrackCreator();
68 /** The track object creator. */
69 @SuppressWarnings("synthetic-access")
70 private final ObjectCreator<TrackDerivative> trackDerivativeCreator = new TrackDerivativeCreator();
72 /** The style object creator. */
73 @SuppressWarnings("synthetic-access")
74 private final ObjectCreator<Style> styleCreator = new StyleCreator();
76 /** The party object creator. */
77 @SuppressWarnings("synthetic-access")
78 private final ObjectCreator<Party> partyCreator = new PartyCreator();
80 /** The {@link User} object creator. */
81 @SuppressWarnings("synthetic-access")
82 private final ObjectCreator<User> userCreator = new UserCreator();
85 private final Database database;
88 * Creates a new data manager.
91 * The database to operate on
93 public DataManager(Database database) {
94 this.database = database;
98 * Returns all artists.
100 * @return All artists
101 * @throws DatabaseException
102 * if a database error occurs
104 public Collection<Artist> getAllArtists() throws DatabaseException {
105 Query query = new Query(Type.SELECT, "ARTISTS");
106 query.addField(new Field("ARTISTS.*"));
107 return loadArtistProperties(database.getMultiple(query, artistCreator));
111 * Returns the artist with the given ID.
114 * The ID of the artist
115 * @return The artist with the given ID, or {@code null} if there is no
116 * artist with the given ID
117 * @throws DatabaseException
118 * if a database error occurs
120 public Artist getArtistById(String id) throws DatabaseException {
121 Query query = new Query(Type.SELECT, "ARTISTS");
122 query.addField(new Field("ARTISTS.*"));
123 query.addWhereClause(new ValueFieldWhereClause(new ValueField("ARTISTS.ID", new StringParameter(id))));
124 return loadArtistProperties(database.getSingle(query, artistCreator));
128 * Returns all artists that belong to the group with the given ID.
131 * The ID of the group
132 * @return All artists belonging to the given group
133 * @throws DatabaseException
134 * if a database error occurs
136 public Collection<Artist> getArtistsByGroup(String groupId) throws DatabaseException {
137 Query query = new Query(Type.SELECT, "ARTISTS");
138 query.addField(new Field("ARTISTS.*"));
139 query.addJoin(new Join(JoinType.INNER, "GROUP_ARTISTS", new Field("ARTISTS.ID"), new Field("GROUP_ARTISTS.ARTIST")));
140 query.addWhereClause(new ValueFieldWhereClause(new ValueField("GROUP_ARTISTS.GROUP_", new StringParameter(groupId))));
141 return loadArtistProperties(database.getMultiple(query, artistCreator));
145 * Returns all artists involved in the track with the given ID.
148 * The ID of the track
149 * @return All artists involved in the track, in preferred order
150 * @throws DatabaseException
151 * if a database error occurs
153 public List<Artist> getArtistsByTrack(String trackId) throws DatabaseException {
154 Query query = new Query(Type.SELECT, "ARTISTS");
155 query.addField(new Field("ARTISTS.*"));
156 query.addJoin(new Join(JoinType.INNER, "TRACK_ARTISTS", new Field("TRACK_ARTISTS.ARTIST"), new Field("ARTISTS.ID")));
157 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_ARTISTS.TRACK", new StringParameter(trackId))));
158 query.addOrderField(new OrderField(new Field("TRACK_ARTISTS.DISPLAY_ORDER")));
159 return loadArtistProperties(database.getMultiple(query, artistCreator));
163 * Creates a new artist with the given name.
166 * The name of the artist
167 * @return The created artist
168 * @throws DatabaseException
169 * if a database error occurs
171 public Artist createArtist(String name) throws DatabaseException {
172 Query query = new Query(Type.INSERT, "ARTISTS");
173 String id = UUID.randomUUID().toString();
174 query.addValueField(new ValueField("ID", new StringParameter(id)));
175 query.addValueField(new ValueField("NAME", new StringParameter(name)));
176 database.insert(query);
177 return loadArtistProperties(getArtistById(id));
181 * Saves the given artist.
185 * @throws DatabaseException
186 * if a database error occurs
188 public void saveArtist(Artist artist) throws DatabaseException {
189 Query query = new Query(Type.UPDATE, "ARTISTS");
190 query.addValueField(new ValueField("NAME", new StringParameter(artist.getName())));
191 query.addWhereClause(new ValueFieldWhereClause(new ValueField("ID", new StringParameter(artist.getId()))));
192 database.update(query);
193 saveArtistProperties(artist);
197 * Saves the properties of the given artist.
200 * The artist whose properties to save
201 * @throws DatabaseException
202 * if a database error occurs
204 public void saveArtistProperties(Artist artist) throws DatabaseException {
205 saveProperties(artist.getProperties(), "ARTIST_PROPERTIES", "ARTIST", artist.getId());
209 * Saves the given properties to the given table for the given principal.
212 * The properties to save
214 * The table in which to save the properties
216 * The type of the principal (e. g. “ARTIST” or “TRACK”)
218 * The ID of the principial
219 * @throws DatabaseException
220 * if a database error occurs
222 public void saveProperties(Properties properties, String table, String type, String id) throws DatabaseException {
223 if (!properties.isDirty()) {
226 Query query = new Query(Type.DELETE, table);
227 query.addWhereClause(new ValueFieldWhereClause(new ValueField(type, new StringParameter(id))));
228 database.update(query);
229 for (Entry<String, String> property : properties) {
230 Query insertQuery = new Query(Type.INSERT, table);
231 insertQuery.addValueField(new ValueField(type, new StringParameter(id)));
232 insertQuery.addValueField(new ValueField("PROPERTY", new StringParameter(property.getKey())));
233 insertQuery.addValueField(new ValueField("VALUE", new StringParameter(property.getValue())));
234 database.insert(insertQuery);
236 properties.resetDirty();
240 * Loads the properties for an artist.
243 * The artist to load the properties for
245 * @throws DatabaseException
246 * if a database error occurs
248 public Artist loadArtistProperties(final Artist artist) throws DatabaseException {
249 return loadProperties(artist, "ARTIST_PROPERTIES", "ARTIST");
253 * Loads the properties of all given artists.
256 * The artists to load the properties for
257 * @return The list of artists
258 * @throws DatabaseException
259 * if a database error occurs
261 public List<Artist> loadArtistProperties(List<Artist> artists) throws DatabaseException {
262 for (Artist artist : artists) {
263 loadArtistProperties(artist);
269 * Returns all remix artists involved in the track with the given ID.
272 * The ID of the track
273 * @return All remix artists involved in the track, in preferred order
274 * @throws DatabaseException
275 * if a database error occurs
277 public List<Artist> getRemixArtistsByTrack(String trackId) throws DatabaseException {
278 Query query = new Query(Type.SELECT, "ARTISTS");
279 query.addField(new Field("ARTISTS.*"));
280 query.addJoin(new Join(JoinType.INNER, "TRACK_REMIX_ARTISTS", new Field("TRACK_REMIX_ARTISTS.ARTIST"), new Field("ARTISTS.ID")));
281 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_REMIX_ARTISTS.TRACK", new StringParameter(trackId))));
282 query.addOrderField(new OrderField(new Field("TRACK_REMIX_ARTISTS.DISPLAY_ORDER")));
283 return database.getMultiple(query, artistCreator);
287 * Returns all related tracks for the track with the given ID.
290 * The ID of the tracks
291 * @return A mapping from relationship to all tracks that match the relation
292 * @throws DatabaseException
293 * if a database error occurs
295 public Map<Relationship, Collection<Track>> getRelatedTracksByTrack(String trackId) throws DatabaseException {
296 Query query = new Query(Type.SELECT, "TRACKS");
297 query.addField(new Field("TRACKS.*"));
298 query.addField(new Field("TRACK_RELATIONS.*"));
299 query.addJoin(new Join(JoinType.INNER, "TRACK_RELATIONS", new Field("TRACK_RELATIONS.TRACK"), new Field("TRACK_RELATIONS.RELATED_TRACK")));
300 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_RELATIONS.TRACK", new StringParameter(trackId))));
301 final Map<Relationship, Collection<Track>> relatedTracks = new EnumMap<Relationship, Collection<Track>>(Relationship.class);
302 database.process(query, new ResultProcessor() {
305 @SuppressWarnings("synthetic-access")
306 public void processResult(ResultSet resultSet) throws SQLException {
307 Track track = trackCreator.createObject(resultSet);
308 Relationship relationship = Relationship.valueOf(resultSet.getString("TRACK_RELATIONS.RELATIONSHIP"));
309 if (!relatedTracks.containsKey(relationship)) {
310 relatedTracks.put(relationship, new HashSet<Track>());
312 relatedTracks.get(relationship).add(track);
315 return relatedTracks;
319 * Returns the track with the given ID.
322 * The ID of the track
323 * @return The track with the given ID, or {@code null} if there is no such
325 * @throws DatabaseException
326 * if a database error occurs
328 public Track getTrackById(String id) throws DatabaseException {
329 Query query = new Query(Type.SELECT, "TRACKS");
330 query.addField(new Field("TRACKS.*"));
331 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACKS.ID", new StringParameter(id))));
332 return loadTrackProperties(database.getSingle(query, trackCreator));
336 * Returns the track that contains the derivative with the given ID.
339 * The ID of the track derivative
340 * @return The track the derivative belongs to, or {@code null} if there is
342 * @throws DatabaseException
343 * if a database error occurs
345 public Track getTrackByDerivativeId(String id) throws DatabaseException {
346 Query query = new Query(Type.SELECT, "TRACKS");
347 query.addField(new Field("TRACKS.*"));
348 query.addJoin(new Join(JoinType.INNER, "TRACK_DERIVATIVES", new Field("TRACK_DERIVATIVES.TRACK"), new Field("TRACKS.ID")));
349 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_DERIVATIVES.ID", new StringParameter(id))));
350 return loadTrackProperties(database.getSingle(query, trackCreator));
354 * Returns all tracks by the artist with the given ID.
357 * The ID of the artist
358 * @return All tracks by the given artist
359 * @throws DatabaseException
360 * if a database error occurs
362 public Collection<Track> getTracksByArtist(String artistId) throws DatabaseException {
363 Query query = new Query(Type.SELECT, "TRACKS");
364 query.addField(new Field("TRACKS.*"));
365 query.addJoin(new Join(JoinType.INNER, "TRACK_ARTISTS", new Field("TRACKS.ID"), new Field("TRACK_ARTISTS.TRACK")));
366 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_ARTISTS.ARTIST", new StringParameter(artistId))));
367 return loadTrackProperties(database.getMultiple(query, trackCreator));
371 * Returns all tracks that were released at the party with the given ID.
374 * The ID of the party
375 * @return All tracks that were released at the given party
376 * @throws DatabaseException
377 * if a database error occurs
379 public Collection<Track> getTracksByParty(String partyId) throws DatabaseException {
380 Query query = new Query(Type.SELECT, "TRACKS");
381 query.addField(new Field("TRACKS.*"));
382 query.addJoin(new Join(JoinType.INNER, "PARTY_TRACKS", new Field("PARTY_TRACKS.TRACK"), new Field("TRACKS.ID")));
383 query.addWhereClause(new ValueFieldWhereClause(new ValueField("PARTY_TRACKS.PARTY", new StringParameter(partyId))));
384 return loadTrackProperties(database.getMultiple(query, trackCreator));
388 * Loads the properties for the given track.
391 * The track for which to load the properties
392 * @return The track with the properties loaded
393 * @throws DatabaseException
394 * if a database error occurs
396 public Track loadTrackProperties(Track track) throws DatabaseException {
397 return loadProperties(track, "TRACK_PROPERTIES", "TRACK");
401 * Loads the properties for the given tracks.
404 * The tracks for which to load the properties
405 * @return The tracks with the properties loaded
406 * @throws DatabaseException
407 * if a database error occurs
409 public List<Track> loadTrackProperties(List<Track> tracks) throws DatabaseException {
410 for (Track track : tracks) {
411 loadTrackProperties(track);
417 * Creates a new track with the given name.
420 * The name of the track
421 * @return The created track
422 * @throws DatabaseException
423 * if a database error occurs
425 public Track createTrack(String name) throws DatabaseException {
426 Query query = new Query(Type.INSERT, "TRACKS");
427 String id = UUID.randomUUID().toString();
428 query.addValueField(new ValueField("ID", new StringParameter(id)));
429 query.addValueField(new ValueField("NAME", new StringParameter(name)));
430 database.insert(query);
431 return getTrackById(id);
435 * Saves the given track. This also saves all relationships of the track.
439 * @throws DatabaseException
440 * if a database error occurs
442 public void saveTrack(Track track) throws DatabaseException {
443 Query query = new Query(Type.UPDATE, "TRACKS");
444 query.addValueField(new ValueField("TRACKS.NAME", new StringParameter(track.getName())));
445 query.addValueField(new ValueField("TRACKS.REMIX", new StringParameter(track.getRemix())));
446 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACKS.ID", new StringParameter(track.getId()))));
447 database.update(query);
448 /* store artist information. */
449 track.getArtists(); /* prefetch artists. */
450 query = new Query(Type.DELETE, "TRACK_ARTISTS");
451 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_ARTISTS.TRACK", new StringParameter(track.getId()))));
452 database.update(query);
453 for (int index = 0; index < track.getArtists().size(); ++index) {
454 query = new Query(Type.INSERT, "TRACK_ARTISTS");
455 query.addValueField(new ValueField("TRACK_ARTISTS.TRACK", new StringParameter(track.getId())));
456 query.addValueField(new ValueField("TRACK_ARTISTS.ARTIST", new StringParameter(track.getArtists().get(index).getId())));
457 query.addValueField(new ValueField("TRACK_ARTISTS.DISPLAY_ORDER", new IntegerParameter(index + 1)));
458 database.insert(query);
461 /* store party links. */
462 Collection<Party> parties = track.getParties(); /* prefetch parties. */
463 query = new Query(Type.DELETE, "PARTY_TRACKS");
464 query.addWhereClause(new ValueFieldWhereClause(new ValueField("PARTY_TRACKS.TRACK", new StringParameter(track.getId()))));
465 database.update(query);
466 for (Party party : parties) {
467 query = new Query(Type.INSERT, "PARTY_TRACKS");
468 query.addValueField(new ValueField("PARTY_TRACKS.TRACK", new StringParameter(track.getId())));
469 query.addValueField(new ValueField("PARTY_TRACKS.PARTY", new StringParameter(party.getId())));
470 database.insert(query);
473 /* store properties. */
474 saveProperties(track.getProperties(), "TRACK_PROPERTIES", "TRACK", track.getId());
478 * Returns the derivative with the given ID.
481 * The ID of the derivatives to load
482 * @return The derivative with the given ID
483 * @throws DatabaseException
484 * if a database error occurs
486 public TrackDerivative getTrackDerivativeById(String id) throws DatabaseException {
487 Query query = new Query(Type.SELECT, "TRACK_DERIVATIVES");
488 query.addField(new Field("TRACK_DERIVATIVES.*"));
489 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_DERIVATIVES.ID", new StringParameter(id))));
490 return loadTrackDerivativeProperties(database.getSingle(query, trackDerivativeCreator));
494 * Returns the derivatives for the given track.
497 * The track ID to get the derivatives for
498 * @return The derivatives for the given track
499 * @throws DatabaseException
500 * if a database error occurs
502 public Collection<TrackDerivative> getTrackDerivativesByTrack(String trackId) throws DatabaseException {
503 Query query = new Query(Type.SELECT, "TRACK_DERIVATIVES");
504 query.addField(new Field("TRACK_DERIVATIVES.*"));
505 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_DERIVATIVES.TRACK", new StringParameter(trackId))));
506 return loadTrackDerivativeProperties(database.getMultiple(query, trackDerivativeCreator));
510 * Creates a new derivative for the given track.
513 * The track to create the derivative for
514 * @return The created derivative
515 * @throws DatabaseException
516 * if a database error occurs
518 public TrackDerivative createTrackDerivative(Track track) throws DatabaseException {
519 Query query = new Query(Type.INSERT, "TRACK_DERIVATIVES");
520 String id = UUID.randomUUID().toString();
521 query.addValueField(new ValueField("TRACK_DERIVATIVES.ID", new StringParameter(id)));
522 query.addValueField(new ValueField("TRACK_DERIVATIVES.TRACK", new StringParameter(track.getId())));
523 database.insert(query);
524 return getTrackDerivativeById(id);
528 * Loads the properties for the given track derivative.
530 * @param trackDerivative
531 * The track derivative to load the properties for
532 * @return The track derivative with its properties loaded
533 * @throws DatabaseException
534 * if a database error occurs
536 public TrackDerivative loadTrackDerivativeProperties(TrackDerivative trackDerivative) throws DatabaseException {
537 return loadProperties(trackDerivative, "TRACK_DERIVATIVE_PROPERTIES", "TRACK_DERIVATIVE");
541 * Loads the properties for the given track derivatives.
543 * @param trackDerivatives
544 * The track derivatives to load the properties for
545 * @return The track derivatives with their properties loaded
546 * @throws DatabaseException
547 * if a database error occurs
549 public List<TrackDerivative> loadTrackDerivativeProperties(List<TrackDerivative> trackDerivatives) throws DatabaseException {
550 for (TrackDerivative trackDerivative : trackDerivatives) {
551 loadTrackDerivativeProperties(trackDerivative);
553 return trackDerivatives;
557 * Saves the given track derivative. As a track derivative does not have any
558 * attributes of its own only its properties are saved.
560 * @param trackDerivative
561 * The track derivative to save
562 * @throws DatabaseException
563 * if a database error occurs
565 public void saveTrackDerivate(TrackDerivative trackDerivative) throws DatabaseException {
566 saveProperties(trackDerivative.getProperties(), "TRACK_DERIVATIVE_PROPERTIES", "TRACK_DERIVATIVE", trackDerivative.getId());
570 * Removes the given track derivative and all its properties from the
573 * @param trackDerivative
574 * The track derivative to remove
575 * @throws DatabaseException
576 * if a database error occurs
578 public void removeTrackDerivative(TrackDerivative trackDerivative) throws DatabaseException {
579 Query query = new Query(Type.DELETE, "TRACK_DERIVATIVES");
580 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_DERIVATIVES.ID", new StringParameter(trackDerivative.getId()))));
581 database.update(query);
582 /* remove the properties. */
583 saveProperties(new Properties().set("dirty", "true").removeAll(), "TRACK_DERIVATIVE_PROPERTIES", "TRACK_DERIVATIVE", trackDerivative.getId());
587 * Returns all groups the artist with the given ID belongs to.
590 * The ID of the artist
591 * @return All groups the artist belongs to
592 * @throws DatabaseException
593 * if a database error occurs
595 public Collection<Group> getGroupsByArtist(String artistId) throws DatabaseException {
596 Query query = new Query(Type.SELECT, "GROUPS");
597 query.addField(new Field("GROUPS.*"));
598 query.addJoin(new Join(JoinType.INNER, "GROUP_ARTISTS", new Field("GROUPS.ID"), new Field("GROUP_ARTISTS.GROUP_")));
599 query.addWhereClause(new ValueFieldWhereClause(new ValueField("GROUP_ARTISTS.ARTIST", new StringParameter(artistId))));
600 return database.getMultiple(query, groupCreator);
604 * Returns all styles for the track with the given ID.
607 * The ID of the track
608 * @return All styles for the given track
609 * @throws DatabaseException
610 * if a database error occurs
612 public Collection<Style> getStylesByTrack(String trackId) throws DatabaseException {
613 Query query = new Query(Type.SELECT, "STYLES");
614 query.addField(new Field("STYLES.*"));
615 query.addJoin(new Join(JoinType.INNER, "TRACK_STYLES", new Field("STYLES.ID"), new Field("TRACK_STYLES.STYLE")));
616 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_STYLES.TRACK", new StringParameter(trackId))));
617 return database.getMultiple(query, styleCreator);
621 * Returns all parties.
623 * @return All parties
624 * @throws DatabaseException
625 * if a database error occurs
627 public Collection<Party> getAllParties() throws DatabaseException {
628 Query query = new Query(Type.SELECT, "PARTIES");
629 query.addField(new Field("PARTIES.*"));
630 return loadPartyProperties(database.getMultiple(query, partyCreator));
634 * Returns all parties that the track with the given ID was released at.
637 * The ID of the track
638 * @return All parties the track was released at
639 * @throws DatabaseException
640 * if a database error occurs
642 public Collection<Party> getPartiesByTrackId(String trackId) throws DatabaseException {
643 Query query = new Query(Type.SELECT, "PARTIES");
644 query.addField(new Field("PARTIES.*"));
645 query.addJoin(new Join(JoinType.INNER, "PARTY_TRACKS", new Field("PARTY_TRACKS.PARTY"), new Field("PARTIES.ID")));
646 query.addWhereClause(new ValueFieldWhereClause(new ValueField("PARTY_TRACKS.TRACK", new StringParameter(trackId))));
647 return loadPartyProperties(database.getMultiple(query, partyCreator));
651 * Returns the party with the given ID.
654 * The ID of the party
655 * @return The party with the given ID
656 * @throws DatabaseException
657 * if a database error occurs
659 public Party getPartyById(String partyId) throws DatabaseException {
660 Query query = new Query(Type.SELECT, "PARTIES");
661 query.addField(new Field("PARTIES.*"));
662 query.addWhereClause(new ValueFieldWhereClause(new ValueField("PARTIES.ID", new StringParameter(partyId))));
663 return loadPartyProperties(database.getSingle(query, partyCreator));
667 * Loads the properties of the given party.
670 * The party to load the properties for
671 * @return The party with its properties loaded
672 * @throws DatabaseException
673 * if a database error occurs
675 public Party loadPartyProperties(Party party) throws DatabaseException {
676 return loadProperties(party, "PARTY_PROPERTIES", "PARTY");
680 * Loads the properties of the given parties.
683 * The parties to load the properties for
684 * @return The parties with their properties loaded
685 * @throws DatabaseException
686 * if a database error occurs
688 public List<Party> loadPartyProperties(List<Party> parties) throws DatabaseException {
689 for (Party party : parties) {
690 loadPartyProperties(party);
696 * Saves the given party.
700 * @throws DatabaseException
701 * if a database error occurs
703 public void saveParty(Party party) throws DatabaseException {
704 Query query = new Query(Type.UPDATE, "PARTIES");
705 query.addValueField(new ValueField("NAME", new StringParameter(party.getName())));
706 query.addWhereClause(new ValueFieldWhereClause(new ValueField("ID", new StringParameter(party.getId()))));
707 database.update(query);
708 savePartyProperties(party);
712 * Saves the properties of the given party.
715 * The party whose properties to save
716 * @throws DatabaseException
717 * if a database error occurs
719 public void savePartyProperties(Party party) throws DatabaseException {
720 saveProperties(party.getProperties(), "PARTY_PROPERTIES", "PARTY", party.getId());
724 * Creates a new party with the given name.
727 * The name of the party
728 * @return The new party
729 * @throws DatabaseException
730 * if a database error occurs
732 public Party createParty(String name) throws DatabaseException {
733 Query query = new Query(Type.INSERT, "PARTIES");
734 String id = UUID.randomUUID().toString();
735 query.addValueField(new ValueField("ID", new StringParameter(id)));
736 query.addValueField(new ValueField("NAME", new StringParameter(name)));
737 database.insert(query);
738 return getPartyById(id);
742 * Returns the user with the given name.
745 * The name of the user
746 * @return The user, or {@code null} if the user does not exist
747 * @throws DatabaseException
748 * if a database error occurs
750 public User getUserByName(String username) throws DatabaseException {
751 Query query = new Query(Type.SELECT, "USERS");
752 query.addField(new Field("USERS.*"));
753 query.addWhereClause(new ValueFieldWhereClause(new ValueField("USERS.NAME", new StringParameter(username))));
754 return database.getSingle(query, userCreator);
758 * Returns the user connected with the given OpenID.
761 * The OpenID to find the user for
762 * @return The user connected with the given OpenID, or {@code null} if
763 * there is no such user
764 * @throws DatabaseException
765 * if a database error occurs
767 public User getUserByOpenId(String openId) throws DatabaseException {
768 Query query = new Query(Type.SELECT, "USERS");
769 query.addField(new Field("USERS.*"));
770 query.addJoin(new Join(JoinType.INNER, "USER_OPENIDS", new Field("USER_OPENIDS.USER"), new Field("USERS.ID")));
771 query.addWhereClause(new ValueFieldWhereClause(new ValueField("USER_OPENIDS.OPENID", new StringParameter(openId))));
772 return database.getSingle(query, userCreator);
776 * Returns all OpenIDs connected with the user with the given ID.
780 * @return All OpenIDs connected with the given user
781 * @throws DatabaseException
782 * if a database error occurs
784 public Collection<String> getOpenIdsByUser(String userId) throws DatabaseException {
785 Query query = new Query(Type.SELECT, "USER_OPENIDS");
786 query.addField(new Field("USER_OPENIDS.*"));
787 query.addWhereClause(new ValueFieldWhereClause(new ValueField("USER_OPENIDS.USER", new StringParameter(userId))));
788 return database.getMultiple(query, new StringCreator("USER_OPENIDS.OPENID"));
796 * Loads the properties for the given object.
799 * The type of the object
803 * The table to load the properties from
805 * The type of the object (“ARTIST,” “TRACK,” etc.)
806 * @return The object with its properties loaded
807 * @throws DatabaseException
808 * if a database error occurs
810 private <T extends Base> T loadProperties(final T object, final String table, String type) throws DatabaseException {
811 if (object == null) {
814 Query query = new Query(Type.SELECT, table);
815 query.addField(new Field(table + ".PROPERTY"));
816 query.addField(new Field(table + ".VALUE"));
817 query.addWhereClause(new ValueFieldWhereClause(new ValueField(type, new StringParameter(object.getId()))));
818 database.process(query, new ResultProcessor() {
821 public void processResult(ResultSet resultSet) throws SQLException {
822 if (resultSet.isFirst()) {
823 object.getProperties().removeAll();
825 object.getProperties().set(resultSet.getString(table + ".PROPERTY"), resultSet.getString(table + ".VALUE"));
833 * {@link Artist} implementation that retrieves some attributes (such as
834 * {@link #getGroups()}, and {@link #getTracks()}) from the
835 * {@link DataManager} on demand.
837 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
839 private class LazyArtist extends DefaultArtist {
841 /** Memoizer for the tracks by this artist. */
842 private final Memoizer<Void> tracksMemoizer = new Memoizer<Void>(new Callable<Void>() {
845 public Void call() throws DatabaseException {
846 if (!hasValue("tracks")) {
847 getValue("tracks", Collection.class).set(getTracksByArtist(getId()));
853 /** Memoizer for the groups of this artist. */
854 private final Memoizer<Void> groupsMemoizer = new Memoizer<Void>(new Callable<Void>() {
857 public Void call() throws Exception {
858 if (!hasValue("groups")) {
859 getValue("groups", Collection.class).set(getGroupsByArtist(getId()));
867 * Creates a new lazy artist.
870 * The ID of the artist
872 public LazyArtist(String id) {
877 // DEFAULTARTIST METHODS
884 public Collection<Group> getGroups() {
885 groupsMemoizer.get();
886 return super.getGroups();
893 public Collection<Track> getTracks() {
894 tracksMemoizer.get();
895 return super.getTracks();
901 * {@link ObjectCreator} implementation that can create {@link Artist}
902 * objects. This specific class actually creates {@link LazyArtist}
905 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
907 private class ArtistCreator implements ObjectCreator<Artist> {
913 public Artist createObject(ResultSet resultSet) throws SQLException {
914 return new LazyArtist(resultSet.getString("ARTISTS.ID")).setName(resultSet.getString("ARTISTS.NAME"));
920 * {@link Group} implementation that retrieves some attributes (such as
921 * {@link #getArtists()}) from the {@link DataManager} on demand.
923 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
925 private class LazyGroup extends DefaultGroup {
927 /** Memoizer for the artist. */
928 private final Memoizer<Void> artistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
931 public Void call() throws Exception {
932 if (!hasValue("artists")) {
933 getValue("artists", Collection.class).set(getArtistsByGroup(getId()));
941 * Creates a new lazy group.
944 * The ID of the group
946 public LazyGroup(String id) {
951 // DEFAULTGROUP METHODS
958 public Collection<Artist> getArtists() {
959 artistsMemoizer.get();
960 return super.getArtists();
966 * {@link ObjectCreator} implementation that can create {@link Group}
967 * objects. This specific implementation creates {@link LazyGroup}
970 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
972 private class GroupCreator implements ObjectCreator<Group> {
978 public Group createObject(ResultSet resultSet) throws SQLException {
979 return new LazyGroup(resultSet.getString("GROUPS.ID")).setName(resultSet.getString("GROUPS.NAME")).setUrl(resultSet.getString("GROUPS.URL"));
985 * {@link Track} implementation that retrieves some attributes (such as
986 * {@link #getArtists()}, and {@link #getStyles()}) from the
987 * {@link DataManager} on demand.
989 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
991 private class LazyTrack extends DefaultTrack {
993 /** Memoizer for the artists. */
994 private final Memoizer<Void> artistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
997 public Void call() throws Exception {
998 if (!hasValue("artists")) {
999 getValue("artists", List.class).set(getArtistsByTrack(getId()));
1006 /** Memoizer for the styles. */
1007 private final Memoizer<Void> stylesMemoizer = new Memoizer<Void>(new Callable<Void>() {
1010 public Void call() throws Exception {
1011 if (!hasValue("styles")) {
1012 getValue("styles", Collection.class).set(getStylesByTrack(getId()));
1019 /** Memoizer for the remix artists. */
1020 private final Memoizer<Void> remixArtistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
1023 public Void call() throws Exception {
1024 if (!hasValue("remixArtists")) {
1025 getValue("remixArtists", List.class).set(getRemixArtistsByTrack(getId()));
1032 /** Memoizer for the track derivatives. */
1033 private final Memoizer<Void> derivativesMemoizer = new Memoizer<Void>(new Callable<Void>() {
1036 public Void call() throws Exception {
1037 if (!hasValue("derivatives")) {
1038 getValue("derivatives", Collection.class).set(getTrackDerivativesByTrack(getId()));
1045 /** Memoizer for the related tracks. */
1046 private final Memoizer<Void> relatedTracksMemoizer = new Memoizer<Void>(new Callable<Void>() {
1049 public Void call() throws Exception {
1050 if (!hasValue("relatedTracks")) {
1051 getValue("relatedTracks", Map.class).set(getRelatedTracksByTrack(getId()));
1059 * Creates a new track.
1062 * The ID of the track
1064 public LazyTrack(String id) {
1069 // DEFAULTTRACK METHODS
1076 public List<Artist> getArtists() {
1077 artistsMemoizer.get();
1078 return super.getArtists();
1085 public Collection<Style> getStyles() {
1086 stylesMemoizer.get();
1087 return super.getStyles();
1094 public List<Artist> getRemixArtists() {
1095 remixArtistsMemoizer.get();
1096 return super.getRemixArtists();
1103 public Collection<TrackDerivative> getDerivatives() {
1104 derivativesMemoizer.get();
1105 return super.getDerivatives();
1112 public Map<Relationship, Collection<Track>> getRelatedTracks() {
1113 relatedTracksMemoizer.get();
1114 return super.getRelatedTracks();
1121 public Collection<Party> getParties() {
1122 if (!hasValue("parties")) {
1124 getValue("parties", Collection.class).set(getPartiesByTrackId(getId()));
1125 } catch (DatabaseException de1) {
1126 throw new RuntimeException("Could not load Parties for Track " + getId() + ".", de1);
1129 return super.getParties();
1135 * {@link ObjectCreator} implementation that can create {@link Track}
1136 * objects. This specific implementation creates {@link LazyTrack}
1139 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1141 private class TrackCreator implements ObjectCreator<Track> {
1147 public Track createObject(ResultSet resultSet) throws SQLException {
1148 return new LazyTrack(resultSet.getString("TRACKS.ID")).setName(resultSet.getString("TRACKS.NAME")).setRemix(resultSet.getString("TRACKS.REMIX"));
1154 * {@link ObjectCreator} implementation that can create
1155 * {@link TrackDerivative} objects.
1157 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1159 private class TrackDerivativeCreator implements ObjectCreator<TrackDerivative> {
1165 public TrackDerivative createObject(ResultSet resultSet) throws SQLException {
1166 return new DefaultTrackDerivative(resultSet.getString("TRACK_DERIVATIVES.ID"));
1172 * {@link ObjectCreator} implementation that can create {@link Style}
1175 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1177 private class StyleCreator implements ObjectCreator<Style> {
1183 public Style createObject(ResultSet resultSet) throws SQLException {
1184 return new DefaultStyle(resultSet.getString("STYLES.ID")).setName(resultSet.getString("STYLES.NAME"));
1190 * {@link Party} implementation that loads additional information only on
1193 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1195 private class LazyParty extends DefaultParty {
1198 * Creates a new party.
1201 * The ID of the party
1203 public LazyParty(String id) {
1215 public Collection<Track> getReleases() {
1216 if (!hasValue("releases")) {
1218 getValue("releases", Collection.class).set(getTracksByParty(getId()));
1219 } catch (DatabaseException de1) {
1220 throw new RuntimeException("Could not loaded Tracks for Party " + getId() + ".", de1);
1223 return super.getReleases();
1229 * {@link ObjectCreator} implementation that can create {@link Party}
1232 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1234 private class PartyCreator implements ObjectCreator<Party> {
1240 public Party createObject(ResultSet resultSet) throws SQLException {
1241 return new LazyParty(resultSet.getString("PARTIES.ID")).setName(resultSet.getString("PARTIES.NAME"));
1247 * {@link User} implementation that retrieves some attributes (such as
1248 * {@link #getOpenIds()}) from the {@link DataManager} on demand.
1250 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1252 private class LazyUser extends DefaultUser {
1254 /** Memoizer for a user’s OpenIDs. */
1255 private final Memoizer<Void> openIdMemoizer = new Memoizer<Void>(new Callable<Void>() {
1258 public Void call() throws Exception {
1259 if (!hasValue("openIds")) {
1260 getValue("openIds", Collection.class).set(getOpenIdsByUser(getId()));
1267 * Creates a new user.
1270 * The ID of the user
1272 public LazyUser(String id) {
1280 public Collection<String> getOpenIds() {
1281 openIdMemoizer.get();
1282 return super.getOpenIds();
1288 * {@link ObjectCreator} implementation that can create {@link User}
1289 * objects. This specific implementation creates {@link LazyUser} instances.
1291 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
1293 private class UserCreator implements ObjectCreator<User> {
1299 public User createObject(ResultSet resultSet) throws SQLException {
1300 return new LazyUser(resultSet.getString("USERS.ID")).setName(resultSet.getString("USERS.NAME")).setPasswordHash(resultSet.getString("USERS.PASSWORD")).setLevel(resultSet.getInt("USERS.LEVEL"));