Add login page.
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / data / DataManager.java
index 6bec2a8..60b8318 100644 (file)
@@ -34,6 +34,7 @@ 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.ObjectCreator.StringCreator;
 import net.pterodactylus.util.database.OrderField;
 import net.pterodactylus.util.database.Parameter.StringParameter;
 import net.pterodactylus.util.database.Query;
@@ -65,6 +66,10 @@ public class DataManager {
        @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;
 
@@ -262,6 +267,56 @@ public class DataManager {
        }
 
        /**
+        * 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"));
+       }
+
+       /**
         * {@link Artist} implementation that retrieves some attributes (such as
         * {@link #getGroups()}, and {@link #getTracks()}) from the
         * {@link DataManager} on demand.
@@ -561,4 +616,63 @@ public class DataManager {
 
        }
 
+       /**
+        * {@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")).setPasswordHash(resultSet.getString("USERS.PASSWORD")).setLevel(resultSet.getInt("USERS.LEVEL"));
+               }
+
+       }
+
 }