+ /**
+ * Returns all parties.
+ *
+ * @return All parties
+ * @throws DatabaseException
+ * if a database error occurs
+ */
+ public Collection<Party> getAllParties() throws DatabaseException {
+ Query query = new Query(Type.SELECT, "PARTIES");
+ query.addField(new Field("PARTIES.*"));
+ return loadPartyProperties(database.getMultiple(query, partyCreator));
+ }
+
+ /**
+ * Returns all parties that the track with the given ID was released at.
+ *
+ * @param trackId
+ * The ID of the track
+ * @return All parties the track was released at
+ * @throws DatabaseException
+ * if a database error occurs
+ */
+ public Collection<Party> getPartiesByTrackId(String trackId) throws DatabaseException {
+ Query query = new Query(Type.SELECT, "PARTIES");
+ query.addField(new Field("PARTIES.*"));
+ query.addJoin(new Join(JoinType.INNER, "PARTY_TRACKS", new Field("PARTY_TRACKS.PARTY"), new Field("PARTIES.ID")));
+ query.addWhereClause(new ValueFieldWhereClause(new ValueField("PARTY_TRACKS.TRACK", new StringParameter(trackId))));
+ return loadPartyProperties(database.getMultiple(query, partyCreator));
+ }
+
+ /**
+ * Returns the party with the given ID.
+ *
+ * @param partyId
+ * The ID of the party
+ * @return The party with the given ID
+ * @throws DatabaseException
+ * if a database error occurs
+ */
+ public Party getPartyById(String partyId) throws DatabaseException {
+ Query query = new Query(Type.SELECT, "PARTIES");
+ query.addField(new Field("PARTIES.*"));
+ query.addWhereClause(new ValueFieldWhereClause(new ValueField("PARTIES.ID", new StringParameter(partyId))));
+ return loadPartyProperties(database.getSingle(query, partyCreator));
+ }
+
+ /**
+ * Loads the properties of the given party.
+ *
+ * @param party
+ * The party to load the properties for
+ * @return The party with its properties loaded
+ * @throws DatabaseException
+ * if a database error occurs
+ */
+ public Party loadPartyProperties(Party party) throws DatabaseException {
+ return loadProperties(party, "PARTY_PROPERTIES", "PARTY");
+ }
+
+ /**
+ * Loads the properties of the given parties.
+ *
+ * @param parties
+ * The parties to load the properties for
+ * @return The parties with their properties loaded
+ * @throws DatabaseException
+ * if a database error occurs
+ */
+ public List<Party> loadPartyProperties(List<Party> parties) throws DatabaseException {
+ return loadProperties(parties, "PARTY_PROPERTIES", "PARTY");
+ }
+
+ /**
+ * Saves the given party.
+ *
+ * @param party
+ * The party to save
+ * @throws DatabaseException
+ * if a database error occurs
+ */
+ public void saveParty(Party party) throws DatabaseException {
+ Query query = new Query(Type.UPDATE, "PARTIES");
+ query.addValueField(new ValueField("NAME", new StringParameter(party.getName())));
+ query.addWhereClause(new ValueFieldWhereClause(new ValueField("ID", new StringParameter(party.getId()))));
+ database.update(query);
+ savePartyProperties(party);
+ }
+
+ /**
+ * Saves the properties of the given party.
+ *
+ * @param party
+ * The party whose properties to save
+ * @throws DatabaseException
+ * if a database error occurs
+ */
+ public void savePartyProperties(Party party) throws DatabaseException {
+ saveProperties(party.getProperties(), "PARTY_PROPERTIES", "PARTY", party.getId());
+ }
+
+ /**
+ * Creates a new party with the given name.
+ *
+ * @param name
+ * The name of the party
+ * @return The new party
+ * @throws DatabaseException
+ * if a database error occurs
+ */
+ public Party createParty(String name) throws DatabaseException {
+ Query query = new Query(Type.INSERT, "PARTIES");
+ 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 getPartyById(id);
+ }
+
+ /**
+ * 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
+ * The OpenID to find the user for
+ * @return The user connected with the given OpenID, or {@code null} if
+ * there is no such user
+ * @throws DatabaseException
+ * if a database error occurs
+ */
+ public User getUserByOpenId(String openId) throws DatabaseException {
+ Query query = new Query(Type.SELECT, "USERS");
+ query.addField(new Field("USERS.*"));
+ query.addJoin(new Join(JoinType.INNER, "USER_OPENIDS", new Field("USER_OPENIDS.USER"), new Field("USERS.ID")));
+ query.addWhereClause(new ValueFieldWhereClause(new ValueField("USER_OPENIDS.OPENID", new StringParameter(openId))));
+ return database.getSingle(query, userCreator);
+ }
+
+ /**
+ * Returns all OpenIDs connected with the user with the given ID.
+ *
+ * @param userId
+ * The ID of the user
+ * @return All OpenIDs connected with the given user
+ * @throws DatabaseException
+ * if a database error occurs
+ */
+ public Collection<String> getOpenIdsByUser(String userId) throws DatabaseException {
+ Query query = new Query(Type.SELECT, "USER_OPENIDS");
+ query.addField(new Field("USER_OPENIDS.*"));
+ query.addWhereClause(new ValueFieldWhereClause(new ValueField("USER_OPENIDS.USER", new StringParameter(userId))));
+ return database.getMultiple(query, new StringCreator("USER_OPENIDS.OPENID"));
+ }
+
+ //
+ // PRIVATE METHODS
+ //
+
+ /**
+ * Loads the properties for the given object.
+ *
+ * @param <T>
+ * The type of the object
+ * @param object
+ * The object
+ * @param table
+ * The table to load the properties from
+ * @param type
+ * The type of the object (“ARTIST,” “TRACK,” etc.)
+ * @return The object with its properties loaded
+ * @throws DatabaseException
+ * if a database error occurs
+ */
+ private <T extends Base> T loadProperties(final T object, final String table, String type) throws DatabaseException {
+ if (object == null) {
+ return null;
+ }
+ Query query = new Query(Type.SELECT, table);
+ query.addField(new Field(table + ".PROPERTY"));
+ query.addField(new Field(table + ".VALUE"));
+ query.addWhereClause(new ValueFieldWhereClause(new ValueField(type, new StringParameter(object.getId()))));
+ database.process(query, new ResultProcessor() {
+
+ @Override
+ public void processResult(ResultSet resultSet) throws SQLException {
+ if (resultSet.isFirst()) {
+ object.getProperties().removeAll();
+ }
+ object.getProperties().set(resultSet.getString(table + ".PROPERTY"), resultSet.getString(table + ".VALUE"));
+ }
+
+ });
+ return object;
+ }
+
+ /**
+ * Loads the properties for the given objects.
+ *
+ * @param <T>
+ * The type of the object
+ * @param objects
+ * The objects
+ * @param table
+ * The table to load the properties from
+ * @param type
+ * The type of the object (“ARTIST,” “TRACK,” etc.)
+ * @return The object with its properties loaded
+ * @throws DatabaseException
+ * if a database error occurs
+ */
+ private <T extends Base> List<T> loadProperties(List<T> objects, final String table, final String type) throws DatabaseException {
+ final Map<String, T> objectMap = new HashMap<String, T>();
+ final Set<String> firstObjects = new HashSet<String>();
+ for (T object : objects) {
+ objectMap.put(object.getId(), object);
+ firstObjects.add(object.getId());
+ }
+ Query query = new Query(Type.SELECT, table);
+ query.addField(new Field(table + "." + type));
+ query.addField(new Field(table + ".PROPERTY"));
+ query.addField(new Field(table + ".VALUE"));
+ OrWhereClause whereClause = new OrWhereClause();
+ for (T object : objects) {
+ whereClause.add(new ValueFieldWhereClause(new ValueField(type, new StringParameter(object.getId()))));
+ }
+ query.addWhereClause(whereClause);
+ database.process(query, new ResultProcessor() {
+
+ @Override
+ public void processResult(ResultSet resultSet) throws SQLException {
+ String id = resultSet.getString(table + "." + type);
+ T object = objectMap.get(id);
+ if (firstObjects.remove(id)) {
+ object.getProperties().removeAll();
+ }
+ object.getProperties().set(resultSet.getString(table + ".PROPERTY"), resultSet.getString(table + ".VALUE"));
+ }
+
+ });
+ return objects;
+ }
+
+ /**
+ * {@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>
+ */