root / src / gr / grnet / pithos / web / client / commands / ToTrashCommand.java @ 3586ad5e
History | View | Annotate | Download (9.4 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2011-2012 GRNET S.A. All rights reserved.
|
3 |
*
|
4 |
* Redistribution and use in source and binary forms, with or
|
5 |
* without modification, are permitted provided that the following
|
6 |
* conditions are met:
|
7 |
*
|
8 |
* 1. Redistributions of source code must retain the above
|
9 |
* copyright notice, this list of conditions and the following
|
10 |
* disclaimer.
|
11 |
*
|
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.
|
16 |
*
|
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.
|
29 |
*
|
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.
|
34 |
*/
|
35 |
package gr.grnet.pithos.web.client.commands; |
36 |
|
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; |
45 |
|
46 |
import java.util.Iterator; |
47 |
import java.util.List; |
48 |
|
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; |
55 |
|
56 |
/**
|
57 |
*
|
58 |
* Move file or folder to trash.
|
59 |
*
|
60 |
*
|
61 |
*/
|
62 |
public class ToTrashCommand implements Command{ |
63 |
private PopupPanel containerPanel;
|
64 |
protected Pithos app;
|
65 |
protected Object resource; |
66 |
|
67 |
public ToTrashCommand(Pithos _app, PopupPanel _containerPanel, Object _resource){ |
68 |
containerPanel = _containerPanel; |
69 |
app = _app; |
70 |
resource = _resource; |
71 |
} |
72 |
|
73 |
@Override
|
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") |
82 |
@Override
|
83 |
public void execute() { |
84 |
Folder f = ((List<File>) resource).get(0).getParent(); |
85 |
if (app.isMySharedSelected())
|
86 |
app.updateSharedFolder(f, true, new Command() { |
87 |
|
88 |
@Override
|
89 |
public void execute() { |
90 |
app.updateTrash(false, null); |
91 |
} |
92 |
}); |
93 |
else
|
94 |
app.updateFolder(f, true, new Command() { |
95 |
|
96 |
@Override
|
97 |
public void execute() { |
98 |
app.updateTrash(false, null); |
99 |
} |
100 |
}, true);
|
101 |
} |
102 |
}); |
103 |
} |
104 |
else if (resource instanceof Folder) { |
105 |
final Folder toBeTrashed = (Folder) resource;
|
106 |
trashFolder(toBeTrashed, new Command() {
|
107 |
@Override
|
108 |
public void execute() { |
109 |
app.updateFolder(toBeTrashed.getParent(), true, new Command() { |
110 |
|
111 |
@Override
|
112 |
public void execute() { |
113 |
app.updateTrash(false, null); |
114 |
} |
115 |
}, true);
|
116 |
} |
117 |
}); |
118 |
|
119 |
} |
120 |
} |
121 |
|
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) {
|
125 |
@Override
|
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) { |
128 |
|
129 |
@Override
|
130 |
public void onSuccess(final Folder _f) { |
131 |
Iterator<File> iter = _f.getFiles().iterator(); |
132 |
trashFiles(iter, new Command() {
|
133 |
@Override
|
134 |
public void execute() { |
135 |
Iterator<Folder> iterf = _f.getSubfolders().iterator();
|
136 |
trashSubfolders(iterf, new Command() {
|
137 |
|
138 |
@Override
|
139 |
public void execute() { |
140 |
DeleteRequest deleteFolder = new DeleteRequest(app.getApiPath(), _f.getOwner(), URL.encode(_f.getUri())) { |
141 |
|
142 |
@Override
|
143 |
public void onSuccess(Resource _result) { |
144 |
if (callback != null) |
145 |
callback.execute(); |
146 |
} |
147 |
|
148 |
@Override
|
149 |
public void onError(Throwable t) { |
150 |
GWT.log("", t);
|
151 |
app.setError(t); |
152 |
if (t instanceof RestException) { |
153 |
if (((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND)
|
154 |
onSuccess(null);
|
155 |
else
|
156 |
app.displayError("Unable to delete folder: " + ((RestException) t).getHttpStatusText());
|
157 |
} |
158 |
else
|
159 |
app.displayError("System error unable to delete folder: "+t.getMessage());
|
160 |
} |
161 |
|
162 |
@Override
|
163 |
protected void onUnauthorized(Response response) { |
164 |
app.sessionExpired(); |
165 |
} |
166 |
}; |
167 |
deleteFolder.setHeader("X-Auth-Token", app.getToken());
|
168 |
Scheduler.get().scheduleDeferred(deleteFolder); |
169 |
} |
170 |
}); |
171 |
} |
172 |
}); |
173 |
} |
174 |
|
175 |
@Override
|
176 |
public void onError(Throwable t) { |
177 |
GWT.log("", t);
|
178 |
app.setError(t); |
179 |
if (t instanceof RestException) { |
180 |
app.displayError("Unable to get folder: " + ((RestException) t).getHttpStatusText());
|
181 |
} |
182 |
else
|
183 |
app.displayError("System error getting folder: " + t.getMessage());
|
184 |
} |
185 |
|
186 |
@Override
|
187 |
protected void onUnauthorized(Response response) { |
188 |
app.sessionExpired(); |
189 |
} |
190 |
}; |
191 |
getFolder.setHeader("X-Auth-Token", app.getToken());
|
192 |
Scheduler.get().scheduleDeferred(getFolder); |
193 |
} |
194 |
|
195 |
@Override
|
196 |
public void onError(Throwable t) { |
197 |
GWT.log("", t);
|
198 |
app.setError(t); |
199 |
if (t instanceof RestException) { |
200 |
app.displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
|
201 |
} |
202 |
else
|
203 |
app.displayError("System error creating folder:" + t.getMessage());
|
204 |
} |
205 |
|
206 |
@Override
|
207 |
protected void onUnauthorized(Response response) { |
208 |
app.sessionExpired(); |
209 |
} |
210 |
}; |
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); |
216 |
} |
217 |
|
218 |
|
219 |
|
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) {
|
225 |
@Override
|
226 |
public void onSuccess(Resource result) { |
227 |
trashFiles(iter, callback); |
228 |
} |
229 |
|
230 |
@Override
|
231 |
public void onError(Throwable t) { |
232 |
GWT.log("", t);
|
233 |
app.setError(t); |
234 |
if (t instanceof RestException) { |
235 |
app.displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
|
236 |
} |
237 |
else
|
238 |
app.displayError("System error unable to copy file: "+t.getMessage());
|
239 |
} |
240 |
|
241 |
@Override
|
242 |
protected void onUnauthorized(Response response) { |
243 |
app.sessionExpired(); |
244 |
} |
245 |
}; |
246 |
trashFile.setHeader("X-Auth-Token", app.getToken());
|
247 |
trashFile.setHeader("X-Move-From", URL.encodePathSegment(file.getUri())); |
248 |
Scheduler.get().scheduleDeferred(trashFile); |
249 |
} |
250 |
else if (callback != null) { |
251 |
callback.execute(); |
252 |
} |
253 |
} |
254 |
|
255 |
protected void trashSubfolders(final Iterator<Folder> iter, final Command callback) { |
256 |
if (iter.hasNext()) {
|
257 |
final Folder f = iter.next();
|
258 |
trashFolder(f, new Command() {
|
259 |
|
260 |
@Override
|
261 |
public void execute() { |
262 |
trashSubfolders(iter, callback); |
263 |
} |
264 |
}); |
265 |
} |
266 |
else {
|
267 |
app.updateTrash(false, callback);
|
268 |
} |
269 |
} |
270 |
} |