Fix view of versioned file
[pithos-web-client] / src / gr / grnet / pithos / web / client / VersionsList.java
1 /*
2  * Copyright 2011-2013 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 com.google.gwt.core.client.Scheduler;
38 import com.google.gwt.event.dom.client.ClickEvent;
39 import com.google.gwt.event.dom.client.ClickHandler;
40 import com.google.gwt.http.client.Response;
41 import com.google.gwt.http.client.URL;
42 import com.google.gwt.i18n.client.DateTimeFormat;
43 import com.google.gwt.user.client.Window;
44 import com.google.gwt.user.client.ui.*;
45 import gr.grnet.pithos.web.client.FileVersionsDialog.Images;
46 import gr.grnet.pithos.web.client.foldertree.File;
47 import gr.grnet.pithos.web.client.foldertree.Version;
48 import gr.grnet.pithos.web.client.rest.PostRequest;
49 import gr.grnet.pithos.web.client.rest.RestException;
50
51 import java.util.List;
52
53 public class VersionsList extends Composite {
54
55     Pithos app;
56
57     File file;
58     
59     private List<Version> versions = null;
60     
61     private Images images;
62     
63     private CustomScrollPanel permPanel = new CustomScrollPanel();
64     
65     private FlexTable permTable = new FlexTable();
66
67     FileVersionsDialog container;
68
69         public VersionsList(Pithos _app, FileVersionsDialog aContainer, final Images theImages, File _file, List<Version> theVersions) {
70         app = _app;
71                 images = theImages;
72                 container = aContainer;
73                 file = _file;
74                 versions = theVersions;
75                 permTable.setText(0, 0, "Version");
76                 permTable.setText(0, 1, "Date");
77                 permTable.setText(0, 2, "");
78                 permTable.setText(0, 3, "");
79                 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
80                 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
81                 permTable.getFlexCellFormatter().setColSpan(0, 1, 2);
82                 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
83                 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
84                 permPanel.add(permTable);
85                 permPanel.addStyleName("pithos-versionList");
86                 permPanel.setSize("260px", "150px");
87                 permTable.addStyleName("pithos-permList");
88                 initWidget(permPanel);
89                 showVersionsTable();
90         }
91
92         public void showVersionsTable(){
93                 DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
94                 int i = 1;
95                 for (final Version v : versions) {
96                         HTML restoreVersion = new HTML("<a href='#' class='hidden-link info'><span>" + AbstractImagePrototype.create(images.restore()).getHTML() + "</span><div>Restore this Version</div></a>");
97                         restoreVersion.addClickHandler(new ClickHandler() {
98                                 @Override
99                                 public void onClick(ClickEvent event) {
100                                         restoreVersion(v.getVersion());
101                                 }
102                         });
103
104                         permTable.setHTML(i, 0, "<span>" + v.getVersion() + "</span>");
105                         if (v.getDate() != null)
106                                 permTable.setHTML(i, 1, "<span>" + formatter.format(v.getDate()) + "</span>");
107                         else
108                                 permTable.setHTML(i, 1, "<span></span>");
109                         HTML downloadHtml = new HTML("<a class='hidden-link info' href='#'><span>" + AbstractImagePrototype.create(images.download()).getHTML()+"</span><div>View this Version</div></a>");
110                         downloadHtml.addClickHandler(new ClickHandler() {
111                                 @Override
112                                 public void onClick(ClickEvent event) {
113                     final String fileUrl = Pithos.getVersionedFileViewURL(file, v.getVersion());
114                                         Window.open(fileUrl, "_BLANK", "");
115                                 }
116                         });
117                         permTable.setWidget(i, 2, downloadHtml);
118                         permTable.setWidget(i, 3, restoreVersion);
119                         permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
120                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 0, HasHorizontalAlignment.ALIGN_CENTER);
121                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
122                         permTable.getFlexCellFormatter().setColSpan(i, 1, 2);
123                         i++;
124                 }
125         }
126
127         void restoreVersion(int version) {
128                 String path = file.getUri() + "?update=";
129                 PostRequest restoreVersion = new PostRequest(Pithos.getStorageAPIURL(), file.getOwnerID(), path) {
130                         
131                         @Override
132                         public void onSuccess(Resource result) {
133                                 container.hide();
134                         }
135                         
136                         @Override
137                         public void onError(Throwable t) {
138                                 app.setError(t);
139                 if (t instanceof RestException) {
140                         if (((RestException) t).getHttpStatusCode() == Response.SC_NO_CONTENT)
141                                 onSuccess(null);
142                         else
143                                 app.displayError("Unable to restore version: " + ((RestException) t).getHttpStatusText());
144                 }
145                 else
146                     app.displayError("System error unable to restore versions: "+t.getMessage());
147                         }
148
149                         @Override
150                         protected void onUnauthorized(Response response) {
151                                 app.sessionExpired();
152                         }
153                 };
154                 restoreVersion.setHeader("X-Auth-Token", app.getUserToken());
155                 restoreVersion.setHeader("X-Source-Object", URL.encodePathSegment(file.getUri()));
156                 restoreVersion.setHeader("X-Source-Version", String.valueOf(version));
157                 restoreVersion.setHeader("Content-Range", "bytes 0-/*");
158                 Scheduler.get().scheduleDeferred(restoreVersion);
159         }
160 }