Remove the redundant gss top-level directory.
[pithos] / src / gr / ebs / gss / 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.ebs.gss.client;
20
21 import gr.ebs.gss.client.FilePropertiesDialog.Images;
22 import gr.ebs.gss.client.rest.DeleteCommand;
23 import gr.ebs.gss.client.rest.PostCommand;
24 import gr.ebs.gss.client.rest.RestCommand;
25 import gr.ebs.gss.client.rest.RestException;
26 import gr.ebs.gss.client.rest.resource.FileResource;
27
28 import java.util.Collections;
29 import java.util.Comparator;
30 import java.util.Date;
31 import java.util.List;
32
33 import com.google.gwt.core.client.GWT;
34 import com.google.gwt.http.client.URL;
35 import com.google.gwt.i18n.client.DateTimeFormat;
36 import com.google.gwt.user.client.DeferredCommand;
37 import com.google.gwt.user.client.Window;
38 import com.google.gwt.user.client.ui.ClickListener;
39 import com.google.gwt.user.client.ui.Composite;
40 import com.google.gwt.user.client.ui.FlexTable;
41 import com.google.gwt.user.client.ui.HTML;
42 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
43 import com.google.gwt.user.client.ui.VerticalPanel;
44 import com.google.gwt.user.client.ui.Widget;
45
46 /**
47  * @author kman
48  */
49 public class VersionsList extends Composite {
50
51         int selectedRow = -1;
52
53         int permissionCount = -1;
54
55         List<FileResource> versions = null;
56
57         final Images images;
58
59         final VerticalPanel permPanel = new VerticalPanel();
60
61         final FlexTable permTable = new FlexTable();
62
63         FileResource toRemove = null;
64
65         FilePropertiesDialog container;
66
67         public VersionsList(FilePropertiesDialog aContainer, final Images theImages, List<FileResource> theVersions) {
68                 images = theImages;
69                 container = aContainer;
70                 versions = theVersions;
71                 Collections.sort(theVersions, new Comparator<FileResource>(){
72
73                         public int compare(FileResource o1, FileResource o2) {
74                                 return o1.getVersion().compareTo(o2.getVersion());
75                         }
76
77                 });
78                 permTable.setText(0, 0, "Version");
79                 permTable.setText(0, 1, "Created");
80                 permTable.setText(0, 2, "Modified");
81                 permTable.setText(0, 3, "Size");
82                 permTable.setText(0, 4, "");
83                 permTable.setText(0, 5, "");
84                 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
85                 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
86                 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
87                 permTable.getFlexCellFormatter().setStyleName(0, 3, "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                 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
92                 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_CENTER);
93                 permPanel.add(permTable);
94                 permPanel.addStyleName("gwt-TabPanelBottom");
95                 permTable.addStyleName("gss-permList");
96                 initWidget(permPanel);
97                 updateTable();
98         }
99
100         public void updateTable() {
101                 int i = 1;
102                 if (toRemove != null) {
103                         versions.remove(toRemove);
104                         toRemove = null;
105                 }
106                 for (final FileResource dto : versions) {
107                         HTML restoreVersion = new HTML("<a href='#' class='hidden-link info'><span>"+images.restore().getHTML()+"</span><div>Restore this Version</div></a>");
108                         restoreVersion.addClickListener( new ClickListener() {
109
110                                 public void onClick(Widget sender) {
111                                         restoreVersion(dto);
112                                 }
113                         });
114
115                         permTable.setHTML(i, 0, "<span>" + dto.getVersion() + "</span>");
116                         permTable.setHTML(i, 1, "<span>" + formatDate(dto.getCreationDate()) + "</span>");
117                         permTable.setHTML(i, 2, "<span>" + formatDate(dto.getModificationDate()) + "</span>");
118                         permTable.setHTML(i, 3, "<span>" + dto.getFileSizeAsString() + "</span>");
119                         HTML downloadHtml = new HTML("<a class='hidden-link info' href='#'><span>"+images.download().getHTML()+"</span><div>View this Version</div></a>");
120                         downloadHtml.addClickListener(new ClickListener(){
121
122                                 public void onClick(Widget arg0) {
123                                         GSS app = GSS.get();
124                                         String dateString = RestCommand.getDate();
125                                         String resource = dto.getUri().substring(app.getApiPath().length()-1, dto.getUri().length());
126                                         String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken()));
127                                         String fileUrl = dto.getUri() + "&Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString);
128                                         Window.open(fileUrl, "_BLANK", "");
129
130                                 }
131                         });
132                         GWT.log("images:"+images.download().getHTML(), null);
133                         permTable.setWidget(i, 4, downloadHtml);
134                         permTable.setWidget(i, 5, restoreVersion);
135                         permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
136                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 0, HasHorizontalAlignment.ALIGN_CENTER);
137                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
138                         permTable.getFlexCellFormatter().setColSpan(i, 1, 2);
139                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
140                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
141                         i++;
142                 }
143                 for (; i < permTable.getRowCount(); i++)
144                         permTable.removeRow(i);
145         }
146
147
148         void removeVersion(final FileResource version) {
149                 DeleteCommand df = new DeleteCommand(version.getUri()){
150
151                         @Override
152                         public void onComplete() {
153                                 toRemove = version;
154                                 updateTable();
155                                 GSS.get().getFileList().updateFileCache(false, true /*clear selection*/);
156                         }
157
158                         @Override
159                         public void onError(Throwable t) {
160                                 GWT.log("", t);
161                                 if(t instanceof RestException){
162                                         int statusCode = ((RestException)t).getHttpStatusCode();
163                                         if(statusCode == 405)
164                                                 GSS.get().displayError("You don't have the necessary permissions");
165                                         else if(statusCode == 404)
166                                                 GSS.get().displayError("Versions does not exist");
167                                         else
168                                                 GSS.get().displayError("Unable to remove version:"+((RestException)t).getHttpStatusText());
169                                 }
170                                 else
171                                         GSS.get().displayError("System error removing version:"+t.getMessage());
172                         }
173                 };
174                 DeferredCommand.addCommand(df);
175
176         }
177
178         void restoreVersion(final FileResource version) {
179                 FileResource selectedFile = (FileResource) GSS.get().getCurrentSelection();
180                 PostCommand ep = new PostCommand(selectedFile.getUri()+"?restoreVersion="+version.getVersion(),"",200){
181
182
183                         @Override
184                         public void onComplete() {
185                                 container.hide();
186                 GSS.get().getFileList().updateFileCache(true, true /*clear selection*/);
187                         }
188
189                         @Override
190                         public void onError(Throwable t) {
191                                 GWT.log("", t);
192                                 if(t instanceof RestException)
193                                         GSS.get().displayError("Unable to restore version:"+((RestException)t).getHttpStatusText());
194                                 else
195                                         GSS.get().displayError("System error restoring version:"+t.getMessage());
196                         }
197
198                 };
199                 DeferredCommand.addCommand(ep);
200         }
201
202         private String formatDate(Date date){
203                 DateTimeFormat format = DateTimeFormat.getFormat("dd/MM/yyyy : HH:mm");
204                 return format.format(date);
205         }
206
207 }