Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / VersionsList.java @ a60ea262

History | View | Annotate | Download (7.7 kB)

1 14ad7326 pastith
/*
2 14ad7326 pastith
 * Copyright 2008, 2009 Electronic Business Systems Ltd.
3 14ad7326 pastith
 *
4 14ad7326 pastith
 * This file is part of GSS.
5 14ad7326 pastith
 *
6 14ad7326 pastith
 * GSS is free software: you can redistribute it and/or modify
7 14ad7326 pastith
 * it under the terms of the GNU General Public License as published by
8 14ad7326 pastith
 * the Free Software Foundation, either version 3 of the License, or
9 14ad7326 pastith
 * (at your option) any later version.
10 14ad7326 pastith
 *
11 14ad7326 pastith
 * GSS is distributed in the hope that it will be useful,
12 14ad7326 pastith
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 14ad7326 pastith
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 14ad7326 pastith
 * GNU General Public License for more details.
15 14ad7326 pastith
 *
16 14ad7326 pastith
 * You should have received a copy of the GNU General Public License
17 14ad7326 pastith
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18 14ad7326 pastith
 */
19 14ad7326 pastith
package gr.ebs.gss.client;
20 14ad7326 pastith
21 14ad7326 pastith
import gr.ebs.gss.client.FilePropertiesDialog.Images;
22 895035a2 pastith
import gr.ebs.gss.client.rest.DeleteCommand;
23 895035a2 pastith
import gr.ebs.gss.client.rest.PostCommand;
24 bbad17b4 pastith
import gr.ebs.gss.client.rest.RestCommand;
25 a52ea5e4 pastith
import gr.ebs.gss.client.rest.RestException;
26 a52ea5e4 pastith
import gr.ebs.gss.client.rest.resource.FileResource;
27 14ad7326 pastith
28 a52ea5e4 pastith
import java.util.Collections;
29 a52ea5e4 pastith
import java.util.Comparator;
30 14ad7326 pastith
import java.util.Date;
31 14ad7326 pastith
import java.util.List;
32 14ad7326 pastith
33 14ad7326 pastith
import com.google.gwt.core.client.GWT;
34 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.dom.client.ClickEvent;
35 afd3a0ef Giannis Koutsoubos
import com.google.gwt.event.dom.client.ClickHandler;
36 515dbddb koutsoub
import com.google.gwt.http.client.URL;
37 14ad7326 pastith
import com.google.gwt.i18n.client.DateTimeFormat;
38 a52ea5e4 pastith
import com.google.gwt.user.client.DeferredCommand;
39 d74adf02 koutsoub
import com.google.gwt.user.client.Window;
40 afd3a0ef Giannis Koutsoubos
import com.google.gwt.user.client.ui.AbstractImagePrototype;
41 14ad7326 pastith
import com.google.gwt.user.client.ui.Composite;
42 14ad7326 pastith
import com.google.gwt.user.client.ui.FlexTable;
43 14ad7326 pastith
import com.google.gwt.user.client.ui.HTML;
44 14ad7326 pastith
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
45 14ad7326 pastith
import com.google.gwt.user.client.ui.VerticalPanel;
46 14ad7326 pastith
47 14ad7326 pastith
/**
48 14ad7326 pastith
 * @author kman
49 14ad7326 pastith
 */
