update trash node on empty
[pithos] / src / gr / ebs / gss / client / PermissionsList.java
index 5d32ff1..06c548f 100644 (file)
 package gr.ebs.gss.client;
 
 import gr.ebs.gss.client.FilePropertiesDialog.Images;
+import gr.ebs.gss.client.rest.GetCommand;
 import gr.ebs.gss.client.rest.resource.PermissionHolder;
+import gr.ebs.gss.client.rest.resource.UserResource;
+import gr.ebs.gss.client.rest.resource.UserSearchResource;
 
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.Set;
 
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.event.dom.client.ClickHandler;
+import com.google.gwt.user.client.DeferredCommand;
+import com.google.gwt.user.client.ui.AbstractImagePrototype;
 import com.google.gwt.user.client.ui.CheckBox;
-import com.google.gwt.user.client.ui.ClickListener;
 import com.google.gwt.user.client.ui.Composite;
 import com.google.gwt.user.client.ui.FlexTable;
 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
 import com.google.gwt.user.client.ui.PushButton;
 import com.google.gwt.user.client.ui.VerticalPanel;
-import com.google.gwt.user.client.ui.Widget;
 
 
 /**
@@ -41,21 +48,30 @@ import com.google.gwt.user.client.ui.Widget;
 public class PermissionsList extends Composite {
 
        int selectedRow = -1;
-       int permissionCount=-1;
+       
+       int permissionCount = -1;
+       
        Set<PermissionHolder> permissions = null;
+       
        final Images images;
+       
        final VerticalPanel permPanel = new VerticalPanel();
+       
        final FlexTable permTable = new FlexTable();
+       
        final String owner;
+       
        PermissionHolder toRemove = null;
+       
        private boolean hasChanges = false;
+       
        private boolean hasAddition = false;
-
-       public PermissionsList(final Images images, Set<PermissionHolder> permissions, String owner){
-               this.images = images;
-               this.owner = owner;
-               this.permissions =  new HashSet<PermissionHolder>();
-               this.permissions.addAll(permissions);
+       
+       public PermissionsList(final Images theImages, Set<PermissionHolder> thePermissions, String anOwner){
+               images = theImages;
+               owner = anOwner;
+               permissions =  new HashSet<PermissionHolder>();
+               permissions.addAll(thePermissions);
                permTable.setText(0, 0, "Users/Groups");
                permTable.setText(0, 1, "Read");
                permTable.setText(0, 2, "Write");
@@ -66,7 +82,7 @@ public class PermissionsList extends Composite {
                permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
                permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
                permPanel.add(permTable);
-               permPanel.addStyleName("gwt-TabPanelBottom");
+               permPanel.addStyleName("gss-TabPanelBottom");
                initWidget(permPanel);
                updateTable();
        }
@@ -77,42 +93,125 @@ public class PermissionsList extends Composite {
 
 
        public void updateTable(){
+               copySetAndContinue(permissions);
+       }
+
+       public void updatePermissionsAccordingToInput(){
                int i=1;
+               for(PermissionHolder dto : permissions){
+                       /*if(dto.getId() == null)
+                               hasChanges =true;*/
+                       CheckBox r = (CheckBox) permTable.getWidget(i, 1);
+                       CheckBox w = (CheckBox) permTable.getWidget(i, 2);
+                       CheckBox m = (CheckBox) permTable.getWidget(i, 3);
+                       if(dto.isRead() != r.getValue() || dto.isWrite() != w.getValue() || dto.isModifyACL() != m.getValue())
+                               hasChanges = true;
+                       dto.setRead(r.getValue());
+                       dto.setWrite(w.getValue());
+                       dto.setModifyACL(m.getValue());
+                       i++;
+               }               
+       }
+
+       /**
+        * Retrieve the permissions.
+        *
+        * @return the permissions
+        */
+       public Set<PermissionHolder> getPermissions() {
+               return permissions;
+       }
+
+       public void addPermission(PermissionHolder permission){
+               permissions.add(permission);
+               hasAddition = true;
+       }
+       /**
+        * Copies the input Set to a new Set
+        * @param input
+        */
+       private void copySetAndContinue(Set<PermissionHolder> input){
+               Set<PermissionHolder> copiedInput = new HashSet<PermissionHolder>();            
+               for(PermissionHolder dto : input) {
+                       copiedInput.add(dto);
+               }
+               handleFullNames(copiedInput);
+       }
+       
+       /**
+        * Examines whether or not the user's full name exists in the 
+        * userFullNameMap in the GSS.java for every element of the input list.
+        * If the user's full name does not exist in the map then a request is being made
+        * for the specific username.  
+        * 
+        * @param filesInput
+        */
+       private void handleFullNames(Set<PermissionHolder> aPermissions){               
+               if(aPermissions.isEmpty()){
+                       showPermissionTable();
+                       return;
+               }
+               
+               final PermissionHolder dto = aPermissions.iterator().next();
+               if(dto.getGroup() != null){
+                       if(aPermissions.size() >= 1){
+                               aPermissions.remove(dto);                               
+                               handleFullNames(aPermissions);                          
+                       }
+               }else if(GSS.get().findUserFullName(dto.getUser()) != null){
+                       if(aPermissions.size() >= 1){
+                               aPermissions.remove(dto);                               
+                               handleFullNames(aPermissions);                          
+                       }
+               }else{
+                       findFullNameAndUpdate(aPermissions);
+               }
+       }
+       
+       /**
+        * Shows the permission table 
+        * 
+        * @param aPermissions
+        */
+       private void showPermissionTable(){
+               int i = 1;
                if(toRemove != null){
                        permissions.remove(toRemove);
                        toRemove = null;
                }
                for(final PermissionHolder dto : permissions){
-
-                       PushButton removeButton = new PushButton(images.delete().createImage(), new ClickListener() {
-
-                               public void onClick(Widget sender) {
+                       PushButton removeButton = new PushButton(AbstractImagePrototype.create(images.delete()).createImage(), new ClickHandler() {
+                               @Override
+                               public void onClick(ClickEvent event) {
                                        toRemove = dto;
                                        updateTable();
                                        hasChanges = true;
                                }
                        });
-
-                       if(dto.getUser() !=null)
-                               if(dto.getUser()!=null && dto.getUser().equals(owner)){
-                                       permTable.setHTML(i, 0, "<span>" + images.permUser().getHTML() + "&nbsp;Owner</span>");
+                                               
+                       if(dto.getUser() != null){
+                               if(dto.getUser() != null && dto.getUser().equals(owner)){
+                                       permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;Owner</span>");
                                        removeButton.setVisible(false);
+                               }else{
+                                       permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;"+ GSS.get().findUserFullName(dto.getUser()) + "</span>");
                                }
-                               else
-                                       permTable.setHTML(i, 0, "<span>" + images.permUser().getHTML() + "&nbsp;"+dto.getUser()+"</span>");
-                       else if(dto.getGroup() != null)
-                               permTable.setHTML(i, 0, "<span>" + images.permGroup().getHTML() + "&nbsp;"+dto.getGroup()+"</span>");
+                       }else if(dto.getGroup() != null){
+                               permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permGroup()).getHTML() + "&nbsp;"+ dto.getGroup() + "</span>");
+                       }
+                       
                        CheckBox read = new CheckBox();
-                       read.setChecked(dto.isRead());
+                       read.setValue(dto.isRead());
                        CheckBox write = new CheckBox();
-                       write.setChecked(dto.isWrite());
+                       write.setValue(dto.isWrite());
                        CheckBox modify = new CheckBox();
-                       modify.setChecked(dto.isModifyACL());
+                       modify.setValue(dto.isModifyACL());
                        if (dto.getUser()!=null && dto.getUser().equals(owner)) {
                                read.setEnabled(false);
                                write.setEnabled(false);
                                modify.setEnabled(false);
                        }
+                       
                        permTable.setWidget(i, 1, read);
                        permTable.setWidget(i, 2, write);
                        permTable.setWidget(i, 3, modify);
@@ -121,45 +220,57 @@ public class PermissionsList extends Composite {
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
-                       i++;
+                       i++;            
                }
                for(; i<permTable.getRowCount(); i++)
                        permTable.removeRow(i);
                hasChanges = false;
-
-       }
-
-       public void updatePermissionsAccordingToInput(){
-               int i=1;
-               for(PermissionHolder dto : permissions){
-                       /*if(dto.getId() == null)
-                               hasChanges =true;*/
-                       CheckBox r = (CheckBox) permTable.getWidget(i, 1);
-                       CheckBox w = (CheckBox) permTable.getWidget(i, 2);
-                       CheckBox m = (CheckBox) permTable.getWidget(i, 3);
-                       if(dto.isRead() != r.isChecked() || dto.isWrite() != w.isChecked() || dto.isModifyACL() != m.isChecked())
-                               hasChanges = true;
-                       dto.setRead(r.isChecked());
-                       dto.setWrite(w.isChecked());
-                       dto.setModifyACL(m.isChecked());
-                       i++;
-               }
        }
-
-
+       
        /**
-        * Retrieve the permissions.
-        *
-        * @return the permissions
+        * Makes a request to search for full name from a given username
+        * and continues checking the next element of the Set.
+        *  
+        * @param filesInput
         */
-       public Set<PermissionHolder> getPermissions() {
-               return permissions;
-       }
 
-       public void addPermission(PermissionHolder permission){
-               permissions.add(permission);
-               hasAddition = true;
-       }
+       private void findFullNameAndUpdate(final Set<PermissionHolder> aPermissions){                           
+               final PermissionHolder dto = aPermissions.iterator().next();
+               String path = GSS.get().getApiPath() + "users/" + dto.getUser(); 
 
+               GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(UserSearchResource.class, path, false,null) {
+                       @Override
+                       public void onComplete() {
+                               final UserSearchResource result = getResult();
+                               for (UserResource user : result.getUsers()){
+                                       String username = user.getUsername();
+                                       String userFullName = user.getName();
+                                       GSS.get().putUserToMap(username, userFullName);
+                                       if(aPermissions.size() >= 1){
+                                               aPermissions.remove(dto);                                               
+                                               if(aPermissions.isEmpty()){
+                                                       showPermissionTable();
+                                                       return;
+                                               }
+                                               handleFullNames(aPermissions);                                                                          
+                                       }                                                                       
+                               }
+                       }
+                       @Override
+                       public void onError(Throwable t) {                              
+                               GSS.get().displayError("Unable to fetch user's full name from the given username " + dto.getUser());
+                               if(aPermissions.size() >= 1){
+                                       aPermissions.remove(dto);
+                                       if(aPermissions.isEmpty()){
+                                               showPermissionTable();
+                                               return;
+                                       }
+                                       handleFullNames(aPermissions);
+                               }
+                       }
+               };
+               DeferredCommand.addCommand(gg);
+       
+       }
 
 }