Add accessor for profile fields.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 10 Jan 2011 11:10:39 +0000 (12:10 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 10 Jan 2011 11:10:39 +0000 (12:10 +0100)
src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/web/WebInterface.java

diff --git a/src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java b/src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java
new file mode 100644 (file)
index 0000000..52f39dd
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Sone - ProfileAccessor.java - Copyright © 2011 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.sone.template;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import net.pterodactylus.sone.data.Profile;
+import net.pterodactylus.util.template.Accessor;
+import net.pterodactylus.util.template.DataProvider;
+import net.pterodactylus.util.template.ReflectionAccessor;
+
+/**
+ * {@link Accessor} for {@link Profile}s, especially for the profile fields.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ProfileAccessor extends ReflectionAccessor {
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public Object get(DataProvider dataProvider, Object object, String member) {
+               Profile profile = (Profile) object;
+               if ("fields".equals(member)) {
+                       Map<String, String> fields = new LinkedHashMap<String, String>();
+                       for (String field : profile.getFields()) {
+                               fields.put(field, profile.getField(field));
+                       }
+                       return fields;
+               }
+               return super.get(dataProvider, object, member);
+       }
+
+}
index 6085ff8..e18521c 100644 (file)
@@ -36,6 +36,7 @@ import java.util.logging.Logger;
 import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.core.CoreListener;
 import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.freenet.L10nFilter;
@@ -48,6 +49,7 @@ import net.pterodactylus.sone.template.GetPagePlugin;
 import net.pterodactylus.sone.template.IdentityAccessor;
 import net.pterodactylus.sone.template.NotificationManagerAccessor;
 import net.pterodactylus.sone.template.PostAccessor;
+import net.pterodactylus.sone.template.ProfileAccessor;
 import net.pterodactylus.sone.template.ReplyAccessor;
 import net.pterodactylus.sone.template.RequestChangeFilter;
 import net.pterodactylus.sone.template.SoneAccessor;
@@ -159,6 +161,7 @@ public class WebInterface implements CoreListener {
                templateFactory.addAccessor(Object.class, new ReflectionAccessor());
                templateFactory.addAccessor(Collection.class, new CollectionAccessor());
                templateFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
+               templateFactory.addAccessor(Profile.class, new ProfileAccessor());
                templateFactory.addAccessor(Post.class, new PostAccessor(getCore(), templateFactory));
                templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore(), templateFactory));
                templateFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));