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.*"));
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.*"));
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.*"));
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.*"));
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.*"));
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.*"));
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.*"));
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 {
}
});
+ /** Memoizer for the groups of this artist. */
private final Memoizer<Void> groupsMemoizer = new Memoizer<Void>(new Callable<Void>() {
@Override
});
+ /**
+ * Creates a new lazy artist.
+ *
+ * @param id
+ * The ID of the artist
+ */
public LazyArtist(String id) {
super(id);
}
+ //
+ // DEFAULTARTIST METHODS
+ //
+
/**
* {@inheritDoc}
*/
return super.getGroups();
}
+ /**
+ * {@inheritDoc}
+ */
@Override
public Collection<Track> getTracks() {
tracksMemoizer.get();
}
+ /**
+ * {@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> {
/**
}
+ /**
+ * {@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
});
+ /**
+ * Creates a new lazy group.
+ *
+ * @param id
+ * The ID of the group
+ */
public LazyGroup(String id) {
super(id);
}
+ //
+ // DEFAULTGROUP METHODS
+ //
+
/**
* {@inheritDoc}
*/
}
+ /**
+ * {@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> {
/**
}
+ /**
+ * {@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
});
+ /** Memoizer for the styles. */
private final Memoizer<Void> stylesMemoizer = new Memoizer<Void>(new Callable<Void>() {
@Override
});
/**
+ * Creates a new track.
+ *
* @param id
+ * The ID of the track
*/
public LazyTrack(String id) {
super(id);
}
+ //
+ // DEFAULTTRACK METHODS
+ //
+
/**
* {@inheritDoc}
*/
}
+ /**
+ * {@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> {
/**
}
+ /**
+ * {@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> {
/**