2 * Copyright 2008, 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
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.
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.
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/>.
19 package gr.ebs.gss.client;
21 import gr.ebs.gss.client.FilePropertiesDialog.Images;
22 import gr.ebs.gss.client.commands.GetUserCommand;
23 import gr.ebs.gss.client.rest.DeleteCommand;
24 import gr.ebs.gss.client.rest.PostCommand;
25 import gr.ebs.gss.client.rest.RestCommand;
26 import gr.ebs.gss.client.rest.RestException;
27 import gr.ebs.gss.client.rest.resource.FileResource;
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.Date;
32 import java.util.List;
34 import com.google.gwt.core.client.GWT;
35 import com.google.gwt.event.dom.client.ClickEvent;
36 import com.google.gwt.event.dom.client.ClickHandler;
37 import com.google.gwt.http.client.URL;
38 import com.google.gwt.i18n.client.DateTimeFormat;
39 import com.google.gwt.user.client.DeferredCommand;
40 import com.google.gwt.user.client.Window;
41 import com.google.gwt.user.client.ui.AbstractImagePrototype;
42 import com.google.gwt.user.client.ui.Composite;
43 import com.google.gwt.user.client.ui.FlexTable;
44 import com.google.gwt.user.client.ui.HTML;
45 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
46 import com.google.gwt.user.client.ui.VerticalPanel;
51 public class VersionsList extends Composite {
55 int permissionCount = -1;
57 List<FileResource> versions = null;
61 final VerticalPanel permPanel = new VerticalPanel();
63 final FlexTable permTable = new FlexTable();
65 FileResource toRemove = null;
67 FilePropertiesDialog container;
69 public VersionsList(FilePropertiesDialog aContainer, final Images theImages, List<FileResource> theVersions) {
71 container = aContainer;
72 versions = theVersions;
73 Collections.sort(theVersions, new Comparator<FileResource>(){
76 public int compare(FileResource o1, FileResource o2) {
77 return o1.getVersion().compareTo(o2.getVersion());
81 permTable.setText(0, 0, "Version");
82 permTable.setText(0, 1, "Created");
83 permTable.setText(0, 2, "Modified");
84 permTable.setText(0, 3, "Size");
85 permTable.setText(0, 4, "");
86 permTable.setText(0, 5, "");
87 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
88 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
89 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
90 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
91 permTable.getFlexCellFormatter().setColSpan(0, 1, 2);
92 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
93 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
94 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
95 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_CENTER);
96 permPanel.add(permTable);
97 permPanel.addStyleName("gss-TabPanelBottom");
98 permTable.addStyleName("gss-permList");
99 initWidget(permPanel);
103 public void updateTable() {
105 if (toRemove != null) {
106 versions.remove(toRemove);
109 for (final FileResource dto : versions) {
110 HTML restoreVersion = new HTML("<a href='#' class='hidden-link info'><span>"+AbstractImagePrototype.create(images.restore()).getHTML()+"</span><div>Restore this Version</div></a>");
111 restoreVersion.addClickHandler(new ClickHandler() {
113 public void onClick(ClickEvent event) {
118 permTable.setHTML(i, 0, "<span>" + dto.getVersion() + "</span>");
120 if(GSS.get().findUserFullName(dto.getCreatedBy()) == null){
121 GetUserCommand guc = new GetUserCommand(dto.getCreatedBy());
123 GSS.get().putUserToMap(dto.getCreatedBy(), dto.getCreatedBy());
126 if(GSS.get().findUserFullName(dto.getModifiedBy()) == null){
127 GetUserCommand guc = new GetUserCommand(dto.getModifiedBy());
129 GSS.get().putUserToMap(dto.getModifiedBy(), dto.getModifiedBy());
131 permTable.setHTML(i, 1, "<span>" + formatDate(dto.getCreationDate()) + " by " + GSS.get().findUserFullName(dto.getCreatedBy()) + "</span>");
132 permTable.setHTML(i, 2, "<span>" + formatDate(dto.getModificationDate()) + " by " + GSS.get().findUserFullName(dto.getModifiedBy()) + "</span>");
133 permTable.setHTML(i, 3, "<span>" + dto.getFileSizeAsString() + "</span>");
134 HTML downloadHtml = new HTML("<a class='hidden-link info' href='#'><span>"+AbstractImagePrototype.create(images.download()).getHTML()+"</span><div>View this Version</div></a>");
135 downloadHtml.addClickHandler(new ClickHandler() {
137 public void onClick(ClickEvent event) {
139 String dateString = RestCommand.getDate();
140 String resource = dto.getUri().substring(app.getApiPath().length()-1, dto.getUri().length());
141 String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken()));
142 String fileUrl = dto.getUri() + "?version=" + dto.getVersion() + "&Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString);
143 Window.open(fileUrl, "_BLANK", "");
146 permTable.setWidget(i, 4, downloadHtml);
147 permTable.setWidget(i, 5, restoreVersion);
148 permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
149 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 0, HasHorizontalAlignment.ALIGN_CENTER);
150 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
151 permTable.getFlexCellFormatter().setColSpan(i, 1, 2);
152 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
153 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
156 for (; i < permTable.getRowCount(); i++)
157 permTable.removeRow(i);
161 void removeVersion(final FileResource version) {
162 DeleteCommand df = new DeleteCommand(version.getUri()){
165 public void onComplete() {
168 GSS.get().getFileList().updateFileCache(false, true /*clear selection*/);
172 public void onError(Throwable t) {
174 if(t instanceof RestException){
175 int statusCode = ((RestException)t).getHttpStatusCode();
176 if(statusCode == 405)
177 GSS.get().displayError("You don't have the necessary permissions");
178 else if(statusCode == 404)
179 GSS.get().displayError("Versions does not exist");
181 GSS.get().displayError("Unable to remove version:"+((RestException)t).getHttpStatusText());
184 GSS.get().displayError("System error removing version:"+t.getMessage());
187 DeferredCommand.addCommand(df);
191 void restoreVersion(final FileResource version) {
192 FileResource selectedFile = (FileResource) GSS.get().getCurrentSelection();
193 PostCommand ep = new PostCommand(selectedFile.getUri()+"?restoreVersion="+version.getVersion(),"",200){
197 public void onComplete() {
199 GSS.get().getFileList().updateFileCache(true, true /*clear selection*/);
203 public void onError(Throwable t) {
205 if(t instanceof RestException)
206 GSS.get().displayError("Unable to restore version:"+((RestException)t).getHttpStatusText());
208 GSS.get().displayError("System error restoring version:"+t.getMessage());
212 DeferredCommand.addCommand(ep);
215 private String formatDate(Date date){
216 DateTimeFormat format = DateTimeFormat.getFormat("dd/MM/yyyy : HH:mm");
217 return format.format(date);