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());
141 GSS.get().getStatusPanel().updateStats();
145 public void onError(Throwable t) {
147 if(t instanceof RestException){
148 int statusCode = ((RestException)t).getHttpStatusCode();
149 if(statusCode == 405)
150 GSS.get().displayError("You don't have the necessary permissions");
151 else if(statusCode == 409)
152 GSS.get().displayError("A folder with the same name already exists");
153 else if(statusCode == 413)
154 GSS.get().displayError("Your quota has been exceeded");
156 GSS.get().displayError("Unable to move folder:"+((RestException)t).getHttpStatusText());
159 GSS.get().displayError("System error moving folder:"+t.getMessage());
162 DeferredCommand.addCommand(cf);
165 } else if (citem != null && citem.getFile() != null) {
166 String target = selectedFolder.getUri();
167 target = target.endsWith("/") ? target : target + '/';
168 target = target + URL.encodeComponent(citem.getFile().getName());
169 if (citem.getOperation() == Clipboard.COPY) {
170 PostCommand cf = new PostCommand(citem.getFile().getUri() + "?copy=" + target, "", 200) {
173 public void onComplete() {
174 GSS.get().showFileList(true);
175 GSS.get().getStatusPanel().updateStats();
179 public void onError(Throwable t) {
181 if(t instanceof RestException){
182 int statusCode = ((RestException)t).getHttpStatusCode();
183 if(statusCode == 405)
184 GSS.get().displayError("You don't have the necessary permissions");
185 else if(statusCode == 404)
186 GSS.get().displayError("File not found");
187 else if(statusCode == 409)
188 GSS.get().displayError("A file with the same name already exists");
189 else if(statusCode == 413)
190 GSS.get().displayError("Your quota has been exceeded");
192 GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
195 GSS.get().displayError("System error copying file:"+t.getMessage());
198 DeferredCommand.addCommand(cf);
199 } else if (citem.getOperation() == Clipboard.CUT) {
200 PostCommand cf = new PostCommand(citem.getFile().getUri() + "?move=" + target, "", 200) {
203 public void onComplete() {
204 GSS.get().showFileList(true);
205 GSS.get().getStatusPanel().updateStats();
209 public void onError(Throwable t) {
211 if(t instanceof RestException){
212 int statusCode = ((RestException)t).getHttpStatusCode();
213 if(statusCode == 405)
214 GSS.get().displayError("You don't have the necessary permissions");
215 else if(statusCode == 404)
216 GSS.get().displayError("File not found");
217 else if(statusCode == 409)
218 GSS.get().displayError("A file with the same name already exists");
219 else if(statusCode == 413)
220 GSS.get().displayError("Your quota has been exceeded");
222 GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
225 GSS.get().displayError("System error copying file:"+t.getMessage());
228 DeferredCommand.addCommand(cf);
231 } else if (citem != null && citem.getFiles() != null) {
232 List<FileResource> res = citem.getFiles();
233 List<String> fileIds = new ArrayList<String>();
234 String target = selectedFolder.getUri();
235 target = target.endsWith("/") ? target : target + '/';
237 if (citem.getOperation() == Clipboard.COPY) {
238 for (FileResource fileResource : res) {
239 String fileTarget = target + fileResource.getName();
240 fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
243 executeCopyOrMove(index, fileIds);
245 } else if (citem.getOperation() == Clipboard.CUT) {
246 for (FileResource fileResource : res) {
247 String fileTarget = target + fileResource.getName();
248 fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
251 executeCopyOrMove(index, fileIds);
258 private void executeCopyOrMove(final int index, final List<String> paths){
259 if(index >= paths.size()){
260 GSS.get().showFileList(true);
261 GSS.get().getStatusPanel().updateStats();
264 PostCommand cf = new PostCommand(paths.get(index), "", 200) {
267 public void onComplete() {
268 executeCopyOrMove(index+1, paths);
272 public void onError(Throwable t) {
274 if(t instanceof RestException){
275 int statusCode = ((RestException)t).getHttpStatusCode();
276 if(statusCode == 405)
277 GSS.get().displayError("You don't have the necessary permissions");
278 else if(statusCode == 404)
279 GSS.get().displayError("File not found");
280 else if(statusCode == 409)
281 GSS.get().displayError("A file with the same name already exists");
282 else if(statusCode == 413)
283 GSS.get().displayError("Your quota has been exceeded");
285 GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
288 GSS.get().displayError("System error copying file:"+t.getMessage());
291 DeferredCommand.addCommand(cf);