Statistics
| Branch: | Tag: | Revision:

root / src / org / gss_project / gss / web / client / commands / PasteCommand.java @ 1206:292dec4eae08

History | View | Annotate | Download (11.6 kB)

1
/*
2
 * Copyright 2008, 2009 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
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.
10
 *
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.
15
 *
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/>.
18
 */
19
package org.gss_project.gss.web.client.commands;
20

    
21
import org.gss_project.gss.web.client.GSS;
22
import org.gss_project.gss.web.client.clipboard.Clipboard;
23
import org.gss_project.gss.web.client.clipboard.ClipboardItem;
24
import org.gss_project.gss.web.client.rest.PostCommand;
25
import org.gss_project.gss.web.client.rest.RestException;
26
import org.gss_project.gss.web.client.rest.resource.FileResource;
27
import org.gss_project.gss.web.client.rest.resource.FolderResource;
28
import org.gss_project.gss.web.client.rest.resource.GroupResource;
29
import org.gss_project.gss.web.client.rest.resource.RestResourceWrapper;
30

    
31
import java.util.ArrayList;
32
import java.util.List;
33

    
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

    
40
/**
41
 * @author kman Command for pasting Clipboard contents
42
 */
43
public class PasteCommand implements Command {
44

    
45
        private PopupPanel containerPanel;
46

    
47
        public PasteCommand(PopupPanel _containerPanel) {
48
                containerPanel = _containerPanel;
49
        }
50

    
51
        @Override
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){
60

    
61
                                        @Override
62
                                        public void onComplete() {
63
                                                GSS.get().getGroups().updateGroups();
64
                                                GSS.get().showUserList();
65
                                                GSS.get().getClipboard().setItem(null);
66
                                        }
67

    
68
                                        @Override
69
                                        public void onError(Throwable t) {
70
                                                GWT.log("", t);
71
                                                if(t instanceof RestException){
72
                                                        int statusCode = ((RestException)t).getHttpStatusCode();
73
                                                        if(statusCode == 405)
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");
81
                                                        else
82
                                                                GSS.get().displayError("Unable to add user:"+((RestException)t).getHttpStatusText());
83
                                                }
84
                                                else
85
                                                        GSS.get().displayError("System error adding user:"+t.getMessage());
86
                                        }
87
                                };
88
                                DeferredCommand.addCommand(cg);
89
                                return;
90
                        }
91
                }
92
                FolderResource selectedFolder = null;
93
                if(selection != null && selection instanceof RestResourceWrapper)
94
                        selectedFolder = ((RestResourceWrapper)selection).getResource();
95
                //TODO:CELLTREE
96
                /*
97
                else if(GSS.get().getFolders().getCurrent() != null && ((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource() != null)
98
                        selectedFolder = ((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource();
99
                */
