Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / commands / RestoreTrashCommand.java @ 623:66f69a7348ed

History | View | Annotate | Download (5.7 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.dnd.DnDTreeItem;
23
import gr.ebs.gss.client.rest.MultiplePostCommand;
24
import gr.ebs.gss.client.rest.PostCommand;
25
import gr.ebs.gss.client.rest.RestException;
26
import gr.ebs.gss.client.rest.resource.FileResource;
27
import gr.ebs.gss.client.rest.resource.FolderResource;
28

    
29
import java.util.ArrayList;
30
import java.util.List;
31

    
32
import com.google.gwt.core.client.GWT;
33
import com.google.gwt.user.client.Command;
34
import com.google.gwt.user.client.DeferredCommand;
35
import com.google.gwt.user.client.ui.PopupPanel;
36
import com.google.gwt.user.client.ui.TreeItem;
37

    
38

    
39
/**
40
 *
41
 * Restore trashed files and folders.
42
 *
43
 * @author kman
44
 */
45
public class RestoreTrashCommand implements Command{
46
        private PopupPanel containerPanel;
47

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

    
52
        @Override
53
        public void execute() {
54
                containerPanel.hide();
55
                Object selection = GSS.get().getCurrentSelection();
56
                if (selection == null){
57
                        // Check to see if Trash Node is selected.
58
                        List folderList = new ArrayList();
59
                        TreeItem trashItem = GSS.get().getFolders().getTrashItem();
60
                        for(int i=0 ; i < trashItem.getChildCount() ; i++)
61
                                folderList.add(trashItem.getChild(i).getUserObject());
62
                        return;
63
                }
64
                GWT.log("selection: " + selection.toString(), null);
65
                if (selection instanceof FileResource) {
66
                        final FileResource resource = (FileResource)selection;
67
                        PostCommand rt = new PostCommand(resource.getUri()+"?restore=","", 200){
68

    
69
                                @Override
70
                                public void onComplete() {
71
                                        GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
72
                                        GSS.get().showFileList(true);
73
                                }
74

    
75
                                @Override
76
                                public void onError(Throwable t) {
77
                                        GWT.log("", t);
78
                                        if(t instanceof RestException){
79
                                                int statusCode = ((RestException)t).getHttpStatusCode();
80
                                                if(statusCode == 405)
81
                                                        GSS.get().displayError("You don't have the necessary permissions");
82
                                                else if(statusCode == 404)
83
                                                        GSS.get().displayError("File does not exist");
84
                                                else if(statusCode == 409)
85
                                                        GSS.get().displayError("A file with the same name already exists");
86
                                                else if(statusCode == 413)
87
                                                        GSS.get().displayError("Your quota has been exceeded");
88
                                                else
89
                                                        GSS.get().displayError("Unable to restore file:"+((RestException)t).getHttpStatusText());
90
                                        }
91
                                        else
92
                                                GSS.get().displayError("System error restoring file:"+t.getMessage());
93
                                }
94
                        };
95
                        DeferredCommand.addCommand(rt);
96
                }
97
                else if (selection instanceof List) {
98
                        final List<FileResource> fdtos = (List<FileResource>) selection;
99
                        final List<String> fileIds = new ArrayList<String>();
100
                        for(FileResource f : fdtos)
101
                                fileIds.add(f.getUri()+"?restore=");
102
                        MultiplePostCommand rt = new MultiplePostCommand(fileIds.toArray(new String[0]), 200){
103

    
104
                                @Override
105
                                public void onComplete() {
106
                                        GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
107
                                        GSS.get().showFileList(true);
108
                                }
109

    
110
                                @Override
111
                                public void onError(String p, 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
                                                else if(statusCode == 404)
118
                                                        GSS.get().displayError("File does not exist");
119
                                                else if(statusCode == 409)
120
                                                        GSS.get().displayError("A file with the same name already exists");
121
                                                else if(statusCode == 413)
122
                                                        GSS.get().displayError("Your quota has been exceeded");
123
                                                else
124
                                                        GSS.get().displayError("Unable to restore file::"+((RestException)t).getHttpStatusText());
125
                                        }
126
                                        else
127
                                                GSS.get().displayError("System error restoring file:"+t.getMessage());
128
                                }
129
                        };
130
                        DeferredCommand.addCommand(rt);
131
                }
132
                else if (selection instanceof FolderResource) {
133
                        final FolderResource resource = (FolderResource)selection;
134
                        PostCommand rt = new PostCommand(resource.getUri()+"?restore=","", 200){
135

    
136
                                @Override
137
                                public void onComplete() {
138
                                        GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getRootItem());
139

    
140
                                        GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
141
                                }
142

    
143
                                @Override
144
                                public void onError(Throwable t) {
145
                                        GWT.log("", t);
146
                                        if(t instanceof RestException){
147
                                                int statusCode = ((RestException)t).getHttpStatusCode();
148
                                                if(statusCode == 405)
149
                                                        GSS.get().displayError("You don't have the necessary permissions");
150
                                                else if(statusCode == 404)
151
                                                        GSS.get().displayError("Folder does not exist");
152
                                                else if(statusCode == 409)
153
                                                        GSS.get().displayError("A folder with the same name already exists");
154
                                                else if(statusCode == 413)
155
                                                        GSS.get().displayError("Your quota has been exceeded");
156
                                                else
157
                                                        GSS.get().displayError("Unable to restore folder::"+((RestException)t).getHttpStatusText());
158
                                        }
159
                                        else
160
                                                GSS.get().displayError("System error restoring folder:"+t.getMessage());
161
                                }
162
                        };
163
                        DeferredCommand.addCommand(rt);
164
                }
165

    
166
        }
167

    
168
}