Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.8 kB)

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