root / src / gr / grnet / pithos / web / client / commands / PasteCommand.java @ 1f2a020d
History | View | Annotate | Download (8.3 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 | 1e413f9b | Christos Stathis | import com.google.gwt.core.client.Scheduler; |
38 | 1e413f9b | Christos Stathis | import gr.grnet.pithos.web.client.Clipboard; |
39 | a57faaf0 | Christos Stathis | import gr.grnet.pithos.web.client.GSS; |
40 | 1e413f9b | Christos Stathis | import gr.grnet.pithos.web.client.foldertree.File; |
41 | 1e413f9b | Christos Stathis | import gr.grnet.pithos.web.client.foldertree.Folder; |
42 | 1e413f9b | Christos Stathis | import gr.grnet.pithos.web.client.foldertree.Resource; |
43 | 1e413f9b | Christos Stathis | import gr.grnet.pithos.web.client.rest.PutRequest; |
44 | a57faaf0 | Christos Stathis | import gr.grnet.pithos.web.client.rest.RestException; |
45 | a57faaf0 | Christos Stathis | |
46 | 1e413f9b | Christos Stathis | import java.util.Iterator; |
47 | a57faaf0 | Christos Stathis | import java.util.List; |
48 | a57faaf0 | Christos Stathis | |
49 | a57faaf0 | Christos Stathis | import com.google.gwt.core.client.GWT; |
50 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.Command; |
51 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.PopupPanel; |
52 | a57faaf0 | Christos Stathis | |
53 | a57faaf0 | Christos Stathis | public class PasteCommand implements Command { |
54 | a57faaf0 | Christos Stathis | |
55 | 1e413f9b | Christos Stathis | private GSS app;
|
56 | a57faaf0 | Christos Stathis | private PopupPanel containerPanel;
|
57 | 1e413f9b | Christos Stathis | private Folder folder;
|
58 | a57faaf0 | Christos Stathis | |
59 | 1e413f9b | Christos Stathis | public PasteCommand(GSS _app, PopupPanel _containerPanel, Folder _folder) {
|
60 | 1e413f9b | Christos Stathis | app = _app; |
61 | a57faaf0 | Christos Stathis | containerPanel = _containerPanel; |
62 | 1e413f9b | Christos Stathis | folder = _folder; |
63 | a57faaf0 | Christos Stathis | } |
64 | a57faaf0 | Christos Stathis | |
65 | a57faaf0 | Christos Stathis | @Override
|
66 | a57faaf0 | Christos Stathis | public void execute() { |
67 | a57faaf0 | Christos Stathis | containerPanel.hide(); |
68 | 1e413f9b | Christos Stathis | Object clipboardItem = app.getClipboard().getItem();
|
69 | 1e413f9b | Christos Stathis | if (clipboardItem == null) |
70 | 1e413f9b | Christos Stathis | return;
|
71 | 1e413f9b | Christos Stathis | int operation = app.getClipboard().getOperation();
|
72 | 1e413f9b | Christos Stathis | if (clipboardItem instanceof Folder) { |
73 | 1f2a020d | Christos Stathis | final Folder tobeCopied = (Folder) clipboardItem;
|
74 | 1e413f9b | Christos Stathis | if (operation == Clipboard.COPY) { |
75 | 1f2a020d | Christos Stathis | copyFolder(tobeCopied, folder.getUri(), new Command() {
|
76 | 1f2a020d | Christos Stathis | @Override
|
77 | 1f2a020d | Christos Stathis | public void execute() { |
78 | 1f2a020d | Christos Stathis | app.getClipboard().clear(); |
79 | 1f2a020d | Christos Stathis | app.updateFolder(folder); |
80 | 1f2a020d | Christos Stathis | } |
81 | 1f2a020d | Christos Stathis | }); |
82 | 1e413f9b | Christos Stathis | } |
83 | 1e413f9b | Christos Stathis | else {
|
84 | 1e413f9b | Christos Stathis | |
85 | 1e413f9b | Christos Stathis | } |
86 | 1e413f9b | Christos Stathis | } |
87 | 1e413f9b | Christos Stathis | else {
|
88 | 1e413f9b | Christos Stathis | List<File> tobeCopied = (List<File>) clipboardItem; |
89 | 1e413f9b | Christos Stathis | Iterator<File> iter = tobeCopied.iterator(); |
90 | 1e413f9b | Christos Stathis | if (operation == Clipboard.COPY) { |
91 | 1f2a020d | Christos Stathis | copyFiles(iter, folder.getUri(), new Command() {
|
92 | 1f2a020d | Christos Stathis | @Override
|
93 | 1f2a020d | Christos Stathis | public void execute() { |
94 | 1f2a020d | Christos Stathis | app.getClipboard().clear(); |
95 | 1f2a020d | Christos Stathis | app.updateFolder(folder); |
96 | 1f2a020d | Christos Stathis | } |
97 | 1f2a020d | Christos Stathis | }); |
98 | 1e413f9b | Christos Stathis | } |
99 | 1e413f9b | Christos Stathis | else {
|
100 | 1f2a020d | Christos Stathis | moveFiles(iter, new Command() {
|
101 | 1f2a020d | Christos Stathis | @Override
|
102 | 1f2a020d | Christos Stathis | public void execute() { |
103 | 1f2a020d | Christos Stathis | app.getClipboard().clear(); |
104 | 1f2a020d | Christos Stathis | app.updateFolder(folder); |
105 | 1f2a020d | Christos Stathis | } |
106 | 1f2a020d | Christos Stathis | }); |
107 | 1e413f9b | Christos Stathis | } |
108 | 1e413f9b | Christos Stathis | } |
109 | a57faaf0 | Christos Stathis | } |
110 | a57faaf0 | Christos Stathis | |
111 | 1f2a020d | Christos Stathis | private void moveFiles(final Iterator<File> iter, final Command callback) { |
112 | 1e413f9b | Christos Stathis | if (iter.hasNext()) {
|
113 | 1e413f9b | Christos Stathis | File file = iter.next();
|
114 | 1e413f9b | Christos Stathis | String path = app.getApiPath() + app.getUsername() + folder.getUri() + "/" + file.getName(); |
115 | 1e413f9b | Christos Stathis | PutRequest copyFile = new PutRequest(path) {
|
116 | 1e413f9b | Christos Stathis | @Override
|
117 | 1e413f9b | Christos Stathis | public void onSuccess(Resource result) { |
118 | 1f2a020d | Christos Stathis | moveFiles(iter, callback); |
119 | 1e413f9b | Christos Stathis | } |
120 | 1e413f9b | Christos Stathis | |
121 | 1e413f9b | Christos Stathis | @Override
|
122 | 1e413f9b | Christos Stathis | public void onError(Throwable t) { |
123 | 1e413f9b | Christos Stathis | GWT.log("", t);
|
124 | 1e413f9b | Christos Stathis | if (t instanceof RestException) { |
125 | 1e413f9b | Christos Stathis | GSS.get().displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
|
126 | 1e413f9b | Christos Stathis | } |
127 | 1e413f9b | Christos Stathis | else
|
128 | 1e413f9b | Christos Stathis | GSS.get().displayError("System error unable to copy file: "+t.getMessage());
|
129 | 1e413f9b | Christos Stathis | } |
130 | 1e413f9b | Christos Stathis | }; |
131 | 1e413f9b | Christos Stathis | copyFile.setHeader("X-Auth-Token", app.getToken());
|
132 | 1e413f9b | Christos Stathis | copyFile.setHeader("X-Move-From", file.getUri());
|
133 | 1e413f9b | Christos Stathis | Scheduler.get().scheduleDeferred(copyFile); |
134 | 1e413f9b | Christos Stathis | } |
135 | 1f2a020d | Christos Stathis | else if (callback != null) { |
136 | 1f2a020d | Christos Stathis | callback.execute(); |
137 | 1e413f9b | Christos Stathis | } |
138 | 1e413f9b | Christos Stathis | } |
139 | 1e413f9b | Christos Stathis | |
140 | 1f2a020d | Christos Stathis | private void copyFiles(final Iterator<File> iter, final String targetUri, final Command callback) { |
141 | 1e413f9b | Christos Stathis | if (iter.hasNext()) {
|
142 | 1e413f9b | Christos Stathis | File file = iter.next();
|
143 | 1f2a020d | Christos Stathis | String path = app.getApiPath() + app.getUsername() + targetUri + "/" + file.getName(); |
144 | 1e413f9b | Christos Stathis | PutRequest copyFile = new PutRequest(path) {
|
145 | 1e413f9b | Christos Stathis | @Override
|
146 | 1e413f9b | Christos Stathis | public void onSuccess(Resource result) { |
147 | 1f2a020d | Christos Stathis | copyFiles(iter, targetUri, callback); |
148 | 1e413f9b | Christos Stathis | } |
149 | 1e413f9b | Christos Stathis | |
150 | 1e413f9b | Christos Stathis | @Override
|
151 | 1e413f9b | Christos Stathis | public void onError(Throwable t) { |
152 | 1e413f9b | Christos Stathis | GWT.log("", t);
|
153 | 1e413f9b | Christos Stathis | if (t instanceof RestException) { |
154 | 1e413f9b | Christos Stathis | GSS.get().displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
|
155 | 1e413f9b | Christos Stathis | } |
156 | 1e413f9b | Christos Stathis | else
|
157 | 1e413f9b | Christos Stathis | GSS.get().displayError("System error unable to copy file: "+t.getMessage());
|
158 | 1e413f9b | Christos Stathis | } |
159 | 1e413f9b | Christos Stathis | }; |
160 | 1e413f9b | Christos Stathis | copyFile.setHeader("X-Auth-Token", app.getToken());
|
161 | 1e413f9b | Christos Stathis | copyFile.setHeader("X-Copy-From", file.getUri());
|
162 | 1e413f9b | Christos Stathis | Scheduler.get().scheduleDeferred(copyFile); |
163 | 1e413f9b | Christos Stathis | } |
164 | 1f2a020d | Christos Stathis | else if (callback != null) { |
165 | 1f2a020d | Christos Stathis | callback.execute(); |
166 | 1f2a020d | Christos Stathis | } |
167 | 1f2a020d | Christos Stathis | } |
168 | 1f2a020d | Christos Stathis | |
169 | 1f2a020d | Christos Stathis | private void copyFolder(final Folder f, final String targetUri, final Command callback) { |
170 | 1f2a020d | Christos Stathis | String path = app.getApiPath() + app.getUsername() + targetUri + "/" + f.getName(); |
171 | 1f2a020d | Christos Stathis | PutRequest createFolder = new PutRequest(path) {
|
172 | 1f2a020d | Christos Stathis | @Override
|
173 | 1f2a020d | Christos Stathis | public void onSuccess(Resource result) { |
174 | 1f2a020d | Christos Stathis | Iterator<File> iter = f.getFiles().iterator(); |
175 | 1f2a020d | Christos Stathis | copyFiles(iter, targetUri + "/" + f.getName(), new Command() { |
176 | 1f2a020d | Christos Stathis | @Override
|
177 | 1f2a020d | Christos Stathis | public void execute() { |
178 | 1f2a020d | Christos Stathis | Iterator<Folder> iterf = f.getSubfolders().iterator();
|
179 | 1f2a020d | Christos Stathis | copySubfolders(iterf, targetUri + "/" + f.getName(), new Command() { |
180 | 1f2a020d | Christos Stathis | @Override
|
181 | 1f2a020d | Christos Stathis | public void execute() { |
182 | 1f2a020d | Christos Stathis | callback.execute(); |
183 | 1f2a020d | Christos Stathis | } |
184 | 1f2a020d | Christos Stathis | }); |
185 | 1f2a020d | Christos Stathis | } |
186 | 1f2a020d | Christos Stathis | }); |
187 | 1f2a020d | Christos Stathis | } |
188 | 1f2a020d | Christos Stathis | |
189 | 1f2a020d | Christos Stathis | @Override
|
190 | 1f2a020d | Christos Stathis | public void onError(Throwable t) { |
191 | 1f2a020d | Christos Stathis | GWT.log("", t);
|
192 | 1f2a020d | Christos Stathis | if (t instanceof RestException) { |
193 | 1f2a020d | Christos Stathis | app.displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
|
194 | 1f2a020d | Christos Stathis | } |
195 | 1f2a020d | Christos Stathis | else
|
196 | 1f2a020d | Christos Stathis | app.displayError("System error creating folder:" + t.getMessage());
|
197 | 1f2a020d | Christos Stathis | } |
198 | 1f2a020d | Christos Stathis | }; |
199 | 1f2a020d | Christos Stathis | createFolder.setHeader("X-Auth-Token", app.getToken());
|
200 | 1f2a020d | Christos Stathis | createFolder.setHeader("Accept", "*/*"); |
201 | 1f2a020d | Christos Stathis | createFolder.setHeader("Content-Length", "0"); |
202 | 1f2a020d | Christos Stathis | createFolder.setHeader("Content-Type", "application/folder"); |
203 | 1f2a020d | Christos Stathis | Scheduler.get().scheduleDeferred(createFolder); |
204 | 1f2a020d | Christos Stathis | } |
205 | 1f2a020d | Christos Stathis | |
206 | 1f2a020d | Christos Stathis | private void copySubfolders(final Iterator<Folder> iter, final String targetUri, final Command callback) { |
207 | 1f2a020d | Christos Stathis | if (iter.hasNext()) {
|
208 | 1f2a020d | Christos Stathis | final Folder f = iter.next();
|
209 | 1f2a020d | Christos Stathis | copyFolder(f, targetUri, callback); |
210 | 1f2a020d | Christos Stathis | } |
211 | 1f2a020d | Christos Stathis | else if (callback != null) { |
212 | 1f2a020d | Christos Stathis | callback.execute(); |
213 | 1e413f9b | Christos Stathis | } |
214 | 1e413f9b | Christos Stathis | } |
215 | a57faaf0 | Christos Stathis | } |