Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / commands / PasteCommand.java @ 49df7570

History | View | Annotate | Download (8.6 kB)

1 ab1eb3f8 Christos Stathis
/*
2 58777026 Christos Stathis
 * Copyright 2011 GRNET S.A. All rights reserved.
3 58777026 Christos Stathis
 *
4 58777026 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 58777026 Christos Stathis
 * without modification, are permitted provided that the following
6 58777026 Christos Stathis
 * conditions are met:
7 58777026 Christos Stathis
 *
8 58777026 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 58777026 Christos Stathis
 *      disclaimer.
11 58777026 Christos Stathis
 *
12 58777026 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 58777026 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 58777026 Christos Stathis
 *      provided with the distribution.
16 58777026 Christos Stathis
 *
17 58777026 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 58777026 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 58777026 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 58777026 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 58777026 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 58777026 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 58777026 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 58777026 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 58777026 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 58777026 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 58777026 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 58777026 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 58777026 Christos Stathis
 *
30 58777026 Christos Stathis
 * The views and conclusions contained in the software and
31 58777026 Christos Stathis
 * documentation are those of the authors and should not be
32 58777026 Christos Stathis
 * interpreted as representing official policies, either expressed
33 58777026 Christos Stathis
 * or implied, of GRNET S.A.
34 ab1eb3f8 Christos Stathis
 */