50 14ad7326 pastith
public class VersionsList extends Composite {
51 14ad7326 pastith
52 14ad7326 pastith
        int selectedRow = -1;
53 14ad7326 pastith
54 14ad7326 pastith
        int permissionCount = -1;
55 14ad7326 pastith
56 a52ea5e4 pastith
        List<FileResource> versions = null;
57 14ad7326 pastith
58 14ad7326 pastith
        final Images images;
59 14ad7326 pastith
60 14ad7326 pastith
        final VerticalPanel permPanel = new VerticalPanel();
61 14ad7326 pastith
62 14ad7326 pastith
        final FlexTable permTable = new FlexTable();
63 14ad7326 pastith
64 a52ea5e4 pastith
        FileResource toRemove = null;
65 14ad7326 pastith
66 14ad7326 pastith
        FilePropertiesDialog container;
67 14ad7326 pastith
68 0e4865ee pastith
        public VersionsList(FilePropertiesDialog aContainer, final Images theImages, List<FileResource> theVersions) {
69 0e4865ee pastith
                images = theImages;
70 0e4865ee pastith
                container = aContainer;
71 0e4865ee pastith
                versions = theVersions;
72 0e4865ee pastith
                Collections.sort(theVersions, new Comparator<FileResource>(){
73 a52ea5e4 pastith
74 023f6f1e Panagiotis Astithas
                        @Override
75 a52ea5e4 pastith
                        public int compare(FileResource o1, FileResource o2) {
76 a52ea5e4 pastith
                                return o1.getVersion().compareTo(o2.getVersion());
77 a52ea5e4 pastith
                        }
78 a52ea5e4 pastith
79 a52ea5e4 pastith
                });
80 14ad7326 pastith
                permTable.setText(0, 0, "Version");
81 14ad7326 pastith
                permTable.setText(0, 1, "Created");
82 14ad7326 pastith
                permTable.setText(0, 2, "Modified");
83 14ad7326 pastith
                permTable.setText(0, 3, "Size");
84 14ad7326 pastith
                permTable.setText(0, 4, "");
85 14ad7326 pastith
                permTable.setText(0, 5, "");
86 14ad7326 pastith
                permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
87 14ad7326 pastith
                permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
88 14ad7326 pastith
                permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
89 14ad7326 pastith
                permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
90 14ad7326 pastith
                permTable.getFlexCellFormatter().setColSpan(0, 1, 2);
91 14ad7326 pastith
                permTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
92 14ad7326 pastith
                permTable.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
93 14ad7326 pastith
                permTable.getFlexCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
94 14ad7326 pastith
                permTable.getFlexCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_CENTER);
95 14ad7326 pastith
                permPanel.add(permTable);
96 5c6b2883 Panagiotis Astithas
                permPanel.addStyleName("gss-TabPanelBottom");
97 14ad7326 pastith
                permTable.addStyleName("gss-permList");
98 14ad7326 pastith
                initWidget(permPanel);
99 14ad7326 pastith
                updateTable();
100 14ad7326 pastith
        }
101 14ad7326 pastith
102 14ad7326 pastith
        public void updateTable() {
103 14ad7326 pastith
                int i = 1;
104 14ad7326 pastith
                if (toRemove != null) {
105 14ad7326 pastith
                        versions.remove(toRemove);
106 14ad7326 pastith
                        toRemove = null;
107 14ad7326 pastith
                }
108 a52ea5e4 pastith
                for (final FileResource dto : versions) {
109 afd3a0ef Giannis Koutsoubos
                        HTML restoreVersion = new HTML("<a href='#' class='hidden-link info'><span>"+AbstractImagePrototype.create(images.restore()).getHTML()+"</span><div>Restore this Version</div></a>");
110 d1aaf483 Panagiotis Astithas
                        restoreVersion.addClickHandler(new ClickHandler() {
111 afd3a0ef Giannis Koutsoubos
                                @Override
112 afd3a0ef Giannis Koutsoubos
                                public void onClick(ClickEvent event) {
113 14ad7326 pastith
                                        restoreVersion(dto);
114 14ad7326 pastith
                                }
115 14ad7326 pastith
                        });
116 14ad7326 pastith
117 14ad7326 pastith
                        permTable.setHTML(i, 0, "<span>" + dto.getVersion() + "</span>");
118 4bf05cad Natasa Kapravelou
                        permTable.setHTML(i, 1, "<span>" + formatDate(dto.getCreationDate()) + " by " + GSS.get().getUserFullName(dto.getCreatedBy()) + "</span>");
119 4bf05cad Natasa Kapravelou
                        permTable.setHTML(i, 2, "<span>" + formatDate(dto.getModificationDate()) + " by " + GSS.get().getUserFullName(dto.getModifiedBy()) + "</span>");
120 14ad7326 pastith
                        permTable.setHTML(i, 3, "<span>" + dto.getFileSizeAsString() + "</span>");
121 afd3a0ef Giannis Koutsoubos
                        HTML downloadHtml = new HTML("<a class='hidden-link info' href='#'><span>"+AbstractImagePrototype.create(images.download()).getHTML()+"</span><div>View this Version</div></a>");
122 afd3a0ef Giannis Koutsoubos
                        downloadHtml.addClickHandler(new ClickHandler() {
123 afd3a0ef Giannis Koutsoubos
                                @Override
124 afd3a0ef Giannis Koutsoubos
                                public void onClick(ClickEvent event) {
125 bbad17b4 pastith
                                        GSS app = GSS.get();
126 895035a2 pastith
                                        String dateString = RestCommand.getDate();
127 bbad17b4 pastith
                                        String resource = dto.getUri().substring(app.getApiPath().length()-1, dto.getUri().length());
128 bbad17b4 pastith
                                        String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken()));
129 d1aaf483 Panagiotis Astithas
                                        String fileUrl = dto.getUri() + "?version=" + dto.getVersion() + "&Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString);
130 d74adf02 koutsoub
                                        Window.open(fileUrl, "_BLANK", "");
131 d74adf02 koutsoub
                                }
132 d74adf02 koutsoub
                        });
