d1f35565fac9ad6725dcadcfe4838214abfef34b
[pithos-web-client] / src / gr / grnet / pithos / web / client / commands / ToTrashCommand.java
1 /*
2  * Copyright 2011 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                     app.updateFolder(((List<File>) resource).get(0).getParent(), true, new Command() {
85                                                 
86                                                 @Override
87                                                 public void execute() {
88                                                         app.updateTrash(false, null);
89                                                 }
90                                         });
91                 }
92             });
93         }
94         else if (resource instanceof Folder) {
95             final Folder toBeTrashed = (Folder) resource;
96             trashFolder(toBeTrashed, new Command() {
97                 @Override
98                 public void execute() {
99                     app.updateFolder(toBeTrashed.getParent(), true, new Command() {
100                                                 
101                                                 @Override
102                                                 public void execute() {
103                                                         app.updateTrash(false, null);
104                                                 }
105                                         });
106                 }
107             });
108
109         }
110         }
111
112     private void trashFolder(final Folder f, final Command callback) {
113         String path = "/" + Pithos.TRASH_CONTAINER + "/" + f.getPrefix();
114         PutRequest createFolder = new PutRequest(app.getApiPath(), app.getUsername(), path) {
115             @Override
116             public void onSuccess(Resource result) {
117                 GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix()), f) {
118
119                                         @Override
120                                         public void onSuccess(final Folder _f) {
121                                             Iterator<File> iter = _f.getFiles().iterator();
122                                             trashFiles(iter, new Command() {
123                                                 @Override
124                                                 public void execute() {
125                                                     Iterator<Folder> iterf = _f.getSubfolders().iterator();
126                                                     trashSubfolders(iterf, new Command() {
127                                                                         
128                                                                         @Override
129                                                                         public void execute() {
130                                                                                 DeleteRequest deleteFolder = new DeleteRequest(app.getApiPath(), _f.getOwner(), URL.encode(_f.getUri())) {
131                                                                                         
132                                                                                         @Override
133                                                                                         public void onSuccess(Resource _result) {
134                                                                                                 if (callback != null)
135                                                                                                         callback.execute();
136                                                                                         }
137                                                                                         
138                                                                                         @Override
139                                                                                         public void onError(Throwable t) {
140                                                                             GWT.log("", t);
141                                                                                                 app.setError(t);
142                                                                             if (t instanceof RestException) {
143                                                                                 if (((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND)
144                                                                                         onSuccess(null);
145                                                                                 else
146                                                                                         app.displayError("Unable to delete folder: " + ((RestException) t).getHttpStatusText());
147                                                                             }
148                                                                             else
149                                                                                 app.displayError("System error unable to delete folder: "+t.getMessage());
150                                                                                         }
151
152                                                                                         @Override
153                                                                                         protected void onUnauthorized(Response response) {
154                                                                                                 app.sessionExpired();
155                                                                                         }
156                                                                                 };
157                                                                                 deleteFolder.setHeader("X-Auth-Token", app.getToken());
158                                                                                 Scheduler.get().scheduleDeferred(deleteFolder);
159                                                                         }
160                                                                 });
161                                                 }
162                                             });
163                                         }
164
165                                         @Override
166                                         public void onError(Throwable t) {
167                                 GWT.log("", t);
168                                                 app.setError(t);
169                                 if (t instanceof RestException) {
170                                     app.displayError("Unable to get folder: " + ((RestException) t).getHttpStatusText());
171                                 }
172                                 else
173                                     app.displayError("System error getting folder: " + t.getMessage());
174                                         }
175
176                                         @Override
177                                         protected void onUnauthorized(Response response) {
178                                                 app.sessionExpired();
179                                         }
180                                 };
181                                 getFolder.setHeader("X-Auth-Token", app.getToken());
182                                 Scheduler.get().scheduleDeferred(getFolder);
183             }
184
185             @Override
186             public void onError(Throwable t) {
187                 GWT.log("", t);
188                                 app.setError(t);
189                 if (t instanceof RestException) {
190                     app.displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
191                 }
192                 else
193                     app.displayError("System error creating folder:" + t.getMessage());
194             }
195
196                         @Override
197                         protected void onUnauthorized(Response response) {
198                                 app.sessionExpired();
199                         }
200         };
201         createFolder.setHeader("X-Auth-Token", app.getToken());
202         createFolder.setHeader("Accept", "*/*");
203         createFolder.setHeader("Content-Length", "0");
204         createFolder.setHeader("Content-Type", "application/folder");
205         Scheduler.get().scheduleDeferred(createFolder);
206     }
207
208
209     
210     protected void trashFiles(final Iterator<File> iter, final Command callback) {
211         if (iter.hasNext()) {
212             File file = iter.next();
213             String path = "/" + Pithos.TRASH_CONTAINER + "/" + file.getPath();
214             PutRequest trashFile = new PutRequest(app.getApiPath(), app.getUsername(), path) {
215                 @Override
216                 public void onSuccess(Resource result) {
217                     trashFiles(iter, callback);
218                 }
219
220                 @Override
221                 public void onError(Throwable t) {
222                     GWT.log("", t);
223                                         app.setError(t);
224                     if (t instanceof RestException) {
225                         app.displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
226                     }
227                     else
228                         app.displayError("System error unable to copy file: "+t.getMessage());
229                 }
230
231                                 @Override
232                                 protected void onUnauthorized(Response response) {
233                                         app.sessionExpired();
234                                 }
235             };
236             trashFile.setHeader("X-Auth-Token", app.getToken());
237             trashFile.setHeader("X-Move-From", URL.encodePathSegment(file.getUri()));
238             Scheduler.get().scheduleDeferred(trashFile);
239         }
240         else if (callback != null) {
241             callback.execute();
242         }
243     }
244
245     protected void trashSubfolders(final Iterator<Folder> iter, final Command callback) {
246         if (iter.hasNext()) {
247             final Folder f = iter.next();
248             trashFolder(f, new Command() {
249                                 
250                                 @Override
251                                 public void execute() {
252                                         trashSubfolders(iter, callback);
253                                 }
254                         });
255         }
256         else  {
257                 app.updateTrash(false, callback);
258         }
259     }
260 }