2 * Copyright 2011 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and
31 * documentation are those of the authors and should not be
32 * interpreted as representing official policies, either expressed
33 * or implied, of GRNET S.A.
35 package gr.grnet.pithos.web.client;
37 import gr.grnet.pithos.web.client.FilePropertiesDialog.Images;
38 import gr.grnet.pithos.web.client.rest.GetCommand;
39 import gr.grnet.pithos.web.client.rest.resource.PermissionHolder;
40 import gr.grnet.pithos.web.client.rest.resource.UserResource;
41 import gr.grnet.pithos.web.client.rest.resource.UserSearchResource;
43 import java.util.HashSet;
46 import com.google.gwt.event.dom.client.ClickEvent;
47 import com.google.gwt.event.dom.client.ClickHandler;
48 import com.google.gwt.user.client.DeferredCommand;
49 import com.google.gwt.user.client.ui.AbstractImagePrototype;
50 import com.google.gwt.user.client.ui.CheckBox;
51 import com.google.gwt.user.client.ui.Composite;
52 import com.google.gwt.user.client.ui.FlexTable;
53 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
54 import com.google.gwt.user.client.ui.PushButton;
55 import com.google.gwt.user.client.ui.VerticalPanel;
58 public class PermissionsList extends Composite {
62 int permissionCount = -1;
64 Set<PermissionHolder> permissions = null;
68 final VerticalPanel permPanel = new VerticalPanel();
70 final FlexTable permTable = new FlexTable();
74 PermissionHolder toRemove = null;
76 private boolean hasChanges = false;
78 private boolean hasAddition = false;
80 public PermissionsList(final Images theImages, Set<PermissionHolder> thePermissions, String anOwner){
83 permissions = new HashSet<PermissionHolder>();
84 permissions.addAll(thePermissions);
85 permTable.setText(0, 0, "Users/Groups");
86 permTable.setText(0, 1, "Read");
87 permTable.setText(0, 2, "Write");
88 permTable.setText(0, 3, "Modify Access");
89 permTable.setText(0, 4, "");
90 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
91 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
92 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
93 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
94 permPanel.add(permTable);
95 permPanel.addStyleName("pithos-TabPanelBottom");
96 initWidget(permPanel);
100 public boolean hasChanges(){
101 return hasChanges || hasAddition;
105 public void updateTable(){
106 copySetAndContinue(permissions);
109 public void updatePermissionsAccordingToInput(){
111 for(PermissionHolder dto : permissions){
112 /*if(dto.getId() == null)
114 CheckBox r = (CheckBox) permTable.getWidget(i, 1);
115 CheckBox w = (CheckBox) permTable.getWidget(i, 2);
116 CheckBox m = (CheckBox) permTable.getWidget(i, 3);
118 r.getElement().setId("permissionList.read");
119 w.getElement().setId("permissionList.write");
120 m.getElement().setId("permissionList.modify");
122 if(dto.isRead() != r.getValue() || dto.isWrite() != w.getValue() || dto.isModifyACL() != m.getValue())
124 dto.setRead(r.getValue());
125 dto.setWrite(w.getValue());
126 dto.setModifyACL(m.getValue());
132 * Retrieve the permissions.
134 * @return the permissions
136 public Set<PermissionHolder> getPermissions() {
140 public void addPermission(PermissionHolder permission){
141 permissions.add(permission);
145 * Copies the input Set to a new Set
148 private void copySetAndContinue(Set<PermissionHolder> input){
149 Set<PermissionHolder> copiedInput = new HashSet<PermissionHolder>();
150 for(PermissionHolder dto : input) {
151 copiedInput.add(dto);
153 handleFullNames(copiedInput);
157 * Examines whether or not the user's full name exists in the
158 * userFullNameMap in the GSS.java for every element of the input list.
159 * If the user's full name does not exist in the map then a request is being made
160 * for the specific username.
164 private void handleFullNames(Set<PermissionHolder> aPermissions){
165 if(aPermissions.isEmpty()){
166 showPermissionTable();
170 final PermissionHolder dto = aPermissions.iterator().next();
171 if(dto.getGroup() != null){
172 if(aPermissions.size() >= 1){
173 aPermissions.remove(dto);
174 handleFullNames(aPermissions);
176 }else if(GSS.get().findUserFullName(dto.getUser()) != null){
177 if(aPermissions.size() >= 1){
178 aPermissions.remove(dto);
179 handleFullNames(aPermissions);
182 findFullNameAndUpdate(aPermissions);
187 * Shows the permission table
189 * @param aPermissions
191 private void showPermissionTable(){
193 if(toRemove != null){
194 permissions.remove(toRemove);
197 for(final PermissionHolder dto : permissions){
198 PushButton removeButton = new PushButton(AbstractImagePrototype.create(images.delete()).createImage(), new ClickHandler() {
200 public void onClick(ClickEvent event) {
207 if(dto.getUser() != null){
208 if(dto.getUser() != null && dto.getUser().equals(owner)){
209 permTable.setHTML(i, 0, "<span id=permissionList.Owner>" + AbstractImagePrototype.create(images.permUser()).getHTML() + " Owner</span>");
210 removeButton.setVisible(false);
212 permTable.setHTML(i, 0, "<span id=permissionList."+GSS.get().findUserFullName(dto.getUser())+">"+ AbstractImagePrototype.create(images.permUser()).getHTML() + " "+ GSS.get().findUserFullName(dto.getUser()) + "</span>");
214 }else if(dto.getGroup() != null){
215 permTable.setHTML(i, 0, "<span id=permissionList."+dto.getGroup()+">" + AbstractImagePrototype.create(images.permGroup()).getHTML() + " "+ dto.getGroup() + "</span>");
218 CheckBox read = new CheckBox();
219 read.setValue(dto.isRead());
220 read.getElement().setId("permissionList.read");
222 CheckBox write = new CheckBox();
223 write.setValue(dto.isWrite());
224 write.getElement().setId("permissionList.write");
226 CheckBox modify = new CheckBox();
227 modify.setValue(dto.isModifyACL());
228 modify.getElement().setId("permissionList.modify");
230 if (dto.getUser()!=null && dto.getUser().equals(owner)) {
231 read.setEnabled(false);
232 write.setEnabled(false);
233 modify.setEnabled(false);
236 permTable.setWidget(i, 1, read);
237 permTable.setWidget(i, 2, write);
238 permTable.setWidget(i, 3, modify);
239 permTable.setWidget(i, 4, removeButton);
240 permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
241 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
242 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
243 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
246 for(; i<permTable.getRowCount(); i++)
247 permTable.removeRow(i);
252 * Makes a request to search for full name from a given username
253 * and continues checking the next element of the Set.
258 private void findFullNameAndUpdate(final Set<PermissionHolder> aPermissions){
259 final PermissionHolder dto = aPermissions.iterator().next();
260 String path = GSS.get().getApiPath() + "users/" + dto.getUser();
262 GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(UserSearchResource.class, path, false,null) {
264 public void onComplete() {
265 final UserSearchResource result = getResult();
266 for (UserResource user : result.getUsers()){
267 String username = user.getUsername();
268 String userFullName = user.getName();
269 GSS.get().putUserToMap(username, userFullName);
270 if(aPermissions.size() >= 1){
271 aPermissions.remove(dto);
272 if(aPermissions.isEmpty()){
273 showPermissionTable();
276 handleFullNames(aPermissions);
281 public void onError(Throwable t) {
282 GSS.get().displayError("Unable to fetch user's full name from the given username " + dto.getUser());
283 if(aPermissions.size() >= 1){
284 aPermissions.remove(dto);
285 if(aPermissions.isEmpty()){
286 showPermissionTable();
289 handleFullNames(aPermissions);
293 DeferredCommand.addCommand(gg);