Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (9.6 kB)

1 ab1eb3f8 Christos Stathis
/*
2 58777026 Christos Stathis
 * Copyright 2011 GRNET S.A. All rights reserved.
3 58777026 Christos Stathis
 *
4 58777026 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 58777026 Christos Stathis
 * without modification, are permitted provided that the following
6 58777026 Christos Stathis
 * conditions are met:
7 58777026 Christos Stathis
 *
8 58777026 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 58777026 Christos Stathis
 *      disclaimer.
11 58777026 Christos Stathis
 *
12 58777026 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 58777026 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 58777026 Christos Stathis
 *      provided with the distribution.
16 58777026 Christos Stathis
 *
17 58777026 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 58777026 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 58777026 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 58777026 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 58777026 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 58777026 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 58777026 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 58777026 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 58777026 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 58777026 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 58777026 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 58777026 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 58777026 Christos Stathis
 *
30 58777026 Christos Stathis
 * The views and conclusions contained in the software and
31 58777026 Christos Stathis
 * documentation are those of the authors and should not be
32 58777026 Christos Stathis
 * interpreted as representing official policies, either expressed
33 58777026 Christos Stathis
 * or implied, of GRNET S.A.
34 ab1eb3f8 Christos Stathis
 */
35 ab1eb3f8 Christos Stathis
package gr.grnet.pithos.web.client;
36 ab1eb3f8 Christos Stathis
37 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.FilePropertiesDialog.Images;
38 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.GetCommand;
39 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.resource.PermissionHolder;
40 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.resource.UserResource;
41 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.resource.UserSearchResource;
42 ab1eb3f8 Christos Stathis
43 ab1eb3f8 Christos Stathis
import java.util.HashSet;
44 ab1eb3f8 Christos Stathis
import java.util.Set;
45 ab1eb3f8 Christos Stathis
46 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.ClickEvent;
47 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.ClickHandler;
48 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.DeferredCommand;
49 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
50 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.CheckBox;
51 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.Composite;
52 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.FlexTable;
53 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
54 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.PushButton;
55 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.VerticalPanel;
56 ab1eb3f8 Christos Stathis
57 ab1eb3f8 Christos Stathis
58 ab1eb3f8 Christos Stathis
public class PermissionsList extends Composite {
59 ab1eb3f8 Christos Stathis
60 ab1eb3f8 Christos Stathis
        int selectedRow = -1;
61 ab1eb3f8 Christos Stathis
        
62 ab1eb3f8 Christos Stathis
        int permissionCount = -1;
63 ab1eb3f8 Christos Stathis
        
64 ab1eb3f8 Christos Stathis
        Set<PermissionHolder> permissions = null;
65 ab1eb3f8 Christos Stathis
        
66 ab1eb3f8 Christos Stathis
        final Images images;
67 ab1eb3f8 Christos Stathis
        
68 ab1eb3f8 Christos Stathis
        final VerticalPanel permPanel = new VerticalPanel();
69 ab1eb3f8 Christos Stathis
        
70 ab1eb3f8 Christos Stathis
        final FlexTable permTable = new FlexTable();
71 ab1eb3f8 Christos Stathis
        
72 ab1eb3f8 Christos Stathis
        final String owner;
73 ab1eb3f8 Christos Stathis
        
74 ab1eb3f8 Christos Stathis
        PermissionHolder toRemove = null;
75 ab1eb3f8 Christos Stathis
        
76 ab1eb3f8 Christos Stathis
        private boolean hasChanges = false;
77 ab1eb3f8 Christos Stathis
        
78 ab1eb3f8 Christos Stathis
        private boolean hasAddition = false;
79 e9c9f408 Christos Stathis
80 e9c9f408 Christos Stathis
    private Pithos app;
81 ab1eb3f8 Christos Stathis
        
82 e9c9f408 Christos Stathis
        public PermissionsList(Pithos _app, final Images theImages, Set<PermissionHolder> thePermissions, String anOwner){
83 e9c9f408 Christos Stathis
        app = _app;
84 ab1eb3f8 Christos Stathis
                images = theImages;
85 ab1eb3f8 Christos Stathis
                owner = anOwner;
86 ab1eb3f8 Christos Stathis
                permissions =  new HashSet<PermissionHolder>();
87 ab1eb3f8 Christos Stathis
                permissions.addAll(thePermissions);
88 ab1eb3f8 Christos Stathis
                permTable.setText(0, 0, "Users/Groups");
89 ab1eb3f8 Christos Stathis
                permTable.setText(0, 1, "Read");
90 ab1eb3f8 Christos Stathis
                permTable.setText(0, 2, "Write");
91 ab1eb3f8 Christos Stathis
                permTable.setText(0, 3, "Modify Access");
92 ab1eb3f8 Christos Stathis
                permTable.setText(0, 4, "");
93 ab1eb3f8 Christos Stathis
                permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
94 ab1eb3f8 Christos Stathis
                permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
95 ab1eb3f8 Christos Stathis
                permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
96 ab1eb3f8 Christos Stathis
                permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
97 ab1eb3f8 Christos Stathis
                permPanel.add(permTable);
98 ab1eb3f8 Christos Stathis
                permPanel.addStyleName("pithos-TabPanelBottom");
99 ab1eb3f8 Christos Stathis
                initWidget(permPanel);
100 ab1eb3f8 Christos Stathis
                updateTable();
101 ab1eb3f8 Christos Stathis
        }
102 ab1eb3f8 Christos Stathis
103 ab1eb3f8 Christos Stathis
        public boolean hasChanges(){
104 ab1eb3f8 Christos Stathis
                return hasChanges || hasAddition;
105 ab1eb3f8 Christos Stathis
        }
106 ab1eb3f8 Christos Stathis
107 ab1eb3f8 Christos Stathis
108 ab1eb3f8 Christos Stathis
        public void updateTable(){
109 ab1eb3f8 Christos Stathis
                copySetAndContinue(permissions);
110 ab1eb3f8 Christos Stathis
        }
111 ab1eb3f8 Christos Stathis
112 ab1eb3f8 Christos Stathis
        public void updatePermissionsAccordingToInput(){
113 ab1eb3f8 Christos Stathis
                int i=1;
114 ab1eb3f8 Christos Stathis
                for(PermissionHolder dto : permissions){
115 ab1eb3f8 Christos Stathis
                        /*if(dto.getId() == null)
116 ab1eb3f8 Christos Stathis
                                hasChanges =true;*/
117 ab1eb3f8 Christos Stathis
                        CheckBox r = (CheckBox) permTable.getWidget(i, 1);
118 ab1eb3f8 Christos Stathis
                        CheckBox w = (CheckBox) permTable.getWidget(i, 2);
119 ab1eb3f8 Christos Stathis
                        CheckBox m = (CheckBox) permTable.getWidget(i, 3);
120 ab1eb3f8 Christos Stathis
                        
121 ab1eb3f8 Christos Stathis
                        r.getElement().setId("permissionList.read");
122 ab1eb3f8 Christos Stathis
                        w.getElement().setId("permissionList.write");
123 ab1eb3f8 Christos Stathis
                        m.getElement().setId("permissionList.modify");
124 ab1eb3f8 Christos Stathis
                        
125 ab1eb3f8 Christos Stathis
                        if(dto.isRead() != r.getValue() || dto.isWrite() != w.getValue() || dto.isModifyACL() != m.getValue())
126 ab1eb3f8 Christos Stathis
                                hasChanges = true;
127 ab1eb3f8 Christos Stathis
                        dto.setRead(r.getValue());
128 ab1eb3f8 Christos Stathis
                        dto.setWrite(w.getValue());
129 ab1eb3f8 Christos Stathis
                        dto.setModifyACL(m.getValue());
130 ab1eb3f8 Christos Stathis
                        i++;
131 ab1eb3f8 Christos Stathis
                }                
132 ab1eb3f8 Christos Stathis
        }
133 ab1eb3f8 Christos Stathis
134 ab1eb3f8 Christos Stathis
        /**
135 ab1eb3f8 Christos Stathis
         * Retrieve the permissions.
136 ab1eb3f8 Christos Stathis
         *
137 ab1eb3f8 Christos Stathis
         * @return the permissions
138 ab1eb3f8 Christos Stathis
         */
139 ab1eb3f8 Christos Stathis
        public Set<PermissionHolder> getPermissions() {
140 ab1eb3f8 Christos Stathis
                return permissions;
141 ab1eb3f8 Christos Stathis
        }
142 ab1eb3f8 Christos Stathis
143 ab1eb3f8 Christos Stathis
        public void addPermission(PermissionHolder permission){
144 ab1eb3f8 Christos Stathis
                permissions.add(permission);
145 ab1eb3f8 Christos Stathis
                hasAddition = true;
146 ab1eb3f8 Christos Stathis
        }
147 ab1eb3f8 Christos Stathis
        /**
148 ab1eb3f8 Christos Stathis
         * Copies the input Set to a new Set
149 ab1eb3f8 Christos Stathis
         * @param input
150 ab1eb3f8 Christos Stathis
         */
151 ab1eb3f8 Christos Stathis
        private void copySetAndContinue(Set<PermissionHolder> input){
152 ab1eb3f8 Christos Stathis
                Set<PermissionHolder> copiedInput = new HashSet<PermissionHolder>();                
153 ab1eb3f8 Christos Stathis
                for(PermissionHolder dto : input) {
154 ab1eb3f8 Christos Stathis
                        copiedInput.add(dto);
155 ab1eb3f8 Christos Stathis
                }
156 ab1eb3f8 Christos Stathis
                handleFullNames(copiedInput);
157 ab1eb3f8 Christos Stathis
        }
158 ab1eb3f8 Christos Stathis
        
159 ab1eb3f8 Christos Stathis
        /**
160 ab1eb3f8 Christos Stathis
         * Examines whether or not the user's full name exists in the 
161 f55cf326 Christos Stathis
         * userFullNameMap in the Pithos.java for every element of the input list.
162 ab1eb3f8 Christos Stathis
         * If the user's full name does not exist in the map then a request is being made
163 ab1eb3f8 Christos Stathis
         * for the specific username.  
164 ab1eb3f8 Christos Stathis
         * 
165 ab1eb3f8 Christos Stathis
         */
166 ab1eb3f8 Christos Stathis
        private void handleFullNames(Set<PermissionHolder> aPermissions){                
167 ab1eb3f8 Christos Stathis
                if(aPermissions.isEmpty()){
168 ab1eb3f8 Christos Stathis
                        showPermissionTable();
169 ab1eb3f8 Christos Stathis
                        return;
170 ab1eb3f8 Christos Stathis
                }
171 ab1eb3f8 Christos Stathis
                
172 ab1eb3f8 Christos Stathis
                final PermissionHolder dto = aPermissions.iterator().next();
173 ab1eb3f8 Christos Stathis
                if(dto.getGroup() != null){
174 ab1eb3f8 Christos Stathis
                        if(aPermissions.size() >= 1){
175 ab1eb3f8 Christos Stathis
                                aPermissions.remove(dto);                                
176 ab1eb3f8 Christos Stathis
                                handleFullNames(aPermissions);                                
177 ab1eb3f8 Christos Stathis
                        }
178 e9c9f408 Christos Stathis
                }else if(app.findUserFullName(dto.getUser()) != null){
179 ab1eb3f8 Christos Stathis
                        if(aPermissions.size() >= 1){
180 ab1eb3f8 Christos Stathis
                                aPermissions.remove(dto);                                
181 ab1eb3f8 Christos Stathis
                                handleFullNames(aPermissions);                                
182 ab1eb3f8 Christos Stathis
                        }
183 ab1eb3f8 Christos Stathis
                }else{
184 ab1eb3f8 Christos Stathis
                        findFullNameAndUpdate(aPermissions);
185 ab1eb3f8 Christos Stathis
                }
186 ab1eb3f8 Christos Stathis
        }
187 ab1eb3f8 Christos Stathis
        
188 ab1eb3f8 Christos Stathis
        /**
189 ab1eb3f8 Christos Stathis
         * Shows the permission table 
190 ab1eb3f8 Christos Stathis
         * 
191 ab1eb3f8 Christos Stathis
         */
192 ab1eb3f8 Christos Stathis
        private void showPermissionTable(){
193 ab1eb3f8 Christos Stathis
                int i = 1;
194 ab1eb3f8 Christos Stathis
                if(toRemove != null){
195 ab1eb3f8 Christos Stathis
                        permissions.remove(toRemove);
196 ab1eb3f8 Christos Stathis
                        toRemove = null;
197 ab1eb3f8 Christos Stathis
                }
198 ab1eb3f8 Christos Stathis
                for(final PermissionHolder dto : permissions){
199 ab1eb3f8 Christos Stathis
                        PushButton removeButton = new PushButton(AbstractImagePrototype.create(images.delete()).createImage(), new ClickHandler() {
200 ab1eb3f8 Christos Stathis
                                @Override
201 ab1eb3f8 Christos Stathis
                                public void onClick(ClickEvent event) {
202 ab1eb3f8 Christos Stathis
                                        toRemove = dto;
203 ab1eb3f8 Christos Stathis
                                        updateTable();
204 ab1eb3f8 Christos Stathis
                                        hasChanges = true;
205 ab1eb3f8 Christos Stathis
                                }
206 ab1eb3f8 Christos Stathis
                        });
207 ab1eb3f8 Christos Stathis
                                                
208 ab1eb3f8 Christos Stathis
                        if(dto.getUser() != null){
209 ab1eb3f8 Christos Stathis
                                if(dto.getUser() != null && dto.getUser().equals(owner)){
210 ab1eb3f8 Christos Stathis
                                        permTable.setHTML(i, 0, "<span id=permissionList.Owner>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;Owner</span>");
211 ab1eb3f8 Christos Stathis
                                        removeButton.setVisible(false);
212 ab1eb3f8 Christos Stathis
                                }else{
213 e9c9f408 Christos Stathis
                                        permTable.setHTML(i, 0, "<span id=permissionList."+ app.findUserFullName(dto.getUser())+">"+ AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;"+ app.findUserFullName(dto.getUser()) + "</span>");
214 ab1eb3f8 Christos Stathis
                                }
215 ab1eb3f8 Christos Stathis
                        }else if(dto.getGroup() != null){
216 ab1eb3f8 Christos Stathis
                                permTable.setHTML(i, 0, "<span id=permissionList."+dto.getGroup()+">" + AbstractImagePrototype.create(images.permGroup()).getHTML() + "&nbsp;"+ dto.getGroup() + "</span>");
217 ab1eb3f8 Christos Stathis
                        }
218 ab1eb3f8 Christos Stathis
                        
219 ab1eb3f8 Christos Stathis
                        CheckBox read = new CheckBox();
220 ab1eb3f8 Christos Stathis
                        read.setValue(dto.isRead());
221 ab1eb3f8 Christos Stathis
                        read.getElement().setId("permissionList.read");
222 ab1eb3f8 Christos Stathis
                        
223 ab1eb3f8 Christos Stathis
                        CheckBox write = new CheckBox();
224 ab1eb3f8 Christos Stathis
                        write.setValue(dto.isWrite());
225 ab1eb3f8 Christos Stathis
                        write.getElement().setId("permissionList.write");
226 ab1eb3f8 Christos Stathis
                        
227 ab1eb3f8 Christos Stathis
                        CheckBox modify = new CheckBox();
228 ab1eb3f8 Christos Stathis
                        modify.setValue(dto.isModifyACL());
229 ab1eb3f8 Christos Stathis
                        modify.getElement().setId("permissionList.modify");
230 ab1eb3f8 Christos Stathis
                        
231 ab1eb3f8 Christos Stathis
                        if (dto.getUser()!=null && dto.getUser().equals(owner)) {
232 ab1eb3f8 Christos Stathis
                                read.setEnabled(false);
233 ab1eb3f8 Christos Stathis
                                write.setEnabled(false);
234 ab1eb3f8 Christos Stathis
                                modify.setEnabled(false);
235 ab1eb3f8 Christos Stathis
                        }
236 ab1eb3f8 Christos Stathis
                        
237 ab1eb3f8 Christos Stathis
                        permTable.setWidget(i, 1, read);
238 ab1eb3f8 Christos Stathis
                        permTable.setWidget(i, 2, write);
239 ab1eb3f8 Christos Stathis
                        permTable.setWidget(i, 3, modify);
240 ab1eb3f8 Christos Stathis
                        permTable.setWidget(i, 4, removeButton);
241 ab1eb3f8 Christos Stathis
                        permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
242 ab1eb3f8 Christos Stathis
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
243 ab1eb3f8 Christos Stathis
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
244 ab1eb3f8 Christos Stathis
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
245 ab1eb3f8 Christos Stathis
                        i++;                
246 ab1eb3f8 Christos Stathis
                }
247 ab1eb3f8 Christos Stathis
                for(; i<permTable.getRowCount(); i++)
248 ab1eb3f8 Christos Stathis
                        permTable.removeRow(i);
249 ab1eb3f8 Christos Stathis
                hasChanges = false;
250 ab1eb3f8 Christos Stathis
        }
251 ab1eb3f8 Christos Stathis
        
252 ab1eb3f8 Christos Stathis
        /**
253 ab1eb3f8 Christos Stathis
         * Makes a request to search for full name from a given username
254 ab1eb3f8 Christos Stathis
         * and continues checking the next element of the Set.
255 ab1eb3f8 Christos Stathis
         *  
256 ab1eb3f8 Christos Stathis
         */
257 ab1eb3f8 Christos Stathis
258 ab1eb3f8 Christos Stathis
        private void findFullNameAndUpdate(final Set<PermissionHolder> aPermissions){                                
259 ab1eb3f8 Christos Stathis
                final PermissionHolder dto = aPermissions.iterator().next();
260 e9c9f408 Christos Stathis
                String path = app.getApiPath() + "users/" + dto.getUser();
261 ab1eb3f8 Christos Stathis
262 e9c9f408 Christos Stathis
                GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(app, UserSearchResource.class, path, false,null) {
263 ab1eb3f8 Christos Stathis
                        @Override
264 ab1eb3f8 Christos Stathis
                        public void onComplete() {
265 ab1eb3f8 Christos Stathis
                                final UserSearchResource result = getResult();
266 ab1eb3f8 Christos Stathis
                                for (UserResource user : result.getUsers()){
267 ab1eb3f8 Christos Stathis
                                        String username = user.getUsername();
268 ab1eb3f8 Christos Stathis
                                        String userFullName = user.getName();
269 e9c9f408 Christos Stathis
                                        app.putUserToMap(username, userFullName);
270 ab1eb3f8 Christos Stathis
                                        if(aPermissions.size() >= 1){
271 ab1eb3f8 Christos Stathis
                                                aPermissions.remove(dto);                                                
272 ab1eb3f8 Christos Stathis
                                                if(aPermissions.isEmpty()){
273 ab1eb3f8 Christos Stathis
                                                        showPermissionTable();
274 ab1eb3f8 Christos Stathis
                                                        return;
275 ab1eb3f8 Christos Stathis
                                                }
276 ab1eb3f8 Christos Stathis
                                                handleFullNames(aPermissions);                                                                                
277 ab1eb3f8 Christos Stathis
                                        }                                                                        
278 ab1eb3f8 Christos Stathis
                                }
279 ab1eb3f8 Christos Stathis
                        }
280 ab1eb3f8 Christos Stathis
                        @Override
281 ab1eb3f8 Christos Stathis
                        public void onError(Throwable t) {                                
282 e9c9f408 Christos Stathis
                                app.displayError("Unable to fetch user's full name from the given username " + dto.getUser());
283 ab1eb3f8 Christos Stathis
                                if(aPermissions.size() >= 1){
284 ab1eb3f8 Christos Stathis
                                        aPermissions.remove(dto);
285 ab1eb3f8 Christos Stathis
                                        if(aPermissions.isEmpty()){
286 ab1eb3f8 Christos Stathis
                                                showPermissionTable();
287 ab1eb3f8 Christos Stathis
                                                return;
288 ab1eb3f8 Christos Stathis
                                        }
289 ab1eb3f8 Christos Stathis
                                        handleFullNames(aPermissions);
290 ab1eb3f8 Christos Stathis
                                }
291 ab1eb3f8 Christos Stathis
                        }
292 ab1eb3f8 Christos Stathis
                };
293 ab1eb3f8 Christos Stathis
                DeferredCommand.addCommand(gg);
294 ab1eb3f8 Christos Stathis
        
295 ab1eb3f8 Christos Stathis
        }
296 ab1eb3f8 Christos Stathis
297 ab1eb3f8 Christos Stathis
}