Statistics
| Branch: | Tag: | Revision:

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

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.ExecuteMultiplePost;
24
import gr.ebs.gss.client.rest.ExecutePost;
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
        public void execute() {
53
                containerPanel.hide();
54
                Object selection = GSS.get().getCurrentSelection();
55
                if (selection == null){
56
                        // Check to see if Trash Node is selected.
57
                        List folderList = new ArrayList();
58
                        TreeItem trashItem = GSS.get().getFolders().getTrashItem();
59
                        for(int i=0 ; i < trashItem.getChildCount() ; i++)
60
                                folderList.add(trashItem.getChild(i).getUserObject());
61
                        return;
62
                }
63
                GWT.log("selection: " + selection.toString(), null);
64
                if (selection instanceof FileResource) {
65
                        final FileResource resource = (FileResource)selection;
66
                        ExecutePost rt = new ExecutePost(resource.getUri()+"?restore=","", 200){
67

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

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

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

    
109
                                @Override
110
                                public void onError(String p, Throwable t) {
111
                                        GWT.log("", t);
112
                                        if(t instanceof RestException){
113
                                                int statusCode = ((RestException)t).getHttpStatusCode();
114
                                                if(statusCode == 405)
115
                                                        GSS.get().displayError("You don't have the necessary permissions");
116
                                                else if(statusCode == 404)
117
                                                        GSS.get().displayError("File does not exist");
118
                                                else if(statusCode == 409)
119
                                                        GSS.get().displayError("A file 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 restore file::"+((RestException)t).getHttpStatusText());
124
                                        }
125
                                        else
126
                                                GSS.get().displayError("System error restoring file:"+t.getMessage());
127
                                }
128
                        };
129
                        DeferredCommand.addCommand(rt);
130
                }
131
                else if (selection instanceof FolderResource) {
132
                        final FolderResource resource = (FolderResource)selection;
133
                        ExecutePost rt = new ExecutePost(resource.getUri()+"?restore=","", 200){
134

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

    
139
                                        GSS.get().getFolders().update(GSS.get().getFolders().getTrashItem());
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 == 404)
150
                                                        GSS.get().displayError("Folder does not exist");
151
                                                else if(statusCode == 409)
152
                                                        GSS.get().displayError("A folder with the same name already exists");
153
                                                else if(statusCode == 413)
154
                                                        GSS.get().displayError("Your quota has been exceeded");
155
                                                else
156
                                                        GSS.get().displayError("Unable to restore folder::"+((RestException)t).getHttpStatusText());
157
                                        }
158
                                        else
159
                                                GSS.get().displayError("System error restoring folder:"+t.getMessage());
160
                                }
161
                        };
162
                        DeferredCommand.addCommand(rt);
163
                }
164

    
165
        }
166

    
167
}