Statistics
| Branch: | Tag: | Revision:

root / gss / src / gr / ebs / gss / client / commands / PasteCommand.java @ 555e8e59

History | View | Annotate | Download (10.5 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 gr.ebs.gss.client.commands;
20

    
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.ExecutePost;
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;
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
import com.google.gwt.user.client.ui.TreeItem;
40

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

    
46
        private PopupPanel containerPanel;
47

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

    
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
                                ExecutePost cg = new ExecutePost(group.getUri()+"?name="+citem.getUser().getUsername(), "", 201){
60

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

    
67
                                        @Override
68
                                        public void onError(Throwable t) {
69
                                                GWT.log("", t);
70
                                                if(t instanceof RestException){
71
                                                        int statusCode = ((RestException)t).getHttpStatusCode();
72
                                                        if(statusCode == 405)
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");
80
                                                        else
81
                                                                GSS.get().displayError("Unable to add user:"+((RestException)t).getHttpStatusText());
82
                                                }
83
                                                else
84
                                                        GSS.get().displayError("System error adding user:"+t.getMessage());
85
                                        }
86
                                };
87
                                DeferredCommand.addCommand(cg);
88
                                return;
89
                        }
90
                }
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
                                        ExecutePost cf = new ExecutePost(citem.getFolderResource().getUri() + "?copy=" + target, "", 200) {
104

    
105
                                                @Override
106
                                                public void onComplete() {
107
                                                        GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
108
                                                }
109

    
110
                                                @Override
111
                                                public void onError(Throwable t) {
112
                                                        GWT.log("", t);
113
                                                        if(t instanceof RestException){
114
                                                                int statusCode = ((RestException)t).getHttpStatusCode();
115
                                                                if(statusCode == 405)
116
                                                                        GSS.get().displayError("You don't have the necessary permissions");
117

    
118
                                                                else if(statusCode == 409)
119
                                                                        GSS.get().displayError("A folder with the same name already exists");
120
                                                                else if(statusCode == 413)
121
                                                                        GSS.get().displayError("Your quota has been exceeded");
122
                                                                else
123
                                                                        GSS.get().displayError("Unable to copy folder:"+((RestException)t).getHttpStatusText());
124
                                                        }
125
                                                        else
126
                                                                GSS.get().displayError("System error copying folder:"+t.getMessage());
127
                                                }
128
                                        };
129
                                        DeferredCommand.addCommand(cf);
130
                                } else if (citem.getOperation() == Clipboard.CUT) {
131
                                        ExecutePost cf = new ExecutePost(citem.getFolderResource().getUri() + "?move=" + target, "", 200) {
132

    
133
                                                @Override
134
                                                public void onComplete() {
135
                                                        List<TreeItem> items = GSS.get().getFolders().getItemsOfTreeForPath(citem.getFolderResource().getUri());
136
                                                        for (TreeItem item : items)
137
                                                                if (item.getParentItem() != null && !item.equals(GSS.get().getFolders().getCurrent()))
138
                                                                        GSS.get().getFolders().updateFolder((DnDTreeItem) item.getParentItem());
139
                                                        GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
140
                                                }
141

    
142
                                                @Override
143
                                                public void onError(Throwable t) {
144
                                                        GWT.log("", t);
145
                                                        if(t instanceof RestException){
146
                                                                int statusCode = ((RestException)t).getHttpStatusCode();
147
                                                                if(statusCode == 405)
148
                                                                        GSS.get().displayError("You don't have the necessary permissions");
149
                                                                else if(statusCode == 409)
150
                                                                        GSS.get().displayError("A folder with the same name already exists");
151
                                                                else if(statusCode == 413)
152
                                                                        GSS.get().displayError("Your quota has been exceeded");
153
                                                                else
154
                                                                        GSS.get().displayError("Unable to move folder:"+((RestException)t).getHttpStatusText());
155
                                                        }
156
                                                        else
157
                                                                GSS.get().displayError("System error moving folder:"+t.getMessage());
158
                                                }
159
                                        };
160
                                        DeferredCommand.addCommand(cf);
161
                                }
162
                                return;
