2 * Copyright 2011 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and
31 * documentation are those of the authors and should not be
32 * interpreted as representing official policies, either expressed
33 * or implied, of GRNET S.A.
35 package gr.grnet.pithos.web.client;
37 import gr.grnet.pithos.web.client.FilePropertiesDialog.Images;
38 import gr.grnet.pithos.web.client.rest.DeleteCommand;
39 import gr.grnet.pithos.web.client.rest.GetCommand;
40 import gr.grnet.pithos.web.client.rest.PostCommand;
41 import gr.grnet.pithos.web.client.rest.RestCommand;
42 import gr.grnet.pithos.web.client.rest.RestException;
43 import gr.grnet.pithos.web.client.rest.resource.FileResource;
44 import gr.grnet.pithos.web.client.rest.resource.UserResource;
45 import gr.grnet.pithos.web.client.rest.resource.UserSearchResource;
47 import java.util.ArrayList;
48 import java.util.Collections;
49 import java.util.Comparator;
50 import java.util.Date;
51 import java.util.List;
54 import com.google.gwt.core.client.GWT;
55 import com.google.gwt.event.dom.client.ClickEvent;
56 import com.google.gwt.event.dom.client.ClickHandler;
57 import com.google.gwt.http.client.URL;
58 import com.google.gwt.i18n.client.DateTimeFormat;
59 import com.google.gwt.user.client.DeferredCommand;
60 import com.google.gwt.user.client.Window;
61 import com.google.gwt.user.client.ui.AbstractImagePrototype;
62 import com.google.gwt.user.client.ui.Composite;
63 import com.google.gwt.user.client.ui.FlexTable;
64 import com.google.gwt.user.client.ui.HTML;
65 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
66 import com.google.gwt.user.client.ui.VerticalPanel;
68 public class VersionsList extends Composite {
72 int permissionCount = -1;
74 List<FileResource> versions = null;
78 final VerticalPanel permPanel = new VerticalPanel();
80 final FlexTable permTable = new FlexTable();
82 FileResource toRemove = null;
84 FilePropertiesDialog container;
86 public VersionsList(FilePropertiesDialog aContainer, final Images theImages, List<FileResource> theVersions) {
88 container = aContainer;
89 versions = theVersions;
90 Collections.sort(theVersions, new Comparator<FileResource>(){
93 public int compare(FileResource o1, FileResource o2) {
94 return o1.getVersion().compareTo(o2.getVersion());
98 permTable.setText(0, 0, "Version");
99 permTable.setText(0, 1, "Created");
100 permTable.setText(0, 2, "Modified");
101 permTable.setText(0, 3, "Size");
102 permTable.setText(0, 4, "");
103 permTable.setText(0, 5, "");
104 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
105 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
106 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
107 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
108 permTable.getFlexCellFormatter().setColSpan(0, 1, 2);
109 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
110 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
111 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
112 permTable.getFlexCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_CENTER);
113 permPanel.add(permTable);
114 permPanel.addStyleName("pithos-TabPanelBottom");
115 permTable.addStyleName("pithos-permList");
116 initWidget(permPanel);
120 public void updateTable() {
121 copyListAndContinue(versions);
124 public void showVersionsTable(){
126 if (toRemove != null) {
127 versions.remove(toRemove);
130 for (final FileResource dto : versions) {
131 HTML restoreVersion = new HTML("<a href='#' class='hidden-link info'><span>"+AbstractImagePrototype.create(images.restore()).getHTML()+"</span><div>Restore this Version</div></a>");
132 restoreVersion.addClickHandler(new ClickHandler() {
134 public void onClick(ClickEvent event) {
139 permTable.setHTML(i, 0, "<span>" + dto.getVersion() + "</span>");
140 permTable.setHTML(i, 1, "<span>" + formatDate(dto.getCreationDate()) + " by " + GSS.get().findUserFullName(dto.getCreatedBy()) + "</span>");
141 permTable.setHTML(i, 2, "<span>" + formatDate(dto.getModificationDate()) + " by " + GSS.get().findUserFullName(dto.getModifiedBy()) + "</span>");
142 permTable.setHTML(i, 3, "<span>" + dto.getFileSizeAsString() + "</span>");
143 HTML downloadHtml = new HTML("<a class='hidden-link info' href='#'><span>"+AbstractImagePrototype.create(images.download()).getHTML()+"</span><div>View this Version</div></a>");
144 downloadHtml.addClickHandler(new ClickHandler() {
146 public void onClick(ClickEvent event) {
148 String dateString = RestCommand.getDate();
149 String resource = dto.getUri().substring(app.getApiPath().length()-1, dto.getUri().length());
150 String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken()));
151 String fileUrl = dto.getUri() + "?version=" + dto.getVersion() + "&Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString);
152 Window.open(fileUrl, "_BLANK", "");
155 permTable.setWidget(i, 4, downloadHtml);
156 permTable.setWidget(i, 5, restoreVersion);
157 permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
158 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 0, HasHorizontalAlignment.ALIGN_CENTER);
159 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
160 permTable.getFlexCellFormatter().setColSpan(i, 1, 2);
161 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
162 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
165 for (; i < permTable.getRowCount(); i++)
166 permTable.removeRow(i);
169 void removeVersion(final FileResource version) {
170 DeleteCommand df = new DeleteCommand(version.getUri()){
173 public void onComplete() {
176 GSS.get().getTreeView().refreshCurrentNode(false);
180 public void onError(Throwable t) {
182 if(t instanceof RestException){
183 int statusCode = ((RestException)t).getHttpStatusCode();
184 if(statusCode == 405)
185 GSS.get().displayError("You don't have the necessary permissions");
186 else if(statusCode == 404)
187 GSS.get().displayError("Versions does not exist");
189 GSS.get().displayError("Unable to remove version:"+((RestException)t).getHttpStatusText());
192 GSS.get().displayError("System error removing version:"+t.getMessage());
195 DeferredCommand.addCommand(df);
199 void restoreVersion(final FileResource version) {
200 FileResource selectedFile = (FileResource) GSS.get().getCurrentSelection();
201 PostCommand ep = new PostCommand(selectedFile.getUri()+"?restoreVersion="+version.getVersion(),"",200){
205 public void onComplete() {
207 GSS.get().getTreeView().refreshCurrentNode(false);
211 public void onError(Throwable t) {
213 if(t instanceof RestException)
214 GSS.get().displayError("Unable to restore version:"+((RestException)t).getHttpStatusText());
216 GSS.get().displayError("System error restoring version:"+t.getMessage());
220 DeferredCommand.addCommand(ep);
223 private String formatDate(Date date){
224 DateTimeFormat format = DateTimeFormat.getFormat("dd/MM/yyyy : HH:mm");
225 return format.format(date);
229 * Copies the input List to a new List
232 private void copyListAndContinue(List<FileResource> input){
233 List<FileResource> copiedInput = new ArrayList<FileResource>();
234 for(FileResource dto : input) {
235 copiedInput.add(dto);
237 handleFullNames(copiedInput);
241 * Examines whether or not the user's full name exists in the
242 * userFullNameMap in the GSS.java for every element of the input list.
243 * If the user's full name does not exist in the map then a request is being made
244 * for the specific username.
248 private void handleFullNames(List<FileResource> input){
254 if(GSS.get().findUserFullName(input.get(0).getOwner()) == null){
255 findFullNameAndUpdate(input);
259 if(input.size() >= 1){
260 input.remove(input.get(0));
264 handleFullNames(input);
270 * Makes a request to search for full name from a given username
271 * and continues checking the next element of the List.
276 private void findFullNameAndUpdate(final List<FileResource> input){
277 final String aUserName = input.get(0).getOwner();
278 String path = GSS.get().getApiPath() + "users/" + aUserName;
280 GetCommand<UserSearchResource> gg = new GetCommand<UserSearchResource>(UserSearchResource.class, path, false,null) {
282 public void onComplete() {
283 final UserSearchResource result = getResult();
284 for (UserResource user : result.getUsers()){
285 String username = user.getUsername();
286 String userFullName = user.getName();
287 GSS.get().putUserToMap(username, userFullName);
288 if(input.size() >= 1){
289 input.remove(input.get(0));
294 handleFullNames(input);
299 public void onError(Throwable t) {
300 GSS.get().displayError("Unable to fetch user's full name from the given username " + aUserName);
301 if(input.size() >= 1){
302 input.remove(input.get(0));
303 handleFullNames(input);
307 DeferredCommand.addCommand(gg);