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();
156 if(dto.getGroup() != null){
157 if(aPermissions.size() >= 1){
158 aPermissions.remove(dto);
159 handleFullNames(aPermissions);
161 }else if(GSS.get().findUserFullName(dto.getUser()) != null){
162 if(aPermissions.size() >= 1){
163 aPermissions.remove(dto);
164 handleFullNames(aPermissions);
167 findFullNameAndUpdate(aPermissions);
172 * Shows the permission table
174 * @param aPermissions
176 private void showPermissionTable(){
178 if(toRemove != null){
179 permissions.remove(toRemove);
182 for(final PermissionHolder dto : permissions){
183 PushButton removeButton = new PushButton(AbstractImagePrototype.create(images.delete()).createImage(), new ClickHandler() {
185 public void onClick(ClickEvent event) {
192 if(dto.getUser() != null){
193 if(dto.getUser() != null && dto.getUser().equals(owner)){
194 permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + " Owner</span>");
195 removeButton.setVisible(false);
197 permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + " "+ GSS.get().findUserFullName(dto.getUser()) + "</span>");
199 }else if(dto.getGroup() != null){
200 permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permGroup()).getHTML() + " "+ dto.getGroup() + "</span>");
203 CheckBox read = new CheckBox();
204 read.setValue(dto.isRead());
205 CheckBox write = new CheckBox();
206 write.setValue(dto.isWrite());
207 CheckBox modify = new CheckBox();
208 modify.setValue(dto.isModifyACL());
209 if (dto.getUser()!=null && dto.getUser().equals(owner)) {
210 read.setEnabled(false);
211 write.setEnabled(false);
212 modify.setEnabled(false);
215 permTable.setWidget(i, 1, read);
216 permTable.setWidget(i, 2, write);
217 permTable.setWidget(i, 3, modify);
218 permTable.setWidget(i, 4, removeButton);
219 permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
220 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
221 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
222 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
225 for(; i<permTable.getRowCount(); i++)
226 permTable.removeRow(i);
231 * Makes a request to search for full name from a given username
232 * and continues checking the next element of the Set.
237 private void findFullNameAndUpdate(final Set<PermissionHolder> aPermissions){
238 final PermissionHolder dto = aPermissions.iterator().next();
239 String path = GSS.get().getApiPath() + "users/" + dto.getUser();
241 GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(UserSearchResource.class, path, false,null) {
243 public void onComplete() {
244 final UserSearchResource result = getResult();
245 for (UserResource user : result.getUsers()){
246 String username = user.getUsername();
247 String userFullName = user.getName();
248 GSS.get().putUserToMap(username, userFullName);
249 if(aPermissions.size() >= 1){
250 aPermissions.remove(dto);
251 if(aPermissions.isEmpty()){
252 showPermissionTable();
255 handleFullNames(aPermissions);
260 public void onError(Throwable t) {
261 GSS.get().displayError("Unable to fetch user's full name from the given username " + dto.getUser());
262 if(aPermissions.size() >= 1){
263 aPermissions.remove(dto);
264 if(aPermissions.isEmpty()){
265 showPermissionTable();
268 handleFullNames(aPermissions);
272 DeferredCommand.addCommand(gg);