--- /dev/null
+/*
+ * DemosceneMusic - DataManager.java - Copyright © 2012 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.demoscenemusic.data;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import net.pterodactylus.util.database.Database;
+import net.pterodactylus.util.database.DatabaseException;
+import net.pterodactylus.util.database.Field;
+import net.pterodactylus.util.database.Join;
+import net.pterodactylus.util.database.Join.JoinType;
+import net.pterodactylus.util.database.ObjectCreator;
+import net.pterodactylus.util.database.Parameter.StringParameter;
+import net.pterodactylus.util.database.Query;
+import net.pterodactylus.util.database.Query.Type;
+import net.pterodactylus.util.database.ValueField;
+import net.pterodactylus.util.database.ValueFieldWhereClause;
+
+/**
+ * TODO
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class DataManager {
+
+ private final Database database;
+
+ private final TrackLoader trackLoader = new TrackLoader();
+
+ private final ArtistLoader artistLoader = new ArtistLoader();
+
+ private final StyleLoader styleLoader = new StyleLoader();
+
+ public DataManager(Database database) {
+ this.database = database;
+ }
+
+ public Track trackById(String id) throws DatabaseException {
+ Query query = new Query(Type.SELECT, "TRACKS");
+ query.addField(new Field("TRACKS.*"));
+ query.addField(new Field("ARTISTS.*"));
+ query.addField(new Field("STYLES.*"));
+ query.addJoin(new Join(JoinType.INNER, "ARTISTS", new Field("TRACKS.ARTIST"), new Field("ARTISTS.ID")));
+ query.addJoin(new Join(JoinType.INNER, "STYLES", new Field("TRACKS.STYLE"), new Field("STYLES.ID")));
+ query.addWhereClause(new ValueFieldWhereClause(new ValueField("ID", new StringParameter(id))));
+ return database.getSingle(query, trackLoader);
+ }
+
+ private class TrackLoader implements ObjectCreator<Track> {
+
+ /**
+ * {@inheritDoc}
+ */
+ public Track createObject(ResultSet resultSet) throws SQLException {
+ return new AbstractTrack(resultSet.getString("TRACKS.ID")) {
+
+ public void save() {
+ if (!dirty()) {
+ return;
+ }
+ }
+
+ }.name(resultSet.getString("TRACKS.NAME")).style(styleLoader.createObject(resultSet)).artist(artistLoader.createObject(resultSet));
+ }
+
+ }
+
+ private class ArtistLoader implements ObjectCreator<Artist> {
+
+ /**
+ * {@inheritDoc}
+ */
+ public Artist createObject(ResultSet resultSet) throws SQLException {
+ return new AbstractArtist(resultSet.getString("ARTISTS.ID")) {
+
+ public void save() {
+ if (!dirty()) {
+ return;
+ }
+ }
+
+ }.name(resultSet.getString("ARTISTS.NAME"));
+ }
+
+ }
+
+ private class StyleLoader implements ObjectCreator<Style> {
+
+ /**
+ * {@inheritDoc}
+ */
+ public Style createObject(ResultSet resultSet) throws SQLException {
+ return new AbstractStyle(resultSet.getString("STYLES.ID")) {
+
+ public void save() {
+ if (!dirty()) {
+ return;
+ }
+ }
+
+ }.name(resultSet.getString("STYLES.NAME"));
+ }
+
+ }
+
+}