Load artist properties with artists.
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / data / DataManager.java
1 /*
2  * DemosceneMusic - DataManager.java - Copyright © 2012 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.demoscenemusic.data;
19
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;
26 import java.util.Map;
27 import java.util.UUID;
28 import java.util.concurrent.Callable;
29
30 import net.pterodactylus.demoscenemusic.data.Track.Relationship;
31 import net.pterodactylus.util.collection.Memoizer;
32 import net.pterodactylus.util.database.Database;
33 import net.pterodactylus.util.database.DatabaseException;
34 import net.pterodactylus.util.database.Field;
35 import net.pterodactylus.util.database.Join;
36 import net.pterodactylus.util.database.Join.JoinType;
37 import net.pterodactylus.util.database.ObjectCreator;
38 import net.pterodactylus.util.database.ObjectCreator.StringCreator;
39 import net.pterodactylus.util.database.OrderField;
40 import net.pterodactylus.util.database.Parameter.StringParameter;
41 import net.pterodactylus.util.database.Query;
42 import net.pterodactylus.util.database.Query.Type;
43 import net.pterodactylus.util.database.ResultProcessor;
44 import net.pterodactylus.util.database.ValueField;
45 import net.pterodactylus.util.database.ValueFieldWhereClause;
46
47 /**
48  * Interface between the database and the application.
49  *
50  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
51  */
52 public class DataManager {
53
54         /** The artist object creator. */
55         @SuppressWarnings("synthetic-access")
56         private final ObjectCreator<Artist> artistCreator = new ArtistCreator();
57
58         /** The group object creator. */
59         @SuppressWarnings("synthetic-access")
60         private final ObjectCreator<Group> groupCreator = new GroupCreator();
61
62         /** The track object creator. */
63         @SuppressWarnings("synthetic-access")
64         private final ObjectCreator<Track> trackCreator = new TrackCreator();
65
66         /** The style object creator. */
67         @SuppressWarnings("synthetic-access")
68         private final ObjectCreator<Style> styleCreator = new StyleCreator();
69
70         /** The {@link User} object creator. */
71         @SuppressWarnings("synthetic-access")
72         private final ObjectCreator<User> userCreator = new UserCreator();
73
74         /** The database. */
75         private final Database database;
76
77         /**
78          * Creates a new data manager.
79          *
80          * @param database
81          *            The database to operate on
82          */
83         public DataManager(Database database) {
84                 this.database = database;
85         }
86
87         /**
88          * Returns all artists.
89          *
90          * @return All artists
91          * @throws DatabaseException
92          *             if a database error occurs
93          */
94         public Collection<Artist> getAllArtists() throws DatabaseException {
95                 Query query = new Query(Type.SELECT, "ARTISTS");
96                 query.addField(new Field("ARTISTS.*"));
97                 return loadProperties(database.getMultiple(query, artistCreator));
98         }
99
100         /**
101          * Returns the artist with the given ID.
102          *
103          * @param id
104          *            The ID of the artist
105          * @return The artist with the given ID, or {@code null} if there is no
106          *         artist with the given ID
107          * @throws DatabaseException
108          *             if a database error occurs
109          */
110         public Artist getArtistById(String id) throws DatabaseException {
111                 Query query = new Query(Type.SELECT, "ARTISTS");
112                 query.addField(new Field("ARTISTS.*"));
113                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("ARTISTS.ID", new StringParameter(id))));
114                 return loadProperties(database.getSingle(query, artistCreator));
115         }
116
117         /**
118          * Returns all artists that belong to the group with the given ID.
119          *
120          * @param groupId
121          *            The ID of the group
122          * @return All artists belonging to the given group
123          * @throws DatabaseException
124          *             if a database error occurs
125          */
126         public Collection<Artist> getArtistsByGroup(String groupId) throws DatabaseException {
127                 Query query = new Query(Type.SELECT, "ARTISTS");
128                 query.addField(new Field("ARTISTS.*"));
129                 query.addJoin(new Join(JoinType.INNER, "GROUP_ARTISTS", new Field("ARTISTS.ID"), new Field("GROUP_ARTISTS.ARTIST")));
130                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("GROUP_ARTISTS.GROUP_", new StringParameter(groupId))));
131                 return loadProperties(database.getMultiple(query, artistCreator));
132         }
133
134         /**
135          * Returns all artists involved in the track with the given ID.
136          *
137          * @param trackId
138          *            The ID of the track
139          * @return All artists involved in the track, in preferred order
140          * @throws DatabaseException
141          *             if a database error occurs
142          */
143         public List<Artist> getArtistsByTrack(String trackId) throws DatabaseException {
144                 Query query = new Query(Type.SELECT, "ARTISTS");
145                 query.addField(new Field("ARTISTS.*"));
146                 query.addJoin(new Join(JoinType.INNER, "TRACK_ARTISTS", new Field("TRACK_ARTISTS.ARTIST"), new Field("ARTISTS.ID")));
147                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_ARTISTS.TRACK", new StringParameter(trackId))));
148                 query.addOrderField(new OrderField(new Field("TRACK_ARTISTS.DISPLAY_ORDER")));
149                 return loadProperties(database.getMultiple(query, artistCreator));
150         }
151
152         /**
153          * Creates a new artist with the given name.
154          *
155          * @param name
156          *            The name of the artist
157          * @return The created artist
158          * @throws DatabaseException
159          *             if a database error occurs
160          */
161         public Artist createArtist(String name) throws DatabaseException {
162                 Query query = new Query(Type.INSERT, "ARTISTS");
163                 String id = UUID.randomUUID().toString();
164                 query.addValueField(new ValueField("ID", new StringParameter(id)));
165                 query.addValueField(new ValueField("NAME", new StringParameter(name)));
166                 database.insert(query);
167                 return loadProperties(getArtistById(id));
168         }
169
170         /**
171          * Saves the given artist.
172          *
173          * @param artist
174          *            The artist to save
175          * @throws DatabaseException
176          *             if a database error occurs
177          */
178         public void saveArtist(Artist artist) throws DatabaseException {
179                 Query query = new Query(Type.UPDATE, "ARTISTS");
180                 query.addValueField(new ValueField("NAME", new StringParameter(artist.getName())));
181                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("ID", new StringParameter(artist.getId()))));
182                 database.update(query);
183         }
184
185         /**
186          * Loads the properties for an artist.
187          *
188          * @param artist
189          *            The artist to load the properties for
190          * @return The artist
191          * @throws DatabaseException
192          *             if a database error occurs
193          */
194         public Artist loadProperties(final Artist artist) throws DatabaseException {
195                 Query query = new Query(Type.SELECT, "ARTIST_PROPERTIES");
196                 query.addField(new Field("ARTIST_PROPERTIES.PROPERTY"));
197                 query.addField(new Field("ARTIST_PROPERTIES.VALUE"));
198                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("ARTIST", new StringParameter(artist.getId()))));
199                 database.process(query, new ResultProcessor() {
200
201                         @Override
202                         public void processResult(ResultSet resultSet) throws SQLException {
203                                 if (resultSet.isFirst()) {
204                                         artist.getProperties().removeAll();
205                                 }
206                                 artist.getProperties().set(resultSet.getString("ARTIST_PROPERTIES.PROPERTY"), resultSet.getString("ARTIST_PROPERTIES.VALUE"));
207                         }
208
209                 });
210                 return artist;
211         }
212
213         /**
214          * Loads the properties of all given artists.
215          *
216          * @param artists
217          *            The artists to load the properties for
218          * @return The list of artists
219          * @throws DatabaseException
220          *             if a database error occurs
221          */
222         public List<Artist> loadProperties(List<Artist> artists) throws DatabaseException {
223                 for (Artist artist : artists) {
224                         loadProperties(artist);
225                 }
226                 return artists;
227         }
228
229         /**
230          * Returns all remix artists involved in the track with the given ID.
231          *
232          * @param trackId
233          *            The ID of the track
234          * @return All remix artists involved in the track, in preferred order
235          * @throws DatabaseException
236          *             if a database error occurs
237          */
238         public List<Artist> getRemixArtistsByTrack(String trackId) throws DatabaseException {
239                 Query query = new Query(Type.SELECT, "ARTISTS");
240                 query.addField(new Field("ARTISTS.*"));
241                 query.addJoin(new Join(JoinType.INNER, "TRACK_REMIX_ARTISTS", new Field("TRACK_REMIX_ARTISTS.ARTIST"), new Field("ARTISTS.ID")));
242                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_REMIX_ARTISTS.TRACK", new StringParameter(trackId))));
243                 query.addOrderField(new OrderField(new Field("TRACK_REMIX_ARTISTS.DISPLAY_ORDER")));
244                 return database.getMultiple(query, artistCreator);
245         }
246
247         /**
248          * Returns all related tracks for the track with the given ID.
249          *
250          * @param trackId
251          *            The ID of the tracks
252          * @return A mapping from relationship to all tracks that match the relation
253          * @throws DatabaseException
254          *             if a database error occurs
255          */
256         public Map<Relationship, Collection<Track>> getRelatedTracksByTrack(String trackId) throws DatabaseException {
257                 Query query = new Query(Type.SELECT, "TRACKS");
258                 query.addField(new Field("TRACKS.*"));
259                 query.addField(new Field("TRACK_RELATIONS.*"));
260                 query.addJoin(new Join(JoinType.INNER, "TRACK_RELATIONS", new Field("TRACK_RELATIONS.TRACK"), new Field("TRACK_RELATIONS.RELATED_TRACK")));
261                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_RELATIONS.TRACK", new StringParameter(trackId))));
262                 final Map<Relationship, Collection<Track>> relatedTracks = new EnumMap<Relationship, Collection<Track>>(Relationship.class);
263                 database.process(query, new ResultProcessor() {
264
265                         @Override
266                         @SuppressWarnings("synthetic-access")
267                         public void processResult(ResultSet resultSet) throws SQLException {
268                                 Track track = trackCreator.createObject(resultSet);
269                                 Relationship relationship = Relationship.valueOf(resultSet.getString("TRACK_RELATIONS.RELATIONSHIP"));
270                                 if (!relatedTracks.containsKey(relationship)) {
271                                         relatedTracks.put(relationship, new HashSet<Track>());
272                                 }
273                                 relatedTracks.get(relationship).add(track);
274                         }
275                 });
276                 return relatedTracks;
277         }
278
279         /**
280          * Returns the track with the given ID.
281          *
282          * @param id
283          *            The ID of the track
284          * @return The track with the given ID, or {@code null} if there is no such
285          *         track
286          * @throws DatabaseException
287          *             if a database error occurs
288          */
289         public Track getTrackById(String id) throws DatabaseException {
290                 Query query = new Query(Type.SELECT, "TRACKS");
291                 query.addField(new Field("TRACKS.*"));
292                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACKS.ID", new StringParameter(id))));
293                 return database.getSingle(query, trackCreator);
294         }
295
296         /**
297          * Returns all tracks by the artist with the given ID.
298          *
299          * @param artistId
300          *            The ID of the artist
301          * @return All tracks by the given artist
302          * @throws DatabaseException
303          *             if a database error occurs
304          */
305         public Collection<Track> getTracksByArtist(String artistId) throws DatabaseException {
306                 Query query = new Query(Type.SELECT, "TRACKS");
307                 query.addField(new Field("TRACKS.*"));
308                 query.addJoin(new Join(JoinType.INNER, "TRACK_ARTISTS", new Field("TRACKS.ID"), new Field("TRACK_ARTISTS.TRACK")));
309                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_ARTISTS.ARTIST", new StringParameter(artistId))));
310                 return database.getMultiple(query, trackCreator);
311         }
312
313         /**
314          * Returns all groups the artist with the given ID belongs to.
315          *
316          * @param artistId
317          *            The ID of the artist
318          * @return All groups the artist belongs to
319          * @throws DatabaseException
320          *             if a database error occurs
321          */
322         public Collection<Group> getGroupsByArtist(String artistId) throws DatabaseException {
323                 Query query = new Query(Type.SELECT, "GROUPS");
324                 query.addField(new Field("GROUPS.*"));
325                 query.addJoin(new Join(JoinType.INNER, "GROUP_ARTISTS", new Field("GROUPS.ID"), new Field("GROUP_ARTISTS.GROUP_")));
326                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("GROUP_ARTISTS.ARTIST", new StringParameter(artistId))));
327                 return database.getMultiple(query, groupCreator);
328         }
329
330         /**
331          * Returns all styles for the track with the given ID.
332          *
333          * @param trackId
334          *            The ID of the track
335          * @return All styles for the given track
336          * @throws DatabaseException
337          *             if a database error occurs
338          */
339         public Collection<Style> getStylesByTrack(String trackId) throws DatabaseException {
340                 Query query = new Query(Type.SELECT, "STYLES");
341                 query.addField(new Field("STYLES.*"));
342                 query.addJoin(new Join(JoinType.INNER, "TRACK_STYLES", new Field("STYLES.ID"), new Field("TRACK_STYLES.STYLE")));
343                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_STYLES.TRACK", new StringParameter(trackId))));
344                 return database.getMultiple(query, styleCreator);
345         }
346
347         /**
348          * Returns the user with the given name.
349          *
350          * @param username
351          *            The name of the user
352          * @return The user, or {@code null} if the user does not exist
353          * @throws DatabaseException
354          *             if a database error occurs
355          */
356         public User getUserByName(String username) throws DatabaseException {
357                 Query query = new Query(Type.SELECT, "USERS");
358                 query.addField(new Field("USERS.*"));
359                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("USERS.NAME", new StringParameter(username))));
360                 return database.getSingle(query, userCreator);
361         }
362
363         /**
364          * Returns the user connected with the given OpenID.
365          *
366          * @param openId
367          *            The OpenID to find the user for
368          * @return The user connected with the given OpenID, or {@code null} if
369          *         there is no such user
370          * @throws DatabaseException
371          *             if a database error occurs
372          */
373         public User getUserByOpenId(String openId) throws DatabaseException {
374                 Query query = new Query(Type.SELECT, "USERS");
375                 query.addField(new Field("USERS.*"));
376                 query.addJoin(new Join(JoinType.INNER, "USER_OPENIDS", new Field("USER_OPENIDS.USER"), new Field("USERS.ID")));
377                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("USER_OPENIDS.OPENID", new StringParameter(openId))));
378                 return database.getSingle(query, userCreator);
379         }
380
381         /**
382          * Returns all OpenIDs connected with the user with the given ID.
383          *
384          * @param userId
385          *            The ID of the user
386          * @return All OpenIDs connected with the given user
387          * @throws DatabaseException
388          *             if a database error occurs
389          */
390         public Collection<String> getOpenIdsByUser(String userId) throws DatabaseException {
391                 Query query = new Query(Type.SELECT, "USER_OPENIDS");
392                 query.addField(new Field("USER_OPENIDS.*"));
393                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("USER_OPENIDS.USER", new StringParameter(userId))));
394                 return database.getMultiple(query, new StringCreator("USER_OPENIDS.OPENID"));
395         }
396
397         /**
398          * {@link Artist} implementation that retrieves some attributes (such as
399          * {@link #getGroups()}, and {@link #getTracks()}) from the
400          * {@link DataManager} on demand.
401          *
402          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
403          */
404         private class LazyArtist extends DefaultArtist {
405
406                 /** Memoizer for the tracks by this artist. */
407                 private final Memoizer<Void> tracksMemoizer = new Memoizer<Void>(new Callable<Void>() {
408                         @Override
409                         public Void call() throws DatabaseException {
410                                 if (!hasValue("tracks")) {
411                                         getValue("tracks", Collection.class).set(getTracksByArtist(getId()));
412                                 }
413                                 return null;
414                         }
415                 });
416
417                 /** Memoizer for the groups of this artist. */
418                 private final Memoizer<Void> groupsMemoizer = new Memoizer<Void>(new Callable<Void>() {
419
420                         @Override
421                         public Void call() throws Exception {
422                                 if (!hasValue("groups")) {
423                                         getValue("groups", Collection.class).set(getGroupsByArtist(getId()));
424                                 }
425                                 return null;
426                         }
427
428                 });
429
430                 /**
431                  * Creates a new lazy artist.
432                  *
433                  * @param id
434                  *            The ID of the artist
435                  */
436                 public LazyArtist(String id) {
437                         super(id);
438                 }
439
440                 //
441                 // DEFAULTARTIST METHODS
442                 //
443
444                 /**
445                  * {@inheritDoc}
446                  */
447                 @Override
448                 public Collection<Group> getGroups() {
449                         groupsMemoizer.get();
450                         return super.getGroups();
451                 }
452
453                 /**
454                  * {@inheritDoc}
455                  */
456                 @Override
457                 public Collection<Track> getTracks() {
458                         tracksMemoizer.get();
459                         return super.getTracks();
460                 }
461
462         }
463
464         /**
465          * {@link ObjectCreator} implementation that can create {@link Artist}
466          * objects. This specific class actually creates {@link LazyArtist}
467          * instances.
468          *
469          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
470          */
471         private class ArtistCreator implements ObjectCreator<Artist> {
472
473                 /**
474                  * {@inheritDoc}
475                  */
476                 @Override
477                 public Artist createObject(ResultSet resultSet) throws SQLException {
478                         return new LazyArtist(resultSet.getString("ARTISTS.ID")).setName(resultSet.getString("ARTISTS.NAME"));
479                 }
480
481         }
482
483         /**
484          * {@link Group} implementation that retrieves some attributes (such as
485          * {@link #getArtists()}) from the {@link DataManager} on demand.
486          *
487          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
488          */
489         private class LazyGroup extends DefaultGroup {
490
491                 /** Memoizer for the artist. */
492                 private final Memoizer<Void> artistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
493
494                         @Override
495                         public Void call() throws Exception {
496                                 if (!hasValue("artists")) {
497                                         getValue("artists", Collection.class).set(getArtistsByGroup(getId()));
498                                 }
499                                 return null;
500                         }
501
502                 });
503
504                 /**
505                  * Creates a new lazy group.
506                  *
507                  * @param id
508                  *            The ID of the group
509                  */
510                 public LazyGroup(String id) {
511                         super(id);
512                 }
513
514                 //
515                 // DEFAULTGROUP METHODS
516                 //
517
518                 /**
519                  * {@inheritDoc}
520                  */
521                 @Override
522                 public Collection<Artist> getArtists() {
523                         artistsMemoizer.get();
524                         return super.getArtists();
525                 }
526
527         }
528
529         /**
530          * {@link ObjectCreator} implementation that can create {@link Group}
531          * objects. This specific implementation creates {@link LazyGroup}
532          * instances.
533          *
534          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
535          */
536         private class GroupCreator implements ObjectCreator<Group> {
537
538                 /**
539                  * {@inheritDoc}
540                  */
541                 @Override
542                 public Group createObject(ResultSet resultSet) throws SQLException {
543                         return new LazyGroup(resultSet.getString("GROUPS.ID")).setName(resultSet.getString("GROUPS.NAME")).setUrl(resultSet.getString("GROUPS.URL"));
544                 }
545
546         }
547
548         /**
549          * {@link Track} implementation that retrieves some attributes (such as
550          * {@link #getArtists()}, and {@link #getStyles()}) from the
551          * {@link DataManager} on demand.
552          *
553          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
554          */
555         private class LazyTrack extends DefaultTrack {
556
557                 /** Memoizer for the artists. */
558                 private final Memoizer<Void> artistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
559
560                         @Override
561                         public Void call() throws Exception {
562                                 if (!hasValue("artists")) {
563                                         getValue("artists", List.class).set(getArtistsByTrack(getId()));
564                                 }
565                                 return null;
566                         }
567
568                 });
569
570                 /** Memoizer for the styles. */
571                 private final Memoizer<Void> stylesMemoizer = new Memoizer<Void>(new Callable<Void>() {
572
573                         @Override
574                         public Void call() throws Exception {
575                                 if (!hasValue("styles")) {
576                                         getValue("styles", Collection.class).set(getStylesByTrack(getId()));
577                                 }
578                                 return null;
579                         }
580
581                 });
582
583                 /** Memoizer for the remix artists. */
584                 private final Memoizer<Void> remixArtistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
585
586                         @Override
587                         public Void call() throws Exception {
588                                 if (!hasValue("remixArtists")) {
589                                         getValue("remixArtists", List.class).set(getRemixArtistsByTrack(getId()));
590                                 }
591                                 return null;
592                         }
593
594                 });
595
596                 /** Memoizer for the related tracks. */
597                 private final Memoizer<Void> relatedTracksMemoizer = new Memoizer<Void>(new Callable<Void>() {
598
599                         @Override
600                         public Void call() throws Exception {
601                                 if (!hasValue("relatedTracks")) {
602                                         getValue("relatedTracks", Map.class).set(getRelatedTracksByTrack(getId()));
603                                 }
604                                 return null;
605                         }
606                 });
607
608                 /**
609                  * Creates a new track.
610                  *
611                  * @param id
612                  *            The ID of the track
613                  */
614                 public LazyTrack(String id) {
615                         super(id);
616                 }
617
618                 //
619                 // DEFAULTTRACK METHODS
620                 //
621
622                 /**
623                  * {@inheritDoc}
624                  */
625                 @Override
626                 public List<Artist> getArtists() {
627                         artistsMemoizer.get();
628                         return super.getArtists();
629                 }
630
631                 /**
632                  * {@inheritDoc}
633                  */
634                 @Override
635                 public Collection<Style> getStyles() {
636                         stylesMemoizer.get();
637                         return super.getStyles();
638                 }
639
640                 /**
641                  * {@inheritDoc}
642                  */
643                 @Override
644                 public List<Artist> getRemixArtists() {
645                         remixArtistsMemoizer.get();
646                         return super.getRemixArtists();
647                 }
648
649                 /**
650                  * {@inheritDoc}
651                  */
652                 @Override
653                 public Map<Relationship, Collection<Track>> getRelatedTracks() {
654                         relatedTracksMemoizer.get();
655                         return super.getRelatedTracks();
656                 }
657
658         }
659
660         /**
661          * {@link ObjectCreator} implementation that can create {@link Track}
662          * objects. This specific implementation creates {@link LazyTrack}
663          * instances.
664          *
665          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
666          */
667         private class TrackCreator implements ObjectCreator<Track> {
668
669                 /**
670                  * {@inheritDoc}
671                  */
672                 @Override
673                 public Track createObject(ResultSet resultSet) throws SQLException {
674                         return new LazyTrack(resultSet.getString("TRACKS.ID")).setName(resultSet.getString("TRACKS.NAME")).setRemix(resultSet.getString("TRACKS.REMIX"));
675                 }
676
677         }
678
679         /**
680          * {@link ObjectCreator} implementation that can create {@link Style}
681          * objects.
682          *
683          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
684          */
685         private class StyleCreator implements ObjectCreator<Style> {
686
687                 /**
688                  * {@inheritDoc}
689                  */
690                 @Override
691                 public Style createObject(ResultSet resultSet) throws SQLException {
692                         return new DefaultStyle(resultSet.getString("STYLES.ID")).setName(resultSet.getString("STYLES.NAME"));
693                 }
694
695         }
696
697         /**
698          * {@link User} implementation that retrieves some attributes (such as
699          * {@link #getOpenIds()}) from the {@link DataManager} on demand.
700          *
701          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
702          */
703         private class LazyUser extends DefaultUser {
704
705                 /** Memoizer for a user’s OpenIDs. */
706                 private final Memoizer<Void> openIdMemoizer = new Memoizer<Void>(new Callable<Void>() {
707
708                         @Override
709                         public Void call() throws Exception {
710                                 if (!hasValue("openIds")) {
711                                         getValue("openIds", Collection.class).set(getOpenIdsByUser(getId()));
712                                 }
713                                 return null;
714                         }
715                 });
716
717                 /**
718                  * Creates a new user.
719                  *
720                  * @param id
721                  *            The ID of the user
722                  */
723                 public LazyUser(String id) {
724                         super(id);
725                 }
726
727                 /**
728                  * {@inheritDoc}
729                  */
730                 @Override
731                 public Collection<String> getOpenIds() {
732                         openIdMemoizer.get();
733                         return super.getOpenIds();
734                 }
735
736         }
737
738         /**
739          * {@link ObjectCreator} implementation that can create {@link User}
740          * objects. This specific implementation creates {@link LazyUser} instances.
741          *
742          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
743          */
744         private class UserCreator implements ObjectCreator<User> {
745
746                 /**
747                  * {@inheritDoc}
748                  */
749                 @Override
750                 public User createObject(ResultSet resultSet) throws SQLException {
751                         return new LazyUser(resultSet.getString("USERS.ID")).setName(resultSet.getString("USERS.NAME")).setPasswordHash(resultSet.getString("USERS.PASSWORD")).setLevel(resultSet.getInt("USERS.LEVEL"));
752                 }
753
754         }
755
756 }