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.StringParameter;
42 import net.pterodactylus.util.database.Query;
43 import net.pterodactylus.util.database.Query.Type;
44 import net.pterodactylus.util.database.ResultProcessor;
45 import net.pterodactylus.util.database.ValueField;
46 import net.pterodactylus.util.database.ValueFieldWhereClause;
49 * Interface between the database and the application.
51 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
53 public class DataManager {
55 /** The artist object creator. */
56 @SuppressWarnings("synthetic-access")
57 private final ObjectCreator<Artist> artistCreator = new ArtistCreator();
59 /** The group object creator. */
60 @SuppressWarnings("synthetic-access")
61 private final ObjectCreator<Group> groupCreator = new GroupCreator();
63 /** The track object creator. */
64 @SuppressWarnings("synthetic-access")
65 private final ObjectCreator<Track> trackCreator = new TrackCreator();
67 /** The track object creator. */
68 @SuppressWarnings("synthetic-access")
69 private final ObjectCreator<TrackDerivative> trackDerivativeCreator = new TrackDerivativeCreator();
71 /** The style object creator. */
72 @SuppressWarnings("synthetic-access")
73 private final ObjectCreator<Style> styleCreator = new StyleCreator();
75 /** The {@link User} object creator. */
76 @SuppressWarnings("synthetic-access")
77 private final ObjectCreator<User> userCreator = new UserCreator();
80 private final Database database;
83 * Creates a new data manager.
86 * The database to operate on
88 public DataManager(Database database) {
89 this.database = database;
93 * Returns all artists.
96 * @throws DatabaseException
97 * if a database error occurs
99 public Collection<Artist> getAllArtists() throws DatabaseException {
100 Query query = new Query(Type.SELECT, "ARTISTS");
101 query.addField(new Field("ARTISTS.*"));
102 return loadArtistProperties(database.getMultiple(query, artistCreator));
106 * Returns the artist with the given ID.
109 * The ID of the artist
110 * @return The artist with the given ID, or {@code null} if there is no
111 * artist with the given ID
112 * @throws DatabaseException
113 * if a database error occurs
115 public Artist getArtistById(String id) throws DatabaseException {
116 Query query = new Query(Type.SELECT, "ARTISTS");
117 query.addField(new Field("ARTISTS.*"));
118 query.addWhereClause(new ValueFieldWhereClause(new ValueField("ARTISTS.ID", new StringParameter(id))));
119 return loadArtistProperties(database.getSingle(query, artistCreator));
123 * Returns all artists that belong to the group with the given ID.
126 * The ID of the group
127 * @return All artists belonging to the given group
128 * @throws DatabaseException
129 * if a database error occurs
131 public Collection<Artist> getArtistsByGroup(String groupId) throws DatabaseException {
132 Query query = new Query(Type.SELECT, "ARTISTS");
133 query.addField(new Field("ARTISTS.*"));
134 query.addJoin(new Join(JoinType.INNER, "GROUP_ARTISTS", new Field("ARTISTS.ID"), new Field("GROUP_ARTISTS.ARTIST")));
135 query.addWhereClause(new ValueFieldWhereClause(new ValueField("GROUP_ARTISTS.GROUP_", new StringParameter(groupId))));
136 return loadArtistProperties(database.getMultiple(query, artistCreator));
140 * Returns all artists involved in the track with the given ID.
143 * The ID of the track
144 * @return All artists involved in the track, in preferred order
145 * @throws DatabaseException
146 * if a database error occurs
148 public List<Artist> getArtistsByTrack(String trackId) throws DatabaseException {
149 Query query = new Query(Type.SELECT, "ARTISTS");
150 query.addField(new Field("ARTISTS.*"));
151 query.addJoin(new Join(JoinType.INNER, "TRACK_ARTISTS", new Field("TRACK_ARTISTS.ARTIST"), new Field("ARTISTS.ID")));
152 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_ARTISTS.TRACK", new StringParameter(trackId))));
153 query.addOrderField(new OrderField(new Field("TRACK_ARTISTS.DISPLAY_ORDER")));
154 return loadArtistProperties(database.getMultiple(query, artistCreator));
158 * Creates a new artist with the given name.
161 * The name of the artist
162 * @return The created artist
163 * @throws DatabaseException
164 * if a database error occurs
166 public Artist createArtist(String name) throws DatabaseException {
167 Query query = new Query(Type.INSERT, "ARTISTS");
168 String id = UUID.randomUUID().toString();
169 query.addValueField(new ValueField("ID", new StringParameter(id)));
170 query.addValueField(new ValueField("NAME", new StringParameter(name)));
171 database.insert(query);
172 return loadArtistProperties(getArtistById(id));
176 * Saves the given artist.
180 * @throws DatabaseException
181 * if a database error occurs
183 public void saveArtist(Artist artist) throws DatabaseException {
184 Query query = new Query(Type.UPDATE, "ARTISTS");
185 query.addValueField(new ValueField("NAME", new StringParameter(artist.getName())));
186 query.addWhereClause(new ValueFieldWhereClause(new ValueField("ID", new StringParameter(artist.getId()))));
187 database.update(query);
188 saveArtistProperties(artist);
192 * Saves the properties of the given artist.
195 * The artist whose properties to save
196 * @throws DatabaseException
197 * if a database error occurs
199 public void saveArtistProperties(Artist artist) throws DatabaseException {
200 saveProperties(artist.getProperties(), "ARTIST_PROPERTIES", "ARTIST", artist.getId());
204 * Saves the given properties to the given table for the given principal.
207 * The properties to save
209 * The table in which to save the properties
211 * The type of the principal (e. g. “ARTIST” or “TRACK”)
213 * The ID of the principial
214 * @throws DatabaseException
215 * if a database error occurs
217 public void saveProperties(Properties properties, String table, String type, String id) throws DatabaseException {
218 if (!properties.isDirty()) {
221 Query query = new Query(Type.DELETE, table);
222 query.addWhereClause(new ValueFieldWhereClause(new ValueField(type, new StringParameter(id))));
223 database.update(query);
224 for (Entry<String, String> property : properties) {
225 Query insertQuery = new Query(Type.INSERT, table);
226 insertQuery.addValueField(new ValueField(type, new StringParameter(id)));
227 insertQuery.addValueField(new ValueField("PROPERTY", new StringParameter(property.getKey())));
228 insertQuery.addValueField(new ValueField("VALUE", new StringParameter(property.getValue())));
229 database.insert(insertQuery);
231 properties.resetDirty();
235 * Loads the properties for an artist.
238 * The artist to load the properties for
240 * @throws DatabaseException
241 * if a database error occurs
243 public Artist loadArtistProperties(final Artist artist) throws DatabaseException {
244 return loadProperties(artist, "ARTIST_PROPERTIES", "ARTIST");
248 * Loads the properties of all given artists.
251 * The artists to load the properties for
252 * @return The list of artists
253 * @throws DatabaseException
254 * if a database error occurs
256 public List<Artist> loadArtistProperties(List<Artist> artists) throws DatabaseException {
257 for (Artist artist : artists) {
258 loadArtistProperties(artist);
264 * Returns all remix artists involved in the track with the given ID.
267 * The ID of the track
268 * @return All remix artists involved in the track, in preferred order
269 * @throws DatabaseException
270 * if a database error occurs
272 public List<Artist> getRemixArtistsByTrack(String trackId) throws DatabaseException {
273 Query query = new Query(Type.SELECT, "ARTISTS");
274 query.addField(new Field("ARTISTS.*"));
275 query.addJoin(new Join(JoinType.INNER, "TRACK_REMIX_ARTISTS", new Field("TRACK_REMIX_ARTISTS.ARTIST"), new Field("ARTISTS.ID")));
276 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_REMIX_ARTISTS.TRACK", new StringParameter(trackId))));
277 query.addOrderField(new OrderField(new Field("TRACK_REMIX_ARTISTS.DISPLAY_ORDER")));
278 return database.getMultiple(query, artistCreator);
282 * Returns all related tracks for the track with the given ID.
285 * The ID of the tracks
286 * @return A mapping from relationship to all tracks that match the relation
287 * @throws DatabaseException
288 * if a database error occurs
290 public Map<Relationship, Collection<Track>> getRelatedTracksByTrack(String trackId) throws DatabaseException {
291 Query query = new Query(Type.SELECT, "TRACKS");
292 query.addField(new Field("TRACKS.*"));
293 query.addField(new Field("TRACK_RELATIONS.*"));
294 query.addJoin(new Join(JoinType.INNER, "TRACK_RELATIONS", new Field("TRACK_RELATIONS.TRACK"), new Field("TRACK_RELATIONS.RELATED_TRACK")));
295 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_RELATIONS.TRACK", new StringParameter(trackId))));
296 final Map<Relationship, Collection<Track>> relatedTracks = new EnumMap<Relationship, Collection<Track>>(Relationship.class);
297 database.process(query, new ResultProcessor() {
300 @SuppressWarnings("synthetic-access")
301 public void processResult(ResultSet resultSet) throws SQLException {
302 Track track = trackCreator.createObject(resultSet);
303 Relationship relationship = Relationship.valueOf(resultSet.getString("TRACK_RELATIONS.RELATIONSHIP"));
304 if (!relatedTracks.containsKey(relationship)) {
305 relatedTracks.put(relationship, new HashSet<Track>());
307 relatedTracks.get(relationship).add(track);
310 return relatedTracks;
314 * Returns the track with the given ID.
317 * The ID of the track
318 * @return The track with the given ID, or {@code null} if there is no such
320 * @throws DatabaseException
321 * if a database error occurs
323 public Track getTrackById(String id) throws DatabaseException {
324 Query query = new Query(Type.SELECT, "TRACKS");
325 query.addField(new Field("TRACKS.*"));
326 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACKS.ID", new StringParameter(id))));
327 return database.getSingle(query, trackCreator);
331 * Returns all tracks by the artist with the given ID.
334 * The ID of the artist
335 * @return All tracks by the given artist
336 * @throws DatabaseException
337 * if a database error occurs
339 public Collection<Track> getTracksByArtist(String artistId) throws DatabaseException {
340 Query query = new Query(Type.SELECT, "TRACKS");
341 query.addField(new Field("TRACKS.*"));
342 query.addJoin(new Join(JoinType.INNER, "TRACK_ARTISTS", new Field("TRACKS.ID"), new Field("TRACK_ARTISTS.TRACK")));
343 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_ARTISTS.ARTIST", new StringParameter(artistId))));
344 return database.getMultiple(query, trackCreator);
348 * Creates a new track with the given name.
351 * The name of the track
352 * @return The created track
353 * @throws DatabaseException
354 * if a database error occurs
356 public Track createTrack(String name) throws DatabaseException {
357 Query query = new Query(Type.INSERT, "TRACKS");
358 String id = UUID.randomUUID().toString();
359 query.addValueField(new ValueField("ID", new StringParameter(id)));
360 query.addValueField(new ValueField("NAME", new StringParameter(name)));
361 database.insert(query);
362 return getTrackById(id);
366 * Returns the derivative with the given ID.
369 * The ID of the derivatives to load
370 * @return The derivative with the given ID
371 * @throws DatabaseException
372 * if a database error occurs
374 public TrackDerivative getTrackDerivativeById(String id) throws DatabaseException {
375 Query query = new Query(Type.SELECT, "TRACK_DERIVATIVES");
376 query.addField(new Field("TRACK_DERIVATIVES.*"));
377 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_DERIVATIVES.ID", new StringParameter(id))));
378 return loadTrackDerivativeProperties(database.getSingle(query, trackDerivativeCreator));
382 * Returns the derivatives for the given track.
385 * The track to get the derivatives for
386 * @return The derivatives for the given track
387 * @throws DatabaseException
388 * if a database error occurs
390 public Collection<TrackDerivative> getTrackDerivativesByTrack(Track track) throws DatabaseException {
391 Query query = new Query(Type.SELECT, "TRACK_DERIVATIVES");
392 query.addField(new Field("TRACK_DERIVATIVES.*"));
393 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_DERIVATIVES.TRACK", new StringParameter(track.getId()))));
394 return loadTrackDerivativeProperties(database.getMultiple(query, trackDerivativeCreator));
398 * Creates a new derivative for the given track.
401 * The track to create the derivative for
402 * @return The created derivative
403 * @throws DatabaseException
404 * if a database error occurs
406 public TrackDerivative createTrackDerivative(Track track) throws DatabaseException {
407 Query query = new Query(Type.INSERT, "TRACK_DERIVATIVES");
408 String id = UUID.randomUUID().toString();
409 query.addValueField(new ValueField("TRACK_DERIVATIVES.ID", new StringParameter(id)));
410 query.addValueField(new ValueField("TRACK_DERIVATIVES.TRACK", new StringParameter(track.getId())));
411 database.insert(query);
412 return getTrackDerivativeById(id);
416 * Loads the properties for the given track derivative.
418 * @param trackDerivative
419 * The track derivative to load the properties for
420 * @return The track derivative with its properties loaded
421 * @throws DatabaseException
422 * if a database error occurs
424 public TrackDerivative loadTrackDerivativeProperties(TrackDerivative trackDerivative) throws DatabaseException {
425 return loadProperties(trackDerivative, "TRACK_DERIVATIVE_PROPERTIES", "TRACK_DERIVATIVE");
429 * Loads the properties for the given track derivatives.
431 * @param trackDerivatives
432 * The track derivatives to load the properties for
433 * @return The track derivatives with their properties loaded
434 * @throws DatabaseException
435 * if a database error occurs
437 public List<TrackDerivative> loadTrackDerivativeProperties(List<TrackDerivative> trackDerivatives) throws DatabaseException {
438 for (TrackDerivative trackDerivative : trackDerivatives) {
439 loadTrackDerivativeProperties(trackDerivative);
441 return trackDerivatives;
445 * Returns all groups the artist with the given ID belongs to.
448 * The ID of the artist
449 * @return All groups the artist belongs to
450 * @throws DatabaseException
451 * if a database error occurs
453 public Collection<Group> getGroupsByArtist(String artistId) throws DatabaseException {
454 Query query = new Query(Type.SELECT, "GROUPS");
455 query.addField(new Field("GROUPS.*"));
456 query.addJoin(new Join(JoinType.INNER, "GROUP_ARTISTS", new Field("GROUPS.ID"), new Field("GROUP_ARTISTS.GROUP_")));
457 query.addWhereClause(new ValueFieldWhereClause(new ValueField("GROUP_ARTISTS.ARTIST", new StringParameter(artistId))));
458 return database.getMultiple(query, groupCreator);
462 * Returns all styles for the track with the given ID.
465 * The ID of the track
466 * @return All styles for the given track
467 * @throws DatabaseException
468 * if a database error occurs
470 public Collection<Style> getStylesByTrack(String trackId) throws DatabaseException {
471 Query query = new Query(Type.SELECT, "STYLES");
472 query.addField(new Field("STYLES.*"));
473 query.addJoin(new Join(JoinType.INNER, "TRACK_STYLES", new Field("STYLES.ID"), new Field("TRACK_STYLES.STYLE")));
474 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_STYLES.TRACK", new StringParameter(trackId))));
475 return database.getMultiple(query, styleCreator);
479 * Returns the user with the given name.
482 * The name of the user
483 * @return The user, or {@code null} if the user does not exist
484 * @throws DatabaseException
485 * if a database error occurs
487 public User getUserByName(String username) throws DatabaseException {
488 Query query = new Query(Type.SELECT, "USERS");
489 query.addField(new Field("USERS.*"));
490 query.addWhereClause(new ValueFieldWhereClause(new ValueField("USERS.NAME", new StringParameter(username))));
491 return database.getSingle(query, userCreator);
495 * Returns the user connected with the given OpenID.
498 * The OpenID to find the user for
499 * @return The user connected with the given OpenID, or {@code null} if
500 * there is no such user
501 * @throws DatabaseException
502 * if a database error occurs
504 public User getUserByOpenId(String openId) throws DatabaseException {
505 Query query = new Query(Type.SELECT, "USERS");
506 query.addField(new Field("USERS.*"));
507 query.addJoin(new Join(JoinType.INNER, "USER_OPENIDS", new Field("USER_OPENIDS.USER"), new Field("USERS.ID")));
508 query.addWhereClause(new ValueFieldWhereClause(new ValueField("USER_OPENIDS.OPENID", new StringParameter(openId))));
509 return database.getSingle(query, userCreator);
513 * Returns all OpenIDs connected with the user with the given ID.
517 * @return All OpenIDs connected with the given user
518 * @throws DatabaseException
519 * if a database error occurs
521 public Collection<String> getOpenIdsByUser(String userId) throws DatabaseException {
522 Query query = new Query(Type.SELECT, "USER_OPENIDS");
523 query.addField(new Field("USER_OPENIDS.*"));
524 query.addWhereClause(new ValueFieldWhereClause(new ValueField("USER_OPENIDS.USER", new StringParameter(userId))));
525 return database.getMultiple(query, new StringCreator("USER_OPENIDS.OPENID"));
533 * Loads the properties for the given object.
536 * The type of the object
540 * The table to load the properties from
542 * The type of the object (“ARTIST,” “TRACK,” etc.)
543 * @return The object with its properties loaded
544 * @throws DatabaseException
545 * if a database error occurs
547 private <T extends Base> T loadProperties(final T object, final String table, String type) throws DatabaseException {
548 Query query = new Query(Type.SELECT, table);
549 query.addField(new Field(table + ".PROPERTY"));
550 query.addField(new Field(table + ".VALUE"));
551 query.addWhereClause(new ValueFieldWhereClause(new ValueField(type, new StringParameter(object.getId()))));
552 database.process(query, new ResultProcessor() {
555 public void processResult(ResultSet resultSet) throws SQLException {
556 if (resultSet.isFirst()) {
557 object.getProperties().removeAll();
559 object.getProperties().set(resultSet.getString(table + ".PROPERTY"), resultSet.getString(table + ".VALUE"));
567 * {@link Artist} implementation that retrieves some attributes (such as
568 * {@link #getGroups()}, and {@link #getTracks()}) from the
569 * {@link DataManager} on demand.
571 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
573 private class LazyArtist extends DefaultArtist {
575 /** Memoizer for the tracks by this artist. */
576 private final Memoizer<Void> tracksMemoizer = new Memoizer<Void>(new Callable<Void>() {
578 public Void call() throws DatabaseException {
579 if (!hasValue("tracks")) {
580 getValue("tracks", Collection.class).set(getTracksByArtist(getId()));
586 /** Memoizer for the groups of this artist. */
587 private final Memoizer<Void> groupsMemoizer = new Memoizer<Void>(new Callable<Void>() {
590 public Void call() throws Exception {
591 if (!hasValue("groups")) {
592 getValue("groups", Collection.class).set(getGroupsByArtist(getId()));
600 * Creates a new lazy artist.
603 * The ID of the artist
605 public LazyArtist(String id) {
610 // DEFAULTARTIST METHODS
617 public Collection<Group> getGroups() {
618 groupsMemoizer.get();
619 return super.getGroups();
626 public Collection<Track> getTracks() {
627 tracksMemoizer.get();
628 return super.getTracks();
634 * {@link ObjectCreator} implementation that can create {@link Artist}
635 * objects. This specific class actually creates {@link LazyArtist}
638 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
640 private class ArtistCreator implements ObjectCreator<Artist> {
646 public Artist createObject(ResultSet resultSet) throws SQLException {
647 return new LazyArtist(resultSet.getString("ARTISTS.ID")).setName(resultSet.getString("ARTISTS.NAME"));
653 * {@link Group} implementation that retrieves some attributes (such as
654 * {@link #getArtists()}) from the {@link DataManager} on demand.
656 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
658 private class LazyGroup extends DefaultGroup {
660 /** Memoizer for the artist. */
661 private final Memoizer<Void> artistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
664 public Void call() throws Exception {
665 if (!hasValue("artists")) {
666 getValue("artists", Collection.class).set(getArtistsByGroup(getId()));
674 * Creates a new lazy group.
677 * The ID of the group
679 public LazyGroup(String id) {
684 // DEFAULTGROUP METHODS
691 public Collection<Artist> getArtists() {
692 artistsMemoizer.get();
693 return super.getArtists();
699 * {@link ObjectCreator} implementation that can create {@link Group}
700 * objects. This specific implementation creates {@link LazyGroup}
703 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
705 private class GroupCreator implements ObjectCreator<Group> {
711 public Group createObject(ResultSet resultSet) throws SQLException {
712 return new LazyGroup(resultSet.getString("GROUPS.ID")).setName(resultSet.getString("GROUPS.NAME")).setUrl(resultSet.getString("GROUPS.URL"));
718 * {@link Track} implementation that retrieves some attributes (such as
719 * {@link #getArtists()}, and {@link #getStyles()}) from the
720 * {@link DataManager} on demand.
722 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
724 private class LazyTrack extends DefaultTrack {
726 /** Memoizer for the artists. */
727 private final Memoizer<Void> artistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
730 public Void call() throws Exception {
731 if (!hasValue("artists")) {
732 getValue("artists", List.class).set(getArtistsByTrack(getId()));
739 /** Memoizer for the styles. */
740 private final Memoizer<Void> stylesMemoizer = new Memoizer<Void>(new Callable<Void>() {
743 public Void call() throws Exception {
744 if (!hasValue("styles")) {
745 getValue("styles", Collection.class).set(getStylesByTrack(getId()));
752 /** Memoizer for the remix artists. */
753 private final Memoizer<Void> remixArtistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
756 public Void call() throws Exception {
757 if (!hasValue("remixArtists")) {
758 getValue("remixArtists", List.class).set(getRemixArtistsByTrack(getId()));
765 /** Memoizer for the track derivatives. */
766 private final Memoizer<Void> derivativesMemoizer = new Memoizer<Void>(new Callable<Void>() {
769 public Void call() throws Exception {
770 if (!hasValue("derivatives")) {
771 getValue("derivatives", Collection.class).set(getTrackDerivativesByTrack(LazyTrack.this));
778 /** Memoizer for the related tracks. */
779 private final Memoizer<Void> relatedTracksMemoizer = new Memoizer<Void>(new Callable<Void>() {
782 public Void call() throws Exception {
783 if (!hasValue("relatedTracks")) {
784 getValue("relatedTracks", Map.class).set(getRelatedTracksByTrack(getId()));
791 * Creates a new track.
794 * The ID of the track
796 public LazyTrack(String id) {
801 // DEFAULTTRACK METHODS
808 public List<Artist> getArtists() {
809 artistsMemoizer.get();
810 return super.getArtists();
817 public Collection<Style> getStyles() {
818 stylesMemoizer.get();
819 return super.getStyles();
826 public List<Artist> getRemixArtists() {
827 remixArtistsMemoizer.get();
828 return super.getRemixArtists();
835 public Collection<TrackDerivative> getDerivatives() {
836 derivativesMemoizer.get();
837 return super.getDerivatives();
844 public Map<Relationship, Collection<Track>> getRelatedTracks() {
845 relatedTracksMemoizer.get();
846 return super.getRelatedTracks();
852 * {@link ObjectCreator} implementation that can create {@link Track}
853 * objects. This specific implementation creates {@link LazyTrack}
856 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
858 private class TrackCreator implements ObjectCreator<Track> {
864 public Track createObject(ResultSet resultSet) throws SQLException {
865 return new LazyTrack(resultSet.getString("TRACKS.ID")).setName(resultSet.getString("TRACKS.NAME")).setRemix(resultSet.getString("TRACKS.REMIX"));
871 * {@link ObjectCreator} implementation that can create
872 * {@link TrackDerivative} objects.
874 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
876 private class TrackDerivativeCreator implements ObjectCreator<TrackDerivative> {
882 public TrackDerivative createObject(ResultSet resultSet) throws SQLException {
883 return new DefaultTrackDerivative(resultSet.getString("TRACK_DERIVATIVES.ID"));
889 * {@link ObjectCreator} implementation that can create {@link Style}
892 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
894 private class StyleCreator implements ObjectCreator<Style> {
900 public Style createObject(ResultSet resultSet) throws SQLException {
901 return new DefaultStyle(resultSet.getString("STYLES.ID")).setName(resultSet.getString("STYLES.NAME"));
907 * {@link User} implementation that retrieves some attributes (such as
908 * {@link #getOpenIds()}) from the {@link DataManager} on demand.
910 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
912 private class LazyUser extends DefaultUser {
914 /** Memoizer for a user’s OpenIDs. */
915 private final Memoizer<Void> openIdMemoizer = new Memoizer<Void>(new Callable<Void>() {
918 public Void call() throws Exception {
919 if (!hasValue("openIds")) {
920 getValue("openIds", Collection.class).set(getOpenIdsByUser(getId()));
927 * Creates a new user.
932 public LazyUser(String id) {
940 public Collection<String> getOpenIds() {
941 openIdMemoizer.get();
942 return super.getOpenIds();
948 * {@link ObjectCreator} implementation that can create {@link User}
949 * objects. This specific implementation creates {@link LazyUser} instances.
951 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
953 private class UserCreator implements ObjectCreator<User> {
959 public User createObject(ResultSet resultSet) throws SQLException {
960 return new LazyUser(resultSet.getString("USERS.ID")).setName(resultSet.getString("USERS.NAME")).setPasswordHash(resultSet.getString("USERS.PASSWORD")).setLevel(resultSet.getInt("USERS.LEVEL"));