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.Composite;
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 theImages, Set<PermissionHolder> thePermissions, String anOwner){
images = theImages;
owner = anOwner;
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(GSS.get().findUserFullName(dto.getUser()) != null){
+ if(aPermissions.size() >= 1){
+ aPermissions.remove(dto);
+ handleFullNames(aPermissions);
+ }else{
+ showPermissionTable();
+ }
+ }else{
+ findFullNameAndUpdate(aPermissions);
+ }
+ }
+
+ /**
+ * Shows the 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(AbstractImagePrototype.create(images.delete()).createImage(), new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
hasChanges = true;
}
});
-
- if(dto.getUser() !=null)
- if(dto.getUser()!=null && dto.getUser().equals(owner)){
+
+ if(dto.getUser() != null){
+ if(dto.getUser() != null && dto.getUser().equals(owner)){
permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + " Owner</span>");
removeButton.setVisible(false);
+ }else{
+ permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + " "+ GSS.get().findUserFullName(dto.getUser()) + "</span>");
}
- else
- permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + " "+ GSS.get().getUserFullName(dto.getUser()) +"</span>");
- else if(dto.getGroup() != null)
- permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permGroup()).getHTML() + " "+dto.getGroup()+"</span>");
+ }else if(dto.getGroup() != null){
+ permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permGroup()).getHTML() + " "+ dto.getGroup() + "</span>");
+ }
+
CheckBox read = new CheckBox();
read.setValue(dto.isRead());
CheckBox write = new CheckBox();
write.setEnabled(false);
modify.setEnabled(false);
}
+
permTable.setWidget(i, 1, read);
permTable.setWidget(i, 2, write);
permTable.setWidget(i, 3, modify);
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++;
- }
- for(; i<permTable.getRowCount(); i++)
- permTable.removeRow(i);
- hasChanges = false;
-
+ 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.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
+ * 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);
+
+ }
}