Revision 50b82d9d

b/src/gr/ebs/gss/client/PermissionsList.java
19 19
package gr.ebs.gss.client;
20 20

  
21 21
import gr.ebs.gss.client.FilePropertiesDialog.Images;
22
import gr.ebs.gss.client.rest.GetCommand;
22 23
import gr.ebs.gss.client.rest.resource.PermissionHolder;
24
import gr.ebs.gss.client.rest.resource.UserResource;
25
import gr.ebs.gss.client.rest.resource.UserSearchResource;
23 26

  
24 27
import java.util.HashSet;
28
import java.util.Iterator;
25 29
import java.util.Set;
26 30

  
31
import com.google.gwt.core.client.GWT;
27 32
import com.google.gwt.event.dom.client.ClickEvent;
28 33
import com.google.gwt.event.dom.client.ClickHandler;
34
import com.google.gwt.user.client.DeferredCommand;
29 35
import com.google.gwt.user.client.ui.AbstractImagePrototype;
30 36
import com.google.gwt.user.client.ui.CheckBox;
31 37
import com.google.gwt.user.client.ui.Composite;
......
42 48
public class PermissionsList extends Composite {
43 49

  
44 50
	int selectedRow = -1;
45
	int permissionCount=-1;
51
	
52
	int permissionCount = -1;
53
	
46 54
	Set<PermissionHolder> permissions = null;
55
	
47 56
	final Images images;
57
	
48 58
	final VerticalPanel permPanel = new VerticalPanel();
59
	
49 60
	final FlexTable permTable = new FlexTable();
61
	
50 62
	final String owner;
63
	
51 64
	PermissionHolder toRemove = null;
65
	
52 66
	private boolean hasChanges = false;
67
	
53 68
	private boolean hasAddition = false;
54

  
69
	
55 70
	public PermissionsList(final Images theImages, Set<PermissionHolder> thePermissions, String anOwner){
56 71
		images = theImages;
57 72
		owner = anOwner;
......
78 93

  
79 94

  
80 95
	public void updateTable(){
96
		copySetAndContinue(permissions);
97
	}
98

  
99
	public void updatePermissionsAccordingToInput(){
81 100
		int i=1;
101
		for(PermissionHolder dto : permissions){
102
			/*if(dto.getId() == null)
103
				hasChanges =true;*/
104
			CheckBox r = (CheckBox) permTable.getWidget(i, 1);
105
			CheckBox w = (CheckBox) permTable.getWidget(i, 2);
106
			CheckBox m = (CheckBox) permTable.getWidget(i, 3);
107
			if(dto.isRead() != r.getValue() || dto.isWrite() != w.getValue() || dto.isModifyACL() != m.getValue())
108
				hasChanges = true;
109
			dto.setRead(r.getValue());
110
			dto.setWrite(w.getValue());
111
			dto.setModifyACL(m.getValue());
112
			i++;
113
		}		
114
	}
115

  
116
	/**
117
	 * Retrieve the permissions.
118
	 *
119
	 * @return the permissions
120
	 */
121
	public Set<PermissionHolder> getPermissions() {
122
		return permissions;
123
	}
124

  
125
	public void addPermission(PermissionHolder permission){
126
		permissions.add(permission);
127
		hasAddition = true;
128
	}
129
	/**
130
	 * Copies the input Set to a new Set
131
	 * @param input
132
	 */
133
	private void copySetAndContinue(Set<PermissionHolder> input){
134
		Set<PermissionHolder> copiedInput = new HashSet<PermissionHolder>();		
135
		for(PermissionHolder dto : input) {
136
			copiedInput.add(dto);
137
		}
138
		handleFullNames(copiedInput);
139
	}
140
	
141
	/**
142
	 * Examines whether or not the user's full name exists in the 
143
	 * userFullNameMap in the GSS.java for every element of the input list.
144
	 * If the user's full name does not exist in the map then a request is being made
145
	 * for the specific username.  
146
	 * 
147
	 * @param filesInput
148
	 */
149
	private void handleFullNames(Set<PermissionHolder> aPermissions){		
150
		if(aPermissions.isEmpty()){
151
			showPermissionTable();
152
			return;
153
		}
154
		
155
		final PermissionHolder dto = aPermissions.iterator().next();
156
		
157
		if(GSS.get().findUserFullName(dto.getUser()) != null){
158
			if(aPermissions.size() >= 1){
159
				aPermissions.remove(dto);				
160
				handleFullNames(aPermissions);				
161
			}else{
162
				showPermissionTable();
163
			}
164
		}else{
165
			findFullNameAndUpdate(aPermissions);
166
		}
167
	}
168
	
169
	/**
170
	 * Shows the table 
171
	 * 
172
	 * @param aPermissions
173
	 */
174
	private void showPermissionTable(){
175
		int i = 1;
82 176
		if(toRemove != null){
83 177
			permissions.remove(toRemove);
84 178
			toRemove = null;
85 179
		}
86 180
		for(final PermissionHolder dto : permissions){
87

  
88 181
			PushButton removeButton = new PushButton(AbstractImagePrototype.create(images.delete()).createImage(), new ClickHandler() {
89 182
				@Override
90 183
				public void onClick(ClickEvent event) {
......
93 186
					hasChanges = true;
94 187
				}
95 188
			});
96

  
97
			if(dto.getUser() !=null)
98
				if(dto.getUser()!=null && dto.getUser().equals(owner)){
189
						
190
			if(dto.getUser() != null){
191
				if(dto.getUser() != null && dto.getUser().equals(owner)){
99 192
					permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;Owner</span>");
100 193
					removeButton.setVisible(false);
194
				}else{
195
					permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;"+ GSS.get().findUserFullName(dto.getUser()) + "</span>");
101 196
				}
102
				else
103
					permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;"+ GSS.get().getUserFullName(dto.getUser()) +"</span>");
104
			else if(dto.getGroup() != null)
105
				permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permGroup()).getHTML() + "&nbsp;"+dto.getGroup()+"</span>");
197
			}else if(dto.getGroup() != null){
198
				permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permGroup()).getHTML() + "&nbsp;"+ dto.getGroup() + "</span>");
199
			}
200
			
106 201
			CheckBox read = new CheckBox();
107 202
			read.setValue(dto.isRead());
108 203
			CheckBox write = new CheckBox();
......
114 209
				write.setEnabled(false);
115 210
				modify.setEnabled(false);
116 211
			}
