Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / VersionsList.java @ 63366925

History | View | Annotate | Download (11.1 kB)

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.DeleteCommand;
39
import gr.grnet.pithos.web.client.rest.GetCommand;
40
import gr.grnet.pithos.web.client.rest.PostCommand;
41
import gr.grnet.pithos.web.client.rest.RestCommand;
42
import gr.grnet.pithos.web.client.rest.RestException;
43
import gr.grnet.pithos.web.client.rest.resource.FileResource;
44
import gr.grnet.pithos.web.client.rest.resource.UserResource;
45
import gr.grnet.pithos.web.client.rest.resource.UserSearchResource;
46

    
47
import java.util.ArrayList;
48
import java.util.Collections;
49
import java.util.Comparator;
50
import java.util.Date;
51
import java.util.List;
52

    
53

    
54
import com.google.gwt.core.client.GWT;
55
import com.google.gwt.event.dom.client.ClickEvent;
56
import com.google.gwt.event.dom.client.ClickHandler;
57
import com.google.gwt.http.client.URL;
58
import com.google.gwt.i18n.client.DateTimeFormat;
59
import com.google.gwt.user.client.DeferredCommand;
60
import com.google.gwt.user.client.Window;
61
import com.google.gwt.user.client.ui.AbstractImagePrototype;
62
import com.google.gwt.user.client.ui.Composite;
63
import com.google.gwt.user.client.ui.FlexTable;
64
import com.google.gwt.user.client.ui.HTML;
65
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
66
import com.google.gwt.user.client.ui.VerticalPanel;
67

    
68
public class VersionsList extends Composite {
69

    
70
        int selectedRow = -1;
71

    
72
        int permissionCount = -1;
73

    
74
        List<FileResource> versions = null;
75

    
76
        final Images images;
77

    
78
        final VerticalPanel permPanel = new VerticalPanel();
79

    
80
        final FlexTable permTable = new FlexTable();
81

    
82
        FileResource toRemove = null;
83

    
84
        FilePropertiesDialog container;
85

    
86
        public VersionsList(FilePropertiesDialog aContainer, final Images theImages, List<FileResource> theVersions) {
87
                images = theImages;
88
                container = aContainer;
89
                versions = theVersions;
90
                Collections.sort(theVersions, new Comparator<FileResource>(){
91

    
92
                        @Override
93
                        public int compare(FileResource o1, FileResource o2) {
94
                                return o1.getVersion().compareTo(o2.getVersion());
95
                        }
96

    
97
                });
98
                permTable.setText(0, 0, "Version");
99
                permTable.setText(0, 1, "Created");
100
                permTable.setText(0, 2, "Modified");
101
                permTable.setText(0, 3, "Size");
102
                permTable.setText(0, 4, "");
103
                permTable.setText(0, 5, "");
104
                permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
105
                permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
106
                permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
107
                permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
108
                permTable.getFlexCellFormatter().setColSpan(0, 1, 2);
109
                permTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
110
                permTable.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
111
                permTable.getFlexCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
112
                permTable.getFlexCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_CENTER);
113
                permPanel.add(permTable);
114
                permPanel.addStyleName("pithos-TabPanelBottom");
115
                permTable.addStyleName("pithos-permList");
116
                initWidget(permPanel);
117
                updateTable();
118
        }
119

    
120
        public void updateTable() {
121
                copyListAndContinue(versions);                
122
        }
123
        
124
        public void showVersionsTable(){
125
                int i = 1;
126
                if (toRemove != null) {
127
                        versions.remove(toRemove);
128
                        toRemove = null;
129
                }
130
                for (final FileResource dto : versions) {
131
                        HTML restoreVersion = new HTML("<a href='#' class='hidden-link info'><span>"+AbstractImagePrototype.create(images.restore()).getHTML()+"</span><div>Restore this Version</div></a>");
132
                        restoreVersion.addClickHandler(new ClickHandler() {
133
                                @Override
134
                                public void onClick(ClickEvent event) {
135
                                        restoreVersion(dto);
136
                                }
137
                        });
138

    
139
                        permTable.setHTML(i, 0, "<span>" + dto.getVersion() + "</span>");
140
                        permTable.setHTML(i, 1, "<span>" + formatDate(dto.getCreationDate()) + " by " + GSS.get().findUserFullName(dto.getCreatedBy()) + "</span>");
141
                        permTable.setHTML(i, 2, "<span>" + formatDate(dto.getModificationDate()) + " by " + GSS.get().findUserFullName(dto.getModifiedBy()) + "</span>");
142
                        permTable.setHTML(i, 3, "<span>" + dto.getFileSizeAsString() + "</span>");
143
                        HTML downloadHtml = new HTML("<a class='hidden-link info' href='#'><span>"+AbstractImagePrototype.create(images.download()).getHTML()+"</span><div>View this Version</div></a>");
144
                        downloadHtml.addClickHandler(new ClickHandler() {
145
                                @Override
146
                                public void onClick(ClickEvent event) {
147
                                        GSS app = GSS.get();
148
                                        String dateString = RestCommand.getDate();
149
                                        String resource = dto.getUri().substring(app.getApiPath().length()-1, dto.getUri().length());
150
                                        String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken()));
151
                                        String fileUrl = dto.getUri() + "?version=" + dto.getVersion() + "&Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString);
152
                                        Window.open(fileUrl, "_BLANK", "");
153
                                }
154
                        });
