Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.9 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 fa68c84d Christos KK Loverdos
import gr.grnet.pithos.web.client.Const;
38 749068ba Christos Stathis
import gr.grnet.pithos.web.client.Pithos;
39 81918908 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.File;
40 81918908 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Folder;
41 cc0120ab Christos KK Loverdos
import gr.grnet.pithos.web.client.Resource;
42 7b28ae07 Christos Stathis
import gr.grnet.pithos.web.client.rest.PutRequest;
43 a57faaf0 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
44 a57faaf0 Christos Stathis
45 81918908 Christos Stathis
import java.util.Iterator;
46 a57faaf0 Christos Stathis
import java.util.List;
47 a57faaf0 Christos Stathis
48 a57faaf0 Christos Stathis
import com.google.gwt.core.client.GWT;
49 abd8738f Christos Stathis
import com.google.gwt.core.client.Scheduler;
50 9539e23d Christos Stathis
import com.google.gwt.http.client.Response;
51 fd136d33 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
 * Move file or folder to trash.
58 a57faaf0 Christos Stathis
 *
59 a57faaf0 Christos Stathis
 *
60 a57faaf0 Christos Stathis
 */
61 a57faaf0 Christos Stathis
public class ToTrashCommand implements Command{
62 a57faaf0 Christos Stathis
        private PopupPanel containerPanel;
63 7811b9d1 Christos Stathis
        protected Pithos app;
64 7811b9d1 Christos Stathis
        protected Object resource;
65 a57faaf0 Christos Stathis
66 749068ba Christos Stathis
        public ToTrashCommand(Pithos _app, PopupPanel _containerPanel, Object _resource){
67 a57faaf0 Christos Stathis
                containerPanel = _containerPanel;
68 81918908 Christos Stathis
        app = _app;
69 81918908 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 031f1fe0 Christos Stathis
        if (containerPanel != null)
75 031f1fe0 Christos Stathis
                    containerPanel.hide();
76 81918908 Christos Stathis
        if (resource instanceof List) {
77 7811b9d1 Christos Stathis
            @SuppressWarnings("unchecked")
78 7811b9d1 Christos Stathis
                        Iterator<File> iter = ((List<File>) resource).iterator();
79 2637c9cd Christos Stathis
            trashFiles(iter, new Command() {
80 7811b9d1 Christos Stathis
                @SuppressWarnings("unchecked")
81 7811b9d1 Christos Stathis
                                @Override
82 2637c9cd Christos Stathis
                public void execute() {
83 3586ad5e Christos Stathis
                        Folder f = ((List<File>) resource).get(0).getParent();
84 3586ad5e Christos Stathis
                        if (app.isMySharedSelected())
85 3586ad5e Christos Stathis
                                app.updateSharedFolder(f, true, new Command() {
86 3586ad5e Christos Stathis
                                                        
87 3586ad5e Christos Stathis
                                                        @Override
88 3586ad5e Christos Stathis
                                                        public void execute() {
89 3586ad5e Christos Stathis
                                                                app.updateTrash(false, null);
90 3586ad5e Christos Stathis
                                                        }
91 3586ad5e Christos Stathis
                                                });
92 3586ad5e Christos Stathis
                        else
93 3586ad5e Christos Stathis
                            app.updateFolder(f, true, new Command() {
94 3586ad5e Christos Stathis
                                                        
95 3586ad5e Christos Stathis
                                                        @Override
96 3586ad5e Christos Stathis
                                                        public void execute() {
97 3586ad5e Christos Stathis
                                                                app.updateTrash(false, null);
98 3586ad5e Christos Stathis
                                                        }
99 3586ad5e Christos Stathis
                                                }, true);
100 2637c9cd Christos Stathis
                }
101 2637c9cd Christos Stathis
            });
102 81918908 Christos Stathis
        }
103 81918908 Christos Stathis
        else if (resource instanceof Folder) {
104 2637c9cd Christos Stathis
            final Folder toBeTrashed = (Folder) resource;
105 2637c9cd Christos Stathis
            trashFolder(toBeTrashed, new Command() {
106 2637c9cd Christos Stathis
                @Override
107 2637c9cd Christos Stathis
                public void execute() {
108 9b055aba Christos Stathis
                    app.updateFolder(toBeTrashed.getParent(), true, new Command() {
109 9b055aba Christos Stathis
                                                
110 9b055aba Christos Stathis
                                                @Override
111 9b055aba Christos Stathis
                                                public void execute() {
112 9b055aba Christos Stathis
                                                        app.updateTrash(false, null);
113 9b055aba Christos Stathis
                                                }
114 6acd4df3 Christos Stathis
                                        }, true);
115 2637c9cd Christos Stathis
                }
116 2637c9cd Christos Stathis
            });
117 a57faaf0 Christos Stathis
118 81918908 Christos Stathis
        }
119 a57faaf0 Christos Stathis
        }
120 a57faaf0 Christos Stathis
121 2637c9cd Christos Stathis
    private void trashFolder(final Folder f, final Command callback) {
122 fa68c84d Christos KK Loverdos
            String path = "/" + Const.TRASH_CONTAINER + "/" + f.getPrefix();
123 cc0120ab Christos KK Loverdos
            app.copyFolder(f, app.getUserID(), path, true, callback);
124 2637c9cd Christos Stathis
    }
125 d58c0da5 Christos Stathis
  
126 7811b9d1 Christos Stathis
    protected void trashFiles(final Iterator<File> iter, final Command callback) {
127 81918908 Christos Stathis
        if (iter.hasNext()) {
128 81918908 Christos Stathis
            File file = iter.next();
129 fa68c84d Christos KK Loverdos
            String path = "/" + Const.TRASH_CONTAINER + "/" + file.getPath();
130 28e270f9 Christos KK Loverdos
            PutRequest trashFile = new PutRequest(Pithos.getStorageAPIURL(), app.getUserID(), path) {
131 81918908 Christos Stathis
                @Override
132 ebead1b5 Christos Stathis
                public void onSuccess(Resource result) {
133 2637c9cd Christos Stathis
                    trashFiles(iter, callback);
134 81918908 Christos Stathis
                }
135 81918908 Christos Stathis
136 81918908 Christos Stathis
                @Override
137 81918908 Christos Stathis
                public void onError(Throwable t) {
138 81918908 Christos Stathis
                    GWT.log("", t);
139 3f8622d4 Christos Stathis
                                        app.setError(t);
140 81918908 Christos Stathis
                    if (t instanceof RestException) {
141 a7c43f26 Christos Stathis
                        app.displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
142 81918908 Christos Stathis
                    }
143 81918908 Christos Stathis
                    else
144 a7c43f26 Christos Stathis
                        app.displayError("System error unable to copy file: "+t.getMessage());
145 81918908 Christos Stathis
                }
146 9539e23d Christos Stathis
147 9539e23d Christos Stathis
                                @Override
148 9539e23d Christos Stathis
                                protected void onUnauthorized(Response response) {
149 9539e23d Christos Stathis
                                        app.sessionExpired();
150 9539e23d Christos Stathis
                                }
151 81918908 Christos Stathis
            };
152 cc0120ab Christos KK Loverdos
            trashFile.setHeader("X-Auth-Token", app.getUserToken());
153 a2411896 Christos Stathis
            trashFile.setHeader("X-Move-From", URL.encodePathSegment(file.getUri()));
154 3c058715 Christos Stathis
            trashFile.setHeader("Content-Type", file.getContentType());
155 81918908 Christos Stathis
            Scheduler.get().scheduleDeferred(trashFile);
156 81918908 Christos Stathis
        }
157 7b28ae07 Christos Stathis
        else if (callback != null) {
158 2637c9cd Christos Stathis
            callback.execute();
159 2637c9cd Christos Stathis
        }
160 2637c9cd Christos Stathis
    }
161 2637c9cd Christos Stathis
162 7811b9d1 Christos Stathis
    protected void trashSubfolders(final Iterator<Folder> iter, final Command callback) {
163 2637c9cd Christos Stathis
        if (iter.hasNext()) {
164 2637c9cd Christos Stathis
            final Folder f = iter.next();
165 fd136d33 Christos Stathis
            trashFolder(f, new Command() {
166 fd136d33 Christos Stathis
                                
167 fd136d33 Christos Stathis
                                @Override
168 fd136d33 Christos Stathis
                                public void execute() {
169 fd136d33 Christos Stathis
                                        trashSubfolders(iter, callback);
170 fd136d33 Christos Stathis
                                }
171 fd136d33 Christos Stathis
                        });
172 2637c9cd Christos Stathis
        }
173 16e8478a Christos Stathis
        else  {
174 16e8478a Christos Stathis
                app.updateTrash(false, callback);
175 81918908 Christos Stathis
        }
176 81918908 Christos Stathis
    }
177 a57faaf0 Christos Stathis
}