2 * Copyright 2008, 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
6 * GSS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
19 package gr.ebs.gss.client;
21 import gr.ebs.gss.client.FilePropertiesDialog.Images;
22 import gr.ebs.gss.client.rest.GetCommand;
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;
27 import java.util.HashSet;
28 import java.util.Iterator;
31 import com.google.gwt.core.client.GWT;
32 import com.google.gwt.event.dom.client.ClickEvent;
33 import com.google.gwt.event.dom.client.ClickHandler;
34 import com.google.gwt.user.client.DeferredCommand;
35 import com.google.gwt.user.client.ui.AbstractImagePrototype;
36 import com.google.gwt.user.client.ui.CheckBox;
37 import com.google.gwt.user.client.ui.Composite;
38 import com.google.gwt.user.client.ui.FlexTable;
39 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
40 import com.google.gwt.user.client.ui.PushButton;
41 import com.google.gwt.user.client.ui.VerticalPanel;
48 public class PermissionsList extends Composite {
52 int permissionCount = -1;
54 Set<PermissionHolder> permissions = null;
58 final VerticalPanel permPanel = new VerticalPanel();
60 final FlexTable permTable = new FlexTable();
64 PermissionHolder toRemove = null;
66 private boolean hasChanges = false;
68 private boolean hasAddition = false;
70 public PermissionsList(final Images theImages, Set<PermissionHolder> thePermissions, String anOwner){
73 permissions = new HashSet<PermissionHolder>();
74 permissions.addAll(thePermissions);
75 permTable.setText(0, 0, "Users/Groups");
76 permTable.setText(0, 1, "Read");
77 permTable.setText(0, 2, "Write");
78 permTable.setText(0, 3, "Modify Access");
79 permTable.setText(0, 4, "");
80 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
81 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
82 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
83 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
84 permPanel.add(permTable);
85 permPanel.addStyleName("gss-TabPanelBottom");
86 initWidget(permPanel);
90 public boolean hasChanges(){
91 return hasChanges || hasAddition;
95 public void updateTable(){
96 copySetAndContinue(permissions);
99 public void updatePermissionsAccordingToInput(){
101 for(PermissionHolder dto : permissions){
102 /*if(dto.getId() == null)
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())
109 dto.setRead(r.getValue());
110 dto.setWrite(w.getValue());
111 dto.setModifyACL(m.getValue());
117 * Retrieve the permissions.
119 * @return the permissions
121 public Set<PermissionHolder> getPermissions() {
125 public void addPermission(PermissionHolder permission){
126 permissions.add(permission);
130 * Copies the input Set to a new Set
133 private void copySetAndContinue(Set<PermissionHolder> input){
134 Set<PermissionHolder> copiedInput = new HashSet<PermissionHolder>();
135 for(PermissionHolder dto : input) {
136 copiedInput.add(dto);
138 handleFullNames(copiedInput);
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.
149 private void handleFullNames(Set<PermissionHolder> aPermissions){
150 if(aPermissions.isEmpty()){
151 showPermissionTable();
155 final PermissionHolder dto = aPermissions.iterator().next();
157 if(GSS.get().findUserFullName(dto.getUser()) != null){
158 if(aPermissions.size() >= 1){
159 aPermissions.remove(dto);
160 handleFullNames(aPermissions);
163 findFullNameAndUpdate(aPermissions);
168 * Shows the permission table
170 * @param aPermissions
172 private void showPermissionTable(){
174 if(toRemove != null){
175 permissions.remove(toRemove);
178 for(final PermissionHolder dto : permissions){
179 PushButton removeButton = new PushButton(AbstractImagePrototype.create(images.delete()).createImage(), new ClickHandler() {
181 public void onClick(ClickEvent event) {
188 if(dto.getUser() != null){
189 if(dto.getUser() != null && dto.getUser().equals(owner)){
190 permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + " Owner</span>");
191 removeButton.setVisible(false);
193 permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + " "+ GSS.get().findUserFullName(dto.getUser()) + "</span>");
195 }else if(dto.getGroup() != null){
196 permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permGroup()).getHTML() + " "+ dto.getGroup() + "</span>");
199 CheckBox read = new CheckBox();
200 read.setValue(dto.isRead());
201 CheckBox write = new CheckBox();
202 write.setValue(dto.isWrite());
203 CheckBox modify = new CheckBox();
204 modify.setValue(dto.isModifyACL());
205 if (dto.getUser()!=null && dto.getUser().equals(owner)) {
206 read.setEnabled(false);
207 write.setEnabled(false);
208 modify.setEnabled(false);
211 permTable.setWidget(i, 1, read);
212 permTable.setWidget(i, 2, write);
213 permTable.setWidget(i, 3, modify);
214 permTable.setWidget(i, 4, removeButton);
215 permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
216 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
217 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
218 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
221 for(; i<permTable.getRowCount(); i++)
222 permTable.removeRow(i);
227 * Makes a request to search for full name from a given username
228 * and continues checking the next element of the Set.
233 private void findFullNameAndUpdate(final Set<PermissionHolder> aPermissions){
234 final PermissionHolder dto = aPermissions.iterator().next();
235 String path = GSS.get().getApiPath() + "users/" + dto.getUser();
237 GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(UserSearchResource.class, path, false,null) {
239 public void onComplete() {
240 final UserSearchResource result = getResult();
241 for (UserResource user : result.getUsers()){
242 String username = user.getUsername();
243 String userFullName = user.getName();
244 GSS.get().putUserToMap(username, userFullName);
245 if(aPermissions.size() >= 1){
246 aPermissions.remove(dto);
247 if(aPermissions.isEmpty()){
248 showPermissionTable();
251 handleFullNames(aPermissions);
256 public void onError(Throwable t) {
257 GSS.get().displayError("Unable to fetch user's full name from the given username " + dto.getUser());
258 if(aPermissions.size() >= 1){
259 aPermissions.remove(dto);
260 if(aPermissions.isEmpty()){
261 showPermissionTable();
264 handleFullNames(aPermissions);
268 DeferredCommand.addCommand(gg);