Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.8 kB)

1 a57faaf0 Christos Stathis
/*
2 e6e9f6e6 Christos KK Loverdos
 * Copyright 2011-2013 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 fa68c84d Christos KK Loverdos
import gr.grnet.pithos.web.client.Const;
41 749068ba Christos Stathis
import gr.grnet.pithos.web.client.Pithos;
42 31477486 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.File;
43 31477486 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Folder;
44 cc0120ab Christos KK Loverdos
import gr.grnet.pithos.web.client.Resource;
45 31477486 Christos Stathis
import gr.grnet.pithos.web.client.rest.PutRequest;
46 31477486 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
47 a57faaf0 Christos Stathis
48 31477486 Christos Stathis
import com.google.gwt.core.client.GWT;
49 31477486 Christos Stathis
import com.google.gwt.core.client.Scheduler;
50 9539e23d Christos Stathis
import com.google.gwt.http.client.Response;
51 340bf0d6 Christos Stathis
import com.google.gwt.http.client.URL;
52 a57faaf0 Christos Stathis
import com.google.gwt.user.client.Command;
53 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.PopupPanel;
54 a57faaf0 Christos Stathis
55 a57faaf0 Christos Stathis
56 a57faaf0 Christos Stathis
/**
57 a57faaf0 Christos Stathis
 *
58 a57faaf0 Christos Stathis
 * Restore trashed files and folders.
59 a57faaf0 Christos Stathis
 *
60 a57faaf0 Christos Stathis
 */
