689e7ce9e1e261978d9775ba8d8c1c7cd97464d5
[pithos] / src / gr / ebs / gss / client / commands / ToTrashCommand.java
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.rest.MultiplePostCommand;
23 import gr.ebs.gss.client.rest.PostCommand;
24 import gr.ebs.gss.client.rest.RestException;
25 import gr.ebs.gss.client.rest.resource.FileResource;
26 import gr.ebs.gss.client.rest.resource.FolderResource;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import com.google.gwt.core.client.GWT;
32 import com.google.gwt.user.client.Command;
33 import com.google.gwt.user.client.DeferredCommand;
34 import com.google.gwt.user.client.ui.PopupPanel;
35 import com.google.gwt.user.client.ui.TreeItem;
36
37 /**
38  *
39  * Move file or folder to trash.
40  *
41  * @author kman
42  *
43  */
44 public class ToTrashCommand implements Command{
45         private PopupPanel containerPanel;
46
47         public ToTrashCommand(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)
56                         return;
57                 GWT.log("selection: " + selection.toString(), null);
58                 if (selection instanceof FolderResource) {
59                         FolderResource fdto = (FolderResource) selection;
60                         PostCommand tot = new PostCommand(fdto.getUri()+"?trash=","",200){
61
62                                 @Override
63                                 public void onComplete() {
64                                         //TODO:CELLTREE
65                                         /*
66                                         TreeItem folder = GSS.get().getFolders().getCurrent();
67                                         if(folder.getParentItem() != null){
68                                                 GSS.get().getFolders().select(folder.getParentItem());
69                                                 GSS.get().getFolders().updateFolder((DnDTreeItem) folder.getParentItem());
70                                         }
71                                         GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
72                                         */
73                                         GSS.get().showFileList(true);
74                                 }
75
76                                 @Override
77                                 public void onError(Throwable t) {
78                                         GWT.log("", t);
79                                         if(t instanceof RestException){
80                                                 int statusCode = ((RestException)t).getHttpStatusCode();
81                                                 if(statusCode == 405)
82                                                         GSS.get().displayError("You don't have the necessary permissions");
83                                                 else if(statusCode == 404)
84                                                         GSS.get().displayError("Folder does not exist");
85                                                 else
86                                                         GSS.get().displayError("Unable to trash folder:"+((RestException)t).getHttpStatusText());
87                                         }
88                                         else
89                                                 GSS.get().displayError("System error trashing folder:"+t.getMessage());
90                                 }
91                         };
92                         DeferredCommand.addCommand(tot);
93                 } else if (selection instanceof FileResource) {
94                         FileResource fdto = (FileResource) selection;
95                         PostCommand tot = new PostCommand(fdto.getUri()+"?trash=","",200){
96
97                                 @Override
98                                 public void onComplete() {
99                                         GSS.get().showFileList(true);
100                                 }
101
102                                 @Override
103                                 public void onError(Throwable t) {
104                                         GWT.log("", t);
105                                         if(t instanceof RestException){
106                                                 int statusCode = ((RestException)t).getHttpStatusCode();
107                                                 if(statusCode == 405)
108                                                         GSS.get().displayError("You don't have the necessary permissions");
109                                                 else if(statusCode == 404)
110                                                         GSS.get().displayError("File does not exist");
111                                                 else
112                                                         GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
113                                         }
114                                         else
115                                                 GSS.get().displayError("System error trashing file:"+t.getMessage());
116                                 }
117                         };
118                         DeferredCommand.addCommand(tot);
119
120                 }
121                 else if (selection instanceof List) {
122                         List<FileResource> fdtos = (List<FileResource>) selection;
123                         final List<String> fileIds = new ArrayList<String>();
124                         for(FileResource f : fdtos)
125                                 fileIds.add(f.getUri()+"?trash=");
126                         MultiplePostCommand tot = new MultiplePostCommand(fileIds.toArray(new String[0]),200){
127
128                                 @Override
129                                 public void onComplete() {
130                                         GSS.get().showFileList(true);
131                                 }
132
133                                 @Override
134                                 public void onError(String p, Throwable t) {
135                                         GWT.log("", t);
136                                         if(t instanceof RestException){
137                                                 int statusCode = ((RestException)t).getHttpStatusCode();
138                                                 if(statusCode == 405)
139                                                         GSS.get().displayError("You don't have the necessary permissions");
140                                                 else if(statusCode == 404)
141                                                         GSS.get().displayError("File does not exist");
142                                                 else
143                                                         GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
144                                         }
145                                         else
146                                                 GSS.get().displayError("System error trashing file:"+t.getMessage());
147                                 }
148                         };
149                         DeferredCommand.addCommand(tot);
150                 }
151         }
152
153 }