Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / PermissionsList.java @ 58777026

History | View | Annotate | Download (9.7 kB)

1
/*
2
 * Copyright 2011 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
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.
16
 *
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.
29
 *
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.
34
 */
35
package gr.grnet.pithos.web.client;
36

    
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;
42

    
43
import java.util.HashSet;
44
import java.util.Set;
45

    
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;
56

    
57

    
58
public class PermissionsList extends Composite {
59

    
60
        int selectedRow = -1;
61
        
62
        int permissionCount = -1;
63
        
64
        Set<PermissionHolder> permissions = null;
65
        
66
        final Images images;
67
        
68
        final VerticalPanel permPanel = new VerticalPanel();
69
        
70
        final FlexTable permTable = new FlexTable();
71
        
72
        final String owner;
73
        
74
        PermissionHolder toRemove = null;
75
        
76
        private boolean hasChanges = false;
77
        
78
        private boolean hasAddition = false;
79
        
80
        public PermissionsList(final Images theImages, Set<PermissionHolder> thePermissions, String anOwner){
81
                images = theImages;
82
                owner = 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);
97
                updateTable();
98
        }
99

    
100
        public boolean hasChanges(){
101
                return hasChanges || hasAddition;
102
        }
103

    
104

    
105
        public void updateTable(){
106
                copySetAndContinue(permissions);
107
        }
108

    
109
        public void updatePermissionsAccordingToInput(){
110
                int i=1;
111
                for(PermissionHolder dto : permissions){
112
                        /*if(dto.getId() == null)
113
                                hasChanges =true;*/
114
                        CheckBox r = (CheckBox) permTable.getWidget(i, 1);
115
                        CheckBox w = (CheckBox) permTable.getWidget(i, 2);
116
                        CheckBox m = (CheckBox) permTable.getWidget(i, 3);
117
                        
118
                        r.getElement().setId("permissionList.read");
119
                        w.getElement().setId("permissionList.write");
120
                        m.getElement().setId("permissionList.modify");
121
                        
122
                        if(dto.isRead() != r.getValue() || dto.isWrite() != w.getValue() || dto.isModifyACL() != m.getValue())
123
                                hasChanges = true;
124
                        dto.setRead(r.getValue());
125
                        dto.setWrite(w.getValue());
126
                        dto.setModifyACL(m.getValue());
127
                        i++;
128
                }                
129
        }
130

    
131
        /**
132
         * Retrieve the permissions.
133
         *
134
         * @return the permissions
135
         */
136
        public Set<PermissionHolder> getPermissions() {
137
                return permissions;
138
        }
139

    
140
        public void addPermission(PermissionHolder permission){
141
                permissions.add(permission);
142
                hasAddition = true;
143
        }
144
        /**
145
         * Copies the input Set to a new Set
146
         * @param input
147
         */
148
        private void copySetAndContinue(Set<PermissionHolder> input){
149
                Set<PermissionHolder> copiedInput = new HashSet<PermissionHolder>();                
150
                for(PermissionHolder dto : input) {
151
                        copiedInput.add(dto);
152
                }
153
                handleFullNames(copiedInput);
154
        }
155
        
156
        /**
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.  
161
         * 
162
         * @param filesInput
163
         */
164
        private void handleFullNames(Set<PermissionHolder> aPermissions){                
165
                if(aPermissions.isEmpty()){
166
                        showPermissionTable();
167
                        return;
168
                }
169
                
170
                final PermissionHolder dto = aPermissions.iterator().next();
171
                if(dto.getGroup() != null){
172
                        if(aPermissions.size() >= 1){
173
                                aPermissions.remove(dto);                                
174
                                handleFullNames(aPermissions);                                
175
                        }
176
                }else if(GSS.get().findUserFullName(dto.getUser()) != null){
177
                        if(aPermissions.size() >= 1){
178
                                aPermissions.remove(dto);                                
179
                                handleFullNames(aPermissions);                                
180
                        }
181
                }else{
182
                        findFullNameAndUpdate(aPermissions);
183
                }
184
        }
185
        
186
        /**
187
         * Shows the permission table 
188
         * 
189
         * @param aPermissions
190
         */
191
        private void showPermissionTable(){
192
                int i = 1;
193
                if(toRemove != null){
194
                        permissions.remove(toRemove);
195
                        toRemove = null;
196
                }
197
                for(final PermissionHolder dto : permissions){
198
                        PushButton removeButton = new PushButton(AbstractImagePrototype.create(images.delete()).createImage(), new ClickHandler() {
199
                                @Override
200
                                public void onClick(ClickEvent event) {
201
                                        toRemove = dto;
202
                                        updateTable();
203
                                        hasChanges = true;
204
                                }
205
                        });
206
                                                
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() + "&nbsp;Owner</span>");
210
                                        removeButton.setVisible(false);
211
                                }else{
212
                                        permTable.setHTML(i, 0, "<span id=permissionList."+GSS.get().findUserFullName(dto.getUser())+">"+ AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;"+ GSS.get().findUserFullName(dto.getUser()) + "</span>");
213
                                }
214
                        }else if(dto.getGroup() != null){
215
                                permTable.setHTML(i, 0, "<span id=permissionList."+dto.getGroup()+">" + AbstractImagePrototype.create(images.permGroup()).getHTML() + "&nbsp;"+ dto.getGroup() + "</span>");
216
                        }
217
                        
218
                        CheckBox read = new CheckBox();
219
                        read.setValue(dto.isRead());
220
                        read.getElement().setId("permissionList.read");
221
                        
222
                        CheckBox write = new CheckBox();
223
                        write.setValue(dto.isWrite());
224
                        write.getElement().setId("permissionList.write");
225
                        
226
                        CheckBox modify = new CheckBox();
227
                        modify.setValue(dto.isModifyACL());
228
                        modify.getElement().setId("permissionList.modify");
229
                        
230
                        if (dto.getUser()!=null && dto.getUser().equals(owner)) {
231
                                read.setEnabled(false);
232
                                write.setEnabled(false);
233
                                modify.setEnabled(false);
234
                        }
235
                        
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);
244
                        i++;                
245
                }
246
                for(; i<permTable.getRowCount(); i++)
247
                        permTable.removeRow(i);
248
                hasChanges = false;
249
        }
250
        
251
        /**
252
         * Makes a request to search for full name from a given username
253
         * and continues checking the next element of the Set.
254
         *  
255
         * @param filesInput
256
         */
257

    
258
        private void findFullNameAndUpdate(final Set<PermissionHolder> aPermissions){                                
259
                final PermissionHolder dto = aPermissions.iterator().next();
260
                String path = GSS.get().getApiPath() + "users/" + dto.getUser(); 
261

    
262
                GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(UserSearchResource.class, path, false,null) {
263
                        @Override
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();
274
                                                        return;
275
                                                }
276
                                                handleFullNames(aPermissions);                                                                                
277
                                        }                                                                        
278
                                }
279
                        }
280
                        @Override
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();
287
                                                return;
288
                                        }
289
                                        handleFullNames(aPermissions);
290
                                }
291
                        }
292
                };
293
                DeferredCommand.addCommand(gg);
294
        
295
        }
296

    
297
}