163
                        } else if (citem != null && citem.getFile() != null) {
164
                                String target = selectedFolder.getUri();
165
                                target = target.endsWith("/") ? target : target + '/';
166
                                target = target + URL.encodeComponent(citem.getFile().getName());
167
                                if (citem.getOperation() == Clipboard.COPY) {
168
                                        ExecutePost cf = new ExecutePost(citem.getFile().getUri() + "?copy=" + target, "", 200) {
169

    
170
                                                @Override
171
                                                public void onComplete() {
172
                                                        GSS.get().showFileList(true);
173
                                                }
174

    
175
                                                @Override
176
                                                public void onError(Throwable t) {
177
                                                        GWT.log("", t);
178
                                                        if(t instanceof RestException){
179
                                                                int statusCode = ((RestException)t).getHttpStatusCode();
180
                                                                if(statusCode == 405)
181
                                                                        GSS.get().displayError("You don't have the necessary permissions");
182
                                                                else if(statusCode == 404)
183
                                                                        GSS.get().displayError("File not found");
184
                                                                else if(statusCode == 409)
185
                                                                        GSS.get().displayError("A file with the same name already exists");
186
                                                                else if(statusCode == 413)
187
                                                                        GSS.get().displayError("Your quota has been exceeded");
188
                                                                else
189
                                                                        GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
190
                                                        }
191
                                                        else
192
                                                                GSS.get().displayError("System error copying file:"+t.getMessage());
193
                                                }
194
                                        };
195
                                        DeferredCommand.addCommand(cf);
196
                                } else if (citem.getOperation() == Clipboard.CUT) {
197
                                        ExecutePost cf = new ExecutePost(citem.getFile().getUri() + "?move=" + target, "", 200) {
198

    
199
                                                @Override
200
                                                public void onComplete() {
201
                                                        GSS.get().showFileList(true);
202
                                                }
203

    
204
                                                @Override
205
                                                public void onError(Throwable t) {
206
                                                        GWT.log("", t);
207
                                                        if(t instanceof RestException){
208
                                                                int statusCode = ((RestException)t).getHttpStatusCode();
209
                                                                if(statusCode == 405)
210
                                                                        GSS.get().displayError("You don't have the necessary permissions");
211
                                                                else if(statusCode == 404)
212
                                                                        GSS.get().displayError("File not found");
213
                                                                else if(statusCode == 409)
214
                                                                        GSS.get().displayError("A file with the same name already exists");
215
                                                                else if(statusCode == 413)
216
                                                                        GSS.get().displayError("Your quota has been exceeded");
217
                                                                else
218
                                                                        GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
219
                                                        }
220
                                                        else
221
                                                                GSS.get().displayError("System error copying file:"+t.getMessage());
222
                                                }
223
                                        };
224
                                        DeferredCommand.addCommand(cf);
225
                                }
226
                                return;
227
                        } else if (citem != null && citem.getFiles() != null) {
228
                                List<FileResource> res = citem.getFiles();
229
                                List<String> fileIds = new ArrayList<String>();
230
                                String target = selectedFolder.getUri();
231
                                target = target.endsWith("/") ? target : target + '/';
232

    
233
                                if (citem.getOperation() == Clipboard.COPY) {
234
                                        for (FileResource fileResource : res) {
235
                                                String fileTarget = target + fileResource.getName();
236
                                                fileIds.add(fileResource.getUri() + "?copy=" + fileTarget);
237
                                        }
238
                                        int index = 0;
239
                                        executeCopyOrMove(index, fileIds);
240

    
241
                                } else if (citem.getOperation() == Clipboard.CUT) {
242
                                        for (FileResource fileResource : res) {
243
                                                String fileTarget = target + fileResource.getName();
244
                                                fileIds.add(fileResource.getUri() + "?move=" + fileTarget);
245
                                        }
246
                                        int index =0;
247
                                        executeCopyOrMove(index, fileIds);
248
                                }
249
                                return;
250
                        }
251
                }
252
        }
253

    
254
        private void executeCopyOrMove(final int index, final List<String> paths){
255
                if(index >= paths.size()){
256
                        GSS.get().showFileList(true);
257
                        return;
258
                }
259
                ExecutePost cf = new ExecutePost(paths.get(index), "", 200) {
260

    
261
                        @Override
262
                        public void onComplete() {
263
                                executeCopyOrMove(index+1, paths);
264
                        }
265

    
266
                        @Override
267
                        public void onError(Throwable t) {
268
                                GWT.log("", t);
269
                                if(t instanceof RestException){
270
                                        int statusCode = ((RestException)t).getHttpStatusCode();
271
                                        if(statusCode == 405)
272
                                                GSS.get().displayError("You don't have the necessary permissions");
273
                                        else if(statusCode == 404)
274
                                                GSS.get().displayError("File not found");
275
                                        else if(statusCode == 409)
276
                                                GSS.get().displayError("A file with the same name already exists");
277
                                        else if(statusCode == 413)
278
                                                GSS.get().displayError("Your quota has been exceeded");
279
                                        else
280
                                                GSS.get().displayError("Unable to copy file:"+((RestException)t).getHttpStatusText());
281
                                }
282
                                else
283
                                        GSS.get().displayError("System error copying file:"+t.getMessage());
284
                        }
285
                };
286
                DeferredCommand.addCommand(cf);
287
        }
288
}