GroupDocsGists
10/24/2017 - 1:19 PM

Examples-GroupDocs.Annotation.Examples.Java-src-main-java-GroupDocs-Annotation-Examples-Java-DataStorage-managesCollaboratorRights.java

// For complete examples and data files, please go to https://github.com/groupdocs-annotation/GroupDocs.Annotation-for-Java
try {
	AnnotationConfig cfg = Utilities.getConfiguration();
	AnnotationImageHandler annotator = new AnnotationImageHandler(cfg);
	IUserDataHandler userRepository = annotator.getUserDataHandler();
	IDocumentDataHandler documentRepository = annotator.getDocumentDataHandler();
	// Create owner.
	User johnOwner = userRepository.getUserByEmail("john@doe.com");
	if (johnOwner == null) {
		final User user = new User();
		user.setFirstName("John");
		user.setLastName("Doe");
		user.setEmail("john@doe.com");
		userRepository.add(user);
		johnOwner = userRepository.getUserByEmail("john@doe.com");
	}
	// Create document data object in storage
	Document document = documentRepository.getDocument(fileName);
	long documentId = document == null ? annotator.createDocument(fileName, DocumentType.Pdf, johnOwner.getId())
			: document.getId();
	// Create reviewer.
	ReviewerInfo reviewerInfo = new ReviewerInfo();
	reviewerInfo.setPrimaryEmail("judy@doe.com");
	reviewerInfo.setFirstName("Judy");
	reviewerInfo.setLastName("Doe");
	reviewerInfo.setAccessRights(AnnotationReviewerRights.CanView);
	// Add collaborator to the document. If user with UserName equals
	// to reviewers PrimaryEmail is absent it will be created.
	SetCollaboratorsResult addCollaboratorResult = annotator.addCollaborator(documentId, reviewerInfo);
	// Get document collaborators
	GetCollaboratorsResult getCollaboratorsResult = annotator.getCollaborators(documentId);
	User judy = userRepository.getUserByEmail("judy@doe.com");
	// Create annotation object
	AnnotationInfo pointAnnotation = new AnnotationInfo();
	pointAnnotation.setAnnotationPosition(new Point(852.0, 81.0));
	pointAnnotation.setBox(new Rectangle(212f, 81f, 142f, 0.0f));
	pointAnnotation.setType(AnnotationType.Point);
	pointAnnotation.setPageNumber(0);
	pointAnnotation.setCreatorName("Anonym A.");
	// John try to add annotations
	CreateAnnotationResult johnResult = annotator.createAnnotation(pointAnnotation, documentId,
			johnOwner.getId());
	System.out.println(johnResult);
	// Judy try to add annotations
	try {
		CreateAnnotationResult judyResult = annotator.createAnnotation(pointAnnotation, documentId,
				judy.getId());
		System.out.println(judyResult);
	} catch (AnnotatorException e) {
		System.out.println(e.getMessage());
	}
	// Allow Judy create annotations.
	reviewerInfo.setAccessRights(AnnotationReviewerRights.CanAnnotate);
	SetCollaboratorsResult updateCollaboratorResult = annotator.updateCollaborator(documentId, reviewerInfo);
	System.out.println(updateCollaboratorResult);
	// Judy try to add annotations
	CreateAnnotationResult judyResultCanAnnotate = annotator.createAnnotation(pointAnnotation, documentId,
			judy.getId());
	System.out.println(judyResultCanAnnotate);
} catch (Exception e) {
	System.out.println("Exception: " + e.getMessage());
	e.printStackTrace();
}