Revision bdda6b2f src/gr/grnet/pithos/web/client/FileUploadGearsDialog.java

b/src/gr/grnet/pithos/web/client/FileUploadGearsDialog.java
34 34
 */
35 35
package gr.grnet.pithos.web.client;
36 36

  
37
import gr.grnet.pithos.web.client.foldertree.Folder;
37 38
import gr.grnet.pithos.web.client.rest.PostCommand;
38 39
import gr.grnet.pithos.web.client.rest.RestCommand;
39 40
import gr.grnet.pithos.web.client.rest.RestException;
40 41
import gr.grnet.pithos.web.client.rest.resource.FileResource;
41
import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
42 42

  
43 43
import java.util.ArrayList;
44 44
import java.util.Arrays;
......
71 71
/**
72 72
 * The 'File upload' dialog box implementation with Google Gears support.
73 73
 */
74
public class FileUploadGearsDialog extends FileUploadDialog implements Updateable {
74
public class FileUploadGearsDialog extends FileUploadDialog {
75 75

  
76 76
	protected final Factory factory = Factory.getInstance();
77 77

  
......
97 97

  
98 98
	private FlexTable generalTable;
99 99

  
100
	private Map<String, FileResource> toRename;
100
	private Map<String, gr.grnet.pithos.web.client.foldertree.File> toRename;
101 101

  
102 102
	protected List<HttpRequest> requests = new ArrayList<HttpRequest>();
103 103
	
104 104
	private boolean canContinue = true;
105 105

  
106
    protected FileUploadGearsDialog() {
107
    }
108

  
106 109
	/**
107 110
	 * The widget's constructor.
108 111
	 */
109
	public FileUploadGearsDialog() {
112
	public FileUploadGearsDialog(GSS _app, Folder _folder) {
113
        this.folder = _folder;
114
        this.app = _app;
110 115
		// Set the dialog's caption.
111 116
		setText("File upload");
112 117
		setAnimationEnabled(true);
......
117 122
		panel.add(info);
118 123
		// Add an informative label with the folder name.
119 124
		Object selection = GSS.get().getTreeView().getSelection();
120
		folder = ((RestResourceWrapper) selection).getResource();
121 125

  
122 126
		browse = new Button("Browse...");
123 127

  
......
206 210
		hide();		
207 211
	}
208 212

  
209
	/**
210
	 * Check whether the specified file name exists in the selected folder.
211
	 */
212
	private boolean canContinue(File file) {
213
		String fileName = getFilename(file.getName());
214
		if (getFileForName(fileName) == null)
215
			// For file creation, check to see if the file already exists.
216
			for (FileResource fileRes : files)
217
				if (!fileRes.isDeleted() && fileRes.getName().equals(fileName))
218
					return false;
219
		return true;
220
	}
221

  
222 213
	@Override
223 214
	public void prepareAndSubmit() {
224 215
		GSS app = GSS.get();
......
227 218
			hide();
228 219
			return;
229 220
		}
230
		for (File file: selectedFiles)
231
			if (!canContinue(file)) {
232
				app.displayError("The file name " + file.getName() +
233
							" already exists in this folder");
234
				hide();
235
				return;
236
			}
237 221
		submit.setEnabled(false);
238 222
		browse.setVisible(false);
239 223
		List<String> toUpdate = new ArrayList<String>();
240
		toRename = new HashMap<String, FileResource>();
224
		toRename = new HashMap<String, gr.grnet.pithos.web.client.foldertree.File>();
241 225
		for (File file: selectedFiles) {
242 226
			String fname = getFilename(file.getName());
243 227
			if (getFileForName(fname) == null) {
244 228
				// We are going to create a file, so we check to see if there is a
245 229
				// trashed file with the same name.
246
				FileResource same = null;
247
				for (FileResource fres : folder.getFiles())
248
					if (fres.isDeleted() && fres.getName().equals(fname))
230
				gr.grnet.pithos.web.client.foldertree.File same = null;
231
				for (gr.grnet.pithos.web.client.foldertree.File fres : folder.getFiles())
232
					if (fres.isInTrash() && fres.getName().equals(fname))
249 233
						same = fres;
250 234
				// In that case add it to the list of files to rename.
251 235
				if (same != null)
......
286 270
	private void confirmRename() {
287 271
		if (!toRename.isEmpty()) {
288 272
			StringBuffer sb = new StringBuffer();
289
			for (FileResource file: toRename.values())
273
			for (gr.grnet.pithos.web.client.foldertree.File file: toRename.values())
290 274
				sb.append(file.getName()).append("<br/>");
291 275
			ConfirmationDialog confirm = new ConfirmationDialog("Files " +
292 276
					"with the following names already exist in the trash. If" +
......
454 438
		return retv;
455 439
	}
456 440

  
441
    protected String getBackupFilename(String filename) {
442
        List<gr.grnet.pithos.web.client.foldertree.File> filesInSameFolder = new ArrayList<gr.grnet.pithos.web.client.foldertree.File>();
443
        for (gr.grnet.pithos.web.client.foldertree.File deleted : folder.getFiles())
444
            if (deleted.isInTrash())
445
                filesInSameFolder.add(deleted);
446
        int i = 1;
447
        for (gr.grnet.pithos.web.client.foldertree.File same : filesInSameFolder)
448
            if (same.getName().startsWith(filename)) {
449
                String toCheck = same.getName().substring(filename.length(), same.getName().length());
450
                if (toCheck.startsWith(" ")) {
451
                    int test = -1;
452
                    try {
453
                        test = Integer.valueOf(toCheck.replace(" ", ""));
454
                    } catch (NumberFormatException e) {
455
                        // Do nothing since string is not a number.
456
                    }
457
                    if (test >= i)
458
                        i = test + 1;
459
                }
460
            }
461

  
462
        return filename + " " + i;
463
    }
457 464
}

Also available in: Unified diff