*/
package gr.grnet.pithos.web.client.commands;
+import com.google.gwt.core.client.Scheduler;
import gr.grnet.pithos.web.client.GSS;
+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.MultiplePostCommand;
import gr.grnet.pithos.web.client.rest.PostCommand;
+import gr.grnet.pithos.web.client.rest.PostRequest;
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;
*/
public class ToTrashCommand implements Command{
private PopupPanel containerPanel;
+ private GSS app;
+ private Object resource;
- public ToTrashCommand(PopupPanel _containerPanel){
+ public ToTrashCommand(GSS _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 (resource instanceof List) {
+ Iterator<File> iter = ((List<File>) resource).iterator();
+ deleteFiles(iter);
+ }
+ else if (resource instanceof Folder) {
- @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);
- }
-
- @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());
- }
- 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){
- 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());
- }
- };
- DeferredCommand.addCommand(tot);
-
- }
- 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
- public void onComplete() {
- GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
- }
-
- @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());
- }
- };
- DeferredCommand.addCommand(tot);
- }
+ }
}
+ private void deleteFiles(final Iterator<File> iter) {
+ if (iter.hasNext()) {
+ File file = iter.next();
+ String path = app.getApiPath() + app.getUsername() + file.getUri() + "?update=";
+ PostRequest trashFile = new PostRequest(path) {
+ @Override
+ public void onSuccess(Resource result) {
+ deleteFiles(iter);
+ }
+
+ @Override
+ public void onError(Throwable t) {
+ GWT.log("", t);
+ if (t instanceof RestException) {
+ GSS.get().displayError("Unable to move file to trash: " + ((RestException) t).getHttpStatusText());
+ }
+ else
+ GSS.get().displayError("System error unable to move file to trash: "+t.getMessage());
+ }
+ };
+ trashFile.setHeader("X-Auth-Token", app.getToken());
+ trashFile.setHeader("X-Object-Meta-Trash", "true");
+ Scheduler.get().scheduleDeferred(trashFile);
+ }
+ else {
+ app.get().updateFolder(((List<File>) resource).get(0).getParent());
+ }
+ }
}
--- /dev/null
+/*
+ * 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.rest;
+
+import com.google.gwt.core.client.Scheduler.ScheduledCommand;
+import com.google.gwt.http.client.Request;
+import com.google.gwt.http.client.RequestBuilder;
+import com.google.gwt.http.client.RequestException;
+import com.google.gwt.http.client.Response;
+import gr.grnet.pithos.web.client.foldertree.Resource;
+import java.util.HashMap;
+import java.util.Map;
+
+public abstract class PostRequest implements ScheduledCommand {
+
+ private String path;
+
+ private Map<String, String> headers = new HashMap<String, String>();
+
+ public abstract void onSuccess(Resource result);
+
+ public abstract void onError(Throwable t);
+
+ public PostRequest(String path) {
+ this.path = path;
+ }
+
+ @Override
+ public void execute() {
+ RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, path);
+ for (String header : headers.keySet()) {
+ builder.setHeader(header, headers.get(header));
+ }
+ try {
+ builder.sendRequest("", new RestRequestCallback(path, Response.SC_ACCEPTED) {
+ @Override
+ public void onSuccess(Resource object) {
+ PostRequest.this.onSuccess(object);
+ }
+
+ @Override
+ public Resource deserialize(Response response) {
+ return Resource.createFromResponse(Resource.class, response, null);
+ }
+
+ @Override
+ public void onError(Request request, Throwable throwable) {
+ PostRequest.this.onError(throwable);
+ }
+ });
+ }
+ catch (RequestException e) {
+ }
+ }
+
+ public void setHeader(String header, String value) {
+ headers.put(header, value);
+ }
+}