Renamed all packages to gr.grnet.pithos.*
[pithos-web-client] / src / gr / grnet / pithos / web / client / VersionsList.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.grnet.pithos.web.client;
20
21 import gr.grnet.pithos.web.client.FilePropertiesDialog.Images;
22 import gr.grnet.pithos.web.client.rest.DeleteCommand;
23 import gr.grnet.pithos.web.client.rest.GetCommand;
24 import gr.grnet.pithos.web.client.rest.PostCommand;
25 import gr.grnet.pithos.web.client.rest.RestCommand;
26 import gr.grnet.pithos.web.client.rest.RestException;
27 import gr.grnet.pithos.web.client.rest.resource.FileResource;
28 import gr.grnet.pithos.web.client.rest.resource.UserResource;
29 import gr.grnet.pithos.web.client.rest.resource.UserSearchResource;
30
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.Comparator;
34 import java.util.Date;
35 import java.util.List;
36
37
38 import com.google.gwt.core.client.GWT;
39 import com.google.gwt.event.dom.client.ClickEvent;
40 import com.google.gwt.event.dom.client.ClickHandler;
41 import com.google.gwt.http.client.URL;
42 import com.google.gwt.i18n.client.DateTimeFormat;
43 import com.google.gwt.user.client.DeferredCommand;
44 import com.google.gwt.user.client.Window;
45 import com.google.gwt.user.client.ui.AbstractImagePrototype;
46 import com.google.gwt.user.client.ui.Composite;
47 import com.google.gwt.user.client.ui.FlexTable;
48 import com.google.gwt.user.client.ui.HTML;
49 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
50 import com.google.gwt.user.client.ui.VerticalPanel;
51
52 /**
53  * @author kman
54  */
55 public class VersionsList extends Composite {
56
57         int selectedRow = -1;
58
59         int permissionCount = -1;
60
61         List<FileResource> versions = null;
62
63         final Images images;
64
65         final VerticalPanel permPanel = new VerticalPanel();
66
67         final FlexTable permTable = new FlexTable();
68
69         FileResource toRemove = null;
70
71         FilePropertiesDialog container;
72
73         public VersionsList(FilePropertiesDialog aContainer, final Images theImages, List<FileResource> theVersions) {
74                 images = theImages;
75                 container = aContainer;
76                 versions = theVersions;
77                 Collections.sort(theVersions, new Comparator<FileResource>(){
78
79                         @Override
80                         public int compare(FileResource o1, FileResource o2) {
81                                 return o1.getVersion().compareTo(o2.getVersion());
82                         }
83
84                 });
85                 permTable.setText(0, 0, "Version");
86                 permTable.setText(0, 1, "Created");
87                 permTable.setText(0, 2, "Modified");
88                 permTable.setText(0, 3, "Size");
89                 permTable.setText(0, 4, "");
90                 permTable.setText(0, 5, "");
91                 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
92                 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
93                 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
94                 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
95                 permTable.getFlexCellFormatter().setColSpan(0, 1, 2);
96                 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
97                 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
98                 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
99                 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_CENTER);
100                 permPanel.add(permTable);
101                 permPanel.addStyleName("pithos-TabPanelBottom");
102                 permTable.addStyleName("pithos-permList");
103                 initWidget(permPanel);
104                 updateTable();
105         }
106
107         public void updateTable() {
108                 copyListAndContinue(versions);          
109         }
110         
111         public void showVersionsTable(){
112                 int i = 1;
113                 if (toRemove != null) {
114                         versions.remove(toRemove);
115                         toRemove = null;
116                 }
117                 for (final FileResource dto : versions) {
118                         HTML restoreVersion = new HTML("<a href='#' class='hidden-link info'><span>"+AbstractImagePrototype.create(images.restore()).getHTML()+"</span><div>Restore this Version</div></a>");
119                         restoreVersion.addClickHandler(new ClickHandler() {
120                                 @Override
121                                 public void onClick(ClickEvent event) {
122                                         restoreVersion(dto);
123                                 }
124                         });
125
126                         permTable.setHTML(i, 0, "<span>" + dto.getVersion() + "</span>");
127                         permTable.setHTML(i, 1, "<span>" + formatDate(dto.getCreationDate()) + " by " + GSS.get().findUserFullName(dto.getCreatedBy()) + "</span>");
128                         permTable.setHTML(i, 2, "<span>" + formatDate(dto.getModificationDate()) + " by " + GSS.get().findUserFullName(dto.getModifiedBy()) + "</span>");
129                         permTable.setHTML(i, 3, "<span>" + dto.getFileSizeAsString() + "</span>");
130                         HTML downloadHtml = new HTML("<a class='hidden-link info' href='#'><span>"+AbstractImagePrototype.create(images.download()).getHTML()+"</span><div>View this Version</div></a>");
131                         downloadHtml.addClickHandler(new ClickHandler() {
132                                 @Override
133                                 public void onClick(ClickEvent event) {
134                                         GSS app = GSS.get();
135                                         String dateString = RestCommand.getDate();
136                                         String resource = dto.getUri().substring(app.getApiPath().length()-1, dto.getUri().length());
137                                         String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken()));
138                                         String fileUrl = dto.getUri() + "?version=" + dto.getVersion() + "&Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString);
139                                         Window.open(fileUrl, "_BLANK", "");
140                                 }
141                         });
142                         permTable.setWidget(i, 4, downloadHtml);
143                         permTable.setWidget(i, 5, restoreVersion);
144                         permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
145                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 0, HasHorizontalAlignment.ALIGN_CENTER);
146                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
147                         permTable.getFlexCellFormatter().setColSpan(i, 1, 2);
148                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
149                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
150                         i++;
151                 }
152                 for (; i < permTable.getRowCount(); i++)
153                         permTable.removeRow(i);
154         }
155
156         void removeVersion(final FileResource version) {
157                 DeleteCommand df = new DeleteCommand(version.getUri()){
158
159                         @Override
160                         public void onComplete() {
161                                 toRemove = version;
162                                 updateTable();
163                                 GSS.get().getTreeView().refreshCurrentNode(false);
164                         }
165
166                         @Override
167                         public void onError(Throwable t) {
168                                 GWT.log("", t);
169                                 if(t instanceof RestException){
170                                         int statusCode = ((RestException)t).getHttpStatusCode();
171                                         if(statusCode == 405)
172                                                 GSS.get().displayError("You don't have the necessary permissions");
173                                         else if(statusCode == 404)
174                                                 GSS.get().displayError("Versions does not exist");
175                                         else
176                                                 GSS.get().displayError("Unable to remove version:"+((RestException)t).getHttpStatusText());
177                                 }
178                                 else
179                                         GSS.get().displayError("System error removing version:"+t.getMessage());
180                         }
181                 };
182                 DeferredCommand.addCommand(df);
183
184         }
185
186         void restoreVersion(final FileResource version) {
187                 FileResource selectedFile = (FileResource) GSS.get().getCurrentSelection();
188                 PostCommand ep = new PostCommand(selectedFile.getUri()+"?restoreVersion="+version.getVersion(),"",200){
189
190
191                         @Override
192                         public void onComplete() {
193                                 container.hide();
194                 GSS.get().getTreeView().refreshCurrentNode(false);
195                         }
196
197                         @Override
198                         public void onError(Throwable t) {
199                                 GWT.log("", t);
200                                 if(t instanceof RestException)
201                                         GSS.get().displayError("Unable to restore version:"+((RestException)t).getHttpStatusText());
202                                 else
203                                         GSS.get().displayError("System error restoring version:"+t.getMessage());
204                         }
205
206                 };
207                 DeferredCommand.addCommand(ep);
208         }
209
210         private String formatDate(Date date){
211                 DateTimeFormat format = DateTimeFormat.getFormat("dd/MM/yyyy : HH:mm");
212                 return format.format(date);
213         }
214         
215         /**
216          * Copies the input List to a new List
217          * @param input
218          */
219         private void copyListAndContinue(List<FileResource> input){
220                 List<FileResource> copiedInput = new ArrayList<FileResource>();         
221                 for(FileResource dto : input) {
222                         copiedInput.add(dto);
223                 }
224                 handleFullNames(copiedInput);
225         }
226         
227         /**
228          * Examines whether or not the user's full name exists in the 
229          * userFullNameMap in the GSS.java for every element of the input list.
230          * If the user's full name does not exist in the map then a request is being made
231          * for the specific username.  
232          * 
233          * @param input
234          */
235         private void handleFullNames(List<FileResource> input){         
236                 if(input.isEmpty()){
237                         showVersionsTable();
238                         return;
239                 }
240                 
241                 if(GSS.get().findUserFullName(input.get(0).getOwner()) == null){
242                         findFullNameAndUpdate(input);           
243                         return;
244                 }
245                                 
246                 if(input.size() >= 1){
247                         input.remove(input.get(0));
248                         if(input.isEmpty()){
249                                 showVersionsTable();                    
250                         }else{
251                                 handleFullNames(input);
252                         }
253                 }                                       
254         }
255         
256         /**
257          * Makes a request to search for full name from a given username
258          * and continues checking the next element of the List.
259          *  
260          * @param input
261          */
262
263         private void findFullNameAndUpdate(final List<FileResource> input){                             
264                 final String aUserName = input.get(0).getOwner();
265                 String path = GSS.get().getApiPath() + "users/" + aUserName; 
266
267                 GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(UserSearchResource.class, path, false,null) {
268                         @Override
269                         public void onComplete() {
270                                 final UserSearchResource result = getResult();
271                                 for (UserResource user : result.getUsers()){
272                                         String username = user.getUsername();
273                                         String userFullName = user.getName();
274                                         GSS.get().putUserToMap(username, userFullName);
275                                         if(input.size() >= 1){
276                                                 input.remove(input.get(0));                                             
277                                                 if(input.isEmpty()){
278                                                         showVersionsTable();
279                                                         return;
280                                                 }
281                                                 handleFullNames(input);                                                                         
282                                         }                                                                       
283                                 }
284                         }
285                         @Override
286                         public void onError(Throwable t) {                              
287                                 GSS.get().displayError("Unable to fetch user's full name from the given username " + aUserName);
288                                 if(input.size() >= 1){
289                                         input.remove(input.get(0));
290                                         handleFullNames(input);                                 
291                                 }
292                         }
293                 };
294                 DeferredCommand.addCommand(gg);
295         
296         }
297
298 }