Add user data container.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 26 Apr 2012 10:17:25 +0000 (12:17 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 26 Apr 2012 10:17:25 +0000 (12:17 +0200)
src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java
src/main/java/net/pterodactylus/demoscenemusic/data/DefaultUser.java [new file with mode: 0644]
src/main/java/net/pterodactylus/demoscenemusic/data/User.java [new file with mode: 0644]

index 6bec2a8..e6bc6da 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,40 @@ public class DataManager {
        }
 
        /**
+        * 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 +600,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"));
+               }
+
+       }
+
 }
diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultUser.java b/src/main/java/net/pterodactylus/demoscenemusic/data/DefaultUser.java
new file mode 100644 (file)
index 0000000..74a65a1
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * 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;
+       }
+
+}
diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/User.java b/src/main/java/net/pterodactylus/demoscenemusic/data/User.java
new file mode 100644 (file)
index 0000000..cfb32a7
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * 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);
+
+}