Revision 81918908 src/gr/grnet/pithos/web/client/commands/ToTrashCommand.java

b/src/gr/grnet/pithos/web/client/commands/ToTrashCommand.java
34 34
 */
35 35
package gr.grnet.pithos.web.client.commands;
36 36

  
37
import com.google.gwt.core.client.Scheduler;
37 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;
38 42
import gr.grnet.pithos.web.client.rest.MultiplePostCommand;
39 43
import gr.grnet.pithos.web.client.rest.PostCommand;
44
import gr.grnet.pithos.web.client.rest.PostRequest;
40 45
import gr.grnet.pithos.web.client.rest.RestException;
41 46
import gr.grnet.pithos.web.client.rest.resource.FileResource;
42 47
import gr.grnet.pithos.web.client.rest.resource.FolderResource;
43 48
import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
44 49

  
45 50
import java.util.ArrayList;
51
import java.util.Iterator;
46 52
import java.util.List;
47 53

  
48 54

  
......
59 65
 */
60 66
public class ToTrashCommand implements Command{
61 67
	private PopupPanel containerPanel;
68
    private GSS app;
69
    private Object resource;
62 70

  
63
	public ToTrashCommand(PopupPanel _containerPanel){
71
	public ToTrashCommand(GSS _app, PopupPanel _containerPanel, Object _resource){
64 72
		containerPanel = _containerPanel;
73
        app = _app;
74
        resource = _resource;
65 75
	}
66 76

  
67 77
	@Override
68 78
	public void execute() {
69 79
		containerPanel.hide();
70
		Object selection = GSS.get().getCurrentSelection();
71
		if (selection == null)
72
			return;
73
		GWT.log("selection: " + selection.toString(), null);
74
		if (selection instanceof RestResourceWrapper) {
75
			FolderResource fdto = ((RestResourceWrapper) selection).getResource();
76
			PostCommand tot = new PostCommand(fdto.getUri()+"?trash=","",200){
80
        if (resource instanceof List) {
81
            Iterator<File> iter = ((List<File>) resource).iterator();
82
            deleteFiles(iter);
83
        }
84
        else if (resource instanceof Folder) {
77 85

  
78
				@Override
79
				public void onComplete() {
80
					//TODO:CELLTREE
81
					/*
82
					TreeItem folder = GSS.get().getFolders().getCurrent();
83
					if(folder.getParentItem() != null){
84
						GSS.get().getFolders().select(folder.getParentItem());
85
						GSS.get().getFolders().updateFolder((DnDTreeItem) folder.getParentItem());
86
					}
87
					GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
88
					*/
89
					FolderResource fres = ((RestResourceWrapper) GSS.get().getTreeView().getSelection()).getResource();
90
					GSS.get().getTreeView().updateNodeChildrenForRemove(fres.getParentURI());
91
					GSS.get().getTreeView().clearSelection();
92
					//GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getTrash());
93
					GSS.get().getTreeView().updateTrashNode();
94
					GSS.get().showFileList(true);
95
				}
96

  
97
				@Override
98
				public void onError(Throwable t) {
99
					GWT.log("", t);
100
					if(t instanceof RestException){
101
						int statusCode = ((RestException)t).getHttpStatusCode();
102
						if(statusCode == 405)
103
							GSS.get().displayError("You don't have the necessary permissions");
104
						else if(statusCode == 404)
105
							GSS.get().displayError("Folder does not exist");
106
						else
107
							GSS.get().displayError("Unable to trash folder:"+((RestException)t).getHttpStatusText());
108
					}
109
					else
110
						GSS.get().displayError("System error trashing folder:"+t.getMessage());
111
				}
112
			};
113
			DeferredCommand.addCommand(tot);
114
		} else if (selection instanceof FileResource) {
115
			FileResource fdto = (FileResource) selection;
116
			PostCommand tot = new PostCommand(fdto.getUri()+"?trash=","",200){
117

  
118
				@Override
119
				public void onComplete() {
120
					GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
121
				}
122

  
123
				@Override
124
				public void onError(Throwable t) {
125
					GWT.log("", t);
126
					if(t instanceof RestException){
127
						int statusCode = ((RestException)t).getHttpStatusCode();
128
						if(statusCode == 405)
129
							GSS.get().displayError("You don't have the necessary permissions");
130
						else if(statusCode == 404)
131
							GSS.get().displayError("File does not exist");
132
						else
133
							GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
134
					}
135
					else
136
						GSS.get().displayError("System error trashing file:"+t.getMessage());
137
				}
138
			};
139
			DeferredCommand.addCommand(tot);
140

  
141
		}
142
		else if (selection instanceof List) {
143
			List<FileResource> fdtos = (List<FileResource>) selection;
144
			final List<String> fileIds = new ArrayList<String>();
145
			for(FileResource f : fdtos)
146
				fileIds.add(f.getUri()+"?trash=");
147
			MultiplePostCommand tot = new MultiplePostCommand(fileIds.toArray(new String[0]),200){
148

  
149
				@Override
150
				public void onComplete() {
151
					GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
152
				}
153

  
154
				@Override
155
				public void onError(String p, Throwable t) {
156
					GWT.log("", t);
157
					if(t instanceof RestException){
158
						int statusCode = ((RestException)t).getHttpStatusCode();
159
						if(statusCode == 405)
160
							GSS.get().displayError("You don't have the necessary permissions");
161
						else if(statusCode == 404)
162
							GSS.get().displayError("File does not exist");
163
						else
164
							GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
165
					}
166
					else
167
						GSS.get().displayError("System error trashing file:"+t.getMessage());
168
				}
169
			};
170
			DeferredCommand.addCommand(tot);
171
		}
86
        }
172 87
	}
173 88

  
89
    private void deleteFiles(final Iterator<File> iter) {
90
        if (iter.hasNext()) {
91
            File file = iter.next();
92
            String path = app.getApiPath() + app.getUsername() + file.getUri() + "?update=";
93
            PostRequest trashFile = new PostRequest(path) {
94
                @Override
95
                public void onSuccess(Resource result) {
96
                    deleteFiles(iter);
97
                }
98

  
99
                @Override
100
                public void onError(Throwable t) {
101
                    GWT.log("", t);
102
                    if (t instanceof RestException) {
103
                        GSS.get().displayError("Unable to move file to trash: " + ((RestException) t).getHttpStatusText());
104
                    }
105
                    else
106
                        GSS.get().displayError("System error unable to move file to trash: "+t.getMessage());
107
                }
108
            };
109
            trashFile.setHeader("X-Auth-Token", app.getToken());
110
            trashFile.setHeader("X-Object-Meta-Trash", "true");
111
            Scheduler.get().scheduleDeferred(trashFile);
112
        }
113
        else {
114
            app.get().updateFolder(((List<File>) resource).get(0).getParent());
115
        }
116
    }
174 117
}

Also available in: Unified diff