61 31477486 Christos Stathis
public class RestoreTrashCommand implements Command {
62 a57faaf0 Christos Stathis
        private PopupPanel containerPanel;
63 31477486 Christos Stathis
        protected Pithos app;
64 31477486 Christos Stathis
        protected Object resource;
65 a57faaf0 Christos Stathis
66 31477486 Christos Stathis
        public RestoreTrashCommand(Pithos _app, PopupPanel _containerPanel, Object _resource){
67 a57faaf0 Christos Stathis
                containerPanel = _containerPanel;
68 31477486 Christos Stathis
        app = _app;
69 31477486 Christos Stathis
        resource = _resource;
70 a57faaf0 Christos Stathis
        }
71 a57faaf0 Christos Stathis
72 a57faaf0 Christos Stathis
        @Override
73 a57faaf0 Christos Stathis
        public void execute() {
74 31477486 Christos Stathis
        if (containerPanel != null)
75 31477486 Christos Stathis
                    containerPanel.hide();
76 31477486 Christos Stathis
        if (resource instanceof List) {
77 31477486 Christos Stathis
            @SuppressWarnings("unchecked")
78 31477486 Christos Stathis
                        Iterator<File> iter = ((List<File>) resource).iterator();
79 31477486 Christos Stathis
            untrashFiles(iter, new Command() {
80 31477486 Christos Stathis
                @SuppressWarnings("unchecked")
81 31477486 Christos Stathis
                                @Override
82 31477486 Christos Stathis
                public void execute() {
83 67bb2128 Christos Stathis
                    app.updateFolder(((List<File>) resource).get(0).getParent(), true, new Command() {
84 67bb2128 Christos Stathis
                                                
85 67bb2128 Christos Stathis
                                                @Override
86 67bb2128 Christos Stathis
                                                public void execute() {
87 6acd4df3 Christos Stathis
                                                        app.updateFolder(app.getAccount().getPithos(), false, null, true);
88 67bb2128 Christos Stathis
                                                }
89 6acd4df3 Christos Stathis
                                        }, true);
90 31477486 Christos Stathis
                }
91 31477486 Christos Stathis
            });
92 31477486 Christos Stathis
        }
93 31477486 Christos Stathis
        else if (resource instanceof Folder) {
94 31477486 Christos Stathis
            final Folder toBeUnTrashed = (Folder) resource;
95 31477486 Christos Stathis
            untrashFolder(toBeUnTrashed, new Command() {
96 31477486 Christos Stathis
                @Override
97 31477486 Christos Stathis
                public void execute() {
98 67bb2128 Christos Stathis
                    app.updateFolder(toBeUnTrashed.getParent(), true, new Command() {
99 67bb2128 Christos Stathis
                                                
100 67bb2128 Christos Stathis
                                                @Override
101 67bb2128 Christos Stathis
                                                public void execute() {
102 6acd4df3 Christos Stathis
                                                        app.updateFolder(app.getAccount().getPithos(), false, null, true);
103 67bb2128 Christos Stathis
                                                }
104 6acd4df3 Christos Stathis
                                        }, true);
105 31477486 Christos Stathis
                }
106 31477486 Christos Stathis
            });
107 a57faaf0 Christos Stathis
108 31477486 Christos Stathis
        }
109 a57faaf0 Christos Stathis
        }
110 a57faaf0 Christos Stathis
111 31477486 Christos Stathis
    private void untrashFolder(final Folder f, final Command callback) {
112 fa68c84d Christos KK Loverdos
        String path = "/" + Const.HOME_CONTAINER + "/" + f.getPrefix();
113 cc0120ab Christos KK Loverdos
        app.copyFolder(f, app.getUserID(), path, true, callback);
114 31477486 Christos Stathis
    }
115 31477486 Christos Stathis
116 31477486 Christos Stathis
    protected void untrashFiles(final Iterator<File> iter, final Command callback) {
117 31477486 Christos Stathis
        if (iter.hasNext()) {
118 31477486 Christos Stathis
            File file = iter.next();
119 fa68c84d Christos KK Loverdos
            String path = "/" + Const.HOME_CONTAINER + "/" + file.getPath();
120 28e270f9 Christos KK Loverdos
            PutRequest untrashFile = new PutRequest(Pithos.getStorageAPIURL(), app.getUserID(), path) {
121 31477486 Christos Stathis
                @Override
122 ebead1b5 Christos Stathis
                public void onSuccess(Resource result) {
123 31477486 Christos Stathis
                    untrashFiles(iter, callback);
124 31477486 Christos Stathis
                }
125 31477486 Christos Stathis
126 31477486 Christos Stathis
                @Override
127 31477486 Christos Stathis
                public void onError(Throwable t) {
128 31477486 Christos Stathis
                    GWT.log("", t);
129 3f8622d4 Christos Stathis
                                        app.setError(t);
130 31477486 Christos Stathis
                    if (t instanceof RestException) {
131 31477486 Christos Stathis
                        app.displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
132 31477486 Christos Stathis
                    }
133 31477486 Christos Stathis
                    else
134 31477486 Christos Stathis
                        app.displayError("System error unable to copy file: "+t.getMessage());
135 31477486 Christos Stathis
                }
136 9539e23d Christos Stathis
137 9539e23d Christos Stathis
                                @Override
138 9539e23d Christos Stathis
                                protected void onUnauthorized(Response response) {
139 9539e23d Christos Stathis
                                        app.sessionExpired();
140 9539e23d Christos Stathis
                                }
141 31477486 Christos Stathis
            };
142 cc0120ab Christos KK Loverdos
            untrashFile.setHeader("X-Auth-Token", app.getUserToken());
143 a2411896 Christos Stathis
            untrashFile.setHeader("X-Move-From", URL.encodePathSegment(file.getUri()));
144 3c058715 Christos Stathis
            untrashFile.setHeader("Content-Type", file.getContentType());
145 3c058715 Christos Stathis
            
146 31477486 Christos Stathis
            Scheduler.get().scheduleDeferred(untrashFile);
147 31477486 Christos Stathis
        }
148 31477486 Christos Stathis
        else if (callback != null) {
149 31477486 Christos Stathis
            callback.execute();
150 31477486 Christos Stathis
        }
151 31477486 Christos Stathis
    }
152 31477486 Christos Stathis
153 31477486 Christos Stathis
    protected void untrashSubfolders(final Iterator<Folder> iter, final Command callback) {
154 31477486 Christos Stathis
        if (iter.hasNext()) {
155 31477486 Christos Stathis
            final Folder f = iter.next();
156 340bf0d6 Christos Stathis
            untrashFolder(f, new Command() {
157 340bf0d6 Christos Stathis
                                
158 340bf0d6 Christos Stathis
                                @Override
159 340bf0d6 Christos Stathis
                                public void execute() {
160 340bf0d6 Christos Stathis
                                        untrashSubfolders(iter, callback);
161 340bf0d6 Christos Stathis
                                }
162 340bf0d6 Christos Stathis
                        });
163 31477486 Christos Stathis
        }
164 31477486 Christos Stathis
        else  {
165 31477486 Christos Stathis
                if (callback != null)
166 31477486 Christos Stathis
                        callback.execute();
167 31477486 Christos Stathis
        }
168 31477486 Christos Stathis
    }
169 a57faaf0 Christos Stathis
}