Merge branch 'master' into packaging
[pithos-web-client] / src / gr / grnet / pithos / web / client / commands / RemoveUserCommand.java
index 4c925dd..7bc14fa 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2011 GRNET S.A. All rights reserved.
+ * Copyright 2011-2012 GRNET S.A. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or
  * without modification, are permitted provided that the following
@@ -43,8 +43,9 @@ import gr.grnet.pithos.web.client.rest.RestException;
 
 import com.google.gwt.core.client.GWT;
 import com.google.gwt.core.client.Scheduler;
+import com.google.gwt.http.client.Response;
+import com.google.gwt.http.client.URL;
 import com.google.gwt.user.client.Command;
-import com.google.gwt.user.client.Window;
 import com.google.gwt.user.client.ui.PopupPanel;
 
 /**
@@ -71,31 +72,55 @@ public class RemoveUserCommand implements Command {
        public void execute() {
         if (containerPanel != null)
                    containerPanel.hide();
-       final Group group = user.getGroup();
+       final String groupName = user.getGroup();
+       final Group group = app.getAccount().getGroup(groupName);
+       if (group == null)
+               return;
        group.removeMember(user.getName());
        String path = "?update=";
        PostRequest updateGroup = new PostRequest(app.getApiPath(), app.getUsername(), path) {
                        
                        @Override
                        public void onSuccess(Resource result) {
-                               app.updateGroupNode(group);
+                               app.fetchAccount(new Command() {
+                                       
+                                       @Override
+                                       public void execute() {
+                                               Group updatedGroup2 = app.getAccount().getGroup(groupName);
+                                               if (updatedGroup2 != null)
+                                                       app.updateGroupNode(updatedGroup2);
+                                               else {
+                                                       app.updateGroupNode(null);
+                                               }
+                                       }
+                               });
                        }
                        
                        @Override
                        public void onError(Throwable t) {
                                GWT.log("", t);
+                               app.setError(t);
                                if (t instanceof RestException) {
                                        app.displayError("Unable to update group:" + ((RestException) t).getHttpStatusText());
                                }
                                else
                                        app.displayError("System error updating group:" + t.getMessage());
                        }
+
+                       @Override
+                       protected void onUnauthorized(Response response) {
+                               app.sessionExpired();
+                       }
                };
                updateGroup.setHeader("X-Auth-Token", app.getToken());
                String groupMembers = "";
-               for (String u : group.getMembers())
-                       groupMembers += (u + ",");
-               updateGroup.setHeader("X-Account-Group-" + group.getName(), groupMembers);
+               if (!group.getMembers().isEmpty()) {
+                       for (String u : group.getMembers())
+                               groupMembers += (URL.encodePathSegment(u) + ",");
+               }
+               else
+                       groupMembers = "~";
+               updateGroup.setHeader("X-Account-Group-" + URL.encodePathSegment(group.getName()), groupMembers);
                Scheduler.get().scheduleDeferred(updateGroup);
        }
 }