Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / FileUploadGearsDialog.java @ 58777026

History | View | Annotate | Download (14.4 kB)

1 ab1eb3f8 Christos Stathis
/*
2 58777026 Christos Stathis
 * Copyright 2011 GRNET S.A. All rights reserved.
3 58777026 Christos Stathis
 *
4 58777026 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 58777026 Christos Stathis
 * without modification, are permitted provided that the following
6 58777026 Christos Stathis
 * conditions are met:
7 58777026 Christos Stathis
 *
8 58777026 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 58777026 Christos Stathis
 *      disclaimer.
11 58777026 Christos Stathis
 *
12 58777026 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 58777026 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 58777026 Christos Stathis
 *      provided with the distribution.
16 58777026 Christos Stathis
 *
17 58777026 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 58777026 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 58777026 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 58777026 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 58777026 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 58777026 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 58777026 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 58777026 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 58777026 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 58777026 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 58777026 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 58777026 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 58777026 Christos Stathis
 *
30 58777026 Christos Stathis
 * The views and conclusions contained in the software and
31 58777026 Christos Stathis
 * documentation are those of the authors and should not be
32 58777026 Christos Stathis
 * interpreted as representing official policies, either expressed
33 58777026 Christos Stathis
 * or implied, of GRNET S.A.
34 ab1eb3f8 Christos Stathis
 */
35 ab1eb3f8 Christos Stathis
package gr.grnet.pithos.web.client;
36 ab1eb3f8 Christos Stathis
37 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.PostCommand;
38 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestCommand;
39 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
40 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.resource.FileResource;
41 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
42 ab1eb3f8 Christos Stathis
43 ab1eb3f8 Christos Stathis
import java.util.ArrayList;
44 ab1eb3f8 Christos Stathis
import java.util.Arrays;
45 ab1eb3f8 Christos Stathis
import java.util.HashMap;
46 ab1eb3f8 Christos Stathis
import java.util.List;
47 ab1eb3f8 Christos Stathis
import java.util.Map;
48 ab1eb3f8 Christos Stathis
49 ab1eb3f8 Christos Stathis
import com.google.gwt.core.client.GWT;
50 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.ClickEvent;
51 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.ClickHandler;
52 ab1eb3f8 Christos Stathis
import com.google.gwt.gears.client.Factory;
53 ab1eb3f8 Christos Stathis
import com.google.gwt.gears.client.desktop.Desktop;
54 ab1eb3f8 Christos Stathis
import com.google.gwt.gears.client.desktop.File;
55 ab1eb3f8 Christos Stathis
import com.google.gwt.gears.client.desktop.OpenFilesHandler;
56 ab1eb3f8 Christos Stathis
import com.google.gwt.gears.client.httprequest.HttpRequest;
57 ab1eb3f8 Christos Stathis
import com.google.gwt.gears.client.httprequest.ProgressEvent;
58 ab1eb3f8 Christos Stathis
import com.google.gwt.gears.client.httprequest.ProgressHandler;
59 ab1eb3f8 Christos Stathis
import com.google.gwt.gears.client.httprequest.RequestCallback;
60 ab1eb3f8 Christos Stathis
import com.google.gwt.http.client.URL;
61 ab1eb3f8 Christos Stathis
import com.google.gwt.json.client.JSONObject;
62 ab1eb3f8 Christos Stathis
import com.google.gwt.json.client.JSONString;
63 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.DeferredCommand;
64 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.Button;
65 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.FlexTable;
66 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.HTML;
67 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
68 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.HorizontalPanel;
69 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.VerticalPanel;
70 ab1eb3f8 Christos Stathis
71 ab1eb3f8 Christos Stathis
/**
72 ab1eb3f8 Christos Stathis
 * The 'File upload' dialog box implementation with Google Gears support.
73 ab1eb3f8 Christos Stathis
 */