155
                        permTable.setWidget(i, 4, downloadHtml);
156
                        permTable.setWidget(i, 5, restoreVersion);
157
                        permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
158
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 0, HasHorizontalAlignment.ALIGN_CENTER);
159
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
160
                        permTable.getFlexCellFormatter().setColSpan(i, 1, 2);
161
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
162
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
163
                        i++;
164
                }
165
                for (; i < permTable.getRowCount(); i++)
166
                        permTable.removeRow(i);
167
        }
168

    
169
        void removeVersion(final FileResource version) {
170
                DeleteCommand df = new DeleteCommand(version.getUri()){
171

    
172
                        @Override
173
                        public void onComplete() {
174
                                toRemove = version;
175
                                updateTable();
176
                                GSS.get().getTreeView().refreshCurrentNode(false);
177
                        }
178

    
179
                        @Override
180
                        public void onError(Throwable t) {
181
                                GWT.log("", t);
182
                                if(t instanceof RestException){
183
                                        int statusCode = ((RestException)t).getHttpStatusCode();
184
                                        if(statusCode == 405)
185
                                                GSS.get().displayError("You don't have the necessary permissions");
186
                                        else if(statusCode == 404)
187
                                                GSS.get().displayError("Versions does not exist");
188
                                        else
189
                                                GSS.get().displayError("Unable to remove version:"+((RestException)t).getHttpStatusText());
190
                                }
191
                                else
192
                                        GSS.get().displayError("System error removing version:"+t.getMessage());
193
                        }
194
                };
195
                DeferredCommand.addCommand(df);
196

    
197
        }
198

    
199
        void restoreVersion(final FileResource version) {
200
                FileResource selectedFile = (FileResource) GSS.get().getCurrentSelection();
201
                PostCommand ep = new PostCommand(selectedFile.getUri()+"?restoreVersion="+version.getVersion(),"",200){
202

    
203

    
204
                        @Override
205
                        public void onComplete() {
206
                                container.hide();
207
                GSS.get().getTreeView().refreshCurrentNode(false);
208
                        }
209

    
210
                        @Override
211
                        public void onError(Throwable t) {
212
                                GWT.log("", t);
213
                                if(t instanceof RestException)
214
                                        GSS.get().displayError("Unable to restore version:"+((RestException)t).getHttpStatusText());
215
                                else
216
                                        GSS.get().displayError("System error restoring version:"+t.getMessage());
217
                        }
218

    
219
                };
220
                DeferredCommand.addCommand(ep);
221
        }
222

    
223
        private String formatDate(Date date){
224
                DateTimeFormat format = DateTimeFormat.getFormat("dd/MM/yyyy : HH:mm");
225
                return format.format(date);
226
        }
227
        
228
        /**
229
         * Copies the input List to a new List
230
         * @param input
231
         */
232
        private void copyListAndContinue(List<FileResource> input){
233
                List<FileResource> copiedInput = new ArrayList<FileResource>();                
234
                for(FileResource dto : input) {
235
                        copiedInput.add(dto);
236
                }
237
                handleFullNames(copiedInput);
238
        }
239
        
240
        /**
241
         * Examines whether or not the user's full name exists in the 
242
         * userFullNameMap in the GSS.java for every element of the input list.
243
         * If the user's full name does not exist in the map then a request is being made
244
         * for the specific username.  
245
         * 
246
         * @param input
247
         */
248
        private void handleFullNames(List<FileResource> input){                
249
                if(input.isEmpty()){
250
                        showVersionsTable();
251
                        return;
252
                }
253
                
254
                if(GSS.get().findUserFullName(input.get(0).getOwner()) == null){
255
                        findFullNameAndUpdate(input);                
256
                        return;
257
                }
258
                                
259
                if(input.size() >= 1){
260
                        input.remove(input.get(0));
261
                        if(input.isEmpty()){
262
                                showVersionsTable();                        
263
                        }else{
264
                                handleFullNames(input);
265
                        }
266
                }                                        
267
        }
268
        
269
        /**
270
         * Makes a request to search for full name from a given username
271
         * and continues checking the next element of the List.
272
         *  
273
         * @param input
274
         */
275

    
276
        private void findFullNameAndUpdate(final List<FileResource> input){                                
277
                final String aUserName = input.get(0).getOwner();
278
                String path = GSS.get().getApiPath() + "users/" + aUserName; 
279

    
280
                GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(UserSearchResource.class, path, false,null) {
281
                        @Override
282
                        public void onComplete() {
283
                                final UserSearchResource result = getResult();
284
                                for (UserResource user : result.getUsers()){
285
                                        String username = user.getUsername();
286
                                        String userFullName = user.getName();
287
                                        GSS.get().putUserToMap(username, userFullName);
288
                                        if(input.size() >= 1){
289
                                                input.remove(input.get(0));                                                
290
                                                if(input.isEmpty()){
291
                                                        showVersionsTable();
292
                                                        return;
293
                                                }
294
                                                handleFullNames(input);                                                                                
295
                                        }                                                                        
296
                                }
297
                        }
298
                        @Override
299
                        public void onError(Throwable t) {                                
300
                                GSS.get().displayError("Unable to fetch user's full name from the given username " + aUserName);
301
                                if(input.size() >= 1){
302
                                        input.remove(input.get(0));
303
                                        handleFullNames(input);                                        
304
                                }
305
                        }
306
                };
307
                DeferredCommand.addCommand(gg);
308
        
309
        }
310

    
311
}