100
                if (selectedFolder != null) {
101
                        final ClipboardItem citem = GSS.get().getClipboard().getItem();
102
                        if (citem != null && citem.getRestResourceWrapper() != null) {
103
                                String target = selectedFolder.getUri();
104
                                target = target.endsWith("/") ? target : target + '/';
105
                                target = target + URL.encodeComponent(citem.getRestResourceWrapper().getResource().getName());
106
                                if (citem.getOperation() == Clipboard.COPY) {
107
                                        PostCommand cf = new PostCommand(citem.getRestResourceWrapper().getUri() + "?copy=" + target, "", 200) {
108

    
109
                                                @Override
110
                                                public void onComplete() {
111
                                                        //TODO:CELLTREE
112
                                                        //GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
113
                                                        GSS.get().getTreeView().updateNodeChildren(GSS.get().getTreeView().getSelection());
114
                                                        GSS.get().getStatusPanel().updateStats();
115
                                                        GSS.get().getClipboard().setItem(null);
116
                                                }
117

    
118
                                                @Override
119
                                                public void onError(Throwable t) {
120
                                                        GWT.log("", t);
121
                                                        if(t instanceof RestException){
122
                                                                int statusCode = ((RestException)t).getHttpStatusCode();
123
                                                                if(statusCode == 405)
124
                                                                        GSS.get().displayError("You don't have the necessary permissions");
125

    
126
                                                                else if(statusCode == 409)
127
                                                                        GSS.get().displayError("A folder with the same name already exists");
128
                                                                else if(statusCode == 413)
129
                                                                        GSS.get().displayError("Your quota has been exceeded");
130
                                                                else
131
                                                                        GSS.get().displayError("Unable to copy folder:"+((RestException)t).getHttpStatusText());
132
                                                        }
133
                                                        else
134
                                                                GSS.get().displayError("System error copying folder:"+t.getMessage());
135
                                                }
136
                                        };
137
                                        DeferredCommand.addCommand(cf);
138
                                } else if (citem.getOperation() == Clipboard.CUT) {
139
                                        PostCommand cf = new PostCommand(citem.getRestResourceWrapper().getUri() + "?move=" + target, "", 200) {
140

    
141
                                                @Override
142
                                                public void onComplete() {
143
                                                        //TODO:CELLTREE
144
                                                        /*
145
                                                        List<TreeItem> items = GSS.get().getFolders().getItemsOfTreeForPath(citem.getFolderResource().getUri());
146
                                                        for (TreeItem item : items)
147
                                                                if (item.getParentItem() != null && !item.equals(GSS.get().getFolders().getCurrent()))
148
                                                                        GSS.get().getFolders().updateFolder((DnDTreeItem) item.getParentItem());
149
                                                        GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
150
                                                        */
151
                                                        GSS.get().getTreeView().updateNodeChildren(GSS.get().getTreeView().getSelection());
152
                                                        GSS.get().getTreeView().updateNodeChildrenForRemove(citem.getRestResourceWrapper().getResource().getParentURI());
153
                                                        GSS.get().getStatusPanel().updateStats();                
154
                                                        GSS.get().getClipboard().setItem(null);
155
                                                }
156

    
157
                                                @Override
158
                                                public void onError(Throwable t) {
159
                                                        GWT.log("", t);
160
                                                        if(t instanceof RestException){
161
                                                                int statusCode = ((RestException)t).getHttpStatusCode();
162
                                                                if(statusCode == 405)
163
                                                                        GSS.get().displayError("You don't have the necessary permissions");
164
                                                                else if(statusCode == 409)
165
                                                                        GSS.get().displayError("A folder with the same name already exists");
166
                                                                else if(statusCode == 413)
167
                                                                        GSS.get().displayError("Your quota has been exceeded");
168
                                                                else
169
                                                                        GSS.get().displayError("Unable to move folder:"+((RestException)t).getHttpStatusText());
170
                                                        }
171
                                                        else
172
                                                                GSS.get().displayError("System error moving folder:"+t.getMessage());
173
                                                }
174
                                        };
175
                                        DeferredCommand.addCommand(cf);
176
                                }
177
                                return;
178
                        } else if (citem != null && citem.getFile() != null) {
179
                                String target = selectedFolder.getUri();
180
                                target = target.endsWith("/") ? target : target + '/';
181
                                target = target + URL.encodeComponent(citem.getFile().getName());
182
                                if (citem.getOperation() == Clipboard.COPY) {
183
                                        PostCommand cf = new PostCommand(citem.getFile().getUri() + "?copy=" + target, "", 200) {
184

    
185
                                                @Override
186
                                                public void onComplete() {
187
                                                        GSS.get().showFileList(true);
188
                                                        GSS.get().getStatusPanel().updateStats();
189
                                                        GSS.get().getClipboard().setItem(null);
190
                                                }
191

    
192
                                                @Override
193
                                                public void onError(Throwable t) {
194
                                                        GWT.log("", t);
195
                                                        if(t instanceof RestException){
196
                                                                int statusCode = ((RestException)t).getHttpStatusCode();
197
                                                                if(statusCode == 405)
198
                                                                        GSS.get().displayError("You don't have the necessary permissions");
199
                                                                else if(statusCode == 404)
200
                                                                        GSS.get().displayError("File not found");
201
                                                                else if(statusCode == 409)
202
                                                                        GSS.get().displayError("A file with the same name already exists");
203
                                                                else if(statusCode == 413)
204
                                                                        GSS.get().displayError("Your quota has been exceeded");
205
                                                                else
206
                                                                        GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
207
                                                        }
208
                                                        else
209
                                                                GSS.get().displayError("System error copying file:"+t.getMessage());
210
                                                }
211
                                        };
212
                                        DeferredCommand.addCommand(cf);
213
                                } else if (citem.getOperation() == Clipboard.CUT) {
214
                                        PostCommand cf = new PostCommand(citem.getFile().getUri() + "?move=" + target, "", 200) {
215

    
216
                                                @Override
217
                                                public void onComplete() {
218
                                                        GSS.get().showFileList(true);
219
                                                        GSS.get().getStatusPanel().updateStats();
220
                                                        GSS.get().getClipboard().setItem(null);
221
                                                }
222

    
223
                                                @Override
224
                                                public void onError(Throwable t) {
225
                                                        GWT.log("", t);
226
                                                        if(t instanceof RestException){
227
                                                                int statusCode = ((RestException)t).getHttpStatusCode();
228
                                                                if(statusCode == 405)
229
                                                                        GSS.get().displayError("You don't have the necessary permissions");
230
                                                                else if(statusCode == 404)
231
                                                                        GSS.get().displayError("File not found");
232
                                                                else if(statusCode == 409)
233
                                                                        GSS.get().displayError("A file with the same name already exists");
234
                                                                else if(statusCode == 413)
235
                                                                        GSS.get().displayError("Your quota has been exceeded");
236
                                                                else
237
                                                                        GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
238
                                                        }
239
                                                        else
240
                                                                GSS.get().displayError("System error copying file:"+t.getMessage());
241
                                                }
242
                                        };
243
                                        DeferredCommand.addCommand(cf);
244
                                }
245
                                return;
246
                        } else if (citem != null && citem.getFiles() != null) {
247
                                List<FileResource> res = citem.getFiles();
248
                                List<String> fileIds = new ArrayList<String>();
249
                                String target = selectedFolder.getUri();
250
                                target = target.endsWith("/") ? target : target + '/';
251

    
252
                                if (citem.getOperation() == Clipboard.COPY) {
253
                                        for (FileResource fileResource : res) {
254
                                                String fileTarget = target + fileResource.getName();
255
                                                fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
256
                                        }
257
                                        int index = 0;
258
                                        executeCopyOrMove(index, fileIds);
259

    
260
                                } else if (citem.getOperation() == Clipboard.CUT) {
261
                                        for (FileResource fileResource : res) {
262
                                                String fileTarget = target + fileResource.getName();
263
                                                fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
264
                                        }
265
                                        int index = 0;
266
                                        executeCopyOrMove(index, fileIds);
267
                                }
268
                                return;
269
                        }
270
                }
