import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
+import java.util.UUID;
import java.util.concurrent.Callable;
import net.pterodactylus.demoscenemusic.data.Track.Relationship;
public Collection<Artist> getAllArtists() throws DatabaseException {
Query query = new Query(Type.SELECT, "ARTISTS");
query.addField(new Field("ARTISTS.*"));
- return database.getMultiple(query, artistCreator);
+ return loadProperties(database.getMultiple(query, artistCreator));
}
/**
Query query = new Query(Type.SELECT, "ARTISTS");
query.addField(new Field("ARTISTS.*"));
query.addWhereClause(new ValueFieldWhereClause(new ValueField("ARTISTS.ID", new StringParameter(id))));
- return database.getSingle(query, artistCreator);
+ return loadProperties(database.getSingle(query, artistCreator));
}
/**
query.addField(new Field("ARTISTS.*"));
query.addJoin(new Join(JoinType.INNER, "GROUP_ARTISTS", new Field("ARTISTS.ID"), new Field("GROUP_ARTISTS.ARTIST")));
query.addWhereClause(new ValueFieldWhereClause(new ValueField("GROUP_ARTISTS.GROUP_", new StringParameter(groupId))));
- return database.getMultiple(query, artistCreator);
+ return loadProperties(database.getMultiple(query, artistCreator));
}
/**
query.addJoin(new Join(JoinType.INNER, "TRACK_ARTISTS", new Field("TRACK_ARTISTS.ARTIST"), new Field("ARTISTS.ID")));
query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACK_ARTISTS.TRACK", new StringParameter(trackId))));
query.addOrderField(new OrderField(new Field("TRACK_ARTISTS.DISPLAY_ORDER")));
- return database.getMultiple(query, artistCreator);
+ return loadProperties(database.getMultiple(query, artistCreator));
+ }
+
+ /**
+ * Creates a new artist with the given name.
+ *
+ * @param name
+ * The name of the artist
+ * @return The created artist
+ * @throws DatabaseException
+ * if a database error occurs
+ */
+ public Artist createArtist(String name) throws DatabaseException {
+ Query query = new Query(Type.INSERT, "ARTISTS");
+ String id = UUID.randomUUID().toString();
+ query.addValueField(new ValueField("ID", new StringParameter(id)));
+ query.addValueField(new ValueField("NAME", new StringParameter(name)));
+ database.insert(query);
+ return loadProperties(getArtistById(id));
+ }
+
+ /**
+ * Saves the given artist.
+ *
+ * @param artist
+ * The artist to save
+ * @throws DatabaseException
+ * if a database error occurs
+ */
+ public void saveArtist(Artist artist) throws DatabaseException {
+ Query query = new Query(Type.UPDATE, "ARTISTS");
+ query.addValueField(new ValueField("NAME", new StringParameter(artist.getName())));
+ query.addWhereClause(new ValueFieldWhereClause(new ValueField("ID", new StringParameter(artist.getId()))));
+ database.update(query);
+ saveArtistProperties(artist);
+ }
+
+ /**
+ * Saves the properties of the given artist.
+ *
+ * @param artist
+ * The artist whose properties to save
+ * @throws DatabaseException
+ * if a database error occurs
+ */
+ public void saveArtistProperties(Artist artist) throws DatabaseException {
+ saveProperties(artist.getProperties(), "ARTIST_PROPERTIES", "ARTIST", artist.getId());
+ }
+
+ /**
+ * Saves the given properties to the given table for the given principal.
+ *
+ * @param properties
+ * The properties to save
+ * @param table
+ * The table in which to save the properties
+ * @param type
+ * The type of the principal (e. g. “ARTIST” or “TRACK”)
+ * @param id
+ * The ID of the principial
+ * @throws DatabaseException
+ * if a database error occurs
+ */
+ public void saveProperties(Properties properties, String table, String type, String id) throws DatabaseException {
+ if (!properties.isDirty()) {
+ return;
+ }
+ Query query = new Query(Type.DELETE, table);
+ query.addWhereClause(new ValueFieldWhereClause(new ValueField(type, new StringParameter(id))));
+ database.update(query);
+ for (Entry<String, String> property : properties) {
+ Query insertQuery = new Query(Type.INSERT, table);
+ insertQuery.addValueField(new ValueField(type, new StringParameter(id)));
+ insertQuery.addValueField(new ValueField("PROPERTY", new StringParameter(property.getKey())));
+ insertQuery.addValueField(new ValueField("VALUE", new StringParameter(property.getValue())));
+ database.insert(insertQuery);
+ }
+ properties.resetDirty();
+ }
+
+ /**
+ * Loads the properties for an artist.
+ *
+ * @param artist
+ * The artist to load the properties for
+ * @return The artist
+ * @throws DatabaseException
+ * if a database error occurs
+ */
+ public Artist loadProperties(final Artist artist) throws DatabaseException {
+ Query query = new Query(Type.SELECT, "ARTIST_PROPERTIES");
+ query.addField(new Field("ARTIST_PROPERTIES.PROPERTY"));
+ query.addField(new Field("ARTIST_PROPERTIES.VALUE"));
+ query.addWhereClause(new ValueFieldWhereClause(new ValueField("ARTIST", new StringParameter(artist.getId()))));
+ database.process(query, new ResultProcessor() {
+
+ @Override
+ public void processResult(ResultSet resultSet) throws SQLException {
+ if (resultSet.isFirst()) {
+ artist.getProperties().removeAll();
+ }
+ artist.getProperties().set(resultSet.getString("ARTIST_PROPERTIES.PROPERTY"), resultSet.getString("ARTIST_PROPERTIES.VALUE"));
+ }
+
+ });
+ return artist;
+ }
+
+ /**
+ * Loads the properties of all given artists.
+ *
+ * @param artists
+ * The artists to load the properties for
+ * @return The list of artists
+ * @throws DatabaseException
+ * if a database error occurs
+ */
+ public List<Artist> loadProperties(List<Artist> artists) throws DatabaseException {
+ for (Artist artist : artists) {
+ loadProperties(artist);
+ }
+ return artists;
}
/**
}
/**
+ * Returns the user with the given name.
+ *
+ * @param username
+ * The name of the user
+ * @return The user, or {@code null} if the user does not exist
+ * @throws DatabaseException
+ * if a database error occurs
+ */
+ public User getUserByName(String username) throws DatabaseException {
+ Query query = new Query(Type.SELECT, "USERS");
+ query.addField(new Field("USERS.*"));
+ query.addWhereClause(new ValueFieldWhereClause(new ValueField("USERS.NAME", new StringParameter(username))));
+ return database.getSingle(query, userCreator);
+ }
+
+ /**
* Returns the user connected with the given OpenID.
*
* @param openId