Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / FilePropertiesDialog.java @ 71875b42

History | View | Annotate | Download (12.8 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;
36

    
37
import gr.grnet.pithos.web.client.foldertree.File;
38
import gr.grnet.pithos.web.client.foldertree.Resource;
39
import gr.grnet.pithos.web.client.rest.PostRequest;
40
import gr.grnet.pithos.web.client.rest.PutRequest;
41

    
42
import java.util.HashMap;
43
import java.util.Map;
44

    
45
import com.google.gwt.core.client.GWT;
46
import com.google.gwt.core.client.Scheduler;
47
import com.google.gwt.event.dom.client.ClickEvent;
48
import com.google.gwt.event.dom.client.ClickHandler;
49
import com.google.gwt.http.client.Response;
50
import com.google.gwt.http.client.URL;
51
import com.google.gwt.i18n.client.DateTimeFormat;
52
import com.google.gwt.user.client.Command;
53
import com.google.gwt.user.client.ui.Anchor;
54
import com.google.gwt.user.client.ui.Button;
55
import com.google.gwt.user.client.ui.FlexTable;
56
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
57
import com.google.gwt.user.client.ui.HorizontalPanel;
58
import com.google.gwt.user.client.ui.Image;
59
import com.google.gwt.user.client.ui.Label;
60
import com.google.gwt.user.client.ui.TextBox;
61
import com.google.gwt.user.client.ui.VerticalPanel;
62

    
63
/**
64
 * The 'File properties' dialog box implementation.
65
 *
66
 */
67
public class FilePropertiesDialog extends AbstractPropertiesDialog {
68

    
69
        /**
70
         * The widget that holds the name of the file.
71
         */
72
        private TextBox name = new TextBox();
73

    
74
        final File file;
75

    
76
    FlexTable metaTable;
77
        /**
78
         * The widget's constructor.
79
         */
80
        public FilePropertiesDialog(Pithos _app, File _file) {
81
        super(_app);
82
        file = _file;
83

    
84
                Anchor close = new Anchor();
85
                close.addStyleName("close");
86
                close.addClickHandler(new ClickHandler() {
87
                        
88
                        @Override
89
                        public void onClick(ClickEvent event) {
90
                                hide();
91
                        }
92
                });
93
                // Set the dialog's caption.
94
                setText("File properties");
95
                setAnimationEnabled(true);
96
                setGlassEnabled(true);
97
                setStyleName("pithos-DialogBox");
98

    
99
                // Outer contains inner and buttons.
100
                final VerticalPanel outer = new VerticalPanel();
101
                outer.add(close);
102
                // Inner contains generalPanel and permPanel.
103
                inner = new VerticalPanel();
104
                inner.addStyleName("inner");
105

    
106
        inner.add(createGeneralPanel());
107

    
108
        outer.add(inner);
109

    
110
                // Create the 'OK' button, along with a listener that hides the dialog
111
                // when the button is clicked.
112
                final Button ok = new Button("OK", new ClickHandler() {
113
                        @Override
114
                        public void onClick(ClickEvent event) {
115
                                accept();
116
                                closeDialog();
117
                        }
118
                });
119
                ok.addStyleName("button");
120

    
121
        outer.add(ok);
122
        outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
123

    
124
        setWidget(outer);
125
        }
126

    
127
        private VerticalPanel createGeneralPanel() {
128
        final VerticalPanel generalPanel = new VerticalPanel();
129
        final FlexTable generalTable = new FlexTable();
130
        generalTable.setText(0, 0, "Name");
131
        generalTable.setText(1, 0, "Folder");
132
        generalTable.setText(2, 0, "Owner");
133
        generalTable.setText(3, 0, "Last modified");
134

    
135
        name.setWidth("100%");
136
        name.setText(file.getName());
137
        generalTable.setWidget(0, 1, name);
138
        if(file.getParent() != null)
139
            generalTable.setText(1, 1, file.getParent().getName());
140
        else
141
            generalTable.setText(1, 1, "-");
142
        generalTable.setText(2, 1, file.getOwner());
143

    
144
        final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
145
        generalTable.setText(3, 1, file.getLastModified() != null ? formatter.format(file.getLastModified()) : "");
146

    
147
        generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
148
        generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
149
        generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
150
        generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
151
        generalTable.getFlexCellFormatter().setStyleName(4, 0, "props-labels");
152
        generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
153
        generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
154
        generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
155
        generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
156
        generalTable.setCellSpacing(4);
157

    
158
        generalPanel.add(generalTable);
159

    
160
        HorizontalPanel metaTitlePanel = new HorizontalPanel();
161
        metaTitlePanel.setSpacing(5);
162
        Label meta = new Label("Meta data");
163
        meta.addStyleName("pithos-metaTitle");
164
        metaTitlePanel.add(meta);
165
        
166
                Image plus = new Image();
167
                plus.addStyleName("pithos-addMetaImg");
168
                metaTitlePanel.add(plus);
169
                
170
                generalPanel.add(metaTitlePanel);
171
                
172
                metaTable = new FlexTable();
173
                metaTable.setCellSpacing(0);
174
                metaTable.setHTML(0, 0, "Name");
175
                metaTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
176
                metaTable.setText(0, 1, "Value");
177
                metaTable.getFlexCellFormatter().setStyleName(0, 1, "props-labels");
178
                int rows = 1;
179
                for (String metaKey : file.getMeta().keySet()) {
180
                        addFormLine(metaTable, rows++, metaKey, file.getMeta().get(metaKey));
181
                }
182
                if (rows == 1) //If no meta add an empty line
183
                        addFormLine(metaTable, rows++, "", "");
184
                
185
                plus.addClickHandler(new ClickHandler() {
186
                        
187
                        @Override
188
                        public void onClick(ClickEvent event) {
189
                                addFormLine(metaTable, metaTable.getRowCount(), "", "");
190
                        }
191
                });
192

    
193
                generalPanel.add(metaTable);
194
        generalPanel.setSpacing(4);
195
        return generalPanel;
196
    }
197

    
198
        void addFormLine(final FlexTable table, int row, String _name, String _value) {
199
                TextBox nameBox = new TextBox();
200
                nameBox.setText(_name);
201
                nameBox.addStyleName("pithos-metaName");
202
                table.setWidget(row, 0, nameBox);
203
                table.getFlexCellFormatter().setStyleName(1, 0, "props-values");
204

    
205
                TextBox valueBox = new TextBox();
206
                valueBox.setText(_value);
207
                valueBox.addStyleName("pithos-metaValue");
208
                table.setWidget(row, 1, valueBox);
209
                table.getFlexCellFormatter().setStyleName(1, 1, "props-values");
210
                
211
                Image delete = new Image();
212
                delete.addStyleName("pithos-metaDeleteImg");
213
                delete.addClickHandler(new ClickHandler() {
214
                        
215
                        @Override
216
                        public void onClick(ClickEvent event) {
217
                                int rowIndex = table.getCellForEvent(event).getRowIndex();
218
                                table.removeRow(rowIndex);
219
                        }
220
                });
221
                table.setWidget(row, 2, delete);
222
        }
223

    
224
        /**
225
         * Accepts any change and updates the file
226
         *
227
         */
228
        @Override
229
        protected void accept() {
230
                String newFilename = null;
231

    
232
                if (!name.getText().trim().equals(file.getName())) {
233
                        newFilename = name.getText().trim();
234
                }
235

    
236
        final Map<String, String> newMeta = new HashMap<String, String>();
237
        for (int row = 1; row < metaTable.getRowCount(); row++) {
238
                String key = ((TextBox) metaTable.getWidget(row, 0)).getText().trim();
239
                String value = ((TextBox) metaTable.getWidget(row, 1)).getText().trim();
240
                if (key.length() > 0 && value.length() > 0)
241
                        newMeta.put(key, value);
242
        }
243

    
244
        if (newFilename != null) {
245
            final String path = file.getParent().getUri() + "/" + newFilename;
246
            PutRequest updateFile = new PutRequest(app.getApiPath(), app.getUsername(), path) {
247
                @Override
248
                public void onSuccess(Resource result) {
249
                    updateMetaData(app.getApiPath(), file.getOwner(), path + "?update=", newMeta);
250
                }
251

    
252
                @Override
253
                public void onError(Throwable t) {
254
                    GWT.log("", t);
255
                                        app.setError(t);
256
                    app.displayError("System error modifying file:" + t.getMessage());
257
                }
258

    
259
                                @Override
260
                                protected void onUnauthorized(Response response) {
261
                                        app.sessionExpired();
262
                                }
263
            };
264
            updateFile.setHeader("X-Auth-Token", app.getToken());
265
            updateFile.setHeader("X-Move-From", URL.encodePathSegment(file.getUri()));
266
            updateFile.setHeader("Content-Type", file.getContentType());
267
            for (String key : file.getMeta().keySet())
268
                updateFile.setHeader("X-Object-Meta-" + URL.encodePathSegment(key.trim()), URL.encodePathSegment(newMeta.get(key)));
269
            if (file.isPublished())
270
                updateFile.setHeader("X-Object-Public", "true");
271
            String readPermHeader = "read=";
272
            String writePermHeader = "write=";
273
            for (String u : file.getPermissions().keySet()) {
274
                Boolean[] p = file.getPermissions().get(u);
275
                if (p[0] != null && p[0])
276
                    readPermHeader += u + ",";
277
                if (p[1] != null && p[1])
278
                    writePermHeader += u + ",";
279
            }
280
            if (readPermHeader.endsWith("="))
281
                readPermHeader = "";
282
            else if (readPermHeader.endsWith(","))
283
                readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
284
            if (writePermHeader.endsWith("="))
285
                writePermHeader = "";
286
            else if (writePermHeader.endsWith(","))
287
                writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
288
            String permHeader = readPermHeader +  ((readPermHeader.length()  > 0 && writePermHeader.length() > 0) ?  ";" : "") + writePermHeader;
289
            if (permHeader.length() == 0)
290
                permHeader="~";
291
            else
292
                    permHeader = URL.encodePathSegment(permHeader);
293
            updateFile.setHeader("X-Object-Sharing", permHeader);
294

    
295
            Scheduler.get().scheduleDeferred(updateFile);
296
        }
297
        else
298
            updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", newMeta);
299
        }
300

    
301
        protected void updateMetaData(String api, String owner, String path, Map<String, String> newMeta) {
302
        if (newMeta != null) {
303
            PostRequest updateFile = new PostRequest(api, owner, path) {
304
                @Override
305
                public void onSuccess(Resource result) {
306
                    app.updateFolder(file.getParent(), true, new Command() {
307
                                                
308
                                                @Override
309
                                                public void execute() {
310
                                                        app.updateMySharedRoot();
311
                                                }
312
                                        }, true);
313
                }
314

    
315
                @Override
316
                public void onError(Throwable t) {
317
                    GWT.log("", t);
318
                                        app.setError(t);
319
                    app.displayError("System error modifying file:" + t.getMessage());
320
                }
321

    
322
                                @Override
323
                                protected void onUnauthorized(Response response) {
324
                                        app.sessionExpired();
325
                                }
326
            };
327
            updateFile.setHeader("X-Auth-Token", app.getToken());
328
            
329
            for (String t : file.getMeta().keySet()) {
330
                        updateFile.setHeader("X-Object-Meta-" + URL.encodePathSegment(t.trim()), "~");
331
            }
332
            
333
            for (String key : newMeta.keySet())
334
                updateFile.setHeader("X-Object-Meta-" + URL.encodePathSegment(key.trim()), URL.encodePathSegment(newMeta.get(key)));
335
            
336
            Scheduler.get().scheduleDeferred(updateFile);
337
        }
338
        else
339
            app.updateFolder(file.getParent(), true, new Command() {
340
                                
341
                                @Override
342
                                public void execute() {
343
                                        if (file.isSharedOrPublished())
344
                                                app.updateMySharedRoot();
345
                                }
346
                        }, true);
347
    }
348
}