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.commands;
37 import com.google.gwt.core.client.Scheduler;
38 import gr.grnet.pithos.web.client.GSS;
39 import gr.grnet.pithos.web.client.foldertree.File;
40 import gr.grnet.pithos.web.client.foldertree.Folder;
41 import gr.grnet.pithos.web.client.foldertree.Resource;
42 import gr.grnet.pithos.web.client.rest.MultiplePostCommand;
43 import gr.grnet.pithos.web.client.rest.PostCommand;
44 import gr.grnet.pithos.web.client.rest.PostRequest;
45 import gr.grnet.pithos.web.client.rest.PutRequest;
46 import gr.grnet.pithos.web.client.rest.RestException;
47 import gr.grnet.pithos.web.client.rest.resource.FileResource;
48 import gr.grnet.pithos.web.client.rest.resource.FolderResource;
49 import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
51 import java.util.ArrayList;
52 import java.util.Iterator;
53 import java.util.List;
56 import com.google.gwt.core.client.GWT;
57 import com.google.gwt.user.client.Command;
58 import com.google.gwt.user.client.DeferredCommand;
59 import com.google.gwt.user.client.ui.PopupPanel;
63 * Move file or folder to trash.
67 public class ToTrashCommand implements Command{
68 private PopupPanel containerPanel;
70 private Object resource;
72 public ToTrashCommand(GSS _app, PopupPanel _containerPanel, Object _resource){
73 containerPanel = _containerPanel;
79 public void execute() {
80 containerPanel.hide();
81 if (resource instanceof List) {
82 Iterator<File> iter = ((List<File>) resource).iterator();
83 trashFiles(iter, new Command() {
85 public void execute() {
86 app.get().updateFolder(((List<File>) resource).get(0).getParent());
90 else if (resource instanceof Folder) {
91 final Folder toBeTrashed = (Folder) resource;
92 trashFolder(toBeTrashed, new Command() {
94 public void execute() {
95 app.updateFolder(toBeTrashed.getParent());
102 private void trashFolder(final Folder f, final Command callback) {
103 String path = app.getApiPath() + app.getUsername() + f.getUri() + "?update=";
104 PostRequest trashFolder = new PostRequest(path) {
106 public void onSuccess(Resource result) {
107 Iterator<File> iter = f.getFiles().iterator();
108 trashFiles(iter, new Command() {
110 public void execute() {
111 Iterator<Folder> iterf = f.getSubfolders().iterator();
112 trashSubfolders(iterf, new Command() {
114 public void execute() {
123 public void onError(Throwable t) {
125 if (t instanceof RestException) {
126 app.displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
129 app.displayError("System error creating folder:" + t.getMessage());
132 trashFolder.setHeader("X-Auth-Token", app.getToken());
133 trashFolder.setHeader("X-Object-Meta-Trash", "true");
134 Scheduler.get().scheduleDeferred(trashFolder);
137 private void trashFiles(final Iterator<File> iter, final Command callback) {
138 if (iter.hasNext()) {
139 File file = iter.next();
140 String path = app.getApiPath() + app.getUsername() + file.getUri() + "?update=";
141 PostRequest trashFile = new PostRequest(path) {
143 public void onSuccess(Resource result) {
144 trashFiles(iter, callback);
148 public void onError(Throwable t) {
150 if (t instanceof RestException) {
151 GSS.get().displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
154 GSS.get().displayError("System error unable to copy file: "+t.getMessage());
157 trashFile.setHeader("X-Auth-Token", app.getToken());
158 trashFile.setHeader("X-Object-Meta-Trash", "true");
159 Scheduler.get().scheduleDeferred(trashFile);
161 else if (callback != null) {
166 private void trashSubfolders(final Iterator<Folder> iter, final Command callback) {
167 if (iter.hasNext()) {
168 final Folder f = iter.next();
169 trashFolder(f, callback);
171 else if (callback != null) {