Add javadoc.
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / data / DataManager.java
index 482e717..f3252f0 100644 (file)
@@ -38,29 +38,64 @@ import net.pterodactylus.util.database.ValueField;
 import net.pterodactylus.util.database.ValueFieldWhereClause;
 
 /**
- * TODO
+ * Interface between the database and the application.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
 public class DataManager {
 
+       /** The artist object creator. */
+       @SuppressWarnings("synthetic-access")
        private final ObjectCreator<Artist> artistCreator = new ArtistCreator();
+
+       /** The group object creator. */
+       @SuppressWarnings("synthetic-access")
        private final ObjectCreator<Group> groupCreator = new GroupCreator();
+
+       /** The track object creator. */
+       @SuppressWarnings("synthetic-access")
        private final ObjectCreator<Track> trackCreator = new TrackCreator();
+
+       /** The style object creator. */
+       @SuppressWarnings("synthetic-access")
        private final ObjectCreator<Style> styleCreator = new StyleCreator();
 
+       /** The database. */
        private final Database database;
 
+       /**
+        * Creates a new data manager.
+        *
+        * @param database
+        *            The database to operate on
+        */
        public DataManager(Database database) {
                this.database = database;
        }
 
+       /**
+        * Returns all artists.
+        *
+        * @return All artists
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
        public Collection<Artist> getAllArtists() throws DatabaseException {
                Query query = new Query(Type.SELECT, "ARTISTS");
                query.addField(new Field("ARTISTS.*"));
                return database.getMultiple(query, artistCreator);
        }
 
+       /**
+        * Returns the artist with the given ID.
+        *
+        * @param id
+        *            The ID of the artist
+        * @return The artist with the given ID, or {@code null} if there is no
+        *         artist with the given ID
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
        public Artist getArtistById(String id) throws DatabaseException {
                Query query = new Query(Type.SELECT, "ARTISTS");
                query.addField(new Field("ARTISTS.*"));
@@ -68,6 +103,15 @@ public class DataManager {
                return database.getSingle(query, artistCreator);
        }
 
+       /**
+        * Returns all artists that belong to the group with the given ID.
+        *
+        * @param groupId
+        *            The ID of the group
+        * @return All artists belonging to the given group
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
        public Collection<Artist> getArtistsByGroup(String groupId) throws DatabaseException {
                Query query = new Query(Type.SELECT, "ARTISTS");
                query.addField(new Field("ARTISTS.*"));
@@ -76,6 +120,15 @@ public class DataManager {
                return database.getMultiple(query, artistCreator);
        }
 
+       /**
+        * Returns all artists involved in the track with the given ID.
+        *
+        * @param trackId
+        *            The ID of the track
+        * @return All artists involved in the track, in preferred order
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
        public List<Artist> getArtistsByTrack(String trackId) throws DatabaseException {
                Query query = new Query(Type.SELECT, "ARTISTS");
                query.addField(new Field("ARTISTS.*"));
@@ -85,6 +138,16 @@ public class DataManager {
                return database.getMultiple(query, artistCreator);
        }
 
+       /**
+        * Returns the track with the given ID.
+        *
+        * @param id
+        *            The ID of the track
+        * @return The track with the given ID, or {@code null} if there is no such
+        *         track
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
        public Track getTrackById(String id) throws DatabaseException {
                Query query = new Query(Type.SELECT, "TRACKS");
                query.addField(new Field("TRACKS.*"));
@@ -92,6 +155,15 @@ public class DataManager {
                return database.getSingle(query, trackCreator);
        }
 
+       /**
+        * Returns all tracks by the artist with the given ID.
+        *
+        * @param artistId
+        *            The ID of the artist
+        * @return All tracks by the given artist
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
        public Collection<Track> getTracksByArtist(String artistId) throws DatabaseException {
                Query query = new Query(Type.SELECT, "TRACKS");
                query.addField(new Field("TRACKS.*"));
@@ -100,6 +172,15 @@ public class DataManager {
                return database.getMultiple(query, trackCreator);
        }
 
+       /**
+        * Returns all groups the artist with the given ID belongs to.
+        *
+        * @param artistId
+        *            The ID of the artist
+        * @return All groups the artist belongs to
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
        public Collection<Group> getGroupsByArtist(String artistId) throws DatabaseException {
                Query query = new Query(Type.SELECT, "GROUPS");
                query.addField(new Field("GROUPS.*"));
@@ -108,49 +189,97 @@ public class DataManager {
                return database.getMultiple(query, groupCreator);
        }
 
+       /**
+        * Returns all styles for the track with the given ID.
+        *
+        * @param trackId
+        *            The ID of the track
+        * @return All styles for the given track
+        * @throws DatabaseException
+        *             if a database error occurs
+        */
+       public Collection<Style> getStylesByTrack(String trackId) throws DatabaseException {
+               Query query = new Query(Type.SELECT, "STYLES");
+               query.addField(new Field("STYLES.*"));
+               query.addJoin(new Join(JoinType.INNER, "TRACK_STYLES", new Field("STYLES.ID"), new Field("TRACK_STYLES.STYLE")));
+               query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_STYLES.TRACK", new StringParameter(trackId))));
+               return database.getMultiple(query, styleCreator);
+       }
+
+       /**
+        * {@link Artist} implementation that retrieves some attributes (such as
+        * {@link #getGroups()}, and {@link #getTracks()}) from the
+        * {@link DataManager} on demand.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
        private class LazyArtist extends DefaultArtist {
 
+               /** Memoizer for the tracks by this artist. */
                private final Memoizer<Void> tracksMemoizer = new Memoizer<Void>(new Callable<Void>() {
+                       @Override
                        public Void call() throws DatabaseException {
                                if (!hasValue("tracks")) {
-                                       value("tracks", Collection.class).set(getTracksByArtist(id()));
+                                       getValue("tracks", Collection.class).set(getTracksByArtist(getId()));
                                }
                                return null;
                        }
                });
 
