Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / commands / ToTrashCommand.java @ f5c86c34

History | View | Annotate | Download (9.1 kB)

1 a57faaf0 Christos Stathis
/*
2 cae2a8db Christos Stathis
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
3 63366925 Christos Stathis
 *
4 63366925 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 63366925 Christos Stathis
 * without modification, are permitted provided that the following
6 63366925 Christos Stathis
 * conditions are met:
7 63366925 Christos Stathis
 *
8 63366925 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 63366925 Christos Stathis
 *      disclaimer.
11 63366925 Christos Stathis
 *
12 63366925 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 63366925 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 63366925 Christos Stathis
 *      provided with the distribution.
16 63366925 Christos Stathis
 *
17 63366925 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 63366925 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 63366925 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 63366925 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 63366925 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 63366925 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 63366925 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 63366925 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 63366925 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 63366925 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 63366925 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 63366925 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 63366925 Christos Stathis
 *
30 63366925 Christos Stathis
 * The views and conclusions contained in the software and
31 63366925 Christos Stathis
 * documentation are those of the authors and should not be
32 63366925 Christos Stathis
 * interpreted as representing official policies, either expressed
33 63366925 Christos Stathis
 * or implied, of GRNET S.A.
34 a57faaf0 Christos Stathis
 */
35 a57faaf0 Christos Stathis
package gr.grnet.pithos.web.client.commands;
36 a57faaf0 Christos Stathis
37 749068ba Christos Stathis
import gr.grnet.pithos.web.client.Pithos;
38 81918908 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.File;
39 81918908 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Folder;
40 81918908 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Resource;
41 16e8478a Christos Stathis
import gr.grnet.pithos.web.client.rest.DeleteRequest;
42 fd136d33 Christos Stathis
import gr.grnet.pithos.web.client.rest.GetRequest;
43 7b28ae07 Christos Stathis
import gr.grnet.pithos.web.client.rest.PutRequest;
44 a57faaf0 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
45 a57faaf0 Christos Stathis
46 81918908 Christos Stathis
import java.util.Iterator;
47 a57faaf0 Christos Stathis
import java.util.List;
48 a57faaf0 Christos Stathis
49 a57faaf0 Christos Stathis
import com.google.gwt.core.client.GWT;
50 abd8738f Christos Stathis
import com.google.gwt.core.client.Scheduler;
51 9539e23d Christos Stathis
import com.google.gwt.http.client.Response;
52 fd136d33 Christos Stathis
import com.google.gwt.http.client.URL;
53 a57faaf0 Christos Stathis
import com.google.gwt.user.client.Command;
54 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.PopupPanel;
55 a57faaf0 Christos Stathis
56 a57faaf0 Christos Stathis
/**
57 a57faaf0 Christos Stathis
 *
58 a57faaf0 Christos Stathis
 * Move file or folder to trash.
59 a57faaf0 Christos Stathis
 *
60 a57faaf0 Christos Stathis
 *
61 a57faaf0 Christos Stathis
 */
