Revision 22eb1f84 src/gr/ebs/gss/client/FileUploadGearsDialog.java

b/src/gr/ebs/gss/client/FileUploadGearsDialog.java
26 26

  
27 27
import java.util.ArrayList;
28 28
import java.util.Arrays;
29
import java.util.HashMap;
29 30
import java.util.List;
31
import java.util.Map;
30 32

  
31 33
import com.google.gwt.core.client.GWT;
32 34
import com.google.gwt.gears.client.Factory;
......
79 81

  
80 82
	private FlexTable generalTable;
81 83

  
84
	private Map<String, FileResource> toRename;
85

  
82 86
	/**
83 87
	 * The widget's constructor.
84 88
	 */
......
88 92
		setAnimationEnabled(true);
89 93
		// Create a panel to hold all of the dialog widgets.
90 94
		VerticalPanel panel = new VerticalPanel();
91
		final HTML info = new HTML("Select one or more files to upload.");
95
		final HTML info = new HTML("You may select one or more files to upload.");
92 96
		info.addStyleName("gss-uploadNote");
93 97
		panel.add(info);
94 98
		// Add an informative label with the folder name.
......
190 194
			hide();
191 195
			return;
192 196
		}
193
		for(File file: selectedFiles)
197
		for (File file: selectedFiles)
194 198
			if (!canContinue(file)) {
195 199
				app.displayError("The file name " + file.getName() +
196 200
							" already exists in this folder");
......
199 203
			}
200 204
		submit.setEnabled(false);
201 205
		browse.setVisible(false);
202
		final String fname = getFilename(selectedFiles.get(0).getName());
203
		if (getFileForName(fname) == null) {
204
			// We are going to create a file, so we check to see if there is a
205
			// trashed file with the same name.
206
			FileResource same = null;
207
			for (FileResource fres : folder.getFiles())
208
				if (fres.isDeleted() && fres.getName().equals(fname))
209
					same = fres;
210
			if (same == null)
211
				uploadFiles();
212
			else {
213
				final FileResource sameFile = same;
214
				GWT.log("Same deleted file", null);
215
				ConfirmationDialog confirm = new ConfirmationDialog("A file " +
216
						"with the same name exists in the trash. If you " +
217
						"continue,<br/>the trashed file  '" + fname +
218
						"' will be renamed automatically for you.", "Continue") {
219

  
220
					@Override
221
					public void cancel() {
222
						hide();
223
					}
224

  
225
					@Override
226
					public void confirm() {
227
						updateTrashedFile(getBackupFilename(fname), sameFile);
228
					}
206
		List<String> toUpdate = new ArrayList<String>();
207
		toRename = new HashMap<String, FileResource>();
208
		for (File file: selectedFiles) {
209
			String fname = getFilename(file.getName());
210
			if (getFileForName(fname) == null) {
211
				// We are going to create a file, so we check to see if there is a
212
				// trashed file with the same name.
213
				FileResource same = null;
214
				for (FileResource fres : folder.getFiles())
215
					if (fres.isDeleted() && fres.getName().equals(fname))
216
						same = fres;
217
				// In that case add it to the list of files to rename.
218
				if (same != null)
219
					toRename.put(getBackupFilename(fname), same);
220
			} else
221
				// If we are updating a file add it to the list of files to update.
222
				toUpdate.add(fname);
223
		}
229 224

  
230
				};
231
				confirm.center();
232
			}
233
		} else {
234
			// We are going to update an existing file, so show a confirmation dialog.
225
		if (!toUpdate.isEmpty()) {
226
			StringBuffer sb = new StringBuffer();
227
			for (String name: toUpdate)
228
				sb.append(name).append("<br/>");
229
			// We are going to update existing files, so show a confirmation dialog.
235 230
			ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " +
236
					"you want to update " + fname + "?", "Update"){
231
					"you want to update the following files?<br/><i>" + sb +
232
					"</i>", "Update") {
237 233

  
238 234
				@Override
239 235
				public void cancel() {
......
242 238

  
243 239
				@Override
244 240
				public void confirm() {
245
					uploadFiles();
241
					confirmRename();
246 242
				}
247 243

  
248 244
			};
249 245
			confirm.center();
250
		}
246
		} else
247
			confirmRename();
251 248
	}
