fix node open on hover when using dnd
[pithos] / src / gr / ebs / gss / client / PermissionsList.java
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                         
108                         r.getElement().setId("permissionList.read");
109                         w.getElement().setId("permissionList.write");
110                         m.getElement().setId("permissionList.modify");
111                         
112                         if(dto.isRead() != r.getValue() || dto.isWrite() != w.getValue() || dto.isModifyACL() != m.getValue())
113                                 hasChanges = true;
114                         dto.setRead(r.getValue());
115                         dto.setWrite(w.getValue());
116                         dto.setModifyACL(m.getValue());
117                         i++;
118                 }               
119         }
120
121         /**
122          * Retrieve the permissions.
123          *
124          * @return the permissions
125          */
126         public Set<PermissionHolder> getPermissions() {
127                 return permissions;
128         }
129
130         public void addPermission(PermissionHolder permission){
131                 permissions.add(permission);
132                 hasAddition = true;
133         }
134         /**
135          * Copies the input Set to a new Set
136          * @param input
137          */
138         private void copySetAndContinue(Set<PermissionHolder> input){
139                 Set<PermissionHolder> copiedInput = new HashSet<PermissionHolder>();            
140                 for(PermissionHolder dto : input) {
141                         copiedInput.add(dto);
142                 }
143                 handleFullNames(copiedInput);
144         }
145         
146         /**
147          * Examines whether or not the user's full name exists in the 
148          * userFullNameMap in the GSS.java for every element of the input list.
149          * If the user's full name does not exist in the map then a request is being made
150          * for the specific username.  
151          * 
152          * @param filesInput
153          */
154         private void handleFullNames(Set<PermissionHolder> aPermissions){               
155                 if(aPermissions.isEmpty()){
156                         showPermissionTable();
157                         return;
158                 }
159                 
160                 final PermissionHolder dto = aPermissions.iterator().next();
161                 if(dto.getGroup() != null){
162                         if(aPermissions.size() >= 1){
163                                 aPermissions.remove(dto);                               
164                                 handleFullNames(aPermissions);                          
165                         }
166                 }else if(GSS.get().findUserFullName(dto.getUser()) != null){
167                         if(aPermissions.size() >= 1){
168                                 aPermissions.remove(dto);                               
169                                 handleFullNames(aPermissions);                          
170                         }
171                 }else{
172                         findFullNameAndUpdate(aPermissions);
173                 }
174         }
175         
176         /**
177          * Shows the permission table 
178          * 
179          * @param aPermissions
180          */
181         private void showPermissionTable(){
182                 int i = 1;
183                 if(toRemove != null){
184                         permissions.remove(toRemove);
185                         toRemove = null;
186                 }
187                 for(final PermissionHolder dto : permissions){
188                         PushButton removeButton = new PushButton(AbstractImagePrototype.create(images.delete()).createImage(), new ClickHandler() {
189                                 @Override
190                                 public void onClick(ClickEvent event) {
191                                         toRemove = dto;
192                                         updateTable();
193                                         hasChanges = true;
194                                 }
195                         });
196                                                 
197                         if(dto.getUser() != null){
198                                 if(dto.getUser() != null && dto.getUser().equals(owner)){
199                                         permTable.setHTML(i, 0, "<span id=permissionList.Owner>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;Owner</span>");
200                                         removeButton.setVisible(false);
201                                 }else{
202                                         permTable.setHTML(i, 0, "<span id=permissionList."+GSS.get().findUserFullName(dto.getUser())+">"+ AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;"+ GSS.get().findUserFullName(dto.getUser()) + "</span>");
203                                 }
204                         }else if(dto.getGroup() != null){
205                                 permTable.setHTML(i, 0, "<span id=permissionList."+dto.getGroup()+">" + AbstractImagePrototype.create(images.permGroup()).getHTML() + "&nbsp;"+ dto.getGroup() + "</span>");
206                         }
207                         
208                         CheckBox read = new CheckBox();
209                         read.setValue(dto.isRead());
210                         read.getElement().setId("permissionList.read");
211                         
212                         CheckBox write = new CheckBox();
213                         write.setValue(dto.isWrite());
214                         write.getElement().setId("permissionList.write");
215                         
216                         CheckBox modify = new CheckBox();
217                         modify.setValue(dto.isModifyACL());
218                         modify.getElement().setId("permissionList.modify");
219                         
220                         if (dto.getUser()!=null && dto.getUser().equals(owner)) {
221                                 read.setEnabled(false);
222                                 write.setEnabled(false);
223                                 modify.setEnabled(false);
224                         }
225                         
226                         permTable.setWidget(i, 1, read);
227                         permTable.setWidget(i, 2, write);
228                         permTable.setWidget(i, 3, modify);
229                         permTable.setWidget(i, 4, removeButton);
230                         permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
231                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
232                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
233                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
234                         i++;            
235                 }
236                 for(; i<permTable.getRowCount(); i++)
237                         permTable.removeRow(i);
238                 hasChanges = false;
239         }
240         
241         /**
242          * Makes a request to search for full name from a given username
243          * and continues checking the next element of the Set.
244          *  
245          * @param filesInput
246          */
247
248         private void findFullNameAndUpdate(final Set<PermissionHolder> aPermissions){                           
249                 final PermissionHolder dto = aPermissions.iterator().next();
250                 String path = GSS.get().getApiPath() + "users/" + dto.getUser(); 
251
252                 GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(UserSearchResource.class, path, false,null) {
253                         @Override
254                         public void onComplete() {
255                                 final UserSearchResource result = getResult();
256                                 for (UserResource user : result.getUsers()){
257                                         String username = user.getUsername();
258                                         String userFullName = user.getName();
259                                         GSS.get().putUserToMap(username, userFullName);
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                         @Override
271                         public void onError(Throwable t) {                              
272                                 GSS.get().displayError("Unable to fetch user's full name from the given username " + dto.getUser());
273                                 if(aPermissions.size() >= 1){
274                                         aPermissions.remove(dto);
275                                         if(aPermissions.isEmpty()){
276                                                 showPermissionTable();
277                                                 return;
278                                         }
279                                         handleFullNames(aPermissions);
280                                 }
281                         }
282                 };
283                 DeferredCommand.addCommand(gg);
284         
285         }
286
287 }