Cleaned up File menu
[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 com.google.gwt.core.client.Scheduler;
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;
42 import gr.grnet.pithos.web.client.rest.MultiplePostCommand;
43 import gr.grnet.pithos.web.client.rest.PostCommand;
44 import gr.grnet.pithos.web.client.rest.PostRequest;
45 import gr.grnet.pithos.web.client.rest.PutRequest;
46 import gr.grnet.pithos.web.client.rest.RestException;
47 import gr.grnet.pithos.web.client.rest.resource.FileResource;
48 import gr.grnet.pithos.web.client.rest.resource.FolderResource;
49 import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
50
51 import java.util.ArrayList;
52 import java.util.Iterator;
53 import java.util.List;
54
55
56 import com.google.gwt.core.client.GWT;
57 import com.google.gwt.user.client.Command;
58 import com.google.gwt.user.client.DeferredCommand;
59 import com.google.gwt.user.client.ui.PopupPanel;
60
61 /**
62  *
63  * Move file or folder to trash.
64  *
65  *
66  */
67 public class ToTrashCommand implements Command{
68         private PopupPanel containerPanel;
69     private GSS app;
70     private Object resource;
71
72         public ToTrashCommand(GSS _app, PopupPanel _containerPanel, Object _resource){
73                 containerPanel = _containerPanel;
74         app = _app;
75         resource = _resource;
76         }
77
78         @Override
79         public void execute() {
80                 containerPanel.hide();
81         if (resource instanceof List) {
82             Iterator<File> iter = ((List<File>) resource).iterator();
83             trashFiles(iter, new Command() {
84                 @Override
85                 public void execute() {
86                     app.get().updateFolder(((List<File>) resource).get(0).getParent());
87                 }
88             });
89         }
90         else if (resource instanceof Folder) {
91             final Folder toBeTrashed = (Folder) resource;
92             trashFolder(toBeTrashed, new Command() {
93                 @Override
94                 public void execute() {
95                     app.updateFolder(toBeTrashed.getParent());
96                 }
97             });
98
99         }
100         }
101
102     private void trashFolder(final Folder f, final Command callback) {
103         String path = app.getApiPath() + app.getUsername() + f.getUri() + "?update=";
104         PostRequest trashFolder = new PostRequest(path) {
105             @Override
106             public void onSuccess(Resource result) {
107                 Iterator<File> iter = f.getFiles().iterator();
108                 trashFiles(iter, new Command() {
109                     @Override
110                     public void execute() {
111                         Iterator<Folder> iterf = f.getSubfolders().iterator();
112                         trashSubfolders(iterf, new Command() {
113                             @Override
114                             public void execute() {
115                                 callback.execute();
116                             }
117                         });
118                     }
119                 });
120             }
121
122             @Override
123             public void onError(Throwable t) {
124                 GWT.log("", t);
125                 if (t instanceof RestException) {
126                     app.displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
127                 }
128                 else
129                     app.displayError("System error creating folder:" + t.getMessage());
130             }
131         };
132         trashFolder.setHeader("X-Auth-Token", app.getToken());
133         trashFolder.setHeader("X-Object-Meta-Trash", "true");
134         Scheduler.get().scheduleDeferred(trashFolder);
135     }
136
137     private void trashFiles(final Iterator<File> iter, final Command callback) {
138         if (iter.hasNext()) {
139             File file = iter.next();
140             String path = app.getApiPath() + app.getUsername() + file.getUri() + "?update=";
141             PostRequest trashFile = new PostRequest(path) {
142                 @Override
143                 public void onSuccess(Resource result) {
144                     trashFiles(iter, callback);
145                 }
146
147                 @Override
148                 public void onError(Throwable t) {
149                     GWT.log("", t);
150                     if (t instanceof RestException) {
151                         GSS.get().displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
152                     }
153                     else
154                         GSS.get().displayError("System error unable to copy file: "+t.getMessage());
155                 }
156             };
157             trashFile.setHeader("X-Auth-Token", app.getToken());
158             trashFile.setHeader("X-Object-Meta-Trash", "true");
159             Scheduler.get().scheduleDeferred(trashFile);
160         }
161         else  if (callback != null) {
162             callback.execute();
163         }
164     }
165
166     private void trashSubfolders(final Iterator<Folder> iter, final Command callback) {
167         if (iter.hasNext()) {
168             final Folder f = iter.next();
169             trashFolder(f, callback);
170         }
171         else  if (callback != null) {
172             callback.execute();
173         }
174     }
175 }