Revision ecf95c9e src/gr/grnet/pithos/web/client/FilePropertiesDialog.java

b/src/gr/grnet/pithos/web/client/FilePropertiesDialog.java
35 35
package gr.grnet.pithos.web.client;
36 36

  
37 37
import com.google.gwt.core.client.Scheduler;
38
import com.google.gwt.json.client.JSONArray;
39
import com.google.gwt.json.client.JSONString;
40 38
import com.google.gwt.user.client.Window;
41 39
import com.google.gwt.user.client.ui.Anchor;
42 40
import com.google.gwt.user.client.ui.Label;
......
55 53
import com.google.gwt.json.client.JSONObject;
56 54
import com.google.gwt.resources.client.ClientBundle;
57 55
import com.google.gwt.resources.client.ImageResource;
58
import com.google.gwt.user.client.Command;
59 56
import com.google.gwt.user.client.DeferredCommand;
60 57
import com.google.gwt.user.client.ui.Button;
61 58
import com.google.gwt.user.client.ui.CheckBox;
......
69 66
import com.google.gwt.user.client.ui.TextBox;
70 67
import com.google.gwt.user.client.ui.VerticalPanel;
71 68
import gr.grnet.pithos.web.client.tagtree.Tag;
72
import java.util.Iterator;
69
import java.util.Map;
73 70

  
74 71
/**
75 72
 * The 'File properties' dialog box implementation.
......
107 104
	 */
108 105
	private TextBox name = new TextBox();
109 106

  
110
	private final CheckBox versioned = new CheckBox();
111

  
112 107
	final File file;
113 108

  
114
	private String userFullName;
115

  
116 109
    Images images = GWT.create(Images.class);
