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