62 a57faaf0 Christos Stathis
public class ToTrashCommand implements Command{
63 a57faaf0 Christos Stathis
        private PopupPanel containerPanel;
64 7811b9d1 Christos Stathis
        protected Pithos app;
65 7811b9d1 Christos Stathis
        protected Object resource;
66 a57faaf0 Christos Stathis
67 749068ba Christos Stathis
        public ToTrashCommand(Pithos _app, PopupPanel _containerPanel, Object _resource){
68 a57faaf0 Christos Stathis
                containerPanel = _containerPanel;
69 81918908 Christos Stathis
        app = _app;
70 81918908 Christos Stathis
        resource = _resource;
71 a57faaf0 Christos Stathis
        }
72 a57faaf0 Christos Stathis
73 a57faaf0 Christos Stathis
        @Override
74 a57faaf0 Christos Stathis
        public void execute() {
75 031f1fe0 Christos Stathis
        if (containerPanel != null)
76 031f1fe0 Christos Stathis
                    containerPanel.hide();
77 81918908 Christos Stathis
        if (resource instanceof List) {
78 7811b9d1 Christos Stathis
            @SuppressWarnings("unchecked")
79 7811b9d1 Christos Stathis
                        Iterator<File> iter = ((List<File>) resource).iterator();
80 2637c9cd Christos Stathis
            trashFiles(iter, new Command() {
81 7811b9d1 Christos Stathis
                @SuppressWarnings("unchecked")
82 7811b9d1 Christos Stathis
                                @Override
83 2637c9cd Christos Stathis
                public void execute() {
84 9b055aba Christos Stathis
                    app.updateFolder(((List<File>) resource).get(0).getParent(), true, new Command() {
85 9b055aba Christos Stathis
                                                
86 9b055aba Christos Stathis
                                                @Override
87 9b055aba Christos Stathis
                                                public void execute() {
88 9b055aba Christos Stathis
                                                        app.updateTrash(false, null);
89 9b055aba Christos Stathis
                                                }
90 6acd4df3 Christos Stathis
                                        }, true);
91 2637c9cd Christos Stathis
                }
92 2637c9cd Christos Stathis
            });
93 81918908 Christos Stathis
        }
94 81918908 Christos Stathis
        else if (resource instanceof Folder) {
95 2637c9cd Christos Stathis
            final Folder toBeTrashed = (Folder) resource;
96 2637c9cd Christos Stathis
            trashFolder(toBeTrashed, new Command() {
97 2637c9cd Christos Stathis
                @Override
98 2637c9cd Christos Stathis
                public void execute() {
99 9b055aba Christos Stathis
                    app.updateFolder(toBeTrashed.getParent(), true, new Command() {
100 9b055aba Christos Stathis
                                                
101 9b055aba Christos Stathis
                                                @Override
102 9b055aba Christos Stathis
                                                public void execute() {
103 9b055aba Christos Stathis
                                                        app.updateTrash(false, null);
104 9b055aba Christos Stathis
                                                }
105 6acd4df3 Christos Stathis
                                        }, true);
106 2637c9cd Christos Stathis
                }
107 2637c9cd Christos Stathis
            });
108 a57faaf0 Christos Stathis
109 81918908 Christos Stathis
        }
110 a57faaf0 Christos Stathis
        }
111 a57faaf0 Christos Stathis
112 2637c9cd Christos Stathis
    private void trashFolder(final Folder f, final Command callback) {
113 16e8478a Christos Stathis
        String path = "/" + Pithos.TRASH_CONTAINER + "/" + f.getPrefix();
114 7b28ae07 Christos Stathis
        PutRequest createFolder = new PutRequest(app.getApiPath(), app.getUsername(), path) {
115 2637c9cd Christos Stathis
            @Override
116 ebead1b5 Christos Stathis
            public void onSuccess(Resource result) {
117 fd136d33 Christos Stathis
                    GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix()), f) {
118 fd136d33 Christos Stathis
119 fd136d33 Christos Stathis
                                        @Override
120 fd136d33 Christos Stathis
                                        public void onSuccess(final Folder _f) {
121 fd136d33 Christos Stathis
                                            Iterator<File> iter = _f.getFiles().iterator();
122 fd136d33 Christos Stathis
                                            trashFiles(iter, new Command() {
123 fd136d33 Christos Stathis
                                                @Override
124 fd136d33 Christos Stathis
                                                public void execute() {
125 fd136d33 Christos Stathis
                                                    Iterator<Folder> iterf = _f.getSubfolders().iterator();
126 fd136d33 Christos Stathis
                                                    trashSubfolders(iterf, new Command() {
127 16e8478a Christos Stathis
                                                                        
128 16e8478a Christos Stathis
                                                                        @Override
129 fd136d33 Christos Stathis
                                                                        public void execute() {
130 5f91f72d Christos Stathis
                                                                                DeleteRequest deleteFolder = new DeleteRequest(app.getApiPath(), _f.getOwner(), URL.encode(_f.getUri())) {
131 fd136d33 Christos Stathis
                                                                                        
132 fd136d33 Christos Stathis
                                                                                        @Override
133 ebead1b5 Christos Stathis
                                                                                        public void onSuccess(Resource _result) {
134 fd136d33 Christos Stathis
                                                                                                if (callback != null)
135 fd136d33 Christos Stathis
                                                                                                        callback.execute();
136 fd136d33 Christos Stathis
                                                                                        }
137 fd136d33 Christos Stathis
                                                                                        
138 fd136d33 Christos Stathis
                                                                                        @Override
139 fd136d33 Christos Stathis
                                                                                        public void onError(Throwable t) {
140 fd136d33 Christos Stathis
                                                                            GWT.log("", t);
141 3f8622d4 Christos Stathis
                                                                                                app.setError(t);
142 fd136d33 Christos Stathis
                                                                            if (t instanceof RestException) {
143 340bf0d6 Christos Stathis
                                                                                    if (((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND)
144 340bf0d6 Christos Stathis
                                                                                            onSuccess(null);
145 340bf0d6 Christos Stathis
                                                                                    else
146 340bf0d6 Christos Stathis
                                                                                            app.displayError("Unable to delete folder: " + ((RestException) t).getHttpStatusText());
147 fd136d33 Christos Stathis
                                                                            }
148 fd136d33 Christos Stathis
                                                                            else
149 fd136d33 Christos Stathis
                                                                                app.displayError("System error unable to delete folder: "+t.getMessage());
150 fd136d33 Christos Stathis
                                                                                        }
151 9539e23d Christos Stathis
152 fd136d33 Christos Stathis
                                                                                        @Override
153 fd136d33 Christos Stathis
                                                                                        protected void onUnauthorized(Response response) {
154 fd136d33 Christos Stathis
                                                                                                app.sessionExpired();
155 fd136d33 Christos Stathis
                                                                                        }
156 fd136d33 Christos Stathis
                                                                                };
157 fd136d33 Christos Stathis
                                                                                deleteFolder.setHeader("X-Auth-Token", app.getToken());
158 fd136d33 Christos Stathis
                                                                                Scheduler.get().scheduleDeferred(deleteFolder);
159 9539e23d Christos Stathis
                                                                        }
160 fd136d33 Christos Stathis
                                                                });
161 fd136d33 Christos Stathis
                                                }
162 fd136d33 Christos Stathis
                                            });
163 fd136d33 Christos Stathis
                                        }
164 fd136d33 Christos Stathis
165 fd136d33 Christos Stathis
                                        @Override
166 fd136d33 Christos Stathis
                                        public void onError(Throwable t) {
167 fd136d33 Christos Stathis
                                GWT.log("", t);
168 3f8622d4 Christos Stathis
                                                app.setError(t);
169 fd136d33 Christos Stathis
                                if (t instanceof RestException) {
170 fd136d33 Christos Stathis
                                    app.displayError("Unable to get folder: " + ((RestException) t).getHttpStatusText());
171 fd136d33 Christos Stathis
                                }
172 fd136d33 Christos Stathis
                                else
173 fd136d33 Christos Stathis
                                    app.displayError("System error getting folder: " + t.getMessage());
174 fd136d33 Christos Stathis
                                        }
175 fd136d33 Christos Stathis
176 fd136d33 Christos Stathis
                                        @Override
177 fd136d33 Christos Stathis
                                        protected void onUnauthorized(Response response) {
178 fd136d33 Christos Stathis
                                                app.sessionExpired();
179 fd136d33 Christos Stathis
                                        }
180 fd136d33 Christos Stathis
                                };
181 fd136d33 Christos Stathis
                                getFolder.setHeader("X-Auth-Token", app.getToken());
182 fd136d33 Christos Stathis
                                Scheduler.get().scheduleDeferred(getFolder);
183 2637c9cd Christos Stathis
            }
184 2637c9cd Christos Stathis
185 2637c9cd Christos Stathis
            @Override
186 2637c9cd Christos Stathis
            public void onError(Throwable t) {
187 2637c9cd Christos Stathis
                GWT.log("", t);
188 3f8622d4 Christos Stathis
                                app.setError(t);
189 2637c9cd Christos Stathis
                if (t instanceof RestException) {
190 2637c9cd Christos Stathis
                    app.displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
191 2637c9cd Christos Stathis
                }
192 2637c9cd Christos Stathis
                else
193 2637c9cd Christos Stathis
                    app.displayError("System error creating folder:" + t.getMessage());
194 2637c9cd Christos Stathis
            }
195 9539e23d Christos Stathis
196 9539e23d Christos Stathis
                        @Override
197 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
198 9539e23d Christos Stathis
                                app.sessionExpired();
199 9539e23d Christos Stathis
                        }
200 2637c9cd Christos Stathis
        };
201 7b28ae07 Christos Stathis
        createFolder.setHeader("X-Auth-Token", app.getToken());
202 7b28ae07 Christos Stathis
        createFolder.setHeader("Accept", "*/*");
203 7b28ae07 Christos Stathis
        createFolder.setHeader("Content-Length", "0");
204 7b28ae07 Christos Stathis
        createFolder.setHeader("Content-Type", "application/folder");
205 7b28ae07 Christos Stathis
        Scheduler.get().scheduleDeferred(createFolder);
206 2637c9cd Christos Stathis
    }