117 110

  
118 111
	/**
......
256 249
    private VerticalPanel createSharingPanel() {
257 250
        VerticalPanel permPanel = new VerticalPanel();
258 251

  
259
        permList = new PermissionsList(app, images, file.getPermissions(), file.getOwner());
252
        permList = new PermissionsList(images, file.getPermissions(), file.getOwner(), file.getInheritedPermissionsFrom() != null);
260 253
        permPanel.add(permList);
261 254

  
262
        HorizontalPanel permButtons = new HorizontalPanel();
263
        Button add = new Button("Add Group", new ClickHandler() {
264
            @Override
265
            public void onClick(ClickEvent event) {
266
                PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
267
                dlg.center();
268
                permList.updatePermissionTable();
269
            }
270
        });
271
        permButtons.add(add);
272
        permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
255
        if (file.getInheritedPermissionsFrom() == null) {
256
            HorizontalPanel permButtons = new HorizontalPanel();
257
            Button add = new Button("Add Group", new ClickHandler() {
258
                @Override
259
                public void onClick(ClickEvent event) {
260
                    PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, false);
261
                    dlg.center();
262
                    permList.updatePermissionTable();
263
                }
264
            });
265
            permButtons.add(add);
266
            permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
273 267

  
274
        final Button addUser = new Button("Add User", new ClickHandler() {
275
            @Override
276
            public void onClick(ClickEvent event) {
277
                PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);
278
                dlg.center();
279
                permList.updatePermissionTable();
280
            }
281
        });
282
        permButtons.add(addUser);
283
        permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
268
            final Button addUser = new Button("Add User", new ClickHandler() {
269
                @Override
270
                public void onClick(ClickEvent event) {
271
                    PermissionsAddDialog dlg = new PermissionsAddDialog(app, app.getAccount().getGroups(), permList, true);
272
                    dlg.center();
273
                    permList.updatePermissionTable();
274
                }
275
            });
276
            permButtons.add(addUser);
277
            permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
284 278

  
285
        permButtons.setSpacing(8);
286
        permButtons.addStyleName("pithos-TabPanelBottom");
287
        permPanel.add(permButtons);
279
            permButtons.setSpacing(8);
280
            permButtons.addStyleName("pithos-TabPanelBottom");
281
            permPanel.add(permButtons);
282
        }
288 283

  
289 284
        final Label readForAllNote = new Label("When this option is enabled, the file will be readable" +
290 285
                    " by everyone. By checking this option, you are certifying that you have the right to " +
......
401 396
	@Override
402 397
	protected void accept() {
403 398
		String newFilename = null;
404
//		permList.updatePermissionsAccordingToInput();
405
//		Set<PermissionHolder> perms = permList.getPermissions();
406
//		JSONObject json = new JSONObject();
399

  
400
		final Map<String, Boolean[]> perms = (permList.hasChanges() ? permList.getPermissions() : null);
407 401
		if (!name.getText().trim().equals(file.getName())) {
408 402
			newFilename = name.getText().trim();
409
//			json.put("name", new JSONString(newFilename));
410 403
		}
411
//		if (versioned.getValue() != file.isVersioned())
412
//			json.put("versioned", JSONBoolean.getInstance(versioned.getValue()));
404

  
413 405
		//only update the read for all perm if the user is the owner
414 406
        Boolean published = null;
415 407
		if (readForAll.getValue() != file.isPublished())
416 408
			if (file.getOwner().equals(app.getUsername()))
417 409
                published = readForAll.getValue();
418 410
        final Boolean finalPublished = published;
419
//				json.put("readForAll", JSONBoolean.getInstance(readForAll.getValue()));
420
//		int i = 0;
421
//		if (permList.hasChanges()) {
422
//			GWT.log("Permissions change", null);
423
//			JSONArray perma = new JSONArray();
424
//
425
//			for (PermissionHolder p : perms) {
426
//				JSONObject po = new JSONObject();
427
//				if (p.getUser() != null)
428
//					po.put("user", new JSONString(p.getUser()));
429
//				if (p.getGroup() != null)
430
//					po.put("group", new JSONString(p.getGroup()));
431
//				po.put("read", JSONBoolean.getInstance(p.isRead()));
432
//				po.put("write", JSONBoolean.getInstance(p.isWrite()));
433
//				po.put("modifyACL", JSONBoolean.getInstance(p.isModifyACL()));
434
//				perma.set(i, po);
435
//				i++;
436
//			}
437
//			json.put("permissions", perma);
438
//		}
411

  
439 412
        String[] tagset = null;
440 413
		if (!tags.getText().equals(initialTagText))
441 414
			tagset = tags.getText().split(",");
442
//		String jsonString = json.toString();
443
//		if(jsonString.equals("{}")){
444
//			GWT.log("NO CHANGES", null);
445
//			return;
446
//		}
447 415
        final String[] newTags = tagset;
448 416

  
449 417
        if (newFilename != null) {
......
451 419
            PutRequest updateFile = new PutRequest(app.getApiPath(), app.getUsername(), path) {
452 420
                @Override
453 421
                public void onSuccess(Resource result) {
454
                    if (newTags != null || finalPublished != null)
455
                        updateMetaData(app.getApiPath(), app.getUsername(), path + "?update=", newTags, finalPublished);
456
                    else
457
                        app.updateFolder(file.getParent());
422
                    updateMetaData(app.getApiPath(), app.getUsername(), path + "?update=", newTags, finalPublished, perms);
458 423
                }
459 424

  
460 425
                @Override
......
468 433
            updateFile.setHeader("Content-Type", file.getContentType());
469 434
            Scheduler.get().scheduleDeferred(updateFile);
470 435
        }
471
        else if (newTags != null || finalPublished != null)
472
            updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", newTags, finalPublished);
436
        else
437
            updateMetaData(app.getApiPath(), app.getUsername(), file.getUri() + "?update=", newTags, finalPublished, perms);
473 438
	}
474 439

  
475
    private void updateMetaData(String api, String owner, String path, String[] newTags, Boolean published) {
476
        PostRequest updateFile = new PostRequest(api, owner, path) {
477
            @Override
478
            public void onSuccess(Resource result) {
479
                app.updateFolder(file.getParent());
480
            }
440
    private void updateMetaData(String api, String owner, String path, String[] newTags, Boolean published, Map<String, Boolean[]> newPermissions) {
441
        if (newTags != null || published != null || newPermissions != null) {
442
            PostRequest updateFile = new PostRequest(api, owner, path) {
443
                @Override
444
                public void onSuccess(Resource result) {
445
                    app.updateFolder(file.getParent());
446
                }
481 447

  
482
            @Override
483
            public void onError(Throwable t) {
484
                GWT.log("", t);
485
                app.displayError("System error modifying file:" + t.getMessage());
448
                @Override
449
                public void onError(Throwable t) {
450
                    GWT.log("", t);
451
                    app.displayError("System error modifying file:" + t.getMessage());
452
                }
453
            };
454
            updateFile.setHeader("X-Auth-Token", app.getToken());
455
            if (newTags != null)
456
                for (String t : newTags)
457
                    updateFile.setHeader("X-Object-Meta-" + t.trim(), "true");
458
            if (published != null)
459
                updateFile.setHeader("X-Object-Public", published.toString());
460
            if (newPermissions != null) {
461
                String readPermHeader = "read=" + app.getUsername() + ",";
462
                String writePermHeader = "write=" + app.getUsername() + ",";
463
                for (String u : newPermissions.keySet()) {
464
                    Boolean[] p = newPermissions.get(u);
465
                    if (p[0] != null && p[0])
466
                        readPermHeader += u + ",";
467
                    if (p[1] != null && p[1])
468
                        writePermHeader += u + ",";
469
                }
470
                if (readPermHeader.endsWith(","))
471
                    readPermHeader = readPermHeader.substring(0, readPermHeader.length() - 1);
472
                if (writePermHeader.endsWith(","))
473
                    writePermHeader = writePermHeader.substring(0, writePermHeader.length() - 1);
474
                String permHeader = readPermHeader +  ";" + writePermHeader;
475
                updateFile.setHeader("X-Object-Sharing", permHeader);
486 476
            }
487
        };
488
        updateFile.setHeader("X-Auth-Token", app.getToken());
489
        if (newTags != null)
490
            for (String t : newTags)
491
                updateFile.setHeader("X-Object-Meta-" + t.trim(), "true");
492
        if (published != null)
493
            updateFile.setHeader("X-Object-Public", published.toString());
494
        Scheduler.get().scheduleDeferred(updateFile);
477
            Scheduler.get().scheduleDeferred(updateFile);
478
        }
479
        else
480
            app.updateFolder(file.getParent());
495 481
    }
496 482

  
497 483
	private void removeAllOldVersions() {
498
		JSONObject json = new JSONObject();
499
		json.put("versioned", JSONBoolean.getInstance(false));
500
		GWT.log(json.toString(), null);
501
		PostCommand cf = new PostCommand(app, file.getUri() + "?update=", json.toString(), 200) {
502

  
503
			@Override
504
			public void onComplete() {
505
				toggleVersioned(true);
506
			}
507

  
508
			@Override
509
			public void onError(Throwable t) {
510
				GWT.log("", t);
511
				if (t instanceof RestException) {
512
					int statusCode = ((RestException) t).getHttpStatusCode();
513
					if (statusCode == 405)
514
						app.displayError("You don't have the necessary permissions");
515
					else if (statusCode == 404)
516
						app.displayError("User in permissions does not exist");
517
					else if (statusCode == 409)
518
						app.displayError("A folder with the same name already exists");
519
					else if (statusCode == 413)
520
						app.displayError("Your quota has been exceeded");
521
					else
522
						app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
523
				} else
524
					app.displayError("System error moifying file:" + t.getMessage());
525
			}
526
		};
527
		DeferredCommand.addCommand(cf);
528
	}
529

  
530
	private void toggleVersioned(boolean versionedValue) {
531
		JSONObject json = new JSONObject();
532
		json.put("versioned", JSONBoolean.getInstance(versionedValue));
533
		GWT.log(json.toString(), null);
534
		PostCommand cf = new PostCommand(app, file.getUri() + "?update=", json.toString(), 200) {
535

  
536
			@Override
537
			public void onComplete() {
538
				app.getTreeView().refreshCurrentNode(false);
539
			}
540

  
541
			@Override
542
			public void onError(Throwable t) {
543
				GWT.log("", t);
544
				if (t instanceof RestException) {
545
					int statusCode = ((RestException) t).getHttpStatusCode();
546
					if (statusCode == 405)
547
						app.displayError("You don't have the necessary permissions");
548
					else if (statusCode == 404)
549
						app.displayError("User in permissions does not exist");
550
					else if (statusCode == 409)
551
						app.displayError("A folder with the same name already exists");
552
					else if (statusCode == 413)
553
						app.displayError("Your quota has been exceeded");
554
					else
555
						app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
556
				} else
557
					app.displayError("System error moifying file:" + t.getMessage());
558
			}
559
		};
560
		DeferredCommand.addCommand(cf);
561 484
	}
562

  
563 485
}

Also available in: Unified diff