use of de.catma.repository.git.serialization.models.json_ld.JsonLdWebAnnotation in project catma by forTEXT.
the class GitMarkupCollectionHandlerTest method createTagInstance.
@Test
public void createTagInstance() throws Exception {
try (JGitRepoManager jGitRepoManager = new JGitRepoManager(this.catmaProperties.getProperty(RepositoryPropertyKey.GitBasedRepositoryBasePath.name()), this.catmaUser)) {
this.directoriesToDeleteOnTearDown.add(jGitRepoManager.getRepositoryBasePath());
HashMap<String, Object> getJsonLdWebAnnotationResult = JsonLdWebAnnotationTest.getJsonLdWebAnnotation(jGitRepoManager, this.gitLabServerManager, this.catmaUser);
JsonLdWebAnnotation jsonLdWebAnnotation = (JsonLdWebAnnotation) getJsonLdWebAnnotationResult.get("jsonLdWebAnnotation");
String projectId = (String) getJsonLdWebAnnotationResult.get("projectUuid");
String markupCollectionId = (String) getJsonLdWebAnnotationResult.get("userMarkupCollectionUuid");
String tagsetId = (String) getJsonLdWebAnnotationResult.get("tagsetDefinitionUuid");
String tagInstanceId = (String) getJsonLdWebAnnotationResult.get("tagInstanceUuid");
this.projectsToDeleteOnTearDown.add(projectId);
// add the tagset to the markup collection
GitMarkupCollectionHandler gitMarkupCollectionHandler = new GitMarkupCollectionHandler(jGitRepoManager, this.gitLabServerManager);
// TODO: use the real tagset hash - currently the handler does not validate it at all
gitMarkupCollectionHandler.addTagset(projectId, markupCollectionId, tagsetId, "fakeTagsetVersion");
// the JGitRepoManager instance should always be in a detached state after GitMarkupCollectionHandler calls
// return
assertFalse(jGitRepoManager.isAttached());
// create the tag instance
gitMarkupCollectionHandler.createTagInstance(projectId, markupCollectionId, jsonLdWebAnnotation);
// the JGitRepoManager instance should always be in a detached state after GitMarkupCollectionHandler
// calls return
assertFalse(jGitRepoManager.isAttached());
String projectRootRepositoryName = GitProjectManager.getProjectRootRepositoryName(projectId);
jGitRepoManager.open(projectId, projectRootRepositoryName);
File expectedTagInstanceJsonFilePath = new File(jGitRepoManager.getRepositoryWorkTree(), String.format("%s/%s/annotations/%s.json", GitProjectHandler.MARKUP_COLLECTION_SUBMODULES_DIRECTORY_NAME, markupCollectionId, tagInstanceId));
assert expectedTagInstanceJsonFilePath.exists();
assert expectedTagInstanceJsonFilePath.isFile();
String expectedTagInstanceJsonFileContents = JsonLdWebAnnotationTest.EXPECTED_SERIALIZED_ANNOTATION.replaceAll("[\n\t]", "");
expectedTagInstanceJsonFileContents = String.format(expectedTagInstanceJsonFileContents, getJsonLdWebAnnotationResult.get("projectRootRepositoryName"), getJsonLdWebAnnotationResult.get("tagsetDefinitionUuid"), getJsonLdWebAnnotationResult.get("tagDefinitionUuid"), getJsonLdWebAnnotationResult.get("userPropertyDefinitionUuid"), getJsonLdWebAnnotationResult.get("systemPropertyDefinitionUuid"), getJsonLdWebAnnotationResult.get("userMarkupCollectionUuid"), getJsonLdWebAnnotationResult.get("tagInstanceUuid"), getJsonLdWebAnnotationResult.get("sourceDocumentUuid"));
assertEquals(expectedTagInstanceJsonFileContents, FileUtils.readFileToString(expectedTagInstanceJsonFilePath, StandardCharsets.UTF_8));
}
}
use of de.catma.repository.git.serialization.models.json_ld.JsonLdWebAnnotation in project catma by forTEXT.
the class GitMarkupCollectionHandlerTest method open.
@Test
public void open() throws Exception {
// TODO: don't hardcode anything in assertions (markup collection name...)
try (JGitRepoManager jGitRepoManager = new JGitRepoManager(this.catmaProperties.getProperty(RepositoryPropertyKey.GitBasedRepositoryBasePath.name()), this.catmaUser)) {
this.directoriesToDeleteOnTearDown.add(jGitRepoManager.getRepositoryBasePath());
HashMap<String, Object> getJsonLdWebAnnotationResult = JsonLdWebAnnotationTest.getJsonLdWebAnnotation(jGitRepoManager, this.gitLabServerManager, this.catmaUser);
JsonLdWebAnnotation jsonLdWebAnnotation = (JsonLdWebAnnotation) getJsonLdWebAnnotationResult.get("jsonLdWebAnnotation");
String projectId = (String) getJsonLdWebAnnotationResult.get("projectUuid");
String markupCollectionId = (String) getJsonLdWebAnnotationResult.get("userMarkupCollectionUuid");
String tagsetId = (String) getJsonLdWebAnnotationResult.get("tagsetDefinitionUuid");
this.projectsToDeleteOnTearDown.add(projectId);
// add the tagset to the markup collection
GitMarkupCollectionHandler gitMarkupCollectionHandler = new GitMarkupCollectionHandler(jGitRepoManager, this.gitLabServerManager);
// TODO: use the real tagset hash - currently the handler does not validate it at all
gitMarkupCollectionHandler.addTagset(projectId, markupCollectionId, tagsetId, "fakeTagsetVersion");
// the JGitRepoManager instance should always be in a detached state after GitMarkupCollectionHandler calls
// return
assertFalse(jGitRepoManager.isAttached());
// create the tag instance within the markup collection
gitMarkupCollectionHandler.createTagInstance(projectId, markupCollectionId, jsonLdWebAnnotation);
// the JGitRepoManager instance should always be in a detached state after GitMarkupCollectionHandler calls
// return
assertFalse(jGitRepoManager.isAttached());
UserMarkupCollection markupCollection = gitMarkupCollectionHandler.open(projectId, markupCollectionId);
// the JGitRepoManager instance should always be in a detached state after GitMarkupCollectionHandler
// calls return
assertFalse(jGitRepoManager.isAttached());
assertNotNull(markupCollection);
assertEquals("Test Markup Collection", markupCollection.getContentInfoSet().getTitle());
assertEquals(2, markupCollection.getTagReferences().size());
assertTrue(markupCollection.getTagReferences().get(0).getRange().equals(new Range(12, 18)));
assertNotNull(markupCollection.getRevisionHash());
}
}
use of de.catma.repository.git.serialization.models.json_ld.JsonLdWebAnnotation in project catma by forTEXT.
the class GitMarkupCollectionHandler method openTagReferences.
private ArrayList<TagReference> openTagReferences(String projectId, String markupCollectionId, String collectionName, File parentDirectory, ProgressListener progressListener, AtomicInteger counter) throws Exception {
ArrayList<TagReference> tagReferences = new ArrayList<>();
List<String> contents = Arrays.asList(parentDirectory.list());
for (String item : contents) {
File target = new File(parentDirectory, item);
// if it is a directory, recurse into it adding results to the current tagReferences list
if (target.isDirectory() && !target.getName().equalsIgnoreCase(".git")) {
tagReferences.addAll(this.openTagReferences(projectId, markupCollectionId, collectionName, target, progressListener, counter));
} else // if item is <CATMA_UUID>.json, read it into a list of TagReference objects
if (target.isFile() && isTagInstanceFilename(target.getName())) {
counter.incrementAndGet();
if (counter.intValue() % 1000 == 0) {
progressListener.setProgress("Loading Annotations %1$s %2$d", collectionName, counter.intValue());
}
String serialized = readFileToString(target, StandardCharsets.UTF_8);
JsonLdWebAnnotation jsonLdWebAnnotation = new SerializationHelper<JsonLdWebAnnotation>().deserialize(serialized, JsonLdWebAnnotation.class);
tagReferences.addAll(jsonLdWebAnnotation.toTagReferenceList(projectId, markupCollectionId));
}
}
return tagReferences;
}
use of de.catma.repository.git.serialization.models.json_ld.JsonLdWebAnnotation in project catma by forTEXT.
the class GitMarkupCollectionHandler method getCollection.
public AnnotationCollection getCollection(String projectId, String collectionId, TagLibrary tagLibrary, ProgressListener progressListener, boolean hasWritePermission, Function<String, Boolean> hasTagsetIdReadPermissionGetter) throws Exception {
try (ILocalGitRepositoryManager localGitRepoManager = this.localGitRepositoryManager) {
String projectRootRepositoryName = GitProjectManager.getProjectRootRepositoryName(projectId);
localGitRepoManager.open(projectId, projectRootRepositoryName);
String markupCollectionSubmoduleRelDir = GitProjectHandler.ANNOTATION_COLLECTION_SUBMODULES_DIRECTORY_NAME + "/" + collectionId;
File markupCollectionSubmoduleAbsPath = new File(localGitRepoManager.getRepositoryWorkTree().toString(), markupCollectionSubmoduleRelDir);
String markupCollectionRevisionHash = localGitRepoManager.getSubmoduleHeadRevisionHash(markupCollectionSubmoduleRelDir);
// can't call open on an attached instance
localGitRepoManager.detach();
File markupCollectionHeaderFile = new File(markupCollectionSubmoduleAbsPath, HEADER_FILE_NAME);
String serializedMarkupCollectionHeaderFile = FileUtils.readFileToString(markupCollectionHeaderFile, StandardCharsets.UTF_8);
GitMarkupCollectionHeader markupCollectionHeader = new SerializationHelper<GitMarkupCollectionHeader>().deserialize(serializedMarkupCollectionHeaderFile, GitMarkupCollectionHeader.class);
ContentInfoSet contentInfoSet = new ContentInfoSet(markupCollectionHeader.getAuthor(), markupCollectionHeader.getDescription(), markupCollectionHeader.getPublisher(), markupCollectionHeader.getName());
AtomicInteger counter = new AtomicInteger();
ArrayList<TagReference> tagReferences = this.openTagReferences(projectId, collectionId, contentInfoSet.getTitle(), markupCollectionSubmoduleAbsPath, progressListener, counter);
// handle orphan Annotations
ArrayListMultimap<TagInstance, TagReference> tagInstances = ArrayListMultimap.create();
Set<String> orphanAnnotationIds = new HashSet<>();
Iterator<TagReference> tagReferenceIterator = tagReferences.iterator();
while (tagReferenceIterator.hasNext()) {
TagReference tagReference = tagReferenceIterator.next();
if (!orphanAnnotationIds.contains(tagReference.getTagInstanceId())) {
String tagsetId = tagReference.getTagInstance().getTagsetId();
boolean readPermission = hasTagsetIdReadPermissionGetter.apply(tagsetId);
TagsetDefinition tagset = tagLibrary.getTagsetDefinition(tagsetId);
String tagId = tagReference.getTagDefinitionId();
if (readPermission && (tagset == null || tagset.isDeleted(tagId))) {
// Tag/Tagset has been deleted, we remove the stale Annotation as well
orphanAnnotationIds.add(tagReference.getTagInstanceId());
tagReferenceIterator.remove();
} else {
// other orphan Annotations get ignored upon indexing
// until the corresponding Tag or its "deletion" info come along
tagInstances.put(tagReference.getTagInstance(), tagReference);
}
}
}
if (hasWritePermission) {
removeTagInstances(projectId, collectionId, orphanAnnotationIds);
}
// handle orphan Properties
if (hasWritePermission) {
for (TagInstance tagInstance : tagInstances.keySet()) {
TagsetDefinition tagset = tagLibrary.getTagsetDefinition(tagInstance.getTagsetId());
if (tagset != null) {
Collection<Property> properties = tagInstance.getUserDefinedProperties();
for (Property property : new HashSet<>(properties)) {
// deleted property?
if (tagset.isDeleted(property.getPropertyDefinitionId())) {
// yes, we remove the stale property
tagInstance.removeUserDefinedProperty(property.getPropertyDefinitionId());
// and save the change
JsonLdWebAnnotation annotation = new JsonLdWebAnnotation(CATMAPropertyKey.GitLabServerUrl.getValue(), projectId, tagInstances.get(tagInstance), tagLibrary);
createTagInstance(projectId, collectionId, annotation);
}
}
}
}
}
AnnotationCollection userMarkupCollection = new AnnotationCollection(collectionId, contentInfoSet, tagLibrary, tagReferences, markupCollectionHeader.getSourceDocumentId(), markupCollectionHeader.getSourceDocumentVersion());
userMarkupCollection.setRevisionHash(markupCollectionRevisionHash);
return userMarkupCollection;
}
}
use of de.catma.repository.git.serialization.models.json_ld.JsonLdWebAnnotation in project catma by forTEXT.
the class GitMarkupCollectionHandler method createTagInstance.
/**
* Creates a tag instance (annotation) within the markup collection identified by <code>markupCollectionId</code>.
* <p>
* NB: This method purposefully does NOT perform any Git add/commit operations as it is expected to be called a very
* large number of times when a graph worktree is written to disk.
*
* @param projectId the ID of the project that contains the markup collection within which the tag instance should
* be created
* @param markupCollectionId the ID of the markup collection within which to create the tag instance
* @param annotation a {@link JsonLdWebAnnotation} object representing the tag instance
* @return the tag instance UUID contained within the <code>annotation</code> argument
* @throws IOException if an error occurs while creating the tag instance
*/
public String createTagInstance(String projectId, String markupCollectionId, JsonLdWebAnnotation annotation) throws IOException {
// TODO: check that the tag instance is for the correct document
try (ILocalGitRepositoryManager localGitRepoManager = this.localGitRepositoryManager) {
String projectRootRepositoryName = GitProjectManager.getProjectRootRepositoryName(projectId);
String collectionGitRepositoryName = projectRootRepositoryName + "/" + GitProjectHandler.ANNOTATION_COLLECTION_SUBMODULES_DIRECTORY_NAME + "/" + markupCollectionId;
localGitRepoManager.open(projectId, collectionGitRepositoryName);
// write the serialized tag instance to the markup collection submodule
File targetTagInstanceFilePath = new File(localGitRepoManager.getRepositoryWorkTree(), "annotations/" + annotation.getTagInstanceUuid() + ".json");
String serializedTagInstance = new SerializationHelper<JsonLdWebAnnotation>().serialize(annotation);
try (FileOutputStream fileOutputStream = FileUtils.openOutputStream(targetTagInstanceFilePath)) {
fileOutputStream.write(serializedTagInstance.getBytes(StandardCharsets.UTF_8));
}
// not doing Git add/commit, see method doc comment
}
return annotation.getTagInstanceUuid();
}
Aggregations