212
			
117 213
			permTable.setWidget(i, 1, read);
118 214
			permTable.setWidget(i, 2, write);
119 215
			permTable.setWidget(i, 3, modify);
......
122 218
			permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
123 219
			permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
124 220
			permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
125
			i++;
126
		}
127
		for(; i<permTable.getRowCount(); i++)
128
			permTable.removeRow(i);
129
		hasChanges = false;
130

  
221
			i++;		
222
		}		
223
//		for(; i<permTable.getRowCount(); i++)
224
//			permTable.removeRow(i);
225
//		hasChanges = false;
131 226
	}
132

  
133
	public void updatePermissionsAccordingToInput(){
134
		int i=1;
135
		for(PermissionHolder dto : permissions){
136
			/*if(dto.getId() == null)
137
				hasChanges =true;*/
138
			CheckBox r = (CheckBox) permTable.getWidget(i, 1);
139
			CheckBox w = (CheckBox) permTable.getWidget(i, 2);
140
			CheckBox m = (CheckBox) permTable.getWidget(i, 3);
141
			if(dto.isRead() != r.getValue() || dto.isWrite() != w.getValue() || dto.isModifyACL() != m.getValue())
142
				hasChanges = true;
143
			dto.setRead(r.getValue());
144
			dto.setWrite(w.getValue());
145
			dto.setModifyACL(m.getValue());
146
			i++;
147
		}
148
	}
149

  
150

  
227
	
151 228
	/**
152
	 * Retrieve the permissions.
153
	 *
154
	 * @return the permissions
229
	 * Makes a request to search for full name from a given username
230
	 * and continues checking the next element of the Set.
231
	 *  
232
	 * @param filesInput
155 233
	 */
156
	public Set<PermissionHolder> getPermissions() {
157
		return permissions;
158
	}
159 234

  
160
	public void addPermission(PermissionHolder permission){
161
		permissions.add(permission);
162
		hasAddition = true;
163
	}
235
	private void findFullNameAndUpdate(final Set<PermissionHolder> aPermissions){				
236
		final PermissionHolder dto = aPermissions.iterator().next();
237
		String path = GSS.get().getApiPath() + "users/" + dto.getUser(); 
164 238

  
239
		GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(UserSearchResource.class, path, false,null) {
240
			@Override
241
			public void onComplete() {
242
				final UserSearchResource result = getResult();
243
				for (UserResource user : result.getUsers()){
244
					String username = user.getUsername();
245
					String userFullName = user.getName();
246
					GSS.get().putUserToMap(username, userFullName);
247
					if(aPermissions.size() >= 1){
248
						aPermissions.remove(dto);						
249
						if(aPermissions.isEmpty()){
250
							showPermissionTable();
251
							return;
252
						}
253
						handleFullNames(aPermissions);										
254
					}									
255
				}
256
			}
257
			@Override
258
			public void onError(Throwable t) {				
259
				GSS.get().displayError("Unable to fetch user's full name from the given username " + dto.getUser());
260
				if(aPermissions.size() >= 1){
261
					aPermissions.remove(dto);
262
					if(aPermissions.isEmpty()){
263
						showPermissionTable();
264
						return;
265
					}
266
					handleFullNames(aPermissions);
267
				}
268
			}
269
		};
270
		DeferredCommand.addCommand(gg);
271
	
272
	}
165 273

  
166 274
}

Also available in: Unified diff