e4a971ed7cd8bd6e8d5e86522c3b14fa539a02d3
[pithos] / web_client / src / gr / grnet / pithos / web / client / commands / RestoreTrashCommand.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.Pithos;
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.TrashFolderResource;
44 import gr.grnet.pithos.web.client.rest.resource.TrashResource;
45
46 import java.util.ArrayList;
47 import java.util.List;
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  *
57  * Restore trashed files and folders.
58  *
59  */
60 public class RestoreTrashCommand implements Command{
61         private PopupPanel containerPanel;
62
63         public RestoreTrashCommand(PopupPanel _containerPanel){
64                 containerPanel = _containerPanel;
65         }
66
67         @Override
68         public void execute() {
69                 containerPanel.hide();
70                 Object selection = Pithos.get().getCurrentSelection();
71                 if (selection == null){
72                         // Check to see if Trash Node is selected.
73                         List folderList = new ArrayList();
74                         TrashResource trashItem = Pithos.get().getTreeView().getTrash();
75                         for(int i=0 ; i < trashItem.getFolders().size() ; i++)
76                                 folderList.add(trashItem.getFolders().get(i));
77                         return;
78                 }
79                 GWT.log("selection: " + selection.toString(), null);
80                 if (selection instanceof FileResource) {
81                         final FileResource resource = (FileResource)selection;
82                         PostCommand rt = new PostCommand(resource.getUri()+"?restore=","", 200){
83
84                                 @Override
85                                 public void onComplete() {
86                                         //TODO:CELLTREE
87                                         //Pithos.get().getFolders().update(Pithos.get().getFolders().getTrashItem());
88                                         
89                                         Pithos.get().showFileList(true);
90                                 }
91
92                                 @Override
93                                 public void onError(Throwable t) {
94                                         GWT.log("", t);
95                                         if(t instanceof RestException){
96                                                 int statusCode = ((RestException)t).getHttpStatusCode();
97                                                 if(statusCode == 405)
98                                                         Pithos.get().displayError("You don't have the necessary permissions");
99                                                 else if(statusCode == 404)
100                                                         Pithos.get().displayError("File does not exist");
101                                                 else if(statusCode == 409)
102                                                         Pithos.get().displayError("A file with the same name already exists");
103                                                 else if(statusCode == 413)
104                                                         Pithos.get().displayError("Your quota has been exceeded");
105                                                 else
106                                                         Pithos.get().displayError("Unable to restore file:"+((RestException)t).getHttpStatusText());
107                                         }
108                                         else
109                                                 Pithos.get().displayError("System error restoring file:"+t.getMessage());
110                                 }
111                         };
112                         DeferredCommand.addCommand(rt);
113                 }
114                 else if (selection instanceof List) {
115                         final List<FileResource> fdtos = (List<FileResource>) selection;
116                         final List<String> fileIds = new ArrayList<String>();
117                         for(FileResource f : fdtos)
118                                 fileIds.add(f.getUri()+"?restore=");
119                         MultiplePostCommand rt = new MultiplePostCommand(fileIds.toArray(new String[0]), 200){
120
121                                 @Override
122                                 public void onComplete() {
123                                         //TODO:CELLTREE
124                                         //Pithos.get().getFolders().update(Pithos.get().getFolders().getTrashItem());
125                                         Pithos.get().showFileList(true);
126                                 }
127
128                                 @Override
129                                 public void onError(String p, Throwable t) {
130                                         GWT.log("", t);
131                                         if(t instanceof RestException){
132                                                 int statusCode = ((RestException)t).getHttpStatusCode();
133                                                 if(statusCode == 405)
134                                                         Pithos.get().displayError("You don't have the necessary permissions");
135                                                 else if(statusCode == 404)
136                                                         Pithos.get().displayError("File does not exist");
137                                                 else if(statusCode == 409)
138                                                         Pithos.get().displayError("A file with the same name already exists");
139                                                 else if(statusCode == 413)
140                                                         Pithos.get().displayError("Your quota has been exceeded");
141                                                 else
142                                                         Pithos.get().displayError("Unable to restore file::"+((RestException)t).getHttpStatusText());
143                                         }
144                                         else
145                                                 Pithos.get().displayError("System error restoring file:"+t.getMessage());
146                                 }
147                         };
148                         DeferredCommand.addCommand(rt);
149                 }
150                 else if (selection instanceof TrashFolderResource) {
151                         final FolderResource resource = ((TrashFolderResource)selection).getResource();
152                         PostCommand rt = new PostCommand(resource.getUri()+"?restore=","", 200){
153
154                                 @Override
155                                 public void onComplete() {
156                                         //TODO:CELLTREE
157                                         /*
158                                         Pithos.get().getFolders().updateFolder((DnDTreeItem) Pithos.get().getFolders().getRootItem());
159
160                                         Pithos.get().getFolders().update(Pithos.get().getFolders().getTrashItem());
161                                         */
162                                         
163                                         Pithos.get().getTreeView().updateTrashNode();
164                                         Pithos.get().getTreeView().updateRootNode();
165                                 }
166
167                                 @Override
168                                 public void onError(Throwable t) {
169                                         GWT.log("", t);
170                                         if(t instanceof RestException){
171                                                 int statusCode = ((RestException)t).getHttpStatusCode();
172                                                 if(statusCode == 405)
173                                                         Pithos.get().displayError("You don't have the necessary permissions");
174                                                 else if(statusCode == 404)
175                                                         Pithos.get().displayError("Folder does not exist");
176                                                 else if(statusCode == 409)
177                                                         Pithos.get().displayError("A folder with the same name already exists");
178                                                 else if(statusCode == 413)
179                                                         Pithos.get().displayError("Your quota has been exceeded");
180                                                 else
181                                                         Pithos.get().displayError("Unable to restore folder::"+((RestException)t).getHttpStatusText());
182                                         }
183                                         else
184                                                 Pithos.get().displayError("System error restoring folder:"+t.getMessage());
185                                 }
186                         };
187                         DeferredCommand.addCommand(rt);
188                 }
189
190         }
191
192 }