74 ab1eb3f8 Christos Stathis
public class FileUploadGearsDialog extends FileUploadDialog implements Updateable {
75 ab1eb3f8 Christos Stathis
76 ab1eb3f8 Christos Stathis
        protected final Factory factory = Factory.getInstance();
77 ab1eb3f8 Christos Stathis
78 ab1eb3f8 Christos Stathis
        /**
79 ab1eb3f8 Christos Stathis
         * The array of files to upload.
80 ab1eb3f8 Christos Stathis
         */
81 ab1eb3f8 Christos Stathis
        private File[] fileObjects;
82 ab1eb3f8 Christos Stathis
83 ab1eb3f8 Christos Stathis
        /**
84 ab1eb3f8 Christos Stathis
         * A list of files to upload, created from files array. Used to signal
85 ab1eb3f8 Christos Stathis
         * finished state when empty.
86 ab1eb3f8 Christos Stathis
         */
87 ab1eb3f8 Christos Stathis
        protected List<File> selectedFiles = new ArrayList<File>();
88 ab1eb3f8 Christos Stathis
89 ab1eb3f8 Christos Stathis
        /**
90 ab1eb3f8 Christos Stathis
         * The list of progress bars for individual files.
91 ab1eb3f8 Christos Stathis
         */
92 ab1eb3f8 Christos Stathis
        protected List<ProgressBar> progressBars = new ArrayList<ProgressBar>();
93 ab1eb3f8 Christos Stathis
94 ab1eb3f8 Christos Stathis
        private Button browse;
95 ab1eb3f8 Christos Stathis
96 ab1eb3f8 Christos Stathis
        private Button submit;
97 ab1eb3f8 Christos Stathis
98 ab1eb3f8 Christos Stathis
        private FlexTable generalTable;
99 ab1eb3f8 Christos Stathis
100 ab1eb3f8 Christos Stathis
        private Map<String, FileResource> toRename;
101 ab1eb3f8 Christos Stathis
102 ab1eb3f8 Christos Stathis
        protected List<HttpRequest> requests = new ArrayList<HttpRequest>();
103 ab1eb3f8 Christos Stathis
        
104 ab1eb3f8 Christos Stathis
        private boolean canContinue = true;
105 ab1eb3f8 Christos Stathis
106 ab1eb3f8 Christos Stathis
        /**
107 ab1eb3f8 Christos Stathis
         * The widget's constructor.
108 ab1eb3f8 Christos Stathis
         */
109 ab1eb3f8 Christos Stathis
        public FileUploadGearsDialog() {
110 ab1eb3f8 Christos Stathis
                // Set the dialog's caption.
111 ab1eb3f8 Christos Stathis
                setText("File upload");
112 ab1eb3f8 Christos Stathis
                setAnimationEnabled(true);
113 ab1eb3f8 Christos Stathis
                // Create a panel to hold all of the dialog widgets.
114 ab1eb3f8 Christos Stathis
                VerticalPanel panel = new VerticalPanel();
115 ab1eb3f8 Christos Stathis
                final HTML info = new HTML("You may select one or more files to upload.");
116 ab1eb3f8 Christos Stathis
                info.addStyleName("pithos-uploadNote");
117 ab1eb3f8 Christos Stathis
                panel.add(info);
118 ab1eb3f8 Christos Stathis
                // Add an informative label with the folder name.
119 ab1eb3f8 Christos Stathis
                Object selection = GSS.get().getTreeView().getSelection();
120 ab1eb3f8 Christos Stathis
                folder = ((RestResourceWrapper) selection).getResource();
121 ab1eb3f8 Christos Stathis
122 ab1eb3f8 Christos Stathis
                browse = new Button("Browse...");
123 ab1eb3f8 Christos Stathis
124 ab1eb3f8 Christos Stathis
                HorizontalPanel fileUploadPanel = new HorizontalPanel();
125 ab1eb3f8 Christos Stathis
                fileUploadPanel.add(browse);
126 ab1eb3f8 Christos Stathis
127 ab1eb3f8 Christos Stathis
                generalTable = new FlexTable();
128 ab1eb3f8 Christos Stathis
                generalTable.setText(0, 0, "Folder");
129 ab1eb3f8 Christos Stathis
                generalTable.setText(1, 0, "File");
130 ab1eb3f8 Christos Stathis
                generalTable.setText(0, 1, folder.getName());
131 ab1eb3f8 Christos Stathis
                generalTable.setWidget(1, 1, fileUploadPanel);
132 ab1eb3f8 Christos Stathis
                generalTable.getCellFormatter().setStyleName(0, 0, "props-labels");
133 ab1eb3f8 Christos Stathis
                generalTable.getCellFormatter().setStyleName(1, 0, "props-labels");
134 ab1eb3f8 Christos Stathis
                generalTable.getCellFormatter().setStyleName(0, 1, "props-values");
135 ab1eb3f8 Christos Stathis
                generalTable.getCellFormatter().setStyleName(1, 1, "props-values");
136 ab1eb3f8 Christos Stathis
                generalTable.setCellSpacing(4);
137 ab1eb3f8 Christos Stathis
138 ab1eb3f8 Christos Stathis
                panel.add(generalTable);
139 ab1eb3f8 Christos Stathis
140 ab1eb3f8 Christos Stathis
                // Create a panel to hold the buttons.
141 ab1eb3f8 Christos Stathis
                HorizontalPanel buttons = new HorizontalPanel();
142 ab1eb3f8 Christos Stathis
143 ab1eb3f8 Christos Stathis
                submit = new Button("Upload");
144 ab1eb3f8 Christos Stathis
                submit.addClickHandler(new ClickHandler() {
145 ab1eb3f8 Christos Stathis
                        @Override
146 ab1eb3f8 Christos Stathis
                        public void onClick(ClickEvent event) {
147 ab1eb3f8 Christos Stathis
                                prepareAndSubmit();
148 ab1eb3f8 Christos Stathis
                        }
149 ab1eb3f8 Christos Stathis
                });
150 ab1eb3f8 Christos Stathis
                submit.setEnabled(false);
151 ab1eb3f8 Christos Stathis
                buttons.add(submit);
152 ab1eb3f8 Christos Stathis
                buttons.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_CENTER);
153 ab1eb3f8 Christos Stathis
                // Create the 'Cancel' button, along with a listener that hides the
154 ab1eb3f8 Christos Stathis
                // dialog when the button is clicked.
155 ab1eb3f8 Christos Stathis
                Button cancel = new Button("Cancel", new ClickHandler() {
156 ab1eb3f8 Christos Stathis
                        @Override
157 ab1eb3f8 Christos Stathis
                        public void onClick(ClickEvent event) {
158 ab1eb3f8 Christos Stathis
                                canContinue = false;                                
159 ab1eb3f8 Christos Stathis
                                cancelUpload();                                
160 ab1eb3f8 Christos Stathis
                                GSS.get().showFileList(true);
161 ab1eb3f8 Christos Stathis
                        }
162 ab1eb3f8 Christos Stathis
                });
163 ab1eb3f8 Christos Stathis
                buttons.add(cancel);
164 ab1eb3f8 Christos Stathis
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
165 ab1eb3f8 Christos Stathis
                buttons.setSpacing(8);
166 ab1eb3f8 Christos Stathis
                buttons.addStyleName("pithos-DialogBox");
167 ab1eb3f8 Christos Stathis
168 ab1eb3f8 Christos Stathis
                browse.addClickHandler(new ClickHandler() {
169 ab1eb3f8 Christos Stathis
                        @Override
170 ab1eb3f8 Christos Stathis
                        public void onClick(ClickEvent event) {
171 ab1eb3f8 Christos Stathis
                                Desktop desktop = factory.createDesktop();
172 ab1eb3f8 Christos Stathis
                                desktop.openFiles(new OpenFilesHandler() {
173 ab1eb3f8 Christos Stathis
174 ab1eb3f8 Christos Stathis
                                        @Override
175 ab1eb3f8 Christos Stathis
                                        public void onOpenFiles(OpenFilesEvent ofevent) {
176 ab1eb3f8 Christos Stathis
                                                fileObjects = ofevent.getFiles();
177 ab1eb3f8 Christos Stathis
                                                selectedFiles.addAll(Arrays.asList(fileObjects));
178 ab1eb3f8 Christos Stathis
                                                for (int i = 0; i< selectedFiles.size(); i++) {
179 ab1eb3f8 Christos Stathis
                                                        generalTable.setText(i+1, 0, "File");
180 ab1eb3f8 Christos Stathis
                                                        generalTable.setText(i+1, 1, selectedFiles.get(i).getName());
181 ab1eb3f8 Christos Stathis
                                                        ProgressBar progress = new ProgressBar(20, 0);
182 ab1eb3f8 Christos Stathis
                                                        generalTable.setWidget(i+1, 2, progress);
183 ab1eb3f8 Christos Stathis
                                                        progressBars.add(progress);
184 ab1eb3f8 Christos Stathis
                                                        generalTable.getCellFormatter().setStyleName(i+1, 0, "props-labels");
185 ab1eb3f8 Christos Stathis
                                                        generalTable.getCellFormatter().setStyleName(i+1, 1, "props-values");
186 ab1eb3f8 Christos Stathis
                                                }
187 ab1eb3f8 Christos Stathis
                                                submit.setEnabled(true);
188 ab1eb3f8 Christos Stathis
                                        }
189 ab1eb3f8 Christos Stathis
                                });
190 ab1eb3f8 Christos Stathis
                        }
191 ab1eb3f8 Christos Stathis
                });
192 ab1eb3f8 Christos Stathis
193 ab1eb3f8 Christos Stathis
                panel.add(buttons);
194 ab1eb3f8 Christos Stathis
                panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
195 ab1eb3f8 Christos Stathis
                panel.addStyleName("pithos-DialogBox");
196 ab1eb3f8 Christos Stathis
                addStyleName("pithos-DialogBox");
197 ab1eb3f8 Christos Stathis
                setWidget(panel);
198 ab1eb3f8 Christos Stathis
        }
