Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / PermissionsList.java @ ab91cf27

History | View | Annotate | Download (8.3 kB)

1
/*
2
 * Copyright 2008, 2009 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
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.
10
 *
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.
15
 *
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/>.
18
 */
19
package gr.ebs.gss.client;
20

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

    
27
import java.util.HashSet;
28
import java.util.Iterator;
29
import java.util.Set;
30

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

    
43

    
44
/**
45
 * @author kman
46
 *
47
 */
48
public class PermissionsList extends Composite {
49

    
50
        int selectedRow = -1;
51
        
52
        int permissionCount = -1;
53
        
54
        Set<PermissionHolder> permissions = null;
55
        
56
        final Images images;
57
        
58
        final VerticalPanel permPanel = new VerticalPanel();
59
        
60
        final FlexTable permTable = new FlexTable();
61
        
62
        final String owner;
63
        
64
        PermissionHolder toRemove = null;
65
        
66
        private boolean hasChanges = false;
67
        
68
        private boolean hasAddition = false;
69
        
70
        public PermissionsList(final Images theImages, Set<PermissionHolder> thePermissions, String anOwner){
71
                images = theImages;
72
                owner = 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);
87
                updateTable();
88
        }
89

    
90
        public boolean hasChanges(){
91
                return hasChanges || hasAddition;
92
        }
93

    
94

    
95
        public void updateTable(){
96
                copySetAndContinue(permissions);
97
        }
98

    
99
        public void updatePermissionsAccordingToInput(){
100
                int i=1;
101
                for(PermissionHolder dto : permissions){
102
                        /*if(dto.getId() == null)
103
                                hasChanges =true;*/
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())
108
                                hasChanges = true;
109
                        dto.setRead(r.getValue());
110
                        dto.setWrite(w.getValue());
111
                        dto.setModifyACL(m.getValue());
112
                        i++;
113
                }                
114
        }
115

    
116
        /**
117
         * Retrieve the permissions.
118
         *
119
         * @return the permissions
120
         */
121
        public Set<PermissionHolder> getPermissions() {
122
                return permissions;
123
        }
124

    
125
        public void addPermission(PermissionHolder permission){
126
                permissions.add(permission);
127
                hasAddition = true;
128
        }
129
        /**
130
         * Copies the input Set to a new Set
131
         * @param input
132
         */
133
        private void copySetAndContinue(Set<PermissionHolder> input){
134
                Set<PermissionHolder> copiedInput = new HashSet<PermissionHolder>();                
135
                for(PermissionHolder dto : input) {
136
                        copiedInput.add(dto);
137
                }
138
                handleFullNames(copiedInput);
139
        }
140
        
141
        /**
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.  
146
         * 
147
         * @param filesInput
148
         */
149
        private void handleFullNames(Set<PermissionHolder> aPermissions){                
150
                if(aPermissions.isEmpty()){
151
                        showPermissionTable();
152
                        return;
153
                }
154
                
155
                final PermissionHolder dto = aPermissions.iterator().next();
156
                
157
                if(GSS.get().findUserFullName(dto.getUser()) != null){
158
                        if(aPermissions.size() >= 1){
159
                                aPermissions.remove(dto);                                
160
                                handleFullNames(aPermissions);                                
161
                        }else{
162
                                showPermissionTable();
163
                        }
164
                }else{
165
                        findFullNameAndUpdate(aPermissions);
166
                }
167
        }
168
        
169
        /**
170
         * Shows the permission table 
171
         * 
172
         * @param aPermissions
173
         */
174
        private void showPermissionTable(){
175
                int i = 1;
176
                if(toRemove != null){
177
                        permissions.remove(toRemove);
178
                        toRemove = null;
179
                }
180
                for(final PermissionHolder dto : permissions){
181
                        PushButton removeButton = new PushButton(AbstractImagePrototype.create(images.delete()).createImage(), new ClickHandler() {
182
                                @Override
183
                                public void onClick(ClickEvent event) {
184
                                        toRemove = dto;
185
                                        updateTable();
186
                                        hasChanges = true;
187
                                }
188
                        });
189
                                                
190
                        if(dto.getUser() != null){
191
                                if(dto.getUser() != null && dto.getUser().equals(owner)){
192
                                        permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;Owner</span>");
193
                                        removeButton.setVisible(false);
194
                                }else{
195
                                        permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;"+ GSS.get().findUserFullName(dto.getUser()) + "</span>");
196
                                }
197
                        }else if(dto.getGroup() != null){
198
                                permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permGroup()).getHTML() + "&nbsp;"+ dto.getGroup() + "</span>");
199
                        }
200
                        
201
                        CheckBox read = new CheckBox();
202
                        read.setValue(dto.isRead());
203
                        CheckBox write = new CheckBox();
204
                        write.setValue(dto.isWrite());
205
                        CheckBox modify = new CheckBox();
206
                        modify.setValue(dto.isModifyACL());
207
                        if (dto.getUser()!=null && dto.getUser().equals(owner)) {
208
                                read.setEnabled(false);
209
                                write.setEnabled(false);
210
                                modify.setEnabled(false);
211
                        }
212
                        
213
                        permTable.setWidget(i, 1, read);
214
                        permTable.setWidget(i, 2, write);
215
                        permTable.setWidget(i, 3, modify);
216
                        permTable.setWidget(i, 4, removeButton);
217
                        permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
218
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
219
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
220
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
221
                        i++;                
222
                }
223
                for(; i<permTable.getRowCount(); i++)
224
                        permTable.removeRow(i);
225
                hasChanges = false;
226
        }
227
        
228
        /**
229
         * Makes a request to search for full name from a given username
230
         * and continues checking the next element of the Set.
231
         *  
232
         * @param filesInput
233
         */
234

    
235
        private void findFullNameAndUpdate(final Set<PermissionHolder> aPermissions){                                
236
                final PermissionHolder dto = aPermissions.iterator().next();
237
                String path = GSS.get().getApiPath() + "users/" + dto.getUser(); 
238

    
239
                GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(UserSearchResource.class, path, false,null) {
240
                        @Override
241
                        public void onComplete() {
242
                                final UserSearchResource result = getResult();
243
                                for (UserResource user : result.getUsers()){
244
                                        String username = user.getUsername();
245
                                        String userFullName = user.getName();
246
                                        GSS.get().putUserToMap(username, userFullName);
247
                                        if(aPermissions.size() >= 1){
248
                                                aPermissions.remove(dto);                                                
249
                                                if(aPermissions.isEmpty()){
250
                                                        showPermissionTable();
251
                                                        return;
252
                                                }
253
                                                handleFullNames(aPermissions);                                                                                
254
                                        }                                                                        
255
                                }
256
                        }
257
                        @Override
258
                        public void onError(Throwable t) {                                
259
                                GSS.get().displayError("Unable to fetch user's full name from the given username " + dto.getUser());
260
                                if(aPermissions.size() >= 1){
261
                                        aPermissions.remove(dto);
262
                                        if(aPermissions.isEmpty()){
263
                                                showPermissionTable();
264
                                                return;
265
                                        }
266
                                        handleFullNames(aPermissions);
267
                                }
268
                        }
269
                };
270
                DeferredCommand.addCommand(gg);
271
        
272
        }
273

    
274
}