Statistics
| Branch: | Tag: | Revision:

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

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