199 ab1eb3f8 Christos Stathis
200 ab1eb3f8 Christos Stathis
        /**
201 ab1eb3f8 Christos Stathis
         * Cancels the file upload.
202 ab1eb3f8 Christos Stathis
         */
203 ab1eb3f8 Christos Stathis
        private void cancelUpload() {
204 ab1eb3f8 Christos Stathis
                for (HttpRequest request: requests)
205 ab1eb3f8 Christos Stathis
                        request.abort();
206 ab1eb3f8 Christos Stathis
                hide();                
207 ab1eb3f8 Christos Stathis
        }
208 ab1eb3f8 Christos Stathis
209 ab1eb3f8 Christos Stathis
        /**
210 ab1eb3f8 Christos Stathis
         * Check whether the specified file name exists in the selected folder.
211 ab1eb3f8 Christos Stathis
         */
212 ab1eb3f8 Christos Stathis
        private boolean canContinue(File file) {
213 ab1eb3f8 Christos Stathis
                String fileName = getFilename(file.getName());
214 ab1eb3f8 Christos Stathis
                if (getFileForName(fileName) == null)
215 ab1eb3f8 Christos Stathis
                        // For file creation, check to see if the file already exists.
216 ab1eb3f8 Christos Stathis
                        for (FileResource fileRes : files)
217 ab1eb3f8 Christos Stathis
                                if (!fileRes.isDeleted() && fileRes.getName().equals(fileName))
218 ab1eb3f8 Christos Stathis
                                        return false;
219 ab1eb3f8 Christos Stathis
                return true;
220 ab1eb3f8 Christos Stathis
        }