35 ab1eb3f8 Christos Stathis
package gr.grnet.pithos.web.client.commands;
36 ab1eb3f8 Christos Stathis
37 6b4a2499 Christos Stathis
import com.google.gwt.core.client.Scheduler;
38 6b4a2499 Christos Stathis
import gr.grnet.pithos.web.client.Clipboard;
39 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.GSS;
40 6b4a2499 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.File;
41 6b4a2499 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Folder;
42 6b4a2499 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Resource;
43 6b4a2499 Christos Stathis
import gr.grnet.pithos.web.client.rest.PutRequest;
44 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
45 ab1eb3f8 Christos Stathis
46 6b4a2499 Christos Stathis
import java.util.Iterator;
47 ab1eb3f8 Christos Stathis
import java.util.List;
48 ab1eb3f8 Christos Stathis
49 ab1eb3f8 Christos Stathis
import com.google.gwt.core.client.GWT;
50 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.Command;
51 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.PopupPanel;
52 ab1eb3f8 Christos Stathis
53 ab1eb3f8 Christos Stathis
public class PasteCommand implements Command {
54 ab1eb3f8 Christos Stathis
55 6b4a2499 Christos Stathis
    private GSS app;
56 ab1eb3f8 Christos Stathis
        private PopupPanel containerPanel;
57 6b4a2499 Christos Stathis
    private Folder folder;
58 ab1eb3f8 Christos Stathis
59 6b4a2499 Christos Stathis
        public PasteCommand(GSS _app, PopupPanel _containerPanel, Folder _folder) {
60 6b4a2499 Christos Stathis
        app = _app;
61 ab1eb3f8 Christos Stathis
                containerPanel = _containerPanel;
62 6b4a2499 Christos Stathis
        folder = _folder;
63 ab1eb3f8 Christos Stathis
        }
64 ab1eb3f8 Christos Stathis
65 ab1eb3f8 Christos Stathis
        @Override
66 ab1eb3f8 Christos Stathis
        public void execute() {
67 ab1eb3f8 Christos Stathis
                containerPanel.hide();
68 6b4a2499 Christos Stathis
        Object clipboardItem = app.getClipboard().getItem();
69 6b4a2499 Christos Stathis
        if (clipboardItem == null)
70 6b4a2499 Christos Stathis
            return;
71 6b4a2499 Christos Stathis
        int operation = app.getClipboard().getOperation();
72 6b4a2499 Christos Stathis
        if (clipboardItem instanceof Folder) {
73 2a497ea1 Christos Stathis
            final Folder tobeCopied = (Folder) clipboardItem;
74 6b4a2499 Christos Stathis
            if (operation == Clipboard.COPY) {
75 2a497ea1 Christos Stathis
                copyFolder(tobeCopied, folder.getUri(), new Command() {
76 2a497ea1 Christos Stathis
                    @Override
77 2a497ea1 Christos Stathis
                    public void execute() {
78 2a497ea1 Christos Stathis
                        app.getClipboard().clear();
79 2a497ea1 Christos Stathis
                        app.updateFolder(folder);
80 2a497ea1 Christos Stathis
                    }
81 2a497ea1 Christos Stathis
                });
82 6b4a2499 Christos Stathis
            }
83 6b4a2499 Christos Stathis
            else {
84 49df7570 Christos Stathis
                copyFolder(tobeCopied, folder.getUri(), new Command() {
85 49df7570 Christos Stathis
                    @Override
86 49df7570 Christos Stathis
                    public void execute() {
87 49df7570 Christos Stathis
                        app.getClipboard().clear();
88 49df7570 Christos Stathis
                        app.deleteFolder(tobeCopied);
89 49df7570 Christos Stathis
                        app.updateFolder(folder);
90 49df7570 Christos Stathis
                    }
91 49df7570 Christos Stathis
                });
92 6b4a2499 Christos Stathis
93 6b4a2499 Christos Stathis
            }
94 6b4a2499 Christos Stathis
        }
95 6b4a2499 Christos Stathis
        else {
96 6b4a2499 Christos Stathis
            List<File> tobeCopied = (List<File>) clipboardItem;
97 6b4a2499 Christos Stathis
            Iterator<File> iter = tobeCopied.iterator();
98 6b4a2499 Christos Stathis
            if (operation == Clipboard.COPY) {
99 2a497ea1 Christos Stathis
                copyFiles(iter, folder.getUri(), new Command() {
100 2a497ea1 Christos Stathis
                    @Override
101 2a497ea1 Christos Stathis
                    public void execute() {
102 2a497ea1 Christos Stathis
                        app.getClipboard().clear();
103 2a497ea1 Christos Stathis
                        app.updateFolder(folder);
104 2a497ea1 Christos Stathis
                    }
105 2a497ea1 Christos Stathis
                });
106 6b4a2499 Christos Stathis
            }
107 6b4a2499 Christos Stathis
            else {
108 2a497ea1 Christos Stathis
                moveFiles(iter, new Command() {
109 2a497ea1 Christos Stathis
                    @Override
110 2a497ea1 Christos Stathis
                    public void execute() {
111 2a497ea1 Christos Stathis
                        app.getClipboard().clear();
112 2a497ea1 Christos Stathis
                        app.updateFolder(folder);
113 2a497ea1 Christos Stathis
                    }
114 2a497ea1 Christos Stathis
                });
115 6b4a2499 Christos Stathis
            }
116 6b4a2499 Christos Stathis
        }
117 ab1eb3f8 Christos Stathis
        }
118 ab1eb3f8 Christos Stathis
119 2a497ea1 Christos Stathis
    private void moveFiles(final Iterator<File> iter, final Command callback) {
120 6b4a2499 Christos Stathis
        if (iter.hasNext()) {
121 6b4a2499 Christos Stathis
            File file = iter.next();
122 6b4a2499 Christos Stathis
            String path = app.getApiPath() + app.getUsername() + folder.getUri() + "/" + file.getName();
123 6b4a2499 Christos Stathis
            PutRequest copyFile = new PutRequest(path) {
124 6b4a2499 Christos Stathis
                @Override
125 6b4a2499 Christos Stathis
                public void onSuccess(Resource result) {
126 2a497ea1 Christos Stathis
                    moveFiles(iter, callback);
127 6b4a2499 Christos Stathis
                }
128 6b4a2499 Christos Stathis
129 6b4a2499 Christos Stathis
                @Override
130 6b4a2499 Christos Stathis
                public void onError(Throwable t) {
131 6b4a2499 Christos Stathis
                    GWT.log("", t);
132 6b4a2499 Christos Stathis
                    if (t instanceof RestException) {
133 6b4a2499 Christos Stathis
                        GSS.get().displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
134 6b4a2499 Christos Stathis
                    }
135 6b4a2499 Christos Stathis
                    else
136 6b4a2499 Christos Stathis
                        GSS.get().displayError("System error unable to copy file: "+t.getMessage());
137 6b4a2499 Christos Stathis
                }
138 6b4a2499 Christos Stathis
            };
139 6b4a2499 Christos Stathis
            copyFile.setHeader("X-Auth-Token", app.getToken());
140 6b4a2499 Christos Stathis
            copyFile.setHeader("X-Move-From", file.getUri());
141 6b4a2499 Christos Stathis
            Scheduler.get().scheduleDeferred(copyFile);
142 6b4a2499 Christos Stathis
        }
143 2a497ea1 Christos Stathis
        else if (callback != null) {
144 2a497ea1 Christos Stathis
            callback.execute();
145 6b4a2499 Christos Stathis
        }
146 6b4a2499 Christos Stathis
    }
147 6b4a2499 Christos Stathis
148 2a497ea1 Christos Stathis
    private void copyFiles(final Iterator<File> iter, final String targetUri, final Command callback) {
149 6b4a2499 Christos Stathis
        if (iter.hasNext()) {
150 6b4a2499 Christos Stathis
            File file = iter.next();
151 2a497ea1 Christos Stathis
            String path = app.getApiPath() + app.getUsername() + targetUri + "/" + file.getName();
152 6b4a2499 Christos Stathis
            PutRequest copyFile = new PutRequest(path) {
153 6b4a2499 Christos Stathis
                @Override
154 6b4a2499 Christos Stathis
                public void onSuccess(Resource result) {
155 2a497ea1 Christos Stathis
                    copyFiles(iter, targetUri, callback);
156 6b4a2499 Christos Stathis
                }
157 6b4a2499 Christos Stathis
158 6b4a2499 Christos Stathis
                @Override
159 6b4a2499 Christos Stathis
                public void onError(Throwable t) {
160 6b4a2499 Christos Stathis
                    GWT.log("", t);
161 6b4a2499 Christos Stathis
                    if (t instanceof RestException) {
162 6b4a2499 Christos Stathis
                        GSS.get().displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
163 6b4a2499 Christos Stathis
                    }
164 6b4a2499 Christos Stathis
                    else
165 6b4a2499 Christos Stathis
                        GSS.get().displayError("System error unable to copy file: "+t.getMessage());
166 6b4a2499 Christos Stathis
                }
167 6b4a2499 Christos Stathis
            };
168 6b4a2499 Christos Stathis
            copyFile.setHeader("X-Auth-Token", app.getToken());
169 6b4a2499 Christos Stathis
            copyFile.setHeader("X-Copy-From", file.getUri());
170 6b4a2499 Christos Stathis
            Scheduler.get().scheduleDeferred(copyFile);
171 6b4a2499 Christos Stathis
        }
172 2a497ea1 Christos Stathis
        else  if (callback != null) {
173 2a497ea1 Christos Stathis
            callback.execute();
174 2a497ea1 Christos Stathis
        }
175 2a497ea1 Christos Stathis
    }
176 2a497ea1 Christos Stathis
177 2a497ea1 Christos Stathis
    private void copyFolder(final Folder f, final String targetUri, final Command callback) {
178 2a497ea1 Christos Stathis
        String path = app.getApiPath() + app.getUsername() + targetUri + "/" + f.getName();
179 2a497ea1 Christos Stathis
        PutRequest createFolder = new PutRequest(path) {
180 2a497ea1 Christos Stathis
            @Override
181 2a497ea1 Christos Stathis
            public void onSuccess(Resource result) {
182 2a497ea1 Christos Stathis
                Iterator<File> iter = f.getFiles().iterator();
183 2a497ea1 Christos Stathis
                copyFiles(iter, targetUri + "/" + f.getName(), new Command() {
184 2a497ea1 Christos Stathis
                    @Override
185 2a497ea1 Christos Stathis
                    public void execute() {
186 2a497ea1 Christos Stathis
                        Iterator<Folder> iterf = f.getSubfolders().iterator();
187 2a497ea1 Christos Stathis
                        copySubfolders(iterf, targetUri + "/" + f.getName(), new Command() {
188 2a497ea1 Christos Stathis
                            @Override
189 2a497ea1 Christos Stathis
                            public void execute() {
190 2a497ea1 Christos Stathis
                                callback.execute();
191 2a497ea1 Christos Stathis
                            }
192 2a497ea1 Christos Stathis
                        });
193 2a497ea1 Christos Stathis
                    }
194 2a497ea1 Christos Stathis
                });
195 2a497ea1 Christos Stathis
            }
196 2a497ea1 Christos Stathis
197 2a497ea1 Christos Stathis
            @Override
198 2a497ea1 Christos Stathis
            public void onError(Throwable t) {
199 2a497ea1 Christos Stathis
                GWT.log("", t);
200 2a497ea1 Christos Stathis
                if (t instanceof RestException) {
201 2a497ea1 Christos Stathis
                    app.displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
202 2a497ea1 Christos Stathis
                }
203 2a497ea1 Christos Stathis
                else
204 2a497ea1 Christos Stathis
                    app.displayError("System error creating folder:" + t.getMessage());
205 2a497ea1 Christos Stathis
            }
206 2a497ea1 Christos Stathis
        };
207 2a497ea1 Christos Stathis
        createFolder.setHeader("X-Auth-Token", app.getToken());
208 2a497ea1 Christos Stathis
        createFolder.setHeader("Accept", "*/*");
209 2a497ea1 Christos Stathis
        createFolder.setHeader("Content-Length", "0");
210 2a497ea1 Christos Stathis
        createFolder.setHeader("Content-Type", "application/folder");
211 2a497ea1 Christos Stathis
        Scheduler.get().scheduleDeferred(createFolder);
212 2a497ea1 Christos Stathis
    }
213 2a497ea1 Christos Stathis
214 2a497ea1 Christos Stathis
    private void copySubfolders(final Iterator<Folder> iter, final String targetUri, final Command callback) {
215 2a497ea1 Christos Stathis
        if (iter.hasNext()) {
216 2a497ea1 Christos Stathis
            final Folder f = iter.next();
217 2a497ea1 Christos Stathis
            copyFolder(f, targetUri, callback);
218 2a497ea1 Christos Stathis
        }
219 2a497ea1 Christos Stathis
        else  if (callback != null) {
220 2a497ea1 Christos Stathis
            callback.execute();
221 6b4a2499 Christos Stathis
        }
222 6b4a2499 Christos Stathis
    }
223 ab1eb3f8 Christos Stathis
}