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);
108 r.getElement().setId("permissionList.read");
109 w.getElement().setId("permissionList.write");
110 m.getElement().setId("permissionList.modify");
112 if(dto.isRead() != r.getValue() || dto.isWrite() != w.getValue() || dto.isModifyACL() != m.getValue())
114 dto.setRead(r.getValue());
115 dto.setWrite(w.getValue());
116 dto.setModifyACL(m.getValue());
122 * Retrieve the permissions.
124 * @return the permissions
126 public Set<PermissionHolder> getPermissions() {
130 public void addPermission(PermissionHolder permission){
131 permissions.add(permission);
135 * Copies the input Set to a new Set
138 private void copySetAndContinue(Set<PermissionHolder> input){
139 Set<PermissionHolder> copiedInput = new HashSet<PermissionHolder>();
140 for(PermissionHolder dto : input) {
141 copiedInput.add(dto);
143 handleFullNames(copiedInput);
147 * Examines whether or not the user's full name exists in the
148 * userFullNameMap in the GSS.java for every element of the input list.
149 * If the user's full name does not exist in the map then a request is being made
150 * for the specific username.
154 private void handleFullNames(Set<PermissionHolder> aPermissions){
155 if(aPermissions.isEmpty()){
156 showPermissionTable();
160 final PermissionHolder dto = aPermissions.iterator().next();
161 if(dto.getGroup() != null){
162 if(aPermissions.size() >= 1){
163 aPermissions.remove(dto);
164 handleFullNames(aPermissions);
166 }else if(GSS.get().findUserFullName(dto.getUser()) != null){
167 if(aPermissions.size() >= 1){
168 aPermissions.remove(dto);
169 handleFullNames(aPermissions);
172 findFullNameAndUpdate(aPermissions);
177 * Shows the permission table
179 * @param aPermissions
181 private void showPermissionTable(){
183 if(toRemove != null){
184 permissions.remove(toRemove);
187 for(final PermissionHolder dto : permissions){
188 PushButton removeButton = new PushButton(AbstractImagePrototype.create(images.delete()).createImage(), new ClickHandler() {
190 public void onClick(ClickEvent event) {
197 if(dto.getUser() != null){
198 if(dto.getUser() != null && dto.getUser().equals(owner)){
199 permTable.setHTML(i, 0, "<span id=permissionList.Owner>" + AbstractImagePrototype.create(images.permUser()).getHTML() + " Owner</span>");
200 removeButton.setVisible(false);
202 permTable.setHTML(i, 0, "<span id=permissionList."+GSS.get().findUserFullName(dto.getUser())+">"+ AbstractImagePrototype.create(images.permUser()).getHTML() + " "+ GSS.get().findUserFullName(dto.getUser()) + "</span>");
204 }else if(dto.getGroup() != null){
205 permTable.setHTML(i, 0, "<span id=permissionList."+dto.getGroup()+">" + AbstractImagePrototype.create(images.permGroup()).getHTML() + " "+ dto.getGroup() + "</span>");
208 CheckBox read = new CheckBox();
209 read.setValue(dto.isRead());
210 read.getElement().setId("permissionList.read");
212 CheckBox write = new CheckBox();
213 write.setValue(dto.isWrite());
214 write.getElement().setId("permissionList.write");
216 CheckBox modify = new CheckBox();
217 modify.setValue(dto.isModifyACL());
218 modify.getElement().setId("permissionList.modify");
220 if (dto.getUser()!=null && dto.getUser().equals(owner)) {
221 read.setEnabled(false);
222 write.setEnabled(false);
223 modify.setEnabled(false);
226 permTable.setWidget(i, 1, read);
227 permTable.setWidget(i, 2, write);
228 permTable.setWidget(i, 3, modify);
229 permTable.setWidget(i, 4, removeButton);
230 permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
231 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
232 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
233 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
236 for(; i<permTable.getRowCount(); i++)
237 permTable.removeRow(i);
242 * Makes a request to search for full name from a given username
243 * and continues checking the next element of the Set.
248 private void findFullNameAndUpdate(final Set<PermissionHolder> aPermissions){
249 final PermissionHolder dto = aPermissions.iterator().next();
250 String path = GSS.get().getApiPath() + "users/" + dto.getUser();
252 GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(UserSearchResource.class, path, false,null) {
254 public void onComplete() {
255 final UserSearchResource result = getResult();
256 for (UserResource user : result.getUsers()){
257 String username = user.getUsername();
258 String userFullName = user.getName();
259 GSS.get().putUserToMap(username, userFullName);
260 if(aPermissions.size() >= 1){
261 aPermissions.remove(dto);
262 if(aPermissions.isEmpty()){
263 showPermissionTable();
266 handleFullNames(aPermissions);
271 public void onError(Throwable t) {
272 GSS.get().displayError("Unable to fetch user's full name from the given username " + dto.getUser());
273 if(aPermissions.size() >= 1){
274 aPermissions.remove(dto);
275 if(aPermissions.isEmpty()){
276 showPermissionTable();
279 handleFullNames(aPermissions);
283 DeferredCommand.addCommand(gg);