221 ab1eb3f8 Christos Stathis
222 ab1eb3f8 Christos Stathis
        @Override
223 ab1eb3f8 Christos Stathis
        public void prepareAndSubmit() {
224 ab1eb3f8 Christos Stathis
                GSS app = GSS.get();
225 ab1eb3f8 Christos Stathis
                if (selectedFiles.size() == 0) {
226 ab1eb3f8 Christos Stathis
                        app.displayError("You must select a file!");
227 ab1eb3f8 Christos Stathis
                        hide();
228 ab1eb3f8 Christos Stathis
                        return;
229 ab1eb3f8 Christos Stathis
                }
230 ab1eb3f8 Christos Stathis
                for (File file: selectedFiles)
231 ab1eb3f8 Christos Stathis
                        if (!canContinue(file)) {
232 ab1eb3f8 Christos Stathis
                                app.displayError("The file name " + file.getName() +
233 ab1eb3f8 Christos Stathis
                                                        " already exists in this folder");
234 ab1eb3f8 Christos Stathis
                                hide();
235 ab1eb3f8 Christos Stathis
                                return;
236 ab1eb3f8 Christos Stathis
                        }
237 ab1eb3f8 Christos Stathis
                submit.setEnabled(false);
238 ab1eb3f8 Christos Stathis
                browse.setVisible(false);
239 ab1eb3f8 Christos Stathis
                List<String> toUpdate = new ArrayList<String>();
240 ab1eb3f8 Christos Stathis
                toRename = new HashMap<String, FileResource>();
241 ab1eb3f8 Christos Stathis
                for (File file: selectedFiles) {
242 ab1eb3f8 Christos Stathis
                        String fname = getFilename(file.getName());
243 ab1eb3f8 Christos Stathis
                        if (getFileForName(fname) == null) {
244 ab1eb3f8 Christos Stathis
                                // We are going to create a file, so we check to see if there is a
245 ab1eb3f8 Christos Stathis
                                // trashed file with the same name.
246 ab1eb3f8 Christos Stathis
                                FileResource same = null;
247 ab1eb3f8 Christos Stathis
                                for (FileResource fres : folder.getFiles())
248 ab1eb3f8 Christos Stathis
                                        if (fres.isDeleted() && fres.getName().equals(fname))
249 ab1eb3f8 Christos Stathis
                                                same = fres;
250 ab1eb3f8 Christos Stathis
                                // In that case add it to the list of files to rename.
251 ab1eb3f8 Christos Stathis
                                if (same != null)
252 ab1eb3f8 Christos Stathis
                                        toRename.put(getBackupFilename(fname), same);
253 ab1eb3f8 Christos Stathis
                        } else
254 ab1eb3f8 Christos Stathis
                                // If we are updating a file add it to the list of files to update.
255 ab1eb3f8 Christos Stathis
                                toUpdate.add(fname);
256 ab1eb3f8 Christos Stathis
                }
257 ab1eb3f8 Christos Stathis
258 ab1eb3f8 Christos Stathis
                if (!toUpdate.isEmpty()) {
259 ab1eb3f8 Christos Stathis
                        StringBuffer sb = new StringBuffer();
260 ab1eb3f8 Christos Stathis
                        for (String name: toUpdate)
261 ab1eb3f8 Christos Stathis
                                sb.append(name).append("<br/>");
262 ab1eb3f8 Christos Stathis
                        // We are going to update existing files, so show a confirmation dialog.
263 ab1eb3f8 Christos Stathis
                        ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " +
264 ab1eb3f8 Christos Stathis
                                        "you want to update the following files?<br/><i>" + sb +
265 ab1eb3f8 Christos Stathis
                                        "</i>", "Update") {
266 ab1eb3f8 Christos Stathis
267 ab1eb3f8 Christos Stathis
                                @Override
268 ab1eb3f8 Christos Stathis
                                public void cancel() {
269 ab1eb3f8 Christos Stathis
                                        hide();
270 ab1eb3f8 Christos Stathis
                                }
271 ab1eb3f8 Christos Stathis
272 ab1eb3f8 Christos Stathis
                                @Override
273 ab1eb3f8 Christos Stathis
                                public void confirm() {
274 ab1eb3f8 Christos Stathis
                                        confirmRename();
275 ab1eb3f8 Christos Stathis
                                }
276 ab1eb3f8 Christos Stathis
277 ab1eb3f8 Christos Stathis
                        };
278 ab1eb3f8 Christos Stathis
                        confirm.center();
279 ab1eb3f8 Christos Stathis
                } else
