From 0e69f12c0910ab44057cbd72c93428ed30058099 Mon Sep 17 00:00:00 2001
From: yusuke.kokubo <yusuke.kokubo@gmail.com>
Date: Fri, 13 May 2011 10:20:41 +0900
Subject: [PATCH] =?UTF-8?q?follow/follower/friends=E9=96=A2=E4=BF=82=E3=82=92=E4=BD=9C=E6=88=90=20refs=20#733?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../skillmaps/client/service/AccountService.java   |    7 +
 .../client/service/AccountServiceAsync.java        |    7 +
 .../controller/sys/CreateFollowingController.java  |   36 ++++++
 .../server/service/AccountServiceImpl.java         |   39 ++++++
 .../appspot/skillmaps/shared/model/Following.java  |  133 ++++++++++++++++++++
 .../skillmaps/shared/model/FollowingTest.java      |   16 +++
 6 files changed, 238 insertions(+), 0 deletions(-)
 create mode 100644 src/com/appspot/skillmaps/server/controller/sys/CreateFollowingController.java
 create mode 100644 src/com/appspot/skillmaps/shared/model/Following.java
 create mode 100644 test/com/appspot/skillmaps/shared/model/FollowingTest.java

diff --git a/src/com/appspot/skillmaps/client/service/AccountService.java b/src/com/appspot/skillmaps/client/service/AccountService.java
index a4d364e..2e92fb4 100644
--- a/src/com/appspot/skillmaps/client/service/AccountService.java
+++ b/src/com/appspot/skillmaps/client/service/AccountService.java
@@ -3,6 +3,7 @@ package com.appspot.skillmaps.client.service;
 import com.appspot.skillmaps.shared.dto.UserListResultDto;
 import com.appspot.skillmaps.shared.model.Login;
 import com.appspot.skillmaps.shared.model.Profile;
+import com.google.appengine.api.datastore.Key;
 import com.google.gwt.user.client.rpc.RemoteService;
 import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
 
@@ -30,4 +31,10 @@ public interface AccountService extends RemoteService {
 
     String getSignUrl(String backUrl);
 
+    Profile[] getFollowing(Key key);
+
+    Profile[] getFollower(Key key);
+
+    Profile[] getFriends(Key key);
+
 }
diff --git a/src/com/appspot/skillmaps/client/service/AccountServiceAsync.java b/src/com/appspot/skillmaps/client/service/AccountServiceAsync.java
index 7fbeb96..add2e75 100644
--- a/src/com/appspot/skillmaps/client/service/AccountServiceAsync.java
+++ b/src/com/appspot/skillmaps/client/service/AccountServiceAsync.java
@@ -3,6 +3,7 @@ package com.appspot.skillmaps.client.service;
 import com.appspot.skillmaps.shared.dto.UserListResultDto;
 import com.appspot.skillmaps.shared.model.Login;
 import com.appspot.skillmaps.shared.model.Profile;
