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.dnd.DnDTreeItem;
25 import gr.ebs.gss.client.rest.PostCommand;
26 import gr.ebs.gss.client.rest.RestException;
27 import gr.ebs.gss.client.rest.resource.FileResource;
28 import gr.ebs.gss.client.rest.resource.FolderResource;
29 import gr.ebs.gss.client.rest.resource.GroupResource;
31 import java.util.ArrayList;
32 import java.util.List;
34 import com.google.gwt.core.client.GWT;
35 import com.google.gwt.http.client.URL;
36 import com.google.gwt.user.client.Command;
37 import com.google.gwt.user.client.DeferredCommand;
38 import com.google.gwt.user.client.ui.PopupPanel;
39 import com.google.gwt.user.client.ui.TreeItem;
42 * @author kman Command for pasting Clipboard contents
44 public class PasteCommand implements Command {
46 private PopupPanel containerPanel;
48 public PasteCommand(PopupPanel _containerPanel) {
49 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();
68 public void onError(Throwable t) {
70 if(t instanceof RestException){
71 int statusCode = ((RestException)t).getHttpStatusCode();
73 GSS.get().displayError("You don't have the necessary permissions");
74 else if(statusCode == 404)
75 GSS.get().displayError("User does not exist");
76 else if(statusCode == 409)
77 GSS.get().displayError("A user with the same name already exists");
78 else if(statusCode == 413)
79 GSS.get().displayError("Your quota has been exceeded");
81 GSS.get().displayError("Unable to add user:"+((RestException)t).getHttpStatusText());
84 GSS.get().displayError("System error adding user:"+t.getMessage());
87 DeferredCommand.addCommand(cg);
91 FolderResource selectedFolder = null;
92 if(selection != null && selection instanceof FolderResource)
93 selectedFolder = (FolderResource)selection;
94 else if(GSS.get().getFolders().getCurrent() != null && ((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource() != null)
95 selectedFolder = ((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource();
96 if (selectedFolder != null) {
97 final ClipboardItem citem = GSS.get().getClipboard().getItem();
98 if (citem != null && citem.getFolderResource() != null) {
99 String target = selectedFolder.getUri();
100 target = target.endsWith("/") ? target : target + '/';
101 target = target + URL.encodeComponent(citem.getFolderResource().getName());
102 if (citem.getOperation() == Clipboard.COPY) {
103 PostCommand cf = new PostCommand(citem.getFolderResource().getUri() + "?copy=" + target, "", 200) {
106 public void onComplete() {
107 GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
108 GSS.get().getStatusPanel().updateStats();
112 public void onError(Throwable t) {
114 if(t instanceof RestException){
115 int statusCode = ((RestException)t).getHttpStatusCode();
116 if(statusCode == 405)
117 GSS.get().displayError("You don't have the necessary permissions");
119 else if(statusCode == 409)
120 GSS.get().displayError("A folder with the same name already exists");
121 else if(statusCode == 413)
122 GSS.get().displayError("Your quota has been exceeded");
124 GSS.get().displayError("Unable to copy folder:"+((RestException)t).getHttpStatusText());
127 GSS.get().displayError("System error copying folder:"+t.getMessage());
130 DeferredCommand.addCommand(cf);
131 } else if (citem.getOperation() == Clipboard.CUT) {
132 PostCommand cf = new PostCommand(citem.getFolderResource().getUri() + "?move=" + target, "", 200) {
135 public void onComplete() {
136 List<TreeItem> items = GSS.get().getFolders().getItemsOfTreeForPath(citem.getFolderResource().getUri());
137 for (TreeItem item : items)
138 if (item.getParentItem() != null && !item.equals(GSS.get().getFolders().getCurrent()))
139 GSS.get().getFolders().updateFolder((DnDTreeItem) item.getParentItem());
140 GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
144 public void onError(Throwable t) {
146 if(t instanceof RestException){
147 int statusCode = ((RestException)t).getHttpStatusCode();
148 if(statusCode == 405)
149 GSS.get().displayError("You don't have the necessary permissions");
150 else if(statusCode == 409)
151 GSS.get().displayError("A folder with the same name already exists");
152 else if(statusCode == 413)
153 GSS.get().displayError("Your quota has been exceeded");
155 GSS.get().displayError("Unable to move folder:"+((RestException)t).getHttpStatusText());
158 GSS.get().displayError("System error moving folder:"+t.getMessage());
161 DeferredCommand.addCommand(cf);
164 } else if (citem != null && citem.getFile() != null) {
165 String target = selectedFolder.getUri();
166 target = target.endsWith("/") ? target : target + '/';
167 target = target + URL.encodeComponent(citem.getFile().getName());
168 if (citem.getOperation() == Clipboard.COPY) {
169 PostCommand cf = new PostCommand(citem.getFile().getUri() + "?copy=" + target, "", 200) {
172 public void onComplete() {
173 GSS.get().showFileList(true);
174 GSS.get().getStatusPanel().updateStats();
178 public void onError(Throwable t) {
180 if(t instanceof RestException){
181 int statusCode = ((RestException)t).getHttpStatusCode();
182 if(statusCode == 405)
183 GSS.get().displayError("You don't have the necessary permissions");
184 else if(statusCode == 404)
185 GSS.get().displayError("File not found");
186 else if(statusCode == 409)
187 GSS.get().displayError("A file with the same name already exists");
188 else if(statusCode == 413)
189 GSS.get().displayError("Your quota has been exceeded");
191 GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
194 GSS.get().displayError("System error copying file:"+t.getMessage());
197 DeferredCommand.addCommand(cf);
198 } else if (citem.getOperation() == Clipboard.CUT) {
199 PostCommand cf = new PostCommand(citem.getFile().getUri() + "?move=" + target, "", 200) {
202 public void onComplete() {
203 GSS.get().showFileList(true);
207 public void onError(Throwable t) {
209 if(t instanceof RestException){
210 int statusCode = ((RestException)t).getHttpStatusCode();
211 if(statusCode == 405)
212 GSS.get().displayError("You don't have the necessary permissions");
213 else if(statusCode == 404)
214 GSS.get().displayError("File not found");
215 else if(statusCode == 409)
216 GSS.get().displayError("A file with the same name already exists");
217 else if(statusCode == 413)
218 GSS.get().displayError("Your quota has been exceeded");
220 GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
223 GSS.get().displayError("System error copying file:"+t.getMessage());
226 DeferredCommand.addCommand(cf);
229 } else if (citem != null && citem.getFiles() != null) {
230 List<FileResource> res = citem.getFiles();
231 List<String> fileIds = new ArrayList<String>();
232 String target = selectedFolder.getUri();
233 target = target.endsWith("/") ? target : target + '/';
235 if (citem.getOperation() == Clipboard.COPY) {
236 for (FileResource fileResource : res) {
237 String fileTarget = target + fileResource.getName();
238 fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
241 executeCopyOrMove(index, fileIds);
243 } else if (citem.getOperation() == Clipboard.CUT) {
244 for (FileResource fileResource : res) {
245 String fileTarget = target + fileResource.getName();
246 fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
249 executeCopyOrMove(index, fileIds);
256 private void executeCopyOrMove(final int index, final List<String> paths){
257 if(index >= paths.size()){
258 GSS.get().showFileList(true);
259 GSS.get().getStatusPanel().updateStats();
262 PostCommand cf = new PostCommand(paths.get(index), "", 200) {
265 public void onComplete() {
266 executeCopyOrMove(index+1, paths);
270 public void onError(Throwable t) {
272 if(t instanceof RestException){
273 int statusCode = ((RestException)t).getHttpStatusCode();
274 if(statusCode == 405)
275 GSS.get().displayError("You don't have the necessary permissions");
276 else if(statusCode == 404)
277 GSS.get().displayError("File not found");
278 else if(statusCode == 409)
279 GSS.get().displayError("A file with the same name already exists");
280 else if(statusCode == 413)
281 GSS.get().displayError("Your quota has been exceeded");
283 GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
286 GSS.get().displayError("System error copying file:"+t.getMessage());
289 DeferredCommand.addCommand(cf);