2 * Copyright 2009 Electronic Business Systems Ltd.
\r
4 * This file is part of GSS.
\r
6 * GSS is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * GSS is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
\r
19 package gr.ebs.gss.client;
\r
21 import gr.ebs.gss.client.rest.PostCommand;
\r
22 import gr.ebs.gss.client.rest.RestCommand;
\r
23 import gr.ebs.gss.client.rest.RestException;
\r
24 import gr.ebs.gss.client.rest.resource.FileResource;
\r
25 import gr.ebs.gss.client.rest.resource.FolderResource;
\r
27 import java.util.ArrayList;
\r
28 import java.util.Arrays;
\r
29 import java.util.List;
\r
31 import com.google.gwt.core.client.GWT;
\r
32 import com.google.gwt.gears.client.Factory;
\r
33 import com.google.gwt.gears.client.desktop.Desktop;
\r
34 import com.google.gwt.gears.client.desktop.File;
\r
35 import com.google.gwt.gears.client.desktop.OpenFilesHandler;
\r
36 import com.google.gwt.gears.client.httprequest.HttpRequest;
\r
37 import com.google.gwt.gears.client.httprequest.ProgressEvent;
\r
38 import com.google.gwt.gears.client.httprequest.ProgressHandler;
\r
39 import com.google.gwt.gears.client.httprequest.RequestCallback;
\r
40 import com.google.gwt.json.client.JSONObject;
\r
41 import com.google.gwt.json.client.JSONString;
\r
42 import com.google.gwt.user.client.Command;
\r
43 import com.google.gwt.user.client.DeferredCommand;
\r
44 import com.google.gwt.user.client.ui.Button;
\r
45 import com.google.gwt.user.client.ui.ClickListener;
\r
46 import com.google.gwt.user.client.ui.FlexTable;
\r
47 import com.google.gwt.user.client.ui.HTML;
\r
48 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
\r
49 import com.google.gwt.user.client.ui.HorizontalPanel;
\r
50 import com.google.gwt.user.client.ui.VerticalPanel;
\r
51 import com.google.gwt.user.client.ui.Widget;
\r
54 * The 'File upload' dialog box implementation with Google Gears support.
\r
56 public class FileUploadGearsDialog extends FileUploadDialog implements Updateable {
\r
58 private final Factory factory = Factory.getInstance();
\r
61 * The array of files to upload.
\r
63 private File[] fileObjects;
\r
66 * A list of files to upload, created from files array. Used to signal
\r
67 * finished state when empty.
\r
69 private List<File> selectedFiles = new ArrayList<File>();
\r
72 * The list of progress bars for individual files.
\r
74 private List<ProgressBar> progressBars = new ArrayList<ProgressBar>();
\r
76 private Button browse;
\r
78 private Button submit;
\r
80 private FlexTable generalTable;
\r
83 * The widget's constructor.
\r
85 public FileUploadGearsDialog() {
\r
86 // Set the dialog's caption.
\r
87 setText("File upload");
\r
88 setAnimationEnabled(true);
\r
89 // Create a panel to hold all of the dialog widgets.
\r
90 VerticalPanel panel = new VerticalPanel();
\r
91 final HTML info = new HTML("Select one or more files to upload.");
\r
92 info.addStyleName("gss-uploadNote");
\r
94 // Add an informative label with the folder name.
\r
95 Object selection = GSS.get().getFolders().getCurrent().getUserObject();
\r
96 folder = (FolderResource) selection;
\r
98 browse = new Button("Browse...");
\r
100 HorizontalPanel fileUploadPanel = new HorizontalPanel();
\r
101 fileUploadPanel.add(browse);
\r
103 generalTable = new FlexTable();
\r
104 generalTable.setText(0, 0, "Folder");
\r
105 generalTable.setText(1, 0, "File");
\r
106 generalTable.setText(0, 1, folder.getName());
\r
107 generalTable.setWidget(1, 1, fileUploadPanel);
\r
108 generalTable.getCellFormatter().setStyleName(0, 0, "props-labels");
\r
109 generalTable.getCellFormatter().setStyleName(1, 0, "props-labels");
\r
110 generalTable.getCellFormatter().setStyleName(0, 1, "props-values");
\r
111 generalTable.getCellFormatter().setStyleName(1, 1, "props-values");
\r
112 generalTable.setCellSpacing(4);
\r
114 panel.add(generalTable);
\r
116 // Create a panel to hold the buttons.
\r
117 HorizontalPanel buttons = new HorizontalPanel();
\r
119 submit = new Button("Upload");
\r
120 submit.addClickListener(new ClickListener() {
\r
122 public void onClick(Widget sender) {
\r
123 prepareAndSubmit();
\r
126 submit.setEnabled(false);
\r
127 buttons.add(submit);
\r
128 buttons.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_CENTER);
\r
129 // Create the 'Cancel' button, along with a listener that hides the
\r
130 // dialog when the button is clicked.
\r
131 Button cancel = new Button("Cancel", new ClickListener() {
\r
133 public void onClick(Widget sender) {
\r
137 buttons.add(cancel);
\r
138 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
\r
139 buttons.setSpacing(8);
\r
140 buttons.addStyleName("gss-DialogBox");
\r
142 browse.addClickListener(new ClickListener() {
\r
143 public void onClick(Widget sender) {
\r
144 Desktop desktop = factory.createDesktop();
\r
145 desktop.openFiles(new OpenFilesHandler() {
\r
147 public void onOpenFiles(OpenFilesEvent event) {
\r
148 fileObjects = event.getFiles();
\r
149 selectedFiles.addAll(Arrays.asList(fileObjects));
\r
150 for (int i = 0; i< selectedFiles.size(); i++) {
\r
151 generalTable.setText(i+1, 0, "File");
\r
152 generalTable.setText(i+1, 1, selectedFiles.get(i).getName());
\r
153 ProgressBar progress = new ProgressBar(20, 0);
\r
154 generalTable.setWidget(i+1, 2, progress);
\r
155 progressBars.add(progress);
\r
156 generalTable.getCellFormatter().setStyleName(i+1, 0, "props-labels");
\r
157 generalTable.getCellFormatter().setStyleName(i+1, 1, "props-values");
\r
159 submit.setEnabled(true);
\r
165 panel.add(buttons);
\r
166 panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
\r
167 panel.addStyleName("gss-DialogBox");
\r
168 addStyleName("gss-DialogBox");
\r
173 * Check whether the specified file name exists in the selected folder.
\r
175 private boolean canContinue(File file) {
\r
176 String fileName = getFilename(file.getName());
\r
177 if (getFileForName(fileName) == null)
\r
178 // For file creation, check to see if the file already exists.
\r
179 for (FileResource fileRes : files)
\r
180 if (!fileRes.isDeleted() && fileRes.getName().equals(fileName))
\r
186 public void prepareAndSubmit() {
\r
187 GSS app = GSS.get();
\r
188 if (selectedFiles.size() == 0) {
\r
189 app.displayError("You must select a file!");
\r
193 for(File file: selectedFiles)
\r
194 if (!canContinue(file)) {
\r
195 app.displayError("The file name " + file.getName() +
\r
196 " already exists in this folder");
\r
200 submit.setEnabled(false);
\r
201 browse.setVisible(false);
\r
202 final String fname = getFilename(selectedFiles.get(0).getName());
\r
203 if (getFileForName(fname) == null) {
\r
204 // We are going to create a file, so we check to see if there is a
\r
205 // trashed file with the same name.
\r
206 FileResource same = null;
\r
207 for (FileResource fres : folder.getFiles())
\r
208 if (fres.isDeleted() && fres.getName().equals(fname))
\r
213 final FileResource sameFile = same;
\r
214 GWT.log("Same deleted file", null);
\r
215 ConfirmationDialog confirm = new ConfirmationDialog("A file " +
\r
216 "with the same name exists in the trash. If you " +
\r
217 "continue,<br/>the trashed file '" + fname +
\r
218 "' will be renamed automatically for you.", "Continue") {
\r
221 public void cancel() {
\r
226 public void confirm() {
\r
227 updateTrashedFile(getBackupFilename(fname), sameFile);
\r
234 // We are going to update an existing file, so show a confirmation dialog.
\r
235 ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " +
\r
236 "you want to update " + fname + "?", "Update"){
\r
239 public void cancel() {
\r
244 public void confirm() {
\r
253 private void updateTrashedFile(String newName, FileResource trashedFile) {
\r
254 JSONObject json = new JSONObject();
\r
255 json.put("name", new JSONString(newName));
\r
256 PostCommand cf = new PostCommand(trashedFile.getUri() + "?update=", json.toString(), 200) {
\r
259 public void onComplete() {
\r
264 public void onError(Throwable t) {
\r
266 if (t instanceof RestException) {
\r
267 int statusCode = ((RestException) t).getHttpStatusCode();
\r
268 if (statusCode == 405)
\r
269 GSS.get().displayError("You don't have the necessary permissions");
\r
270 else if (statusCode == 404)
\r
271 GSS.get().displayError("User in permissions does not exist");
\r
272 else if (statusCode == 409)
\r
273 GSS.get().displayError("A file with the same name already exists");
\r
274 else if (statusCode == 413)
\r
275 GSS.get().displayError("Your quota has been exceeded");
\r
277 GSS.get().displayError("Unable to modify file:" +((RestException)t).getHttpStatusText());
\r
279 GSS.get().displayError("System error modifying file:" + t.getMessage());
\r
283 DeferredCommand.addCommand(cf);
\r
287 * Schedule the PUT requests to upload the files.
\r
289 private void uploadFiles() {
\r
290 for (int i = 0; i< fileObjects.length; i++) {
\r
291 final int index = i;
\r
292 DeferredCommand.addCommand(new Command() {
\r
293 public void execute() {
\r
294 doPut(fileObjects[index], index);
\r
301 * Perform the HTTP PUT requests to upload the specified file.
\r
303 protected void doPut(final File file, final int index) {
\r
304 GSS app = GSS.get();
\r
305 HttpRequest request = factory.createHttpRequest();
\r
306 String method = "PUT";
\r
309 String filename = getFilename(file.getName());
\r
310 FileResource selectedResource = getFileForName(filename);
\r
311 if (selectedResource == null ) {
\r
312 // We are going to create a file.
\r
313 path = folder.getUri();
\r
314 if (!path.endsWith("/"))
\r
316 path = path + encodeComponent(filename);
\r
318 path = selectedResource.getUri();
\r
320 String token = app.getToken();
\r
321 String resource = path.substring(app.getApiPath().length()-1, path.length());
\r
322 String date = RestCommand.getDate();
\r
323 String sig = RestCommand.calculateSig(method, date, resource, RestCommand.base64decode(token));
\r
324 request.open(method, path);
\r
325 request.setRequestHeader("X-GSS-Date", date);
\r
326 request.setRequestHeader("Authorization", app.getCurrentUserResource().getUsername() + " " + sig);
\r
327 request.setRequestHeader("Accept", "application/json; charset=utf-8");
\r
328 request.setCallback(new RequestCallback() {
\r
329 public void onResponseReceived(HttpRequest req) {
\r
330 switch(req.getStatus()) {
\r
331 case 201: // Created falls through to updated.
\r
333 selectedFiles.remove(file);
\r
337 SessionExpiredDialog dlg = new SessionExpiredDialog();
\r
341 DisplayHelper.log(req.getStatus() + ":" + req.getStatusText());
\r
345 request.getUpload().setProgressHandler(new ProgressHandler() {
\r
346 public void onProgress(ProgressEvent event) {
\r
347 double pcnt = (double) event.getLoaded() / event.getTotal();
\r
348 progressBars.get(index).setProgress((int) Math.floor(pcnt * 100));
\r
351 request.send(file.getBlob());
\r
355 * Perform the final actions after the files are uploaded.
\r
357 private void finish() {
\r
358 if (!selectedFiles.isEmpty()) return;
\r
360 GSS.get().showFileList(true);
\r
361 GSS.get().getStatusPanel().updateStats();
\r