2 * Copyright 2008, 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
6 * GSS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
19 package gr.ebs.gss.client.commands;
21 import gr.ebs.gss.client.GSS;
22 import gr.ebs.gss.client.clipboard.Clipboard;
23 import gr.ebs.gss.client.clipboard.ClipboardItem;
24 import gr.ebs.gss.client.rest.PostCommand;
25 import gr.ebs.gss.client.rest.RestException;
26 import gr.ebs.gss.client.rest.resource.FileResource;
27 import gr.ebs.gss.client.rest.resource.FolderResource;
28 import gr.ebs.gss.client.rest.resource.GroupResource;
30 import java.util.ArrayList;
31 import java.util.List;
33 import com.google.gwt.core.client.GWT;
34 import com.google.gwt.http.client.URL;
35 import com.google.gwt.user.client.Command;
36 import com.google.gwt.user.client.DeferredCommand;
37 import com.google.gwt.user.client.ui.PopupPanel;
38 import com.google.gwt.user.client.ui.TreeItem;
41 * @author kman Command for pasting Clipboard contents
43 public class PasteCommand implements Command {
45 private PopupPanel containerPanel;
47 public PasteCommand(PopupPanel _containerPanel) {
48 containerPanel = _containerPanel;
52 public void execute() {
53 containerPanel.hide();
54 Object selection = GSS.get().getCurrentSelection();
55 if(selection != null && selection instanceof GroupResource){
56 final ClipboardItem citem = GSS.get().getClipboard().getItem();
57 GroupResource group = (GroupResource) GSS.get().getCurrentSelection();
58 if(citem.getUser() != null){
59 PostCommand cg = new PostCommand(group.getUri()+"?name="+citem.getUser().getUsername(), "", 201){
62 public void onComplete() {
63 GSS.get().getGroups().updateGroups();
64 GSS.get().showUserList();
65 GSS.get().getClipboard().setItem(null);
69 public void onError(Throwable t) {
71 if(t instanceof RestException){
72 int statusCode = ((RestException)t).getHttpStatusCode();
74 GSS.get().displayError("You don't have the necessary permissions");
75 else if(statusCode == 404)
76 GSS.get().displayError("User does not exist");
77 else if(statusCode == 409)
78 GSS.get().displayError("A user with the same name already exists");
79 else if(statusCode == 413)
80 GSS.get().displayError("Your quota has been exceeded");
82 GSS.get().displayError("Unable to add user:"+((RestException)t).getHttpStatusText());
85 GSS.get().displayError("System error adding user:"+t.getMessage());
88 DeferredCommand.addCommand(cg);
92 FolderResource selectedFolder = null;
93 if(selection != null && selection instanceof FolderResource)
94 selectedFolder = (FolderResource)selection;
97 else if(GSS.get().getFolders().getCurrent() != null && ((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource() != null)
98 selectedFolder = ((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource();
100 if (selectedFolder != null) {
101 final ClipboardItem citem = GSS.get().getClipboard().getItem();
102 if (citem != null && citem.getFolderResource() != null) {
103 String target = selectedFolder.getUri();
104 target = target.endsWith("/") ? target : target + '/';
105 target = target + URL.encodeComponent(citem.getFolderResource().getName());
106 if (citem.getOperation() == Clipboard.COPY) {
107 PostCommand cf = new PostCommand(citem.getFolderResource().getUri() + "?copy=" + target, "", 200) {
110 public void onComplete() {
112 //GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
113 GSS.get().getStatusPanel().updateStats();
114 GSS.get().getClipboard().setItem(null);
118 public void onError(Throwable t) {
120 if(t instanceof RestException){
121 int statusCode = ((RestException)t).getHttpStatusCode();
122 if(statusCode == 405)
123 GSS.get().displayError("You don't have the necessary permissions");
125 else if(statusCode == 409)
126 GSS.get().displayError("A folder with the same name already exists");
127 else if(statusCode == 413)
128 GSS.get().displayError("Your quota has been exceeded");
130 GSS.get().displayError("Unable to copy folder:"+((RestException)t).getHttpStatusText());
133 GSS.get().displayError("System error copying folder:"+t.getMessage());
136 DeferredCommand.addCommand(cf);
137 } else if (citem.getOperation() == Clipboard.CUT) {
138 PostCommand cf = new PostCommand(citem.getFolderResource().getUri() + "?move=" + target, "", 200) {
141 public void onComplete() {
144 List<TreeItem> items = GSS.get().getFolders().getItemsOfTreeForPath(citem.getFolderResource().getUri());
145 for (TreeItem item : items)
146 if (item.getParentItem() != null && !item.equals(GSS.get().getFolders().getCurrent()))
147 GSS.get().getFolders().updateFolder((DnDTreeItem) item.getParentItem());
148 GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
150 GSS.get().getStatusPanel().updateStats();
151 GSS.get().getClipboard().setItem(null);
155 public void onError(Throwable t) {
157 if(t instanceof RestException){
158 int statusCode = ((RestException)t).getHttpStatusCode();
159 if(statusCode == 405)
160 GSS.get().displayError("You don't have the necessary permissions");
161 else if(statusCode == 409)
162 GSS.get().displayError("A folder with the same name already exists");
163 else if(statusCode == 413)
164 GSS.get().displayError("Your quota has been exceeded");
166 GSS.get().displayError("Unable to move folder:"+((RestException)t).getHttpStatusText());
169 GSS.get().displayError("System error moving folder:"+t.getMessage());
172 DeferredCommand.addCommand(cf);
175 } else if (citem != null && citem.getFile() != null) {
176 String target = selectedFolder.getUri();
177 target = target.endsWith("/") ? target : target + '/';
178 target = target + URL.encodeComponent(citem.getFile().getName());
179 if (citem.getOperation() == Clipboard.COPY) {
180 PostCommand cf = new PostCommand(citem.getFile().getUri() + "?copy=" + target, "", 200) {
183 public void onComplete() {
184 GSS.get().showFileList(true);
185 GSS.get().getStatusPanel().updateStats();
186 GSS.get().getClipboard().setItem(null);
190 public void onError(Throwable t) {
192 if(t instanceof RestException){
193 int statusCode = ((RestException)t).getHttpStatusCode();
194 if(statusCode == 405)
195 GSS.get().displayError("You don't have the necessary permissions");
196 else if(statusCode == 404)
197 GSS.get().displayError("File not found");
198 else if(statusCode == 409)
199 GSS.get().displayError("A file with the same name already exists");
200 else if(statusCode == 413)
201 GSS.get().displayError("Your quota has been exceeded");
203 GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
206 GSS.get().displayError("System error copying file:"+t.getMessage());
209 DeferredCommand.addCommand(cf);
210 } else if (citem.getOperation() == Clipboard.CUT) {
211 PostCommand cf = new PostCommand(citem.getFile().getUri() + "?move=" + target, "", 200) {
214 public void onComplete() {
215 GSS.get().showFileList(true);
216 GSS.get().getStatusPanel().updateStats();
217 GSS.get().getClipboard().setItem(null);
221 public void onError(Throwable t) {
223 if(t instanceof RestException){
224 int statusCode = ((RestException)t).getHttpStatusCode();
225 if(statusCode == 405)
226 GSS.get().displayError("You don't have the necessary permissions");
227 else if(statusCode == 404)
228 GSS.get().displayError("File not found");
229 else if(statusCode == 409)
230 GSS.get().displayError("A file with the same name already exists");
231 else if(statusCode == 413)
232 GSS.get().displayError("Your quota has been exceeded");
234 GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
237 GSS.get().displayError("System error copying file:"+t.getMessage());
240 DeferredCommand.addCommand(cf);
243 } else if (citem != null && citem.getFiles() != null) {
244 List<FileResource> res = citem.getFiles();
245 List<String> fileIds = new ArrayList<String>();
246 String target = selectedFolder.getUri();
247 target = target.endsWith("/") ? target : target + '/';
249 if (citem.getOperation() == Clipboard.COPY) {
250 for (FileResource fileResource : res) {
251 String fileTarget = target + fileResource.getName();
252 fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
255 executeCopyOrMove(index, fileIds);
257 } else if (citem.getOperation() == Clipboard.CUT) {
258 for (FileResource fileResource : res) {
259 String fileTarget = target + fileResource.getName();
260 fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
263 executeCopyOrMove(index, fileIds);
270 private void executeCopyOrMove(final int index, final List<String> paths){
271 if(index >= paths.size()){
272 GSS.get().showFileList(true);
273 GSS.get().getStatusPanel().updateStats();
274 GSS.get().getClipboard().setItem(null);
277 PostCommand cf = new PostCommand(paths.get(index), "", 200) {
280 public void onComplete() {
281 executeCopyOrMove(index+1, paths);
285 public void onError(Throwable t) {
287 if(t instanceof RestException){
288 int statusCode = ((RestException)t).getHttpStatusCode();
289 if(statusCode == 405)
290 GSS.get().displayError("You don't have the necessary permissions");
291 else if(statusCode == 404)
292 GSS.get().displayError("File not found");
293 else if(statusCode == 409)
294 GSS.get().displayError("A file with the same name already exists");
295 else if(statusCode == 413)
296 GSS.get().displayError("Your quota has been exceeded");
298 GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
301 GSS.get().displayError("System error copying file:"+t.getMessage());
304 DeferredCommand.addCommand(cf);