Add first stab at database interface.
[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
23 import net.pterodactylus.util.database.Database;
24 import net.pterodactylus.util.database.DatabaseException;
25 import net.pterodactylus.util.database.Field;
26 import net.pterodactylus.util.database.Join;
27 import net.pterodactylus.util.database.Join.JoinType;
28 import net.pterodactylus.util.database.ObjectCreator;
29 import net.pterodactylus.util.database.Parameter.StringParameter;
30 import net.pterodactylus.util.database.Query;
31 import net.pterodactylus.util.database.Query.Type;
32 import net.pterodactylus.util.database.ValueField;
33 import net.pterodactylus.util.database.ValueFieldWhereClause;
34
35 /**
36  * TODO
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public class DataManager {
41
42         private final Database database;
43
44         private final TrackLoader trackLoader = new TrackLoader();
45
46         private final ArtistLoader artistLoader = new ArtistLoader();
47
48         private final StyleLoader styleLoader = new StyleLoader();
49
50         public DataManager(Database database) {
51                 this.database = database;
52         }
53
54         public Track trackById(String id) throws DatabaseException {
55                 Query query = new Query(Type.SELECT, "TRACKS");
56                 query.addField(new Field("TRACKS.*"));
57                 query.addField(new Field("ARTISTS.*"));
58                 query.addField(new Field("STYLES.*"));
59                 query.addJoin(new Join(JoinType.INNER, "ARTISTS", new Field("TRACKS.ARTIST"), new Field("ARTISTS.ID")));
60                 query.addJoin(new Join(JoinType.INNER, "STYLES", new Field("TRACKS.STYLE"), new Field("STYLES.ID")));
61                 query.addWhereClause(new ValueFieldWhereClause(new ValueField("ID", new StringParameter(id))));
62                 return database.getSingle(query, trackLoader);
63         }
64
65         private class TrackLoader implements ObjectCreator<Track> {
66
67                 /**
68                  * {@inheritDoc}
69                  */
70                 public Track createObject(ResultSet resultSet) throws SQLException {
71                         return new AbstractTrack(resultSet.getString("TRACKS.ID")) {
72
73                                 public void save() {
74                                         if (!dirty()) {
75                                                 return;
76                                         }
77                                 }
78
79                         }.name(resultSet.getString("TRACKS.NAME")).style(styleLoader.createObject(resultSet)).artist(artistLoader.createObject(resultSet));
80                 }
81
82         }
83
84         private class ArtistLoader implements ObjectCreator<Artist> {
85
86                 /**
87                  * {@inheritDoc}
88                  */
89                 public Artist createObject(ResultSet resultSet) throws SQLException {
90                         return new AbstractArtist(resultSet.getString("ARTISTS.ID")) {
91
92                                 public void save() {
93                                         if (!dirty()) {
94                                                 return;
95                                         }
96                                 }
97
98                         }.name(resultSet.getString("ARTISTS.NAME"));
99                 }
100
101         }
102
103         private class StyleLoader implements ObjectCreator<Style> {
104
105                 /**
106                  * {@inheritDoc}
107                  */
108                 public Style createObject(ResultSet resultSet) throws SQLException {
109                         return new AbstractStyle(resultSet.getString("STYLES.ID")) {
110
111                                 public void save() {
112                                         if (!dirty()) {
113                                                 return;
114                                         }
115                                 }
116
117                         }.name(resultSet.getString("STYLES.NAME"));
118                 }
119
120         }
121
122 }