271
        }
272

    
273
        private void executeCopyOrMove(final int index, final List<String> paths){
274
                if(index >= paths.size()){
275
                        GSS.get().showFileList(true);
276
                        GSS.get().getStatusPanel().updateStats();
277
                        GSS.get().getClipboard().setItem(null);
278
                        return;
279
                }
280
                PostCommand cf = new PostCommand(paths.get(index), "", 200) {
281

    
282
                        @Override
283
                        public void onComplete() {
284
                                executeCopyOrMove(index+1, paths);
285
                        }
286

    
287
                        @Override
288
                        public void onError(Throwable t) {
289
                                GWT.log("", t);
290
                                if(t instanceof RestException){
291
                                        int statusCode = ((RestException)t).getHttpStatusCode();
292
                                        if(statusCode == 405)
293
                                                GSS.get().displayError("You don't have the necessary permissions");
294
                                        else if(statusCode == 404)
295
                                                GSS.get().displayError("File not found");
296
                                        else if(statusCode == 409)
297
                                                GSS.get().displayError("A file with the same name already exists");
298
                                        else if(statusCode == 413)
299
                                                GSS.get().displayError("Your quota has been exceeded");
300
                                        else
301
                                                GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
302
                                }
303
                                else
304
                                        GSS.get().displayError("System error copying file:"+t.getMessage());
305
                        }
306
                };
307
                DeferredCommand.addCommand(cf);
308
        }
309
}