/*
- * Copyright (c) 2011 Greek Research and Technology Network
+ * Copyright 2011 GRNET S.A. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and
+ * documentation are those of the authors and should not be
+ * interpreted as representing official policies, either expressed
+ * or implied, of GRNET S.A.
*/
package gr.grnet.pithos.web.client.commands;
-import gr.grnet.pithos.web.client.GSS;
-import gr.grnet.pithos.web.client.rest.MultiplePostCommand;
-import gr.grnet.pithos.web.client.rest.PostCommand;
+import gr.grnet.pithos.web.client.Pithos;
+import gr.grnet.pithos.web.client.foldertree.File;
+import gr.grnet.pithos.web.client.foldertree.Folder;
+import gr.grnet.pithos.web.client.foldertree.Resource;
+import gr.grnet.pithos.web.client.rest.DeleteRequest;
+import gr.grnet.pithos.web.client.rest.GetRequest;
+import gr.grnet.pithos.web.client.rest.PutRequest;
import gr.grnet.pithos.web.client.rest.RestException;
-import gr.grnet.pithos.web.client.rest.resource.FileResource;
-import gr.grnet.pithos.web.client.rest.resource.FolderResource;
-import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
-import java.util.ArrayList;
+import java.util.Iterator;
import java.util.List;
-
import com.google.gwt.core.client.GWT;
+import com.google.gwt.core.client.Scheduler;
+import com.google.gwt.http.client.Response;
+import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Command;
-import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.PopupPanel;
/**
*/
public class ToTrashCommand implements Command{
private PopupPanel containerPanel;
+ protected Pithos app;
+ protected Object resource;
- public ToTrashCommand(PopupPanel _containerPanel){
+ public ToTrashCommand(Pithos _app, PopupPanel _containerPanel, Object _resource){
containerPanel = _containerPanel;
+ app = _app;
+ resource = _resource;
}
@Override
public void execute() {
- containerPanel.hide();
- Object selection = GSS.get().getCurrentSelection();
- if (selection == null)
- return;
- GWT.log("selection: " + selection.toString(), null);
- if (selection instanceof RestResourceWrapper) {
- FolderResource fdto = ((RestResourceWrapper) selection).getResource();
- PostCommand tot = new PostCommand(fdto.getUri()+"?trash=","",200){
-
+ if (containerPanel != null)
+ containerPanel.hide();
+ if (resource instanceof List) {
+ @SuppressWarnings("unchecked")
+ Iterator<File> iter = ((List<File>) resource).iterator();
+ trashFiles(iter, new Command() {
+ @SuppressWarnings("unchecked")
@Override
- public void onComplete() {
- //TODO:CELLTREE
- /*
- TreeItem folder = GSS.get().getFolders().getCurrent();
- if(folder.getParentItem() != null){
- GSS.get().getFolders().select(folder.getParentItem());
- GSS.get().getFolders().updateFolder((DnDTreeItem) folder.getParentItem());
- }
- GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
- */
- FolderResource fres = ((RestResourceWrapper) GSS.get().getTreeView().getSelection()).getResource();
- GSS.get().getTreeView().updateNodeChildrenForRemove(fres.getParentURI());
- GSS.get().getTreeView().clearSelection();
- //GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getTrash());
- GSS.get().getTreeView().updateTrashNode();
- GSS.get().showFileList(true);
- }
+ public void execute() {
+ app.updateFolder(((List<File>) resource).get(0).getParent(), true, null);
+ }
+ });
+ }
+ else if (resource instanceof Folder) {
+ final Folder toBeTrashed = (Folder) resource;
+ trashFolder(toBeTrashed, new Command() {
+ @Override
+ public void execute() {
+ app.updateFolder(toBeTrashed.getParent(), true, null);
+ }
+ });
- @Override
- public void onError(Throwable t) {
- GWT.log("", t);
- if(t instanceof RestException){
- int statusCode = ((RestException)t).getHttpStatusCode();
- if(statusCode == 405)
- GSS.get().displayError("You don't have the necessary permissions");
- else if(statusCode == 404)
- GSS.get().displayError("Folder does not exist");
- else
- GSS.get().displayError("Unable to trash folder:"+((RestException)t).getHttpStatusText());
+ }
+ }
+
+ private void trashFolder(final Folder f, final Command callback) {
+ String path = "/" + Pithos.TRASH_CONTAINER + "/" + f.getPrefix();
+ PutRequest createFolder = new PutRequest(app.getApiPath(), app.getUsername(), path) {
+ @Override
+ public void onSuccess(@SuppressWarnings("unused") Resource result) {
+ GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix()), f) {
+
+ @Override
+ public void onSuccess(final Folder _f) {
+ Iterator<File> iter = _f.getFiles().iterator();
+ trashFiles(iter, new Command() {
+ @Override
+ public void execute() {
+ Iterator<Folder> iterf = _f.getSubfolders().iterator();
+ trashSubfolders(iterf, new Command() {
+
+ @Override
+ public void execute() {
+ DeleteRequest deleteFolder = new DeleteRequest(app.getApiPath(), _f.getOwner(), _f.getUri()) {
+
+ @Override
+ public void onSuccess(@SuppressWarnings("unused") Resource _result) {
+ if (callback != null)
+ callback.execute();
+ }
+
+ @Override
+ public void onError(Throwable t) {
+ GWT.log("", t);
+ if (t instanceof RestException) {
+ if (((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND)
+ onSuccess(null);
+ else
+ app.displayError("Unable to delete folder: " + ((RestException) t).getHttpStatusText());
+ }
+ else
+ app.displayError("System error unable to delete folder: "+t.getMessage());
+ }
+
+ @Override
+ protected void onUnauthorized(Response response) {
+ app.sessionExpired();
+ }
+ };
+ deleteFolder.setHeader("X-Auth-Token", app.getToken());
+ Scheduler.get().scheduleDeferred(deleteFolder);
+ }
+ });
+ }
+ });
}
- else
- GSS.get().displayError("System error trashing folder:"+t.getMessage());
- }
- };
- DeferredCommand.addCommand(tot);
- } else if (selection instanceof FileResource) {
- FileResource fdto = (FileResource) selection;
- PostCommand tot = new PostCommand(fdto.getUri()+"?trash=","",200){
- @Override
- public void onComplete() {
- GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
- }
+ @Override
+ public void onError(Throwable t) {
+ GWT.log("", t);
+ if (t instanceof RestException) {
+ app.displayError("Unable to get folder: " + ((RestException) t).getHttpStatusText());
+ }
+ else
+ app.displayError("System error getting folder: " + t.getMessage());
+ }
- @Override
- public void onError(Throwable t) {
- GWT.log("", t);
- if(t instanceof RestException){
- int statusCode = ((RestException)t).getHttpStatusCode();
- if(statusCode == 405)
- GSS.get().displayError("You don't have the necessary permissions");
- else if(statusCode == 404)
- GSS.get().displayError("File does not exist");
- else
- GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
+ @Override
+ protected void onUnauthorized(Response response) {
+ app.sessionExpired();
}
- else
- GSS.get().displayError("System error trashing file:"+t.getMessage());
- }
- };
- DeferredCommand.addCommand(tot);
+ };
+ getFolder.setHeader("X-Auth-Token", app.getToken());
+ Scheduler.get().scheduleDeferred(getFolder);
+ }
+
+ @Override
+ public void onError(Throwable t) {
+ GWT.log("", t);
+ if (t instanceof RestException) {
+ app.displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
+ }
+ else
+ app.displayError("System error creating folder:" + t.getMessage());
+ }
- }
- else if (selection instanceof List) {
- List<FileResource> fdtos = (List<FileResource>) selection;
- final List<String> fileIds = new ArrayList<String>();
- for(FileResource f : fdtos)
- fileIds.add(f.getUri()+"?trash=");
- MultiplePostCommand tot = new MultiplePostCommand(fileIds.toArray(new String[0]),200){
+ @Override
+ protected void onUnauthorized(Response response) {
+ app.sessionExpired();
+ }
+ };
+ createFolder.setHeader("X-Auth-Token", app.getToken());
+ createFolder.setHeader("Accept", "*/*");
+ createFolder.setHeader("Content-Length", "0");
+ createFolder.setHeader("Content-Type", "application/folder");
+ Scheduler.get().scheduleDeferred(createFolder);
+ }
+
+
+
+ protected void trashFiles(final Iterator<File> iter, final Command callback) {
+ if (iter.hasNext()) {
+ File file = iter.next();
+ String path = "/" + Pithos.TRASH_CONTAINER + "/" + file.getPath();
+ PutRequest trashFile = new PutRequest(app.getApiPath(), app.getUsername(), path) {
+ @Override
+ public void onSuccess(@SuppressWarnings("unused") Resource result) {
+ trashFiles(iter, callback);
+ }
+
+ @Override
+ public void onError(Throwable t) {
+ GWT.log("", t);
+ if (t instanceof RestException) {
+ app.displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
+ }
+ else
+ app.displayError("System error unable to copy file: "+t.getMessage());
+ }
@Override
- public void onComplete() {
- GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
+ protected void onUnauthorized(Response response) {
+ app.sessionExpired();
}
+ };
+ trashFile.setHeader("X-Auth-Token", app.getToken());
+ trashFile.setHeader("X-Move-From", file.getUri());
+ Scheduler.get().scheduleDeferred(trashFile);
+ }
+ else if (callback != null) {
+ callback.execute();
+ }
+ }
+ protected void trashSubfolders(final Iterator<Folder> iter, final Command callback) {
+ if (iter.hasNext()) {
+ final Folder f = iter.next();
+ trashFolder(f, new Command() {
+
@Override
- public void onError(String p, Throwable t) {
- GWT.log("", t);
- if(t instanceof RestException){
- int statusCode = ((RestException)t).getHttpStatusCode();
- if(statusCode == 405)
- GSS.get().displayError("You don't have the necessary permissions");
- else if(statusCode == 404)
- GSS.get().displayError("File does not exist");
- else
- GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
- }
- else
- GSS.get().displayError("System error trashing file:"+t.getMessage());
+ public void execute() {
+ trashSubfolders(iter, callback);
}
- };
- DeferredCommand.addCommand(tot);
- }
- }
-
+ });
+ }
+ else {
+ app.updateTrash(false, callback);
+ }
+ }
}