+import com.google.appengine.api.datastore.Key;
 import com.google.gwt.user.client.rpc.AsyncCallback;
 
 public interface AccountServiceAsync {
@@ -26,4 +27,10 @@ public interface AccountServiceAsync {
 
     void getSignUrl(String backUrl, AsyncCallback<String> callback);
 
+    void getFollowing(Key key, AsyncCallback<Profile[]> callback);
+
+    void getFollower(Key key, AsyncCallback<Profile[]> callback);
+
+    void getFriends(Key key, AsyncCallback<Profile[]> callback);
+
 }
diff --git a/src/com/appspot/skillmaps/server/controller/sys/CreateFollowingController.java b/src/com/appspot/skillmaps/server/controller/sys/CreateFollowingController.java
new file mode 100644
index 0000000..fae3f2a
--- /dev/null
+++ b/src/com/appspot/skillmaps/server/controller/sys/CreateFollowingController.java
@@ -0,0 +1,36 @@
+package com.appspot.skillmaps.server.controller.sys;
+
+import java.util.List;
+
+import org.slim3.controller.Controller;
+import org.slim3.controller.Navigation;
+import org.slim3.datastore.Datastore;
+
+import com.appspot.skillmaps.server.meta.ProfileMeta;
+import com.appspot.skillmaps.server.meta.SkillRelationMeta;
+import com.appspot.skillmaps.shared.model.Following;
+import com.appspot.skillmaps.shared.model.Profile;
+import com.appspot.skillmaps.shared.model.SkillRelation;
+
+public class CreateFollowingController extends Controller {
+
+    @Override
+    public Navigation run() throws Exception {
+        SkillRelationMeta m = SkillRelationMeta.get();
+        ProfileMeta pm = ProfileMeta.get();
+        
+        List<SkillRelation> rels = Datastore.get(m);
+        
+        for (SkillRelation rel : rels) {
+            Profile follower = Datastore.query(pm).limit(1).asSingle();
+            Profile following = rel.getSkill().getModel().getProfile();
+            
+            Following f = new Following();
+            f.setFollowing(following.getKey());
+            f.setFollower(follower.getKey());
+            
+            Datastore.put(f);
+        }
+        return null;
+    }
+}
diff --git a/src/com/appspot/skillmaps/server/service/AccountServiceImpl.java b/src/com/appspot/skillmaps/server/service/AccountServiceImpl.java
index 21787ce..23bba1c 100644
--- a/src/com/appspot/skillmaps/server/service/AccountServiceImpl.java
+++ b/src/com/appspot/skillmaps/server/service/AccountServiceImpl.java
@@ -1,5 +1,6 @@
 package com.appspot.skillmaps.server.service;
 
+import java.util.ArrayList;
 import java.util.List;
 
 import org.slim3.datastore.Datastore;
@@ -9,12 +10,15 @@ import org.slim3.memcache.Memcache;
 import org.slim3.util.StringUtil;
 
 import com.appspot.skillmaps.client.service.AccountService;
+import com.appspot.skillmaps.server.meta.FollowingMeta;
 import com.appspot.skillmaps.server.meta.IconMeta;
 import com.appspot.skillmaps.server.meta.ProfileMeta;
 import com.appspot.skillmaps.shared.dto.UserListResultDto;
+import com.appspot.skillmaps.shared.model.Following;
 import com.appspot.skillmaps.shared.model.Icon;
 import com.appspot.skillmaps.shared.model.Login;
 import com.appspot.skillmaps.shared.model.Profile;
+import com.google.appengine.api.datastore.Key;
 import com.google.appengine.api.datastore.KeyFactory;
 import com.google.appengine.api.users.User;
 import com.google.appengine.api.users.UserService;
@@ -24,6 +28,7 @@ import com.google.common.collect.Lists;
 
 public class AccountServiceImpl implements AccountService {
     private static final int USER_LISE_SIZE = 80;
+    FollowingMeta fm = FollowingMeta.get();
     ProfileMeta pm = ProfileMeta.get();
 
     @Override
@@ -177,6 +182,40 @@ public class AccountServiceImpl implements AccountService {
         UserService us = UserServiceFactory.getUserService();
         return us.createLoginURL(backUrl);
     }
+
+    @Override
+    public Profile[] getFollowing(Key key) {
+        List<Following> following = Datastore.query(fm).filter(fm.following.equal(key)).asList();
+        List<Key> keys = new ArrayList<Key>();
+        for (Following f : following) {
+            keys.add(f.getFollower());
+        }
+        return Datastore.get(pm, keys).toArray(new Profile[0]);
+    }
+    
+    @Override
+    public Profile[] getFollower(Key key) {
+        List<Following> follower = Datastore.query(fm).filter(fm.follower.equal(key)).asList();
+        List<Key> keys = new ArrayList<Key>();
+        for (Following f : follower) {
+            keys.add(f.getFollowing());
+        }
+        return Datastore.get(pm, keys).toArray(new Profile[0]);
+    }
+    
+    @Override
+    public Profile[] getFriends(Key key) {
+        List<Following> following = Datastore.query(fm).filter(fm.following.equal(key)).asList();
+        List<Following> follower = Datastore.query(fm).filter(fm.follower.equal(key)).asList();
+        List<Key> keys = new ArrayList<Key>();
+        for (Following f : follower) {
+            if (following.contains(f)){
+                keys.add(f.getFollowing());
+            }
+        }
+        return Datastore.get(pm, keys).toArray(new Profile[0]);
+    }
+
     
     private UserListResultDto createUserListResultDto(
             S3QueryResultList<Profile> result) {
diff --git a/src/com/appspot/skillmaps/shared/model/Following.java b/src/com/appspot/skillmaps/shared/model/Following.java
new file mode 100644
index 0000000..d082ddc
--- /dev/null
+++ b/src/com/appspot/skillmaps/shared/model/Following.java
@@ -0,0 +1,133 @@
+package com.appspot.skillmaps.shared.model;
+
+import java.io.Serializable;
+import java.util.Date;
+
+import com.google.appengine.api.datastore.Key;
+
+import org.slim3.datastore.Attribute;
+import org.slim3.datastore.CreationDate;
+import org.slim3.datastore.Model;
+import org.slim3.datastore.ModificationDate;
+
+@Model(schemaVersion = 1)
+public class Following implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @Attribute(primaryKey = true)
+    private Key key;
+
+    @Attribute(version = true)
+    private Long version;
+    
+    private Key following;
+    
+    private Key follower;
+
+    @Attribute(listener=CreationDate.class)
+    private Date createdAt;
+
+    @Attribute(listener=ModificationDate.class)
+    private Date updatedAt;
+
+    /**
+     * Returns the key.
+     *
+     * @return the key
+     */
+    public Key getKey() {
+        return key;
+    }
+
+    /**
+     * Sets the key.
+     *
+     * @param key
+     *            the key
+     */
+    public void setKey(Key key) {
+        this.key = key;
+    }
+
+    /**
+     * Returns the version.
+     *
+     * @return the version
+     */
+    public Long getVersion() {
+        return version;
+    }
+
+    /**
+     * Sets the version.
+     *
+     * @param version
+     *            the version
+     */
+    public void setVersion(Long version) {
+        this.version = version;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((key == null) ? 0 : key.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        Following other = (Following) obj;
+        if (key == null) {
+            if (other.key != null) {
+                return false;
+            }
+        } else if (!key.equals(other.key)) {
+            return false;
+        }
+        return true;
+    }
+
+    public void setCreatedAt(Date createdAt) {
+        this.createdAt = createdAt;
+    }
+
+    public Date getCreatedAt() {
+        return createdAt;
+    }
+
+    public void setUpdatedAt(Date updatedAt) {
+        this.updatedAt = updatedAt;
+    }
+
+    public Date getUpdatedAt() {
+        return updatedAt;
+    }
+
+    public void setFollowing(Key following) {
+        this.following = following;
+    }
+
+    public Key getFollowing() {
+        return following;
+    }
+
+    public void setFollower(Key follower) {
+        this.follower = follower;
+    }
+
+    public Key getFollower() {
+        return follower;
+    }
+}
diff --git a/test/com/appspot/skillmaps/shared/model/FollowingTest.java b/test/com/appspot/skillmaps/shared/model/FollowingTest.java
new file mode 100644
index 0000000..0d51643
--- /dev/null
+++ b/test/com/appspot/skillmaps/shared/model/FollowingTest.java
@@ -0,0 +1,16 @@
+package com.appspot.skillmaps.shared.model;
+
+import org.slim3.tester.AppEngineTestCase;
+import org.junit.Test;
+import static org.junit.Assert.*;
+import static org.hamcrest.CoreMatchers.*;
+
+public class FollowingTest extends AppEngineTestCase {
+
+    private Following model = new Following();
+
+    @Test
+    public void test() throws Exception {
+        assertThat(model, is(notNullValue()));
+    }
+}
-- 
1.6.5.1.1367.gcd48