280 ab1eb3f8 Christos Stathis
                        confirmRename();
281 ab1eb3f8 Christos Stathis
        }
282 ab1eb3f8 Christos Stathis
283 ab1eb3f8 Christos Stathis
        /**
284 ab1eb3f8 Christos Stathis
         * Confirm the renames of synonymous files already in the trash.
285 ab1eb3f8 Christos Stathis
         */
286 ab1eb3f8 Christos Stathis
        private void confirmRename() {
287 ab1eb3f8 Christos Stathis
                if (!toRename.isEmpty()) {
288 ab1eb3f8 Christos Stathis
                        StringBuffer sb = new StringBuffer();
289 ab1eb3f8 Christos Stathis
                        for (FileResource file: toRename.values())
290 ab1eb3f8 Christos Stathis
                                sb.append(file.getName()).append("<br/>");
291 ab1eb3f8 Christos Stathis
                        ConfirmationDialog confirm = new ConfirmationDialog("Files " +
292 ab1eb3f8 Christos Stathis
                                        "with the following names already exist in the trash. If" +
293 ab1eb3f8 Christos Stathis
                                        " you continue,<br/>the trashed files will be renamed " +
294 ab1eb3f8 Christos Stathis
                                        "automatically for you:<br/><i>" + sb + "</i>", "Continue") {
295 ab1eb3f8 Christos Stathis
296 ab1eb3f8 Christos Stathis
                                @Override
297 ab1eb3f8 Christos Stathis
                                public void cancel() {
298 ab1eb3f8 Christos Stathis
                                        hide();
299 ab1eb3f8 Christos Stathis
                                }
300 ab1eb3f8 Christos Stathis
301 ab1eb3f8 Christos Stathis
                                @Override
302 ab1eb3f8 Christos Stathis
                                public void confirm() {
303 ab1eb3f8 Christos Stathis
                                        updateTrashedFiles();
304 ab1eb3f8 Christos Stathis
                                }
305 ab1eb3f8 Christos Stathis
306 ab1eb3f8 Christos Stathis
                        };
307 ab1eb3f8 Christos Stathis
                        confirm.center();
308 ab1eb3f8 Christos Stathis
                } else
309 ab1eb3f8 Christos Stathis
                        uploadFiles();
310 ab1eb3f8 Christos Stathis
        }
