Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.6 kB)

1 a57faaf0 Christos Stathis
/*
2 cae2a8db Christos Stathis
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
3 63366925 Christos Stathis
 *
4 63366925 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 63366925 Christos Stathis
 * without modification, are permitted provided that the following
6 63366925 Christos Stathis
 * conditions are met:
7 63366925 Christos Stathis
 *
8 63366925 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 63366925 Christos Stathis
 *      disclaimer.
11 63366925 Christos Stathis
 *
12 63366925 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 63366925 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 63366925 Christos Stathis
 *      provided with the distribution.
16 63366925 Christos Stathis
 *
17 63366925 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 63366925 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 63366925 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 63366925 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 63366925 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 63366925 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 63366925 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 63366925 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 63366925 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 63366925 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 63366925 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 63366925 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 63366925 Christos Stathis
 *
30 63366925 Christos Stathis
 * The views and conclusions contained in the software and
31 63366925 Christos Stathis
 * documentation are those of the authors and should not be
32 63366925 Christos Stathis
 * interpreted as representing official policies, either expressed
33 63366925 Christos Stathis
 * or implied, of GRNET S.A.
34 a57faaf0 Christos Stathis
 */
35 a57faaf0 Christos Stathis
package gr.grnet.pithos.web.client;
36 a57faaf0 Christos Stathis
37 9326b841 Christos Stathis
import gr.grnet.pithos.web.client.FileVersionsDialog.Images;
38 611de529 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.File;
39 611de529 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Version;
40 611de529 Christos Stathis
import gr.grnet.pithos.web.client.rest.PostRequest;
41 611de529 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
42 611de529 Christos Stathis
43 611de529 Christos Stathis
import java.util.List;
44 611de529 Christos Stathis
45 611de529 Christos Stathis
import com.google.gwt.core.client.Scheduler;
46 611de529 Christos Stathis
import com.google.gwt.event.dom.client.ClickEvent;
47 611de529 Christos Stathis
import com.google.gwt.event.dom.client.ClickHandler;
48 611de529 Christos Stathis
import com.google.gwt.http.client.Response;
49 a2411896 Christos Stathis
import com.google.gwt.http.client.URL;
50 611de529 Christos Stathis
import com.google.gwt.i18n.client.DateTimeFormat;
51 611de529 Christos Stathis
import com.google.gwt.user.client.Window;
52 611de529 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
53 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.Composite;
54 2efd2a81 Christos Stathis
import com.google.gwt.user.client.ui.CustomScrollPanel;
55 611de529 Christos Stathis
import com.google.gwt.user.client.ui.FlexTable;
56 611de529 Christos Stathis
import com.google.gwt.user.client.ui.HTML;
57 611de529 Christos Stathis
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
58 a57faaf0 Christos Stathis
59 a57faaf0 Christos Stathis
public class VersionsList extends Composite {
60 a57faaf0 Christos Stathis
61 611de529 Christos Stathis
    Pithos app;
62 611de529 Christos Stathis
63 611de529 Christos Stathis
    File file;
64 611de529 Christos Stathis
    
65 611de529 Christos Stathis
    private List<Version> versions = null;
66 611de529 Christos Stathis
    
67 611de529 Christos Stathis
    private Images images;
68 611de529 Christos Stathis
    
69 2efd2a81 Christos Stathis
    private CustomScrollPanel permPanel = new CustomScrollPanel();
70 611de529 Christos Stathis
    
71 611de529 Christos Stathis
    private FlexTable permTable = new FlexTable();
72 611de529 Christos Stathis
73 9326b841 Christos Stathis
    FileVersionsDialog container;
74 611de529 Christos Stathis
75 9326b841 Christos Stathis
        public VersionsList(Pithos _app, FileVersionsDialog aContainer, final Images theImages, File _file, List<Version> theVersions) {
76 611de529 Christos Stathis
        app = _app;
77 611de529 Christos Stathis
                images = theImages;
78 611de529 Christos Stathis
                container = aContainer;
79 611de529 Christos Stathis
                file = _file;
80 611de529 Christos Stathis
                versions = theVersions;
81 611de529 Christos Stathis
                permTable.setText(0, 0, "Version");
82 611de529 Christos Stathis
                permTable.setText(0, 1, "Date");
83 611de529 Christos Stathis
                permTable.setText(0, 2, "");
84 611de529 Christos Stathis
                permTable.setText(0, 3, "");
85 611de529 Christos Stathis
                permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
86 611de529 Christos Stathis
                permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
87 611de529 Christos Stathis
                permTable.getFlexCellFormatter().setColSpan(0, 1, 2);
88 611de529 Christos Stathis
                permTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
89 611de529 Christos Stathis
                permTable.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
90 611de529 Christos Stathis
                permPanel.add(permTable);
91 2efd2a81 Christos Stathis
                permPanel.addStyleName("pithos-versionList");
92 2efd2a81 Christos Stathis
                permPanel.setSize("260px", "150px");
93 611de529 Christos Stathis
                permTable.addStyleName("pithos-permList");
94 611de529 Christos Stathis
                initWidget(permPanel);
95 611de529 Christos Stathis
                showVersionsTable();
96 611de529 Christos Stathis
        }
97 611de529 Christos Stathis
98 611de529 Christos Stathis
        public void showVersionsTable(){
99 611de529 Christos Stathis
                DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
100 611de529 Christos Stathis
                int i = 1;
101 611de529 Christos Stathis
                for (final Version v : versions) {
102 611de529 Christos Stathis
                        HTML restoreVersion = new HTML("<a href='#' class='hidden-link info'><span>" + AbstractImagePrototype.create(images.restore()).getHTML() + "</span><div>Restore this Version</div></a>");
103 611de529 Christos Stathis
                        restoreVersion.addClickHandler(new ClickHandler() {
104 611de529 Christos Stathis
                                @Override
105 ebead1b5 Christos Stathis
                                public void onClick(ClickEvent event) {
106 611de529 Christos Stathis
                                        restoreVersion(v.getVersion());
107 611de529 Christos Stathis
                                }
108 611de529 Christos Stathis
                        });
109 611de529 Christos Stathis
110 611de529 Christos Stathis
                        permTable.setHTML(i, 0, "<span>" + v.getVersion() + "</span>");
111 7228fda0 Christos Stathis
                        if (v.getDate() != null)
112 7228fda0 Christos Stathis
                                permTable.setHTML(i, 1, "<span>" + formatter.format(v.getDate()) + "</span>");
113 7228fda0 Christos Stathis
                        else
114 7228fda0 Christos Stathis
                                permTable.setHTML(i, 1, "<span></span>");
115 611de529 Christos Stathis
                        HTML downloadHtml = new HTML("<a class='hidden-link info' href='#'><span>" + AbstractImagePrototype.create(images.download()).getHTML()+"</span><div>View this Version</div></a>");
116 611de529 Christos Stathis
                        downloadHtml.addClickHandler(new ClickHandler() {
117 611de529 Christos Stathis
                                @Override
118 ebead1b5 Christos Stathis
                                public void onClick(ClickEvent event) {
119 cde22209 Christos KK Loverdos
                                        String fileUrl = app.getApiPath() + file.getOwnerID() + file.getUri() + "?version=" + v.getVersion();
120 611de529 Christos Stathis
                                        Window.open(fileUrl, "_BLANK", "");
121 611de529 Christos Stathis
                                }
122 611de529 Christos Stathis
                        });
123 611de529 Christos Stathis
                        permTable.setWidget(i, 2, downloadHtml);
124 611de529 Christos Stathis
                        permTable.setWidget(i, 3, restoreVersion);
125 611de529 Christos Stathis
                        permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
126 611de529 Christos Stathis
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 0, HasHorizontalAlignment.ALIGN_CENTER);
127 611de529 Christos Stathis
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
128 611de529 Christos Stathis
                        permTable.getFlexCellFormatter().setColSpan(i, 1, 2);
129 611de529 Christos Stathis
                        i++;
130 611de529 Christos Stathis
                }
131 611de529 Christos Stathis
        }
