2 * Copyright 2011-2012 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 gr.grnet.pithos.web.client.Pithos;
38 import gr.grnet.pithos.web.client.foldertree.File;
39 import gr.grnet.pithos.web.client.foldertree.Folder;
40 import gr.grnet.pithos.web.client.foldertree.Resource;
41 import gr.grnet.pithos.web.client.rest.DeleteRequest;
42 import gr.grnet.pithos.web.client.rest.GetRequest;
43 import gr.grnet.pithos.web.client.rest.PutRequest;
44 import gr.grnet.pithos.web.client.rest.RestException;
46 import java.util.Iterator;
47 import java.util.List;
49 import com.google.gwt.core.client.GWT;
50 import com.google.gwt.core.client.Scheduler;
51 import com.google.gwt.http.client.Response;
52 import com.google.gwt.http.client.URL;
53 import com.google.gwt.user.client.Command;
54 import com.google.gwt.user.client.ui.PopupPanel;
58 * Move file or folder to trash.
62 public class ToTrashCommand implements Command{
63 private PopupPanel containerPanel;
65 protected Object resource;
67 public ToTrashCommand(Pithos _app, PopupPanel _containerPanel, Object _resource){
68 containerPanel = _containerPanel;
74 public void execute() {
75 if (containerPanel != null)
76 containerPanel.hide();
77 if (resource instanceof List) {
78 @SuppressWarnings("unchecked")
79 Iterator<File> iter = ((List<File>) resource).iterator();
80 trashFiles(iter, new Command() {
81 @SuppressWarnings("unchecked")
83 public void execute() {
84 Folder f = ((List<File>) resource).get(0).getParent();
85 if (app.isMySharedSelected())
86 app.updateSharedFolder(f, true, new Command() {
89 public void execute() {
90 app.updateTrash(false, null);
94 app.updateFolder(f, true, new Command() {
97 public void execute() {
98 app.updateTrash(false, null);
104 else if (resource instanceof Folder) {
105 final Folder toBeTrashed = (Folder) resource;
106 trashFolder(toBeTrashed, new Command() {
108 public void execute() {
109 app.updateFolder(toBeTrashed.getParent(), true, new Command() {
112 public void execute() {
113 app.updateTrash(false, null);
122 private void trashFolder(final Folder f, final Command callback) {
123 String path = "/" + Pithos.TRASH_CONTAINER + "/" + f.getPrefix();
124 PutRequest createFolder = new PutRequest(app.getApiPath(), app.getUsername(), path) {
126 public void onSuccess(Resource result) {
127 GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix()), f) {
130 public void onSuccess(final Folder _f) {
131 Iterator<File> iter = _f.getFiles().iterator();
132 trashFiles(iter, new Command() {
134 public void execute() {
135 Iterator<Folder> iterf = _f.getSubfolders().iterator();
136 trashSubfolders(iterf, new Command() {
139 public void execute() {
140 DeleteRequest deleteFolder = new DeleteRequest(app.getApiPath(), _f.getOwner(), URL.encode(_f.getUri())) {
143 public void onSuccess(Resource _result) {
144 if (callback != null)
149 public void onError(Throwable t) {
152 if (t instanceof RestException) {
153 if (((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND)
156 app.displayError("Unable to delete folder: " + ((RestException) t).getHttpStatusText());
159 app.displayError("System error unable to delete folder: "+t.getMessage());
163 protected void onUnauthorized(Response response) {
164 app.sessionExpired();
167 deleteFolder.setHeader("X-Auth-Token", app.getToken());
168 Scheduler.get().scheduleDeferred(deleteFolder);
176 public void onError(Throwable t) {
179 if (t instanceof RestException) {
180 app.displayError("Unable to get folder: " + ((RestException) t).getHttpStatusText());
183 app.displayError("System error getting folder: " + t.getMessage());
187 protected void onUnauthorized(Response response) {
188 app.sessionExpired();
191 getFolder.setHeader("X-Auth-Token", app.getToken());
192 Scheduler.get().scheduleDeferred(getFolder);
196 public void onError(Throwable t) {
199 if (t instanceof RestException) {
200 app.displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
203 app.displayError("System error creating folder:" + t.getMessage());
207 protected void onUnauthorized(Response response) {
208 app.sessionExpired();
211 createFolder.setHeader("X-Auth-Token", app.getToken());
212 createFolder.setHeader("Accept", "*/*");
213 createFolder.setHeader("Content-Length", "0");
214 createFolder.setHeader("Content-Type", "application/folder");
215 Scheduler.get().scheduleDeferred(createFolder);
220 protected void trashFiles(final Iterator<File> iter, final Command callback) {
221 if (iter.hasNext()) {
222 File file = iter.next();
223 String path = "/" + Pithos.TRASH_CONTAINER + "/" + file.getPath();
224 PutRequest trashFile = new PutRequest(app.getApiPath(), app.getUsername(), path) {
226 public void onSuccess(Resource result) {
227 trashFiles(iter, callback);
231 public void onError(Throwable t) {
234 if (t instanceof RestException) {
235 app.displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
238 app.displayError("System error unable to copy file: "+t.getMessage());
242 protected void onUnauthorized(Response response) {
243 app.sessionExpired();
246 trashFile.setHeader("X-Auth-Token", app.getToken());
247 trashFile.setHeader("X-Move-From", URL.encodePathSegment(file.getUri()));
248 trashFile.setHeader("Content-Type", file.getContentType());
249 Scheduler.get().scheduleDeferred(trashFile);
251 else if (callback != null) {
256 protected void trashSubfolders(final Iterator<Folder> iter, final Command callback) {
257 if (iter.hasNext()) {
258 final Folder f = iter.next();
259 trashFolder(f, new Command() {
262 public void execute() {
263 trashSubfolders(iter, callback);
268 app.updateTrash(false, callback);