import net.pterodactylus.util.database.Join;
import net.pterodactylus.util.database.Join.JoinType;
import net.pterodactylus.util.database.ObjectCreator;
+import net.pterodactylus.util.database.ObjectCreator.StringCreator;
import net.pterodactylus.util.database.OrderField;
import net.pterodactylus.util.database.Parameter.StringParameter;
import net.pterodactylus.util.database.Query;
@SuppressWarnings("synthetic-access")
private final ObjectCreator<Style> styleCreator = new StyleCreator();
+ /** The {@link User} object creator. */
+ @SuppressWarnings("synthetic-access")
+ private final ObjectCreator<User> userCreator = new UserCreator();
+
/** The database. */
private final Database database;
}
/**
+ * 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"));
+ }
+
+ /**
* {@link Artist} implementation that retrieves some attributes (such as
* {@link #getGroups()}, and {@link #getTracks()}) from the
* {@link DataManager} on demand.
}
+ /**
+ * {@link User} implementation that retrieves some attributes (such as
+ * {@link #getOpenIds()}) from the {@link DataManager} on demand.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+ private class LazyUser extends DefaultUser {
+
+ /** Memoizer for a user’s OpenIDs. */
+ private final Memoizer<Void> openIdMemoizer = new Memoizer<Void>(new Callable<Void>() {
+
+ @Override
+ public Void call() throws Exception {
+ if (!hasValue("openIds")) {
+ getValue("openIds", Collection.class).set(getOpenIdsByUser(getId()));
+ }
+ return null;
+ }
+ });
+
+ /**
+ * Creates a new user.
+ *
+ * @param id
+ * The ID of the user
+ */
+ public LazyUser(String id) {
+ super(id);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Collection<String> getOpenIds() {
+ openIdMemoizer.get();
+ return super.getOpenIds();
+ }
+
+ }
+
+ /**
+ * {@link ObjectCreator} implementation that can create {@link User}
+ * objects. This specific implementation creates {@link LazyUser} instances.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+ private class UserCreator implements ObjectCreator<User> {
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public User createObject(ResultSet resultSet) throws SQLException {
+ return new LazyUser(resultSet.getString("USERS.ID")).setName(resultSet.getString("USERS.NAME"));
+ }
+
+ }
+
}
--- /dev/null
+/*
+ * DemosceneMusic - DefaultUser.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.util.Collection;
+
+/**
+ * Default implementation of a user data container.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class DefaultUser extends DefaultBase implements User {
+
+ /**
+ * Creates a new user data container.
+ *
+ * @param id
+ * The ID of the user
+ */
+ public DefaultUser(String id) {
+ super(id);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String getName() {
+ return getValue("name", String.class).get();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public User setName(String name) {
+ getValue("name", String.class).set(name);
+ return this;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ @SuppressWarnings("unchecked")
+ public Collection<String> getOpenIds() {
+ return getValue("openIds", Collection.class).get();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public User setOpenIds(Collection<String> openIds) {
+ getValue("openIds", Collection.class).set(openIds);
+ return this;
+ }
+
+}
--- /dev/null
+/*
+ * DemosceneMusic - User.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.util.Collection;
+
+/**
+ * Data container for users of the website.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public interface User extends Base {
+
+ /**
+ * Returns the name of this user.
+ *
+ * @return The name of this user
+ */
+ public String getName();
+
+ /**
+ * Sets the name of this user.
+ *
+ * @param name
+ * The name of this user
+ * @return This user
+ */
+ public User setName(String name);
+
+ /**
+ * Returns all OpenIDs connected with this user.
+ *
+ * @return All OpenIDs connected with this user
+ */
+ public Collection<String> getOpenIds();
+
+ /**
+ * Sets all OpenIDs connected with this user.
+ *
+ * @param openIds
+ * All OpenIDs connected with this user
+ * @return This user
+ */
+ public User setOpenIds(Collection<String> openIds);
+
+}