Updated licence in code files. Removed some unused methods
[pithos-web-client] / src / gr / grnet / pithos / web / client / commands / ToTrashCommand.java
1 /*
2  * Copyright 2011 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials
15  *      provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and
31  * documentation are those of the authors and should not be
32  * interpreted as representing official policies, either expressed
33  * or implied, of GRNET S.A.
34  */
35 package gr.grnet.pithos.web.client.commands;
36
37 import gr.grnet.pithos.web.client.GSS;
38 import gr.grnet.pithos.web.client.rest.MultiplePostCommand;
39 import gr.grnet.pithos.web.client.rest.PostCommand;
40 import gr.grnet.pithos.web.client.rest.RestException;
41 import gr.grnet.pithos.web.client.rest.resource.FileResource;
42 import gr.grnet.pithos.web.client.rest.resource.FolderResource;
43 import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
44
45 import java.util.ArrayList;
46 import java.util.List;
47
48
49 import com.google.gwt.core.client.GWT;
50 import com.google.gwt.user.client.Command;
51 import com.google.gwt.user.client.DeferredCommand;
52 import com.google.gwt.user.client.ui.PopupPanel;
53
54 /**
55  *
56  * Move file or folder to trash.
57  *
58  *
59  */
60 public class ToTrashCommand implements Command{
61         private PopupPanel containerPanel;
62
63         public ToTrashCommand(PopupPanel _containerPanel){
64                 containerPanel = _containerPanel;
65         }
66
67         @Override
68         public void execute() {
69                 containerPanel.hide();
70                 Object selection = GSS.get().getCurrentSelection();
71                 if (selection == null)
72                         return;
73                 GWT.log("selection: " + selection.toString(), null);
74                 if (selection instanceof RestResourceWrapper) {
75                         FolderResource fdto = ((RestResourceWrapper) selection).getResource();
76                         PostCommand tot = new PostCommand(fdto.getUri()+"?trash=","",200){
77
78                                 @Override
79                                 public void onComplete() {
80                                         //TODO:CELLTREE
81                                         /*
82                                         TreeItem folder = GSS.get().getFolders().getCurrent();
83                                         if(folder.getParentItem() != null){
84                                                 GSS.get().getFolders().select(folder.getParentItem());
85                                                 GSS.get().getFolders().updateFolder((DnDTreeItem) folder.getParentItem());
86                                         }
87                                         GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
88                                         */
89                                         FolderResource fres = ((RestResourceWrapper) GSS.get().getTreeView().getSelection()).getResource();
90                                         GSS.get().getTreeView().updateNodeChildrenForRemove(fres.getParentURI());
91                                         GSS.get().getTreeView().clearSelection();
92                                         //GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getTrash());
93                                         GSS.get().getTreeView().updateTrashNode();
94                                         GSS.get().showFileList(true);
95                                 }
96
97                                 @Override
98                                 public void onError(Throwable t) {
99                                         GWT.log("", t);
100                                         if(t instanceof RestException){
101                                                 int statusCode = ((RestException)t).getHttpStatusCode();
102                                                 if(statusCode == 405)
103                                                         GSS.get().displayError("You don't have the necessary permissions");
104                                                 else if(statusCode == 404)
105                                                         GSS.get().displayError("Folder does not exist");
106                                                 else
107                                                         GSS.get().displayError("Unable to trash folder:"+((RestException)t).getHttpStatusText());
108                                         }
109                                         else
110                                                 GSS.get().displayError("System error trashing folder:"+t.getMessage());
111                                 }
112                         };
113                         DeferredCommand.addCommand(tot);
114                 } else if (selection instanceof FileResource) {
115                         FileResource fdto = (FileResource) selection;
116                         PostCommand tot = new PostCommand(fdto.getUri()+"?trash=","",200){
117
118                                 @Override
119                                 public void onComplete() {
120                                         GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
121                                 }
122
123                                 @Override
124                                 public void onError(Throwable t) {
125                                         GWT.log("", t);
126                                         if(t instanceof RestException){
127                                                 int statusCode = ((RestException)t).getHttpStatusCode();
128                                                 if(statusCode == 405)
129                                                         GSS.get().displayError("You don't have the necessary permissions");
130                                                 else if(statusCode == 404)
131                                                         GSS.get().displayError("File does not exist");
132                                                 else
133                                                         GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
134                                         }
135                                         else
136                                                 GSS.get().displayError("System error trashing file:"+t.getMessage());
137                                 }
138                         };
139                         DeferredCommand.addCommand(tot);
140
141                 }
142                 else if (selection instanceof List) {
143                         List<FileResource> fdtos = (List<FileResource>) selection;
144                         final List<String> fileIds = new ArrayList<String>();
145                         for(FileResource f : fdtos)
146                                 fileIds.add(f.getUri()+"?trash=");
147                         MultiplePostCommand tot = new MultiplePostCommand(fileIds.toArray(new String[0]),200){
148
149                                 @Override
150                                 public void onComplete() {
151                                         GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());
152                                 }
153
154                                 @Override
155                                 public void onError(String p, Throwable t) {
156                                         GWT.log("", t);
157                                         if(t instanceof RestException){
158                                                 int statusCode = ((RestException)t).getHttpStatusCode();
159                                                 if(statusCode == 405)
160                                                         GSS.get().displayError("You don't have the necessary permissions");
161                                                 else if(statusCode == 404)
162                                                         GSS.get().displayError("File does not exist");
163                                                 else
164                                                         GSS.get().displayError("Unable to trash file:"+((RestException)t).getHttpStatusText());
165                                         }
166                                         else
167                                                 GSS.get().displayError("System error trashing file:"+t.getMessage());
168                                 }
169                         };
170                         DeferredCommand.addCommand(tot);
171                 }
172         }
173
174 }