2 * Copyright 2011 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and
31 * documentation are those of the authors and should not be
32 * interpreted as representing official policies, either expressed
33 * or implied, of GRNET S.A.
35 package gr.grnet.pithos.web.client.commands;
37 import gr.grnet.pithos.web.client.GSS;
38 import gr.grnet.pithos.web.client.clipboard.Clipboard;
39 import gr.grnet.pithos.web.client.clipboard.ClipboardItem;
40 import gr.grnet.pithos.web.client.rest.PostCommand;
41 import gr.grnet.pithos.web.client.rest.RestException;
42 import gr.grnet.pithos.web.client.rest.resource.FileResource;
43 import gr.grnet.pithos.web.client.rest.resource.FolderResource;
44 import gr.grnet.pithos.web.client.rest.resource.GroupResource;
45 import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
47 import java.util.ArrayList;
48 import java.util.List;
50 import com.google.gwt.core.client.GWT;
51 import com.google.gwt.http.client.URL;
52 import com.google.gwt.user.client.Command;
53 import com.google.gwt.user.client.DeferredCommand;
54 import com.google.gwt.user.client.ui.PopupPanel;
56 public class PasteCommand implements Command {
58 private PopupPanel containerPanel;
60 public PasteCommand(PopupPanel _containerPanel) {
61 containerPanel = _containerPanel;
65 public void execute() {
66 containerPanel.hide();
67 Object selection = GSS.get().getCurrentSelection();
68 FolderResource selectedFolder = null;
69 if(selection != null && selection instanceof RestResourceWrapper)
70 selectedFolder = ((RestResourceWrapper)selection).getResource();
73 else if(GSS.get().getFolders().getCurrent() != null && ((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource() != null)
74 selectedFolder = ((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource();
76 if (selectedFolder != null) {
77 final ClipboardItem citem = GSS.get().getClipboard().getItem();
78 if (citem != null && citem.getRestResourceWrapper() != null) {
79 String target = selectedFolder.getUri();
80 target = target.endsWith("/") ? target : target + '/';
81 target = target + URL.encodeComponent(citem.getRestResourceWrapper().getResource().getName());
82 if (citem.getOperation() == Clipboard.COPY) {
83 PostCommand cf = new PostCommand(citem.getRestResourceWrapper().getUri() + "?copy=" + target, "", 200) {
86 public void onComplete() {
88 //GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
89 GSS.get().getTreeView().updateNodeChildren(GSS.get().getTreeView().getSelection());
90 GSS.get().getStatusPanel().updateStats();
91 GSS.get().getClipboard().setItem(null);
95 public void onError(Throwable t) {
97 if(t instanceof RestException){
98 int statusCode = ((RestException)t).getHttpStatusCode();
100 GSS.get().displayError("You don't have the necessary permissions");
102 else if(statusCode == 409)
103 GSS.get().displayError("A folder with the same name already exists");
104 else if(statusCode == 413)
105 GSS.get().displayError("Your quota has been exceeded");
107 GSS.get().displayError("Unable to copy folder:"+((RestException)t).getHttpStatusText());
110 GSS.get().displayError("System error copying folder:"+t.getMessage());
113 DeferredCommand.addCommand(cf);
114 } else if (citem.getOperation() == Clipboard.CUT) {
115 PostCommand cf = new PostCommand(citem.getRestResourceWrapper().getUri() + "?move=" + target, "", 200) {
118 public void onComplete() {
121 List<TreeItem> items = GSS.get().getFolders().getItemsOfTreeForPath(citem.getFolderResource().getUri());
122 for (TreeItem item : items)
123 if (item.getParentItem() != null && !item.equals(GSS.get().getFolders().getCurrent()))
124 GSS.get().getFolders().updateFolder((DnDTreeItem) item.getParentItem());
125 GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
127 GSS.get().getTreeView().updateNodeChildren(GSS.get().getTreeView().getSelection());
128 GSS.get().getTreeView().updateNodeChildrenForRemove(citem.getRestResourceWrapper().getResource().getParentURI());
129 GSS.get().getStatusPanel().updateStats();
130 GSS.get().getClipboard().setItem(null);
134 public void onError(Throwable t) {
136 if(t instanceof RestException){
137 int statusCode = ((RestException)t).getHttpStatusCode();
138 if(statusCode == 405)
139 GSS.get().displayError("You don't have the necessary permissions");
140 else if(statusCode == 409)
141 GSS.get().displayError("A folder with the same name already exists");
142 else if(statusCode == 413)
143 GSS.get().displayError("Your quota has been exceeded");
145 GSS.get().displayError("Unable to move folder:"+((RestException)t).getHttpStatusText());
148 GSS.get().displayError("System error moving folder:"+t.getMessage());
151 DeferredCommand.addCommand(cf);
154 } else if (citem != null && citem.getFile() != null) {
155 String target = selectedFolder.getUri();
156 target = target.endsWith("/") ? target : target + '/';
157 target = target + URL.encodeComponent(citem.getFile().getName());
158 if (citem.getOperation() == Clipboard.COPY) {
159 PostCommand cf = new PostCommand(citem.getFile().getUri() + "?copy=" + target, "", 200) {
162 public void onComplete() {
163 GSS.get().showFileList(true);
164 GSS.get().getStatusPanel().updateStats();
165 GSS.get().getClipboard().setItem(null);
169 public void onError(Throwable t) {
171 if(t instanceof RestException){
172 int statusCode = ((RestException)t).getHttpStatusCode();
173 if(statusCode == 405)
174 GSS.get().displayError("You don't have the necessary permissions");
175 else if(statusCode == 404)
176 GSS.get().displayError("File not found");
177 else if(statusCode == 409)
178 GSS.get().displayError("A file with the same name already exists");
179 else if(statusCode == 413)
180 GSS.get().displayError("Your quota has been exceeded");
182 GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
185 GSS.get().displayError("System error copying file:"+t.getMessage());
188 DeferredCommand.addCommand(cf);
189 } else if (citem.getOperation() == Clipboard.CUT) {
190 PostCommand cf = new PostCommand(citem.getFile().getUri() + "?move=" + target, "", 200) {
193 public void onComplete() {
194 GSS.get().showFileList(true);
195 GSS.get().getStatusPanel().updateStats();
196 GSS.get().getClipboard().setItem(null);
200 public void onError(Throwable t) {
202 if(t instanceof RestException){
203 int statusCode = ((RestException)t).getHttpStatusCode();
204 if(statusCode == 405)
205 GSS.get().displayError("You don't have the necessary permissions");
206 else if(statusCode == 404)
207 GSS.get().displayError("File not found");
208 else if(statusCode == 409)
209 GSS.get().displayError("A file with the same name already exists");
210 else if(statusCode == 413)
211 GSS.get().displayError("Your quota has been exceeded");
213 GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
216 GSS.get().displayError("System error copying file:"+t.getMessage());
219 DeferredCommand.addCommand(cf);
222 } else if (citem != null && citem.getFiles() != null) {
223 List<FileResource> res = citem.getFiles();
224 List<String> fileIds = new ArrayList<String>();
225 String target = selectedFolder.getUri();
226 target = target.endsWith("/") ? target : target + '/';
228 if (citem.getOperation() == Clipboard.COPY) {
229 for (FileResource fileResource : res) {
230 String fileTarget = target + fileResource.getName();
231 fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
234 executeCopyOrMove(index, fileIds);
236 } else if (citem.getOperation() == Clipboard.CUT) {
237 for (FileResource fileResource : res) {
238 String fileTarget = target + fileResource.getName();
239 fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
242 executeCopyOrMove(index, fileIds);
249 private void executeCopyOrMove(final int index, final List<String> paths){
250 if(index >= paths.size()){
251 GSS.get().showFileList(true);
252 GSS.get().getStatusPanel().updateStats();
253 GSS.get().getClipboard().setItem(null);
256 PostCommand cf = new PostCommand(paths.get(index), "", 200) {
259 public void onComplete() {
260 executeCopyOrMove(index+1, paths);
264 public void onError(Throwable t) {
266 if(t instanceof RestException){
267 int statusCode = ((RestException)t).getHttpStatusCode();
268 if(statusCode == 405)
269 GSS.get().displayError("You don't have the necessary permissions");
270 else if(statusCode == 404)
271 GSS.get().displayError("File not found");
272 else if(statusCode == 409)
273 GSS.get().displayError("A file with the same name already exists");
274 else if(statusCode == 413)
275 GSS.get().displayError("Your quota has been exceeded");
277 GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
280 GSS.get().displayError("System error copying file:"+t.getMessage());
283 DeferredCommand.addCommand(cf);