usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From snoopd...@apache.org
Subject [06/50] [abbrv] git commit: fix bug if you have other devices
Date Mon, 20 Oct 2014 13:46:34 GMT
fix bug if you have other devices


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/5b1c06ac
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/5b1c06ac
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/5b1c06ac

Branch: refs/heads/two-dot-o-events
Commit: 5b1c06ac88b3b46a93b399bcbfae53285364c978
Parents: d52ad4c
Author: Shawn Feldman <sfeldman@apache.org>
Authored: Thu Oct 16 09:13:31 2014 -0600
Committer: Shawn Feldman <sfeldman@apache.org>
Committed: Thu Oct 16 09:13:31 2014 -0600

----------------------------------------------------------------------
 .../services/devices/DevicesService.java        | 30 +++++++++++++-------
 .../services/users/devices/DevicesService.java  |  8 +++---
 .../apns/NotificationsServiceIT.java            |  5 +++-
 3 files changed, 27 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/5b1c06ac/stack/services/src/main/java/org/apache/usergrid/services/devices/DevicesService.java
----------------------------------------------------------------------
diff --git a/stack/services/src/main/java/org/apache/usergrid/services/devices/DevicesService.java
b/stack/services/src/main/java/org/apache/usergrid/services/devices/DevicesService.java
index d1bb3db..0911865 100644
--- a/stack/services/src/main/java/org/apache/usergrid/services/devices/DevicesService.java
+++ b/stack/services/src/main/java/org/apache/usergrid/services/devices/DevicesService.java
@@ -61,27 +61,35 @@ public class DevicesService extends AbstractCollectionService {
         return super.postItemById( context, id );
     }
 
-    protected void deleteEntityConnection(final EntityRef entityRef){
-        if(entityRef == null) {
+    protected void deleteEntityConnection(final EntityRef deviceRef, final EntityRef owner){
+        if(deviceRef == null) {
             return;
         }
         try {
-            Results entities = em.getCollection(entityRef,"users",null,100, Query.Level.REFS,false);
+            Results entities = em.getCollection(deviceRef,"users",null,100, Query.Level.REFS,false);
             Observable.from(entities.getEntities())
-                    .map(new Func1<Entity, Entity>() {
+                    .map(new Func1<Entity, Boolean>() {
                         @Override
-                        public Entity call(Entity user) {
+                        public Boolean call(Entity user) {
+                            boolean removed = false;
                             try {
-                                Results devicesResults = em.getCollection(user,"devices",null,100,Query.Level.REFS,false);
-                                List<Entity> devices = devicesResults.getEntities();
-                                for(EntityRef device : devices){
-                                    em.removeFromCollection(user, "devices",device);
+                                if(!user.getUuid().equals(owner.getUuid())) { //skip current
user
+                                    Results devicesResults = em.getCollection(user, "devices",
null, 100, Query.Level.REFS, false);
+                                    List<Entity> userDevices = devicesResults.getEntities();
+                                    for (EntityRef userDevice : userDevices) {
+                                        if(userDevice.getUuid().equals(deviceRef.getUuid()))
{ //only remove the current device from user
+                                            em.removeFromCollection(user, "devices", userDevice);
+                                        }
+                                    }
+                                    em.removeFromCollection(deviceRef, "users", user);
+                                    removed = true;
+                                }else{
+                                    removed = false;
                                 }
-                                em.removeFromCollection(entityRef, "users",user);
                             } catch (Exception e) {
                                 logger.error("Failed to delete connection " + user.toString(),
e);
                             }
-                            return user;
+                            return removed;
                         }
                     }).toBlocking().lastOrDefault(null);
         }catch (Exception e){

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/5b1c06ac/stack/services/src/main/java/org/apache/usergrid/services/users/devices/DevicesService.java
----------------------------------------------------------------------
diff --git a/stack/services/src/main/java/org/apache/usergrid/services/users/devices/DevicesService.java
b/stack/services/src/main/java/org/apache/usergrid/services/users/devices/DevicesService.java
index bdcd574..4556eaa 100644
--- a/stack/services/src/main/java/org/apache/usergrid/services/users/devices/DevicesService.java
+++ b/stack/services/src/main/java/org/apache/usergrid/services/users/devices/DevicesService.java
@@ -40,7 +40,7 @@ public class DevicesService extends org.apache.usergrid.services.devices.Devices
     @Override
     public ServiceResults putItemById( ServiceContext context, UUID id ) throws Exception
{
         logger.debug("Registering device {}", id);
-        unregisterDeviceToUsers(id);
+        unregisterDeviceToUsers(id,context.getOwner());
         ServiceResults results = super.putItemById( context, id );
         return results;
     }
@@ -49,15 +49,15 @@ public class DevicesService extends org.apache.usergrid.services.devices.Devices
     @Override
     public ServiceResults postItemById( ServiceContext context, UUID id ) throws Exception
{
         logger.info( "Attempting to connect an entity to device {}", id );
-        unregisterDeviceToUsers(id);
+        unregisterDeviceToUsers(id,context.getOwner());
         ServiceResults results = super.postItemById( context, id );
         return results;
     }
 
-    protected void unregisterDeviceToUsers(UUID deviceId){
+    protected void unregisterDeviceToUsers(UUID deviceId, EntityRef owner){
         try {
             EntityRef device = new SimpleEntityRef("device",deviceId);
-            deleteEntityConnection(device);
+            deleteEntityConnection(device,owner);
         } catch (Exception e) {
             logger.error("Failed to delete connection for " + deviceId.toString(), e);
         }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/5b1c06ac/stack/services/src/test/java/org/apache/usergrid/services/notifications/apns/NotificationsServiceIT.java
----------------------------------------------------------------------
diff --git a/stack/services/src/test/java/org/apache/usergrid/services/notifications/apns/NotificationsServiceIT.java
b/stack/services/src/test/java/org/apache/usergrid/services/notifications/apns/NotificationsServiceIT.java
index 8805b93..a834400 100644
--- a/stack/services/src/test/java/org/apache/usergrid/services/notifications/apns/NotificationsServiceIT.java
+++ b/stack/services/src/test/java/org/apache/usergrid/services/notifications/apns/NotificationsServiceIT.java
@@ -751,7 +751,10 @@ public class NotificationsServiceIT extends AbstractServiceNotificationIT
{
         assertEquals(device1Users.size(),1);
         List user1Devices = app.getEm().getCollection(user1,"devices",null,100, Query.Level.REFS,false).getEntities();
         assertEquals(user1Devices.size(),2);
-
+        app.clear();
+        e = app.testRequest(ServiceAction.POST, 1, "users",user1.getUuid(),"devices",device2.getUuid()).getEntity();
+        user1Devices = app.getEm().getCollection(user1,"devices",null,100, Query.Level.REFS,false).getEntities();
+        assertEquals(user1Devices.size(),2);
         // create push notification //
 
         app.getEm().refreshIndex();


Mime
View raw message