252 249

  
253
	private void updateTrashedFile(String newName, FileResource trashedFile) {
254
		JSONObject json = new JSONObject();
255
		json.put("name", new JSONString(newName));
256
		PostCommand cf = new PostCommand(trashedFile.getUri() + "?update=", json.toString(), 200) {
250
	/**
251
	 * Confirm the renames of synonymous files already in the trash.
252
	 */
253
	private void confirmRename() {
254
		if (!toRename.isEmpty()) {
255
			StringBuffer sb = new StringBuffer();
256
			for (FileResource file: toRename.values())
257
				sb.append(file.getName()).append("<br/>");
258
			ConfirmationDialog confirm = new ConfirmationDialog("Files " +
259
					"with the following names already exist in the trash. If" +
260
					" you continue,<br/>the trashed files will be renamed " +
261
					"automatically for you:<br/><i>" + sb + "</i>", "Continue") {
257 262

  
258
			@Override
259
			public void onComplete() {
260
				uploadFiles();
261
			}
263
				@Override
264
				public void cancel() {
265
					hide();
266
				}
262 267

  
263
			@Override
264
			public void onError(Throwable t) {
265
				GWT.log("", t);
266
				if (t instanceof RestException) {
267
					int statusCode = ((RestException) t).getHttpStatusCode();
268
					if (statusCode == 405)
269
						GSS.get().displayError("You don't have the necessary permissions");
270
					else if (statusCode == 404)
271
						GSS.get().displayError("User in permissions does not exist");
272
					else if (statusCode == 409)
273
						GSS.get().displayError("A file with the same name already exists");
274
					else if (statusCode == 413)
275
						GSS.get().displayError("Your quota has been exceeded");
276
					else
277
						GSS.get().displayError("Unable to modify file:" +((RestException)t).getHttpStatusText());
278
				} else
279
					GSS.get().displayError("System error modifying file:" + t.getMessage());
280
			}
268
				@Override
269
				public void confirm() {
270
					updateTrashedFiles();
271
				}
281 272

  
282
		};
283
		DeferredCommand.addCommand(cf);
273
			};
274
			confirm.center();
275
		} else
276
			uploadFiles();
277
	}
278

  
279
	/**
280
	 * Rename the conflicting trashed files with the supplied new names.
281
	 */
282
	private void updateTrashedFiles() {
283
		for (final String name: toRename.keySet()) {
284
			JSONObject json = new JSONObject();
285
			json.put("name", new JSONString(name));
286
			PostCommand cf = new PostCommand(toRename.get(name).getUri() + "?update=", json.toString(), 200) {
287

  
288
				@Override
289
				public void onComplete() {
290
					toRename.remove(name);
291
					uploadFiles();
292
				}
293

  
294
				@Override
295
				public void onError(Throwable t) {
296
					GSS app = GSS.get();
297
					GWT.log("", t);
298
					if (t instanceof RestException) {
299
						int statusCode = ((RestException) t).getHttpStatusCode();
300
						if (statusCode == 405)
301
							app.displayError("You don't have the necessary permissions");
302
						else if (statusCode == 404)
303
							app.displayError("User in permissions does not exist");
304
						else if (statusCode == 409)
305
							app.displayError("A file with the same name already exists");
306
						else if (statusCode == 413)
307
							app.displayError("Your quota has been exceeded");
308
						else
309
							app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
310
					} else
311
						app.displayError("System error modifying file:" + t.getMessage());
312
				}
313

  
314
			};
315
			DeferredCommand.addCommand(cf);
316
		}
284 317
	}
285 318

  
286 319
	/**
287
	 * Schedule the PUT requests to upload the files.
320
	 * Checks if the renaming step for already trashed files is complete and
321
	 * starts file uploads.
288 322
	 */
289 323
	private void uploadFiles() {
324
		if (!toRename.isEmpty()) return;
290 325
		for (int i = 0; i< fileObjects.length; i++) {
291 326
			final int index = i;
292 327
			DeferredCommand.addCommand(new Command() {
......
301 336
	 * Perform the HTTP PUT requests to upload the specified file.
302 337
	 */
303 338
	protected void doPut(final File file, final int index) {
304
		GSS app = GSS.get();
339
		final GSS app = GSS.get();
305 340
		HttpRequest request = factory.createHttpRequest();
306 341
		String method = "PUT";
307 342

  
308 343
		String path;
309
		String filename = getFilename(file.getName());
344
		final String filename = getFilename(file.getName());
310 345
		FileResource selectedResource = getFileForName(filename);
311 346
		if (selectedResource == null ) {
312 347
			// We are going to create a file.
......
337 372
						SessionExpiredDialog dlg = new SessionExpiredDialog();
338 373
						dlg.center();
339 374
						break;
375
					case 405:
376
						app.displayError("You don't have permission to " +
377
								"upload file " + filename);
378
						break;
379
					case 409:
380
						app.displayError("A folder with the name " + filename +
381
								" already exists at this level");
382
						break;
383
					case 413:
384
						app.displayError("There is not enough free space " +
385
								"available for uploading " + filename);
386
						break;
340 387
					default:
388
						app.displayError("Error uploading file " + filename +
389
									": " + req.getStatus());
341 390
						DisplayHelper.log(req.getStatus() + ":" + req.getStatusText());
342 391
				}
343 392
			}

Also available in: Unified diff