+               /** Memoizer for the groups of this artist. */
                private final Memoizer<Void> groupsMemoizer = new Memoizer<Void>(new Callable<Void>() {
 
                        @Override
                        public Void call() throws Exception {
                                if (!hasValue("groups")) {
-                                       value("groups", Collection.class).set(getGroupsByArtist(id()));
+                                       getValue("groups", Collection.class).set(getGroupsByArtist(getId()));
                                }
                                return null;
                        }
 
                });
 
+               /**
+                * Creates a new lazy artist.
+                *
+                * @param id
+                *            The ID of the artist
+                */
                public LazyArtist(String id) {
                        super(id);
                }
 
+               //
+               // DEFAULTARTIST METHODS
+               //
+
                /**
                 * {@inheritDoc}
                 */
                @Override
-               public Collection<Group> groups() {
+               public Collection<Group> getGroups() {
                        groupsMemoizer.get();
-                       return super.groups();
+                       return super.getGroups();
                }
 
-               public Collection<Track> tracks() {
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               public Collection<Track> getTracks() {
                        tracksMemoizer.get();
-                       return super.tracks();
+                       return super.getTracks();
                }
 
        }
 
+       /**
+        * {@link ObjectCreator} implementation that can create {@link Artist}
+        * objects. This specific class actually creates {@link LazyArtist}
+        * instances.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
        private class ArtistCreator implements ObjectCreator<Artist> {
 
                /**
@@ -158,40 +287,64 @@ public class DataManager {
                 */
                @Override
                public Artist createObject(ResultSet resultSet) throws SQLException {
-                       return new LazyArtist(resultSet.getString("ARTISTS.ID")).name(resultSet.getString("ARTISTS.NAME"));
+                       return new LazyArtist(resultSet.getString("ARTISTS.ID")).setName(resultSet.getString("ARTISTS.NAME"));
                }
 
        }
 
+       /**
+        * {@link Group} implementation that retrieves some attributes (such as
+        * {@link #getArtists()}) from the {@link DataManager} on demand.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
        private class LazyGroup extends DefaultGroup {
 
+               /** Memoizer for the artist. */
                private final Memoizer<Void> artistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
 
                        @Override
                        public Void call() throws Exception {
                                if (!hasValue("artists")) {
-                                       value("artists", Collection.class).set(getArtistsByGroup(id()));
+                                       getValue("artists", Collection.class).set(getArtistsByGroup(getId()));
                                }
                                return null;
                        }
 
                });
 
+               /**
+                * Creates a new lazy group.
+                *
+                * @param id
+                *            The ID of the group
+                */
                public LazyGroup(String id) {
                        super(id);
                }
 