132 611de529 Christos Stathis
133 611de529 Christos Stathis
        void restoreVersion(int version) {
134 611de529 Christos Stathis
                String path = file.getUri() + "?update=";
135 dc8cc18a Christos KK Loverdos
                PostRequest restoreVersion = new PostRequest(app.getApiPath(), file.getOwnerID(), path) {
136 611de529 Christos Stathis
                        
137 611de529 Christos Stathis
                        @Override
138 ebead1b5 Christos Stathis
                        public void onSuccess(Resource result) {
139 611de529 Christos Stathis
                                container.hide();
140 611de529 Christos Stathis
                        }
141 611de529 Christos Stathis
                        
142 611de529 Christos Stathis
                        @Override
143 611de529 Christos Stathis
                        public void onError(Throwable t) {
144 3f8622d4 Christos Stathis
                                app.setError(t);
145 611de529 Christos Stathis
                if (t instanceof RestException) {
146 611de529 Christos Stathis
                        if (((RestException) t).getHttpStatusCode() == Response.SC_NO_CONTENT)
147 611de529 Christos Stathis
                                onSuccess(null);
148 611de529 Christos Stathis
                        else
149 611de529 Christos Stathis
                                app.displayError("Unable to restore version: " + ((RestException) t).getHttpStatusText());
150 611de529 Christos Stathis
                }
151 611de529 Christos Stathis
                else
152 611de529 Christos Stathis
                    app.displayError("System error unable to restore versions: "+t.getMessage());
153 611de529 Christos Stathis
                        }
154 9539e23d Christos Stathis
155 9539e23d Christos Stathis
                        @Override
156 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
157 9539e23d Christos Stathis
                                app.sessionExpired();
158 9539e23d Christos Stathis
                        }
159 611de529 Christos Stathis
                };
160 cc0120ab Christos KK Loverdos
                restoreVersion.setHeader("X-Auth-Token", app.getUserToken());
161 a2411896 Christos Stathis
                restoreVersion.setHeader("X-Source-Object", URL.encodePathSegment(file.getUri()));
162 611de529 Christos Stathis
                restoreVersion.setHeader("X-Source-Version", String.valueOf(version));
163 611de529 Christos Stathis
                restoreVersion.setHeader("Content-Range", "bytes 0-/*");
164 611de529 Christos Stathis
                Scheduler.get().scheduleDeferred(restoreVersion);
165 611de529 Christos Stathis
        }
166 a57faaf0 Christos Stathis
}