311 ab1eb3f8 Christos Stathis
312 ab1eb3f8 Christos Stathis
        /**
313 ab1eb3f8 Christos Stathis
         * Rename the conflicting trashed files with the supplied new names.
314 ab1eb3f8 Christos Stathis
         */
315 ab1eb3f8 Christos Stathis
        private void updateTrashedFiles() {
316 ab1eb3f8 Christos Stathis
                for (final String name: toRename.keySet()) {
317 ab1eb3f8 Christos Stathis
                        JSONObject json = new JSONObject();
318 ab1eb3f8 Christos Stathis
                        json.put("name", new JSONString(name));
319 ab1eb3f8 Christos Stathis
                        PostCommand cf = new PostCommand(toRename.get(name).getUri() + "?update=", json.toString(), 200) {
320 ab1eb3f8 Christos Stathis
321 ab1eb3f8 Christos Stathis
                                @Override
322 ab1eb3f8 Christos Stathis
                                public void onComplete() {
323 ab1eb3f8 Christos Stathis
                                        toRename.remove(name);
324 ab1eb3f8 Christos Stathis
                                        uploadFiles();
325 ab1eb3f8 Christos Stathis
                                }
326 ab1eb3f8 Christos Stathis
327 ab1eb3f8 Christos Stathis
                                @Override
328 ab1eb3f8 Christos Stathis
                                public void onError(Throwable t) {
329 ab1eb3f8 Christos Stathis
                                        GSS app = GSS.get();
330 ab1eb3f8 Christos Stathis
                                        GWT.log("", t);
331 ab1eb3f8 Christos Stathis
                                        if (t instanceof RestException) {
332 ab1eb3f8 Christos Stathis
                                                int statusCode = ((RestException) t).getHttpStatusCode();
333 ab1eb3f8 Christos Stathis
                                                if (statusCode == 405)
334 ab1eb3f8 Christos Stathis
                                                        app.displayError("You don't have the necessary permissions");
335 ab1eb3f8 Christos Stathis
                                                else if (statusCode == 404)
336 ab1eb3f8 Christos Stathis
                                                        app.displayError("User in permissions does not exist");
337 ab1eb3f8 Christos Stathis
                                                else if (statusCode == 409)
338 ab1eb3f8 Christos Stathis
                                                        app.displayError("A file with the same name already exists");
339 ab1eb3f8 Christos Stathis
                                                else if (statusCode == 413)
340 ab1eb3f8 Christos Stathis
                                                        app.displayError("Your quota has been exceeded");
341 ab1eb3f8 Christos Stathis
                                                else
342 ab1eb3f8 Christos Stathis
                                                        app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
343 ab1eb3f8 Christos Stathis
                                        } else
344 ab1eb3f8 Christos Stathis
                                                app.displayError("System error modifying file:" + t.getMessage());
345 ab1eb3f8 Christos Stathis
                                }
346 ab1eb3f8 Christos Stathis
347 ab1eb3f8 Christos Stathis
                        };
348 ab1eb3f8 Christos Stathis
                        DeferredCommand.addCommand(cf);
349 ab1eb3f8 Christos Stathis
                }
350 ab1eb3f8 Christos Stathis
        }