+               //
+               // DEFAULTGROUP METHODS
+               //
+
                /**
                 * {@inheritDoc}
                 */
                @Override
-               public Collection<Artist> artists() {
+               public Collection<Artist> getArtists() {
                        artistsMemoizer.get();
-                       return super.artists();
+                       return super.getArtists();
                }
 
        }
 
+       /**
+        * {@link ObjectCreator} implementation that can create {@link Group}
+        * objects. This specific implementation creates {@link LazyGroup}
+        * instances.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
        private class GroupCreator implements ObjectCreator<Group> {
 
                /**
@@ -199,19 +352,40 @@ public class DataManager {
                 */
                @Override
                public Group createObject(ResultSet resultSet) throws SQLException {
-                       return new LazyGroup(resultSet.getString("GROUPS.ID")).name(resultSet.getString("GROUPS.NAME")).url(resultSet.getString("GROUPS.URL"));
+                       return new LazyGroup(resultSet.getString("GROUPS.ID")).setName(resultSet.getString("GROUPS.NAME")).setUrl(resultSet.getString("GROUPS.URL"));
                }
 
        }
 
+       /**
+        * {@link Track} implementation that retrieves some attributes (such as
+        * {@link #getArtists()}, and {@link #getStyles()}) from the
+        * {@link DataManager} on demand.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
        private class LazyTrack extends DefaultTrack {
 
+               /** Memoizer for the artists. */
                private final Memoizer<Void> artistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
 
                        @Override
                        public Void call() throws Exception {
                                if (!hasValue("artists")) {
-                                       value("artists", List.class).set(getArtistsByTrack(id()));
+                                       getValue("artists", List.class).set(getArtistsByTrack(getId()));
+                               }
+                               return null;
+                       }
+
+               });
+
+               /** Memoizer for the styles. */
+               private final Memoizer<Void> stylesMemoizer = new Memoizer<Void>(new Callable<Void>() {
+
+                       @Override
+                       public Void call() throws Exception {
+                               if (!hasValue("styles")) {
+                                       getValue("styles", Collection.class).set(getStylesByTrack(getId()));
                                }
                                return null;
                        }
@@ -219,32 +393,46 @@ public class DataManager {
                });
 
                /**
+                * Creates a new track.
+                *
                 * @param id
+                *            The ID of the track
                 */
                public LazyTrack(String id) {
                        super(id);
                }
 
+               //
+               // DEFAULTTRACK METHODS
+               //
+
                /**
                 * {@inheritDoc}
                 */
                @Override
-               public List<Artist> artists() {
+               public List<Artist> getArtists() {
                        artistsMemoizer.get();
-                       return super.artists();
+                       return super.getArtists();
                }
 
                /**
                 * {@inheritDoc}
                 */
                @Override
-               public Collection<Style> styles() {
-                       // TODO Auto-generated method stub
-                       return super.styles();
+               public Collection<Style> getStyles() {
+                       stylesMemoizer.get();
+                       return super.getStyles();
                }
 
        }
 
+       /**
+        * {@link ObjectCreator} implementation that can create {@link Track}
+        * objects. This specific implementation creates {@link LazyTrack}
+        * instances.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
        private class TrackCreator implements ObjectCreator<Track> {
 
                /**
@@ -252,11 +440,17 @@ public class DataManager {
                 */
                @Override
                public Track createObject(ResultSet resultSet) throws SQLException {
-                       return new LazyTrack(resultSet.getString("TRACKS.ID")).name(resultSet.getString("TRACKS.NAME"));
+                       return new LazyTrack(resultSet.getString("TRACKS.ID")).setName(resultSet.getString("TRACKS.NAME"));
                }
 
        }
 
+       /**
+        * {@link ObjectCreator} implementation that can create {@link Style}
+        * objects.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
        private class StyleCreator implements ObjectCreator<Style> {
 
                /**
@@ -264,7 +458,7 @@ public class DataManager {
                 */
                @Override
                public Style createObject(ResultSet resultSet) throws SQLException {
-                       return new DefaultStyle(resultSet.getString("STYLES.ID")).name(resultSet.getString("STYLES.NAME"));
+                       return new DefaultStyle(resultSet.getString("STYLES.ID")).setName(resultSet.getString("STYLES.NAME"));
                }
 
        }