207 2637c9cd Christos Stathis
208 fd136d33 Christos Stathis
209 fd136d33 Christos Stathis
    
210 7811b9d1 Christos Stathis
    protected void trashFiles(final Iterator<File> iter, final Command callback) {
211 81918908 Christos Stathis
        if (iter.hasNext()) {
212 81918908 Christos Stathis
            File file = iter.next();
213 7b28ae07 Christos Stathis
            String path = "/" + Pithos.TRASH_CONTAINER + "/" + file.getPath();
214 7b28ae07 Christos Stathis
            PutRequest trashFile = new PutRequest(app.getApiPath(), app.getUsername(), path) {
215 81918908 Christos Stathis
                @Override
216 ebead1b5 Christos Stathis
                public void onSuccess(Resource result) {
217 2637c9cd Christos Stathis
                    trashFiles(iter, callback);
218 81918908 Christos Stathis
                }
219 81918908 Christos Stathis
220 81918908 Christos Stathis
                @Override
221 81918908 Christos Stathis
                public void onError(Throwable t) {
222 81918908 Christos Stathis
                    GWT.log("", t);
223 3f8622d4 Christos Stathis
                                        app.setError(t);
224 81918908 Christos Stathis
                    if (t instanceof RestException) {
225 a7c43f26 Christos Stathis
                        app.displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
226 81918908 Christos Stathis
                    }
227 81918908 Christos Stathis
                    else
228 a7c43f26 Christos Stathis
                        app.displayError("System error unable to copy file: "+t.getMessage());
229 81918908 Christos Stathis
                }
230 9539e23d Christos Stathis
231 9539e23d Christos Stathis
                                @Override
232 9539e23d Christos Stathis
                                protected void onUnauthorized(Response response) {
233 9539e23d Christos Stathis
                                        app.sessionExpired();
234 9539e23d Christos Stathis
                                }
235 81918908 Christos Stathis
            };
236 81918908 Christos Stathis
            trashFile.setHeader("X-Auth-Token", app.getToken());
237 a2411896 Christos Stathis
            trashFile.setHeader("X-Move-From", URL.encodePathSegment(file.getUri()));
238 81918908 Christos Stathis
            Scheduler.get().scheduleDeferred(trashFile);
239 81918908 Christos Stathis
        }
240 7b28ae07 Christos Stathis
        else if (callback != null) {
241 2637c9cd Christos Stathis
            callback.execute();
242 2637c9cd Christos Stathis
        }
243 2637c9cd Christos Stathis
    }
244 2637c9cd Christos Stathis
245 7811b9d1 Christos Stathis
    protected void trashSubfolders(final Iterator<Folder> iter, final Command callback) {
246 2637c9cd Christos Stathis
        if (iter.hasNext()) {
247 2637c9cd Christos Stathis
            final Folder f = iter.next();
248 fd136d33 Christos Stathis
            trashFolder(f, new Command() {
249 fd136d33 Christos Stathis
                                
250 fd136d33 Christos Stathis
                                @Override
251 fd136d33 Christos Stathis
                                public void execute() {
252 fd136d33 Christos Stathis
                                        trashSubfolders(iter, callback);
253 fd136d33 Christos Stathis
                                }
254 fd136d33 Christos Stathis
                        });
255 2637c9cd Christos Stathis
        }
256 16e8478a Christos Stathis
        else  {
257 16e8478a Christos Stathis
                app.updateTrash(false, callback);
258 81918908 Christos Stathis
        }
259 81918908 Christos Stathis
    }
260 a57faaf0 Christos Stathis
}