351 ab1eb3f8 Christos Stathis
352 ab1eb3f8 Christos Stathis
        /**
353 ab1eb3f8 Christos Stathis
         * Checks if the renaming step for already trashed files is complete and
354 ab1eb3f8 Christos Stathis
         * starts file uploads.
355 ab1eb3f8 Christos Stathis
         */
356 ab1eb3f8 Christos Stathis
        private void uploadFiles() {                
357 ab1eb3f8 Christos Stathis
                if (!toRename.isEmpty()) return;
358 ab1eb3f8 Christos Stathis
                if (canContinue){                                                
359 ab1eb3f8 Christos Stathis
                        doSend(selectedFiles);
360 ab1eb3f8 Christos Stathis
                }
361 ab1eb3f8 Christos Stathis
        }
362 ab1eb3f8 Christos Stathis
363 ab1eb3f8 Christos Stathis
        /**
364 ab1eb3f8 Christos Stathis
         * Perform the HTTP request to upload the specified file.
365 ab1eb3f8 Christos Stathis
         */
366 ab1eb3f8 Christos Stathis
        protected void doSend(final List<File> filesRemaining) {
367 ab1eb3f8 Christos Stathis
                final GSS app = GSS.get();
368 ab1eb3f8 Christos Stathis
                HttpRequest request = factory.createHttpRequest();
369 ab1eb3f8 Christos Stathis
                requests.add(request);
370 ab1eb3f8 Christos Stathis
                String method = "PUT";
371 ab1eb3f8 Christos Stathis
372 ab1eb3f8 Christos Stathis
                String path;
373 ab1eb3f8 Christos Stathis
                final String filename = getFilename(filesRemaining.get(0).getName());
374 ab1eb3f8 Christos Stathis
                path = folder.getUri();
375 ab1eb3f8 Christos Stathis
                if (!path.endsWith("/"))
376 ab1eb3f8 Christos Stathis
                        path = path + "/";
377 ab1eb3f8 Christos Stathis
                path = path + encode(filename);
378 ab1eb3f8 Christos Stathis
379 ab1eb3f8 Christos Stathis
                String token = app.getToken();
380 ab1eb3f8 Christos Stathis
                String resource = path.substring(app.getApiPath().length()-1, path.length());
381 ab1eb3f8 Christos Stathis
                String date = RestCommand.getDate();
382 ab1eb3f8 Christos Stathis
                String sig = RestCommand.calculateSig(method, date, resource, RestCommand.base64decode(token));
383 ab1eb3f8 Christos Stathis
                request.open(method, path);
384 ab1eb3f8 Christos Stathis
                request.setRequestHeader("X-GSS-Date", date);
385 ab1eb3f8 Christos Stathis
                request.setRequestHeader("Authorization", app.getCurrentUserResource().getUsername() + " " + sig);
386 ab1eb3f8 Christos Stathis
                request.setRequestHeader("Accept", "application/json; charset=utf-8");
387 ab1eb3f8 Christos Stathis
                request.setCallback(new RequestCallback() {
388 ab1eb3f8 Christos Stathis
                        @Override
389 ab1eb3f8 Christos Stathis
                        public void onResponseReceived(HttpRequest req) {
390 ab1eb3f8 Christos Stathis
                                int state = req.getReadyState();
391 ab1eb3f8 Christos Stathis
                                if (state != 4) return;
392 ab1eb3f8 Christos Stathis
                                switch(req.getStatus()) {
393 ab1eb3f8 Christos Stathis
                                        case 201: // Created falls through to updated.
394 ab1eb3f8 Christos Stathis
                                        case 204:
395 ab1eb3f8 Christos Stathis
                                                filesRemaining.remove(0);
396 ab1eb3f8 Christos Stathis
                                                if(filesRemaining.isEmpty()){                                                        
397 ab1eb3f8 Christos Stathis
                                                        finish();
398 ab1eb3f8 Christos Stathis
                                                        break;
399 ab1eb3f8 Christos Stathis
                                                }                                                
400 ab1eb3f8 Christos Stathis
                                                doSend(filesRemaining);                                
401 ab1eb3f8 Christos Stathis
                                                break;
402 ab1eb3f8 Christos Stathis
                                        case 403:
403 ab1eb3f8 Christos Stathis
                                                SessionExpiredDialog dlg = new SessionExpiredDialog();
404 ab1eb3f8 Christos Stathis
                                                dlg.center();
405 ab1eb3f8 Christos Stathis
                                                break;
406 ab1eb3f8 Christos Stathis
                                        case 405:
407 ab1eb3f8 Christos Stathis
                                                app.displayError("You don't have permission to " +
408 ab1eb3f8 Christos Stathis
                                                                "upload file " + filename);
409 ab1eb3f8 Christos Stathis
                                                break;
410 ab1eb3f8 Christos Stathis
                                        case 409:
411 ab1eb3f8 Christos Stathis
                                                app.displayError("A folder with the name " + filename +
412 ab1eb3f8 Christos Stathis
                                                                " already exists at this level");
413 ab1eb3f8 Christos Stathis
                                                break;
414 ab1eb3f8 Christos Stathis
                                        case 413:
415 ab1eb3f8 Christos Stathis
                                                app.displayError("There is not enough free space " +
416 ab1eb3f8 Christos Stathis
                                                                "available for uploading " + filename);
417 ab1eb3f8 Christos Stathis
                                                break;
418 ab1eb3f8 Christos Stathis
                                        default:
419 ab1eb3f8 Christos Stathis
                                                app.displayError("Error uploading file " + filename +
420 ab1eb3f8 Christos Stathis
                                                                        ": " + req.getStatus());
421 ab1eb3f8 Christos Stathis
                                }
422 ab1eb3f8 Christos Stathis
                        }
423 ab1eb3f8 Christos Stathis
                });
424 ab1eb3f8 Christos Stathis
                request.getUpload().setProgressHandler(new ProgressHandler() {
425 ab1eb3f8 Christos Stathis
                        @Override
426 ab1eb3f8 Christos Stathis
                        public void onProgress(ProgressEvent event) {
427 ab1eb3f8 Christos Stathis
                                double pcnt = (double) event.getLoaded() / event.getTotal();
428 ab1eb3f8 Christos Stathis
                                progressBars.get(0).setProgress((int) Math.floor(pcnt * 100));
429 ab1eb3f8 Christos Stathis
                                if(pcnt*100 == 100)
430 ab1eb3f8 Christos Stathis
                                        progressBars.remove(0);
431 ab1eb3f8 Christos Stathis
                        }
432 ab1eb3f8 Christos Stathis
                });
433 ab1eb3f8 Christos Stathis
                request.send(filesRemaining.get(0).getBlob());
434 ab1eb3f8 Christos Stathis
        }
