package mesfavoris.internal.service.operations;

import org.eclipse.core.runtime.IStatus;

import mesfavoris.BookmarksException;
import mesfavoris.model.Bookmark;
import mesfavoris.model.BookmarkDatabase;
import mesfavoris.model.BookmarkId;
import mesfavoris.validation.IBookmarkModificationValidator;

public class SetBookmarkCommentOperation {
	private final BookmarkDatabase bookmarkDatabase;
	private final IBookmarkModificationValidator bookmarkModificationValidator;

	public SetBookmarkCommentOperation(BookmarkDatabase bookmarkDatabase,
			IBookmarkModificationValidator bookmarkModificationValidator) {
		this.bookmarkDatabase = bookmarkDatabase;
		this.bookmarkModificationValidator = bookmarkModificationValidator;
	}

	public void setComment(final BookmarkId bookmarkId, final String comment) throws BookmarksException {
		bookmarkDatabase.modify(bookmarksTreeModifier -> {
			IStatus status = bookmarkModificationValidator.validateModification(bookmarksTreeModifier.getCurrentTree(),
					bookmarkId);
			if (!status.isOK()) {
				throw new BookmarksException(status);
			}
			bookmarksTreeModifier.setPropertyValue(bookmarkId, Bookmark.PROPERTY_COMMENT, comment);
		});
	}

}

@@ -1080,16 +1080,150 @@
 );%0A%09%09%09%7D%0A
+%09%09%09if (comment.length() == 0) %7B%0A%09%09%09%09bookmarksTreeModifier.setPropertyValue(bookmarkId, Bookmark.PROPERTY_COMMENT, null);%0A%09%09%09%7D else %7B%0A%09
 %09%09%09bookm
@@ -1305,16 +1305,21 @@
 mment);%0A
+%09%09%09%7D%0A
 %09%09%7D);%0A%09%7D

remove comment if comment is empty