Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / commands / PropertiesCommand.java @ b3832e45

History | View | Annotate | Download (7.9 kB)

1
/*
2
 * Copyright 2011-2012 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.FilePermissionsDialog;
38
import gr.grnet.pithos.web.client.FilePropertiesDialog;
39
import gr.grnet.pithos.web.client.FileVersionsDialog;
40
import gr.grnet.pithos.web.client.FilesPropertiesDialog;
41
import gr.grnet.pithos.web.client.FolderPermissionsDialog;
42
import gr.grnet.pithos.web.client.FolderPropertiesDialog;
43
import gr.grnet.pithos.web.client.Pithos;
44
import gr.grnet.pithos.web.client.foldertree.File;
45
import gr.grnet.pithos.web.client.foldertree.Folder;
46
import gr.grnet.pithos.web.client.foldertree.Resource;
47
import gr.grnet.pithos.web.client.rest.HeadRequest;
48
import gr.grnet.pithos.web.client.rest.PutRequest;
49
import gr.grnet.pithos.web.client.rest.RestException;
50

    
51
import java.util.List;
52

    
53
import com.google.gwt.core.client.GWT;
54
import com.google.gwt.core.client.Scheduler;
55
import com.google.gwt.http.client.Response;
56
import com.google.gwt.user.client.Command;
57
import com.google.gwt.user.client.ui.PopupPanel;
58

    
59
/**
60
 * The command that displays the appropriate Properties dialog, according to the
61
 * selected object in the application.
62
 *
63
 */
64
public class PropertiesCommand implements Command {
65

    
66
        public static final int PROPERTIES = 0;
67
        public static final int PERMISSIONS = 1;
68
        public static final int VERSIONS = 2;
69

    
70
        private PopupPanel containerPanel;
71

    
72
        private int tabToShow = 0;
73

    
74
    private Object resource;
75

    
76
    Pithos app;
77

    
78
        /**
79
         * @param _containerPanel
80
         * @param _tab the tab to switch to
81
         */
82
        public PropertiesCommand(Pithos _app, PopupPanel _containerPanel, Object _resource, int _tab) {
83
                containerPanel = _containerPanel;
84
                tabToShow = _tab;
85
        resource = _resource;
86
        app = _app;
87
        }
88

    
89
        @Override
90
        public void execute() {
91
        if (containerPanel != null)
92
                    containerPanel.hide();
93

    
94
        if (resource instanceof Folder) {
95
            final Folder folder = (Folder) resource;
96
            switch (tabToShow) {
97
                                case PROPERTIES:
98
                                        FolderPropertiesDialog dlg = new FolderPropertiesDialog(app, false, folder);
99
                                        dlg.center();
100
                                        break;
101
                                case PERMISSIONS:
102
                                        final FolderPermissionsDialog dlg1 = new FolderPermissionsDialog(app, folder);
103
                                        scheduleFolderHeadCommand(folder, dlg1);
104
                                        break;
105
                                default:
106
                                        break;
107
                        }
108
        }
109
        else if (resource instanceof List) {
110
            @SuppressWarnings("unchecked")
111
                        List<File> files = (List<File>) resource;
112
            if (files.size() > 1) {
113
                FilesPropertiesDialog dlg = new FilesPropertiesDialog(app, files);
114
                dlg.center();
115
            }
116
            else {
117
                                File f = files.get(0);
118
                                PopupPanel dlg = null;
119
                    switch (tabToShow) {
120
                                        case PROPERTIES:
121
                                dlg = new FilePropertiesDialog(app, f);
122
                                scheduleFileHeadCommand(f, dlg);
123
                                                break;
124
                                        case PERMISSIONS:
125
                                dlg = new FilePermissionsDialog(app, f);
126
                                scheduleFileHeadCommand(f, dlg);
127
                                                break;
128
                                        case VERSIONS:
129
                                FileVersionsDialog dlg2 = new FileVersionsDialog(app, files.get(0));
130
                                dlg2.center();
131
                                                break;
132
                                        default:
133
                                                break;
134
                                }
135
            }
136
        }
137
        }
138

    
139
        private void scheduleFileHeadCommand(File f, final PopupPanel dlg) {
140
                HeadRequest<File> headFile = new HeadRequest<File>(File.class, app.getApiPath(), f.getOwner(), f.getUri(), f) {
141

    
142
                        @Override
143
                        public void onSuccess(File _result) {
144
                                dlg.center();
145
                        }
146

    
147
                        @Override
148
                        public void onError(Throwable t) {
149
                        GWT.log("Error heading file", t);
150
                                app.setError(t);
151
                        if (t instanceof RestException)
152
                            app.displayError("Error heading file: " + ((RestException) t).getHttpStatusText());
153
                        else
154
                            app.displayError("System error heading file: " + t.getMessage());
155
                        }
156

    
157
                        @Override
158
                        protected void onUnauthorized(Response response) {
159
                                app.sessionExpired();
160
                        }
161
                };
162
                headFile.setHeader("X-Auth-Token", app.getToken());
163
                Scheduler.get().scheduleDeferred(headFile);
164
        }
165
        
166
        void scheduleFolderHeadCommand(final Folder folder, final FolderPermissionsDialog dlg) {
167
                HeadRequest<Folder> headFolder = new HeadRequest<Folder>(Folder.class, app.getApiPath(), folder.getOwner(), folder.getUri(), folder) {
168

    
169
                        @Override
170
                        public void onSuccess(Folder _result) {
171
                                dlg.center();
172
                        }
173

    
174
                        @Override
175
                        public void onError(Throwable t) {
176
                        if (t instanceof RestException) {
177
                                if (((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND) {
178
                        final String path = folder.getUri();
179
                        PutRequest newFolder = new PutRequest(app.getApiPath(), folder.getOwner(), path) {
180
                            @Override
181
                            public void onSuccess(Resource _result) {
182
                                    scheduleFolderHeadCommand(folder, dlg);
183
                            }
184

    
185
                            @Override
186
                            public void onError(Throwable _t) {
187
                                GWT.log("", _t);
188
                                                        app.setError(_t);
189
                                if(_t instanceof RestException){
190
                                    app.displayError("Unable to create folder: " + ((RestException) _t).getHttpStatusText());
191
                                }
192
                                else
193
                                    app.displayError("System error creating folder: " + _t.getMessage());
194
                            }
195

    
196
                                            @Override
197
                                            protected void onUnauthorized(Response response) {
198
                                                    app.sessionExpired();
199
                                            }
200
                        };
201
                        newFolder.setHeader("X-Auth-Token", app.getToken());
202
                        newFolder.setHeader("Content-Type", "application/folder");
203
                        newFolder.setHeader("Accept", "*/*");
204
                        newFolder.setHeader("Content-Length", "0");
205
                        Scheduler.get().scheduleDeferred(newFolder);
206
                                }
207
                                else
208
                                        app.displayError("Error heading folder: " + ((RestException) t).getHttpStatusText());
209
                        }
210
                        else
211
                            app.displayError("System error heading folder: " + t.getMessage());
212

    
213
                        GWT.log("Error heading folder", t);
214
                                app.setError(t);
215
                        }
216

    
217
                        @Override
218
                        protected void onUnauthorized(Response response) {
219
                                app.sessionExpired();
220
                        }
221
                };
222
                headFolder.setHeader("X-Auth-Token", app.getToken());
223
                Scheduler.get().scheduleDeferred(headFolder);
224
        }
225
}