Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (9.7 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 ab1eb3f8 Christos Stathis
        
80 ab1eb3f8 Christos Stathis
        public PermissionsList(final Images theImages, Set<PermissionHolder> thePermissions, String anOwner){
81 ab1eb3f8 Christos Stathis
                images = theImages;
82 ab1eb3f8 Christos Stathis
                owner = anOwner;
83 ab1eb3f8 Christos Stathis
                permissions =  new HashSet<PermissionHolder>();
84 ab1eb3f8 Christos Stathis
                permissions.addAll(thePermissions);
85 ab1eb3f8 Christos Stathis
                permTable.setText(0, 0, "Users/Groups");
86 ab1eb3f8 Christos Stathis
                permTable.setText(0, 1, "Read");
87 ab1eb3f8 Christos Stathis
                permTable.setText(0, 2, "Write");
88 ab1eb3f8 Christos Stathis
                permTable.setText(0, 3, "Modify Access");
89 ab1eb3f8 Christos Stathis
                permTable.setText(0, 4, "");
90 ab1eb3f8 Christos Stathis
                permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
91 ab1eb3f8 Christos Stathis
                permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
92 ab1eb3f8 Christos Stathis
                permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
93 ab1eb3f8 Christos Stathis
                permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
94 ab1eb3f8 Christos Stathis
                permPanel.add(permTable);
95 ab1eb3f8 Christos Stathis
                permPanel.addStyleName("pithos-TabPanelBottom");
96 ab1eb3f8 Christos Stathis
                initWidget(permPanel);
97 ab1eb3f8 Christos Stathis
                updateTable();
98 ab1eb3f8 Christos Stathis
        }
99 ab1eb3f8 Christos Stathis
100 ab1eb3f8 Christos Stathis
        public boolean hasChanges(){
101 ab1eb3f8 Christos Stathis
                return hasChanges || hasAddition;
102 ab1eb3f8 Christos Stathis
        }
103 ab1eb3f8 Christos Stathis
104 ab1eb3f8 Christos Stathis
105 ab1eb3f8 Christos Stathis
        public void updateTable(){
106 ab1eb3f8 Christos Stathis
                copySetAndContinue(permissions);
107 ab1eb3f8 Christos Stathis
        }
108 ab1eb3f8 Christos Stathis
109 ab1eb3f8 Christos Stathis
        public void updatePermissionsAccordingToInput(){
110 ab1eb3f8 Christos Stathis
                int i=1;
111 ab1eb3f8 Christos Stathis
                for(PermissionHolder dto : permissions){
112 ab1eb3f8 Christos Stathis
                        /*if(dto.getId() == null)
113 ab1eb3f8 Christos Stathis
                                hasChanges =true;*/
114 ab1eb3f8 Christos Stathis
                        CheckBox r = (CheckBox) permTable.getWidget(i, 1);
115 ab1eb3f8 Christos Stathis
                        CheckBox w = (CheckBox) permTable.getWidget(i, 2);
116 ab1eb3f8 Christos Stathis
                        CheckBox m = (CheckBox) permTable.getWidget(i, 3);
117 ab1eb3f8 Christos Stathis
                        
118 ab1eb3f8 Christos Stathis
                        r.getElement().setId("permissionList.read");
119 ab1eb3f8 Christos Stathis
                        w.getElement().setId("permissionList.write");
120 ab1eb3f8 Christos Stathis
                        m.getElement().setId("permissionList.modify");
121 ab1eb3f8 Christos Stathis
                        
122 ab1eb3f8 Christos Stathis
                        if(dto.isRead() != r.getValue() || dto.isWrite() != w.getValue() || dto.isModifyACL() != m.getValue())
123 ab1eb3f8 Christos Stathis
                                hasChanges = true;
124 ab1eb3f8 Christos Stathis
                        dto.setRead(r.getValue());
125 ab1eb3f8 Christos Stathis
                        dto.setWrite(w.getValue());
126 ab1eb3f8 Christos Stathis
                        dto.setModifyACL(m.getValue());
127 ab1eb3f8 Christos Stathis
                        i++;
128 ab1eb3f8 Christos Stathis
                }                
129 ab1eb3f8 Christos Stathis
        }
130 ab1eb3f8 Christos Stathis
131 ab1eb3f8 Christos Stathis
        /**
132 ab1eb3f8 Christos Stathis
         * Retrieve the permissions.
133 ab1eb3f8 Christos Stathis
         *
134 ab1eb3f8 Christos Stathis
         * @return the permissions
135 ab1eb3f8 Christos Stathis
         */
136 ab1eb3f8 Christos Stathis
        public Set<PermissionHolder> getPermissions() {
137 ab1eb3f8 Christos Stathis
                return permissions;
138 ab1eb3f8 Christos Stathis
        }
139 ab1eb3f8 Christos Stathis
140 ab1eb3f8 Christos Stathis
        public void addPermission(PermissionHolder permission){
141 ab1eb3f8 Christos Stathis
                permissions.add(permission);
142 ab1eb3f8 Christos Stathis
                hasAddition = true;
143 ab1eb3f8 Christos Stathis
        }
144 ab1eb3f8 Christos Stathis
        /**
145 ab1eb3f8 Christos Stathis
         * Copies the input Set to a new Set
146 ab1eb3f8 Christos Stathis
         * @param input
147 ab1eb3f8 Christos Stathis
         */
148 ab1eb3f8 Christos Stathis
        private void copySetAndContinue(Set<PermissionHolder> input){
149 ab1eb3f8 Christos Stathis
                Set<PermissionHolder> copiedInput = new HashSet<PermissionHolder>();                
150 ab1eb3f8 Christos Stathis
                for(PermissionHolder dto : input) {
151 ab1eb3f8 Christos Stathis
                        copiedInput.add(dto);
152 ab1eb3f8 Christos Stathis
                }
153 ab1eb3f8 Christos Stathis
                handleFullNames(copiedInput);
154 ab1eb3f8 Christos Stathis
        }
155 ab1eb3f8 Christos Stathis
        
156 ab1eb3f8 Christos Stathis
        /**
157 ab1eb3f8 Christos Stathis
         * Examines whether or not the user's full name exists in the 
158 f55cf326 Christos Stathis
         * userFullNameMap in the Pithos.java for every element of the input list.
159 ab1eb3f8 Christos Stathis
         * If the user's full name does not exist in the map then a request is being made
160 ab1eb3f8 Christos Stathis
         * for the specific username.  
161 ab1eb3f8 Christos Stathis
         * 
162 ab1eb3f8 Christos Stathis
         * @param filesInput
163 ab1eb3f8 Christos Stathis
         */
164 ab1eb3f8 Christos Stathis
        private void handleFullNames(Set<PermissionHolder> aPermissions){                
165 ab1eb3f8 Christos Stathis
                if(aPermissions.isEmpty()){
166 ab1eb3f8 Christos Stathis
                        showPermissionTable();
167 ab1eb3f8 Christos Stathis
                        return;
168 ab1eb3f8 Christos Stathis
                }
169 ab1eb3f8 Christos Stathis
                
170 ab1eb3f8 Christos Stathis
                final PermissionHolder dto = aPermissions.iterator().next();
171 ab1eb3f8 Christos Stathis
                if(dto.getGroup() != null){
172 ab1eb3f8 Christos Stathis
                        if(aPermissions.size() >= 1){
173 ab1eb3f8 Christos Stathis
                                aPermissions.remove(dto);                                
174 ab1eb3f8 Christos Stathis
                                handleFullNames(aPermissions);                                
175 ab1eb3f8 Christos Stathis
                        }
176 f55cf326 Christos Stathis
                }else if(Pithos.get().findUserFullName(dto.getUser()) != null){
177 ab1eb3f8 Christos Stathis
                        if(aPermissions.size() >= 1){
178 ab1eb3f8 Christos Stathis
                                aPermissions.remove(dto);                                
179 ab1eb3f8 Christos Stathis
                                handleFullNames(aPermissions);                                
180 ab1eb3f8 Christos Stathis
                        }
181 ab1eb3f8 Christos Stathis
                }else{
182 ab1eb3f8 Christos Stathis
                        findFullNameAndUpdate(aPermissions);
183 ab1eb3f8 Christos Stathis
                }
184 ab1eb3f8 Christos Stathis
        }
185 ab1eb3f8 Christos Stathis
        
186 ab1eb3f8 Christos Stathis
        /**
187 ab1eb3f8 Christos Stathis
         * Shows the permission table 
188 ab1eb3f8 Christos Stathis
         * 
189 ab1eb3f8 Christos Stathis
         * @param aPermissions
190 ab1eb3f8 Christos Stathis
         */
191 ab1eb3f8 Christos Stathis
        private void showPermissionTable(){
192 ab1eb3f8 Christos Stathis
                int i = 1;
193 ab1eb3f8 Christos Stathis
                if(toRemove != null){
194 ab1eb3f8 Christos Stathis
                        permissions.remove(toRemove);
195 ab1eb3f8 Christos Stathis
                        toRemove = null;
196 ab1eb3f8 Christos Stathis
                }
197 ab1eb3f8 Christos Stathis
                for(final PermissionHolder dto : permissions){
198 ab1eb3f8 Christos Stathis
                        PushButton removeButton = new PushButton(AbstractImagePrototype.create(images.delete()).createImage(), new ClickHandler() {
199 ab1eb3f8 Christos Stathis
                                @Override
200 ab1eb3f8 Christos Stathis
                                public void onClick(ClickEvent event) {
201 ab1eb3f8 Christos Stathis
                                        toRemove = dto;
202 ab1eb3f8 Christos Stathis
                                        updateTable();
203 ab1eb3f8 Christos Stathis
                                        hasChanges = true;
204 ab1eb3f8 Christos Stathis
                                }
205 ab1eb3f8 Christos Stathis
                        });
206 ab1eb3f8 Christos Stathis
                                                
207 ab1eb3f8 Christos Stathis
                        if(dto.getUser() != null){
208 ab1eb3f8 Christos Stathis
                                if(dto.getUser() != null && dto.getUser().equals(owner)){
209 ab1eb3f8 Christos Stathis
                                        permTable.setHTML(i, 0, "<span id=permissionList.Owner>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;Owner</span>");
210 ab1eb3f8 Christos Stathis
                                        removeButton.setVisible(false);
211 ab1eb3f8 Christos Stathis
                                }else{
212 f55cf326 Christos Stathis
                                        permTable.setHTML(i, 0, "<span id=permissionList."+ Pithos.get().findUserFullName(dto.getUser())+">"+ AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;"+ Pithos.get().findUserFullName(dto.getUser()) + "</span>");
213 ab1eb3f8 Christos Stathis
                                }
214 ab1eb3f8 Christos Stathis
                        }else if(dto.getGroup() != null){
215 ab1eb3f8 Christos Stathis
                                permTable.setHTML(i, 0, "<span id=permissionList."+dto.getGroup()+">" + AbstractImagePrototype.create(images.permGroup()).getHTML() + "&nbsp;"+ dto.getGroup() + "</span>");
216 ab1eb3f8 Christos Stathis
                        }
217 ab1eb3f8 Christos Stathis
                        
218 ab1eb3f8 Christos Stathis
                        CheckBox read = new CheckBox();
219 ab1eb3f8 Christos Stathis
                        read.setValue(dto.isRead());
220 ab1eb3f8 Christos Stathis
                        read.getElement().setId("permissionList.read");
221 ab1eb3f8 Christos Stathis
                        
222 ab1eb3f8 Christos Stathis
                        CheckBox write = new CheckBox();
223 ab1eb3f8 Christos Stathis
                        write.setValue(dto.isWrite());
224 ab1eb3f8 Christos Stathis
                        write.getElement().setId("permissionList.write");
225 ab1eb3f8 Christos Stathis
                        
226 ab1eb3f8 Christos Stathis
                        CheckBox modify = new CheckBox();
227 ab1eb3f8 Christos Stathis
                        modify.setValue(dto.isModifyACL());
228 ab1eb3f8 Christos Stathis
                        modify.getElement().setId("permissionList.modify");
229 ab1eb3f8 Christos Stathis
                        
230 ab1eb3f8 Christos Stathis
                        if (dto.getUser()!=null && dto.getUser().equals(owner)) {
231 ab1eb3f8 Christos Stathis
                                read.setEnabled(false);
232 ab1eb3f8 Christos Stathis
                                write.setEnabled(false);
233 ab1eb3f8 Christos Stathis
                                modify.setEnabled(false);
234 ab1eb3f8 Christos Stathis
                        }
235 ab1eb3f8 Christos Stathis
                        
236 ab1eb3f8 Christos Stathis
                        permTable.setWidget(i, 1, read);
237 ab1eb3f8 Christos Stathis
                        permTable.setWidget(i, 2, write);
238 ab1eb3f8 Christos Stathis
                        permTable.setWidget(i, 3, modify);
239 ab1eb3f8 Christos Stathis
                        permTable.setWidget(i, 4, removeButton);
240 ab1eb3f8 Christos Stathis
                        permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
241 ab1eb3f8 Christos Stathis
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
242 ab1eb3f8 Christos Stathis
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
243 ab1eb3f8 Christos Stathis
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
244 ab1eb3f8 Christos Stathis
                        i++;                
245 ab1eb3f8 Christos Stathis
                }
246 ab1eb3f8 Christos Stathis
                for(; i<permTable.getRowCount(); i++)
247 ab1eb3f8 Christos Stathis
                        permTable.removeRow(i);
248 ab1eb3f8 Christos Stathis
                hasChanges = false;
249 ab1eb3f8 Christos Stathis
        }
250 ab1eb3f8 Christos Stathis
        
251 ab1eb3f8 Christos Stathis
        /**
252 ab1eb3f8 Christos Stathis
         * Makes a request to search for full name from a given username
253 ab1eb3f8 Christos Stathis
         * and continues checking the next element of the Set.
254 ab1eb3f8 Christos Stathis
         *  
255 ab1eb3f8 Christos Stathis
         * @param filesInput
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 f55cf326 Christos Stathis
                String path = Pithos.get().getApiPath() + "users/" + dto.getUser();
261 ab1eb3f8 Christos Stathis
262 ab1eb3f8 Christos Stathis
                GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(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 f55cf326 Christos Stathis
                                        Pithos.get().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 f55cf326 Christos Stathis
                                Pithos.get().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
}