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.grnet.pithos.web.client;
21 import gr.grnet.pithos.web.client.FilePropertiesDialog.Images;
22 import gr.grnet.pithos.web.client.rest.DeleteCommand;
23 import gr.grnet.pithos.web.client.rest.GetCommand;
24 import gr.grnet.pithos.web.client.rest.PostCommand;
25 import gr.grnet.pithos.web.client.rest.RestCommand;
26 import gr.grnet.pithos.web.client.rest.RestException;
27 import gr.grnet.pithos.web.client.rest.resource.FileResource;
28 import gr.grnet.pithos.web.client.rest.resource.UserResource;
29 import gr.grnet.pithos.web.client.rest.resource.UserSearchResource;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.Comparator;
34 import java.util.Date;
35 import java.util.List;
38 import com.google.gwt.core.client.GWT;
39 import com.google.gwt.event.dom.client.ClickEvent;
40 import com.google.gwt.event.dom.client.ClickHandler;
41 import com.google.gwt.http.client.URL;
42 import com.google.gwt.i18n.client.DateTimeFormat;
43 import com.google.gwt.user.client.DeferredCommand;
44 import com.google.gwt.user.client.Window;
45 import com.google.gwt.user.client.ui.AbstractImagePrototype;
46 import com.google.gwt.user.client.ui.Composite;
47 import com.google.gwt.user.client.ui.FlexTable;
48 import com.google.gwt.user.client.ui.HTML;
49 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
50 import com.google.gwt.user.client.ui.VerticalPanel;
55 public class VersionsList extends Composite {
59 int permissionCount = -1;
61 List<FileResource> versions = null;
65 final VerticalPanel permPanel = new VerticalPanel();
67 final FlexTable permTable = new FlexTable();
69 FileResource toRemove = null;
71 FilePropertiesDialog container;
73 public VersionsList(FilePropertiesDialog aContainer, final Images theImages, List<FileResource> theVersions) {
75 container = aContainer;
76 versions = theVersions;
77 Collections.sort(theVersions, new Comparator<FileResource>(){
80 public int compare(FileResource o1, FileResource o2) {
81 return o1.getVersion().compareTo(o2.getVersion());
85 permTable.setText(0, 0, "Version");
86 permTable.setText(0, 1, "Created");
87 permTable.setText(0, 2, "Modified");
88 permTable.setText(0, 3, "Size");
89 permTable.setText(0, 4, "");
90 permTable.setText(0, 5, "");
91 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
92 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
93 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
94 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
95 permTable.getFlexCellFormatter().setColSpan(0, 1, 2);
96 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
97 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
98 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
99 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_CENTER);
100 permPanel.add(permTable);
101 permPanel.addStyleName("pithos-TabPanelBottom");
102 permTable.addStyleName("pithos-permList");
103 initWidget(permPanel);
107 public void updateTable() {
108 copyListAndContinue(versions);
111 public void showVersionsTable(){
113 if (toRemove != null) {
114 versions.remove(toRemove);
117 for (final FileResource dto : versions) {
118 HTML restoreVersion = new HTML("<a href='#' class='hidden-link info'><span>"+AbstractImagePrototype.create(images.restore()).getHTML()+"</span><div>Restore this Version</div></a>");
119 restoreVersion.addClickHandler(new ClickHandler() {
121 public void onClick(ClickEvent event) {
126 permTable.setHTML(i, 0, "<span>" + dto.getVersion() + "</span>");
127 permTable.setHTML(i, 1, "<span>" + formatDate(dto.getCreationDate()) + " by " + GSS.get().findUserFullName(dto.getCreatedBy()) + "</span>");
128 permTable.setHTML(i, 2, "<span>" + formatDate(dto.getModificationDate()) + " by " + GSS.get().findUserFullName(dto.getModifiedBy()) + "</span>");
129 permTable.setHTML(i, 3, "<span>" + dto.getFileSizeAsString() + "</span>");
130 HTML downloadHtml = new HTML("<a class='hidden-link info' href='#'><span>"+AbstractImagePrototype.create(images.download()).getHTML()+"</span><div>View this Version</div></a>");
131 downloadHtml.addClickHandler(new ClickHandler() {
133 public void onClick(ClickEvent event) {
135 String dateString = RestCommand.getDate();
136 String resource = dto.getUri().substring(app.getApiPath().length()-1, dto.getUri().length());
137 String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken()));
138 String fileUrl = dto.getUri() + "?version=" + dto.getVersion() + "&Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString);
139 Window.open(fileUrl, "_BLANK", "");
142 permTable.setWidget(i, 4, downloadHtml);
143 permTable.setWidget(i, 5, restoreVersion);
144 permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
145 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 0, HasHorizontalAlignment.ALIGN_CENTER);
146 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
147 permTable.getFlexCellFormatter().setColSpan(i, 1, 2);
148 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
149 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
152 for (; i < permTable.getRowCount(); i++)
153 permTable.removeRow(i);
156 void removeVersion(final FileResource version) {
157 DeleteCommand df = new DeleteCommand(version.getUri()){
160 public void onComplete() {
163 GSS.get().getTreeView().refreshCurrentNode(false);
167 public void onError(Throwable t) {
169 if(t instanceof RestException){
170 int statusCode = ((RestException)t).getHttpStatusCode();
171 if(statusCode == 405)
172 GSS.get().displayError("You don't have the necessary permissions");
173 else if(statusCode == 404)
174 GSS.get().displayError("Versions does not exist");
176 GSS.get().displayError("Unable to remove version:"+((RestException)t).getHttpStatusText());
179 GSS.get().displayError("System error removing version:"+t.getMessage());
182 DeferredCommand.addCommand(df);
186 void restoreVersion(final FileResource version) {
187 FileResource selectedFile = (FileResource) GSS.get().getCurrentSelection();
188 PostCommand ep = new PostCommand(selectedFile.getUri()+"?restoreVersion="+version.getVersion(),"",200){
192 public void onComplete() {
194 GSS.get().getTreeView().refreshCurrentNode(false);
198 public void onError(Throwable t) {
200 if(t instanceof RestException)
201 GSS.get().displayError("Unable to restore version:"+((RestException)t).getHttpStatusText());
203 GSS.get().displayError("System error restoring version:"+t.getMessage());
207 DeferredCommand.addCommand(ep);
210 private String formatDate(Date date){
211 DateTimeFormat format = DateTimeFormat.getFormat("dd/MM/yyyy : HH:mm");
212 return format.format(date);
216 * Copies the input List to a new List
219 private void copyListAndContinue(List<FileResource> input){
220 List<FileResource> copiedInput = new ArrayList<FileResource>();
221 for(FileResource dto : input) {
222 copiedInput.add(dto);
224 handleFullNames(copiedInput);
228 * Examines whether or not the user's full name exists in the
229 * userFullNameMap in the GSS.java for every element of the input list.
230 * If the user's full name does not exist in the map then a request is being made
231 * for the specific username.
235 private void handleFullNames(List<FileResource> input){
241 if(GSS.get().findUserFullName(input.get(0).getOwner()) == null){
242 findFullNameAndUpdate(input);
246 if(input.size() >= 1){
247 input.remove(input.get(0));
251 handleFullNames(input);
257 * Makes a request to search for full name from a given username
258 * and continues checking the next element of the List.
263 private void findFullNameAndUpdate(final List<FileResource> input){
264 final String aUserName = input.get(0).getOwner();
265 String path = GSS.get().getApiPath() + "users/" + aUserName;
267 GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(UserSearchResource.class, path, false,null) {
269 public void onComplete() {
270 final UserSearchResource result = getResult();
271 for (UserResource user : result.getUsers()){
272 String username = user.getUsername();
273 String userFullName = user.getName();
274 GSS.get().putUserToMap(username, userFullName);
275 if(input.size() >= 1){
276 input.remove(input.get(0));
281 handleFullNames(input);
286 public void onError(Throwable t) {
287 GSS.get().displayError("Unable to fetch user's full name from the given username " + aUserName);
288 if(input.size() >= 1){
289 input.remove(input.get(0));
290 handleFullNames(input);
294 DeferredCommand.addCommand(gg);