Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (9.3 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 31477486 Christos Stathis
import java.util.Iterator;
38 31477486 Christos Stathis
import java.util.List;
39 31477486 Christos Stathis
40 749068ba Christos Stathis
import gr.grnet.pithos.web.client.Pithos;
41 31477486 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.File;
42 31477486 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Folder;
43 31477486 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Resource;
44 31477486 Christos Stathis
import gr.grnet.pithos.web.client.rest.DeleteRequest;
45 340bf0d6 Christos Stathis
import gr.grnet.pithos.web.client.rest.GetRequest;
46 31477486 Christos Stathis
import gr.grnet.pithos.web.client.rest.PutRequest;
47 31477486 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
48 a57faaf0 Christos Stathis
49 31477486 Christos Stathis
import com.google.gwt.core.client.GWT;
50 31477486 Christos Stathis
import com.google.gwt.core.client.Scheduler;
51 9539e23d Christos Stathis
import com.google.gwt.http.client.Response;
52 340bf0d6 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
 *
59 a57faaf0 Christos Stathis
 * Restore trashed files and folders.
60 a57faaf0 Christos Stathis
 *
61 a57faaf0 Christos Stathis
 */
62 31477486 Christos Stathis
public class RestoreTrashCommand implements Command {
63 a57faaf0 Christos Stathis
        private PopupPanel containerPanel;
64 31477486 Christos Stathis
        protected Pithos app;
65 31477486 Christos Stathis
        protected Object resource;
66 a57faaf0 Christos Stathis
67 31477486 Christos Stathis
        public RestoreTrashCommand(Pithos _app, PopupPanel _containerPanel, Object _resource){
68 a57faaf0 Christos Stathis
                containerPanel = _containerPanel;
69 31477486 Christos Stathis
        app = _app;
70 31477486 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 31477486 Christos Stathis
        if (containerPanel != null)
76 31477486 Christos Stathis
                    containerPanel.hide();
77 31477486 Christos Stathis
        if (resource instanceof List) {
78 31477486 Christos Stathis
            @SuppressWarnings("unchecked")
79 31477486 Christos Stathis
                        Iterator<File> iter = ((List<File>) resource).iterator();
80 31477486 Christos Stathis
            untrashFiles(iter, new Command() {
81 31477486 Christos Stathis
                @SuppressWarnings("unchecked")
82 31477486 Christos Stathis
                                @Override
83 31477486 Christos Stathis
                public void execute() {
84 67bb2128 Christos Stathis
                    app.updateFolder(((List<File>) resource).get(0).getParent(), true, new Command() {
85 67bb2128 Christos Stathis
                                                
86 67bb2128 Christos Stathis
                                                @Override
87 67bb2128 Christos Stathis
                                                public void execute() {
88 6acd4df3 Christos Stathis
                                                        app.updateFolder(app.getAccount().getPithos(), false, null, true);
89 67bb2128 Christos Stathis
                                                }
90 6acd4df3 Christos Stathis
                                        }, true);
91 31477486 Christos Stathis
                }
92 31477486 Christos Stathis
            });
93 31477486 Christos Stathis
        }
94 31477486 Christos Stathis
        else if (resource instanceof Folder) {
95 31477486 Christos Stathis
            final Folder toBeUnTrashed = (Folder) resource;
96 31477486 Christos Stathis
            untrashFolder(toBeUnTrashed, new Command() {
97 31477486 Christos Stathis
                @Override
98 31477486 Christos Stathis
                public void execute() {
99 67bb2128 Christos Stathis
                    app.updateFolder(toBeUnTrashed.getParent(), true, new Command() {
100 67bb2128 Christos Stathis
                                                
101 67bb2128 Christos Stathis
                                                @Override
102 67bb2128 Christos Stathis
                                                public void execute() {
103 6acd4df3 Christos Stathis
                                                        app.updateFolder(app.getAccount().getPithos(), false, null, true);
104 67bb2128 Christos Stathis
                                                }
105 6acd4df3 Christos Stathis
                                        }, true);
106 31477486 Christos Stathis
                }
107 31477486 Christos Stathis
            });
108 a57faaf0 Christos Stathis
109 31477486 Christos Stathis
        }
110 a57faaf0 Christos Stathis
        }
111 a57faaf0 Christos Stathis
112 31477486 Christos Stathis
    private void untrashFolder(final Folder f, final Command callback) {
113 31477486 Christos Stathis
        String path = "/" + Pithos.HOME_CONTAINER + "/" + f.getPrefix();
114 31477486 Christos Stathis
        PutRequest createFolder = new PutRequest(app.getApiPath(), app.getUsername(), path) {
115 31477486 Christos Stathis
            @Override
116 ebead1b5 Christos Stathis
            public void onSuccess(Resource result) {
117 340bf0d6 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 340bf0d6 Christos Stathis
119 340bf0d6 Christos Stathis
                                        @Override
120 340bf0d6 Christos Stathis
                                        public void onSuccess(final Folder _f) {
121 340bf0d6 Christos Stathis
                                Iterator<File> iter = _f.getFiles().iterator();
122 340bf0d6 Christos Stathis
                                untrashFiles(iter, new Command() {
123 340bf0d6 Christos Stathis
                                    @Override
124 340bf0d6 Christos Stathis
                                    public void execute() {
125 340bf0d6 Christos Stathis
                                        Iterator<Folder> iterf = _f.getSubfolders().iterator();
126 340bf0d6 Christos Stathis
                                        untrashSubfolders(iterf, new Command() {
127 31477486 Christos Stathis
                                                                        
128 31477486 Christos Stathis
                                                                        @Override
129 340bf0d6 Christos Stathis
                                                                        public void execute() {
130 5f91f72d Christos Stathis
                                                                                DeleteRequest deleteFolder = new DeleteRequest(app.getApiPath(), _f.getOwner(), URL.encode(_f.getUri())) {
131 340bf0d6 Christos Stathis
                                                                                        
132 340bf0d6 Christos Stathis
                                                                                        @Override
133 ebead1b5 Christos Stathis
                                                                                        public void onSuccess(Resource _result) {
134 340bf0d6 Christos Stathis
                                                                                                app.updateRootFolder(callback);
135 340bf0d6 Christos Stathis
                                                                                        }
136 340bf0d6 Christos Stathis
                                                                                        
137 340bf0d6 Christos Stathis
                                                                                        @Override
138 340bf0d6 Christos Stathis
                                                                                        public void onError(Throwable t) {
139 340bf0d6 Christos Stathis
                                                                            GWT.log("", t);
140 3f8622d4 Christos Stathis
                                                                                                app.setError(t);
141 340bf0d6 Christos Stathis
                                                                            if (t instanceof RestException) {
142 340bf0d6 Christos Stathis
                                                                                    if (((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND)
143 340bf0d6 Christos Stathis
                                                                                            onSuccess(null);
144 340bf0d6 Christos Stathis
                                                                                    else
145 340bf0d6 Christos Stathis
                                                                                            app.displayError("Unable to delete folder: " + ((RestException) t).getHttpStatusText());
146 340bf0d6 Christos Stathis
                                                                            }
147 340bf0d6 Christos Stathis
                                                                            else
148 340bf0d6 Christos Stathis
                                                                                app.displayError("System error unable to delete folder: "+t.getMessage());
149 340bf0d6 Christos Stathis
                                                                                        }
150 340bf0d6 Christos Stathis
151 340bf0d6 Christos Stathis
                                                                                        @Override
152 340bf0d6 Christos Stathis
                                                                                        protected void onUnauthorized(Response response) {
153 340bf0d6 Christos Stathis
                                                                                                app.sessionExpired();
154 340bf0d6 Christos Stathis
                                                                                        }
155 340bf0d6 Christos Stathis
                                                                                };
156 340bf0d6 Christos Stathis
                                                                                deleteFolder.setHeader("X-Auth-Token", app.getToken());
157 340bf0d6 Christos Stathis
                                                                                Scheduler.get().scheduleDeferred(deleteFolder);
158 31477486 Christos Stathis
                                                                        }
159 340bf0d6 Christos Stathis
                                                                });
160 340bf0d6 Christos Stathis
                                    }
161 340bf0d6 Christos Stathis
                                });
162 340bf0d6 Christos Stathis
                                        }
163 340bf0d6 Christos Stathis
164 340bf0d6 Christos Stathis
                                        @Override
165 340bf0d6 Christos Stathis
                                        public void onError(Throwable t) {
166 340bf0d6 Christos Stathis
                                GWT.log("", t);
167 3f8622d4 Christos Stathis
                                                app.setError(t);
168 340bf0d6 Christos Stathis
                                if (t instanceof RestException) {
169 340bf0d6 Christos Stathis
                                    app.displayError("Unable to get folder: " + ((RestException) t).getHttpStatusText());
170 340bf0d6 Christos Stathis
                                }
171 340bf0d6 Christos Stathis
                                else
172 340bf0d6 Christos Stathis
                                    app.displayError("System error getting folder: " + t.getMessage());
173 340bf0d6 Christos Stathis
                                        }
174 340bf0d6 Christos Stathis
175 340bf0d6 Christos Stathis
                                        @Override
176 340bf0d6 Christos Stathis
                                        protected void onUnauthorized(Response response) {
177 340bf0d6 Christos Stathis
                                                app.sessionExpired();
178 340bf0d6 Christos Stathis
                                        }
179 340bf0d6 Christos Stathis
                                };
180 340bf0d6 Christos Stathis
                                getFolder.setHeader("X-Auth-Token", app.getToken());
181 340bf0d6 Christos Stathis
                                Scheduler.get().scheduleDeferred(getFolder);
182 31477486 Christos Stathis
            }
183 31477486 Christos Stathis
184 31477486 Christos Stathis
            @Override
185 31477486 Christos Stathis
            public void onError(Throwable t) {
186 31477486 Christos Stathis
                GWT.log("", t);
187 3f8622d4 Christos Stathis
                                app.setError(t);
188 31477486 Christos Stathis
                if (t instanceof RestException) {
189 31477486 Christos Stathis
                    app.displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
190 31477486 Christos Stathis
                }
191 31477486 Christos Stathis
                else
192 31477486 Christos Stathis
                    app.displayError("System error creating folder:" + t.getMessage());
193 31477486 Christos Stathis
            }
194 9539e23d Christos Stathis
195 9539e23d Christos Stathis
                        @Override
196 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
197 9539e23d Christos Stathis
                                app.sessionExpired();
198 9539e23d Christos Stathis
                        }
199 31477486 Christos Stathis
        };
200 31477486 Christos Stathis
        createFolder.setHeader("X-Auth-Token", app.getToken());
201 31477486 Christos Stathis
        createFolder.setHeader("Accept", "*/*");
202 31477486 Christos Stathis
        createFolder.setHeader("Content-Length", "0");
203 31477486 Christos Stathis
        createFolder.setHeader("Content-Type", "application/folder");
204 31477486 Christos Stathis
        Scheduler.get().scheduleDeferred(createFolder);
205 31477486 Christos Stathis
    }
206 31477486 Christos Stathis
207 31477486 Christos Stathis
    protected void untrashFiles(final Iterator<File> iter, final Command callback) {
208 31477486 Christos Stathis
        if (iter.hasNext()) {
209 31477486 Christos Stathis
            File file = iter.next();
210 31477486 Christos Stathis
            String path = "/" + Pithos.HOME_CONTAINER + "/" + file.getPath();
211 31477486 Christos Stathis
            PutRequest untrashFile = new PutRequest(app.getApiPath(), app.getUsername(), path) {
212 31477486 Christos Stathis
                @Override
213 ebead1b5 Christos Stathis
                public void onSuccess(Resource result) {
214 31477486 Christos Stathis
                    untrashFiles(iter, callback);
215 31477486 Christos Stathis
                }
216 31477486 Christos Stathis
217 31477486 Christos Stathis
                @Override
218 31477486 Christos Stathis
                public void onError(Throwable t) {
219 31477486 Christos Stathis
                    GWT.log("", t);
220 3f8622d4 Christos Stathis
                                        app.setError(t);
221 31477486 Christos Stathis
                    if (t instanceof RestException) {
222 31477486 Christos Stathis
                        app.displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
223 31477486 Christos Stathis
                    }
224 31477486 Christos Stathis
                    else
225 31477486 Christos Stathis
                        app.displayError("System error unable to copy file: "+t.getMessage());
226 31477486 Christos Stathis
                }
227 9539e23d Christos Stathis
228 9539e23d Christos Stathis
                                @Override
229 9539e23d Christos Stathis
                                protected void onUnauthorized(Response response) {
230 9539e23d Christos Stathis
                                        app.sessionExpired();
231 9539e23d Christos Stathis
                                }
232 31477486 Christos Stathis
            };
233 31477486 Christos Stathis
            untrashFile.setHeader("X-Auth-Token", app.getToken());
234 a2411896 Christos Stathis
            untrashFile.setHeader("X-Move-From", URL.encodePathSegment(file.getUri()));
235 31477486 Christos Stathis
            Scheduler.get().scheduleDeferred(untrashFile);
236 31477486 Christos Stathis
        }
237 31477486 Christos Stathis
        else if (callback != null) {
238 31477486 Christos Stathis
            callback.execute();
239 31477486 Christos Stathis
        }
240 31477486 Christos Stathis
    }
241 31477486 Christos Stathis
242 31477486 Christos Stathis
    protected void untrashSubfolders(final Iterator<Folder> iter, final Command callback) {
243 31477486 Christos Stathis
        if (iter.hasNext()) {
244 31477486 Christos Stathis
            final Folder f = iter.next();
245 340bf0d6 Christos Stathis
            untrashFolder(f, new Command() {
246 340bf0d6 Christos Stathis
                                
247 340bf0d6 Christos Stathis
                                @Override
248 340bf0d6 Christos Stathis
                                public void execute() {
249 340bf0d6 Christos Stathis
                                        untrashSubfolders(iter, callback);
250 340bf0d6 Christos Stathis
                                }
251 340bf0d6 Christos Stathis
                        });
252 31477486 Christos Stathis
        }
253 31477486 Christos Stathis
        else  {
254 31477486 Christos Stathis
                if (callback != null)
255 31477486 Christos Stathis
                        callback.execute();
256 31477486 Christos Stathis
        }
257 31477486 Christos Stathis
    }
258 a57faaf0 Christos Stathis
}