133 14ad7326 pastith
                        permTable.setWidget(i, 4, downloadHtml);
134 14ad7326 pastith
                        permTable.setWidget(i, 5, restoreVersion);
135 14ad7326 pastith
                        permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
136 14ad7326 pastith
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 0, HasHorizontalAlignment.ALIGN_CENTER);
137 14ad7326 pastith
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
138 14ad7326 pastith
                        permTable.getFlexCellFormatter().setColSpan(i, 1, 2);
139 14ad7326 pastith
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
140 14ad7326 pastith
                        permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
141 14ad7326 pastith
                        i++;
142 14ad7326 pastith
                }
143 14ad7326 pastith
                for (; i < permTable.getRowCount(); i++)
144 14ad7326 pastith
                        permTable.removeRow(i);
145 14ad7326 pastith
        }
146 14ad7326 pastith
147 14ad7326 pastith
148 a52ea5e4 pastith
        void removeVersion(final FileResource version) {
149 895035a2 pastith
                DeleteCommand df = new DeleteCommand(version.getUri()){
150 14ad7326 pastith
151 0e4865ee pastith
                        @Override
152 a52ea5e4 pastith
                        public void onComplete() {
153 14ad7326 pastith
                                toRemove = version;
154 14ad7326 pastith
                                updateTable();
155 18a508ee fstamatelopoulos
                                GSS.get().getFileList().updateFileCache(false, true /*clear selection*/);
156 14ad7326 pastith
                        }
157 14ad7326 pastith
158 0e4865ee pastith
                        @Override
159 a52ea5e4 pastith
                        public void onError(Throwable t) {
160 a52ea5e4 pastith
                                GWT.log("", t);
161 a52ea5e4 pastith
                                if(t instanceof RestException){
162 a52ea5e4 pastith
                                        int statusCode = ((RestException)t).getHttpStatusCode();
163 a52ea5e4 pastith
                                        if(statusCode == 405)
164 a52ea5e4 pastith
                                                GSS.get().displayError("You don't have the necessary permissions");
165 a52ea5e4 pastith
                                        else if(statusCode == 404)
166 a52ea5e4 pastith
                                                GSS.get().displayError("Versions does not exist");
167 a52ea5e4 pastith
                                        else
168 da62cc7e fstamatelopoulos
                                                GSS.get().displayError("Unable to remove version:"+((RestException)t).getHttpStatusText());
169 a52ea5e4 pastith
                                }
170 14ad7326 pastith
                                else
171 a52ea5e4 pastith
                                        GSS.get().displayError("System error removing version:"+t.getMessage());
172 14ad7326 pastith
                        }
173 a52ea5e4 pastith
                };
174 a52ea5e4 pastith
                DeferredCommand.addCommand(df);
175 14ad7326 pastith
176 14ad7326 pastith
        }
177 14ad7326 pastith
178 a52ea5e4 pastith
        void restoreVersion(final FileResource version) {
179 515dbddb koutsoub
                FileResource selectedFile = (FileResource) GSS.get().getCurrentSelection();
180 895035a2 pastith
                PostCommand ep = new PostCommand(selectedFile.getUri()+"?restoreVersion="+version.getVersion(),"",200){
181 14ad7326 pastith
182 14ad7326 pastith
183 0e4865ee pastith
                        @Override
184 515dbddb koutsoub
                        public void onComplete() {
185 515dbddb koutsoub
                                container.hide();
186 18a508ee fstamatelopoulos
                GSS.get().getFileList().updateFileCache(true, true /*clear selection*/);
187 515dbddb koutsoub
                        }
188 515dbddb koutsoub
189 0e4865ee pastith
                        @Override
190 515dbddb koutsoub
                        public void onError(Throwable t) {
191 515dbddb koutsoub
                                GWT.log("", t);
192 515dbddb koutsoub
                                if(t instanceof RestException)
193 e08c358f koutsoub
                                        GSS.get().displayError("Unable to restore version:"+((RestException)t).getHttpStatusText());
194 515dbddb koutsoub
                                else
195 515dbddb koutsoub
                                        GSS.get().displayError("System error restoring version:"+t.getMessage());
196 515dbddb koutsoub
                        }
197 515dbddb koutsoub
198 515dbddb koutsoub
                };
199 515dbddb koutsoub
                DeferredCommand.addCommand(ep);
200 14ad7326 pastith
        }
201 14ad7326 pastith
202 14ad7326 pastith
        private String formatDate(Date date){
203 14ad7326 pastith
                DateTimeFormat format = DateTimeFormat.getFormat("dd/MM/yyyy : HH:mm");
204 14ad7326 pastith
                return format.format(date);
205 14ad7326 pastith
        }
206 14ad7326 pastith
207 14ad7326 pastith
}