435 ab1eb3f8 Christos Stathis
436 ab1eb3f8 Christos Stathis
        /**
437 ab1eb3f8 Christos Stathis
         * Perform the final actions after the files are uploaded.
438 ab1eb3f8 Christos Stathis
         */
439 ab1eb3f8 Christos Stathis
        protected void finish() {
440 ab1eb3f8 Christos Stathis
                hide();
441 ab1eb3f8 Christos Stathis
                //GSS.get().showFileList(true);
442 ab1eb3f8 Christos Stathis
                GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());//showFileList(true);
443 ab1eb3f8 Christos Stathis
                GSS.get().getStatusPanel().updateStats();
444 ab1eb3f8 Christos Stathis
        }
445 ab1eb3f8 Christos Stathis
446 ab1eb3f8 Christos Stathis
        /**
447 ab1eb3f8 Christos Stathis
         * Same as URL.encode, but also encode apostrophe since browsers aren't
448 ab1eb3f8 Christos Stathis
         * consistent about it (FF encodes, IE does not).
449 ab1eb3f8 Christos Stathis
         */
450 ab1eb3f8 Christos Stathis
        protected String encode(String decodedURL) {
451 ab1eb3f8 Christos Stathis
                String retv = decodedURL.replaceAll("@", "_"); // Replace bad character
452 ab1eb3f8 Christos Stathis
                retv = URL.encodeComponent(retv);
453 ab1eb3f8 Christos Stathis
                retv = retv.replaceAll("'", "%27");
454 ab1eb3f8 Christos Stathis
                return retv;
455 ab1eb3f8 Christos Stathis
        }
456 ab1eb3f8 Christos Stathis
457 ab1eb3f8 Christos Stathis
}