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