use of de.catma.document.annotation.TagReference in project catma by forTEXT.
the class GraphWriter method addTagReferences.
void addTagReferences(String revisionHash, Vertex collectionV, List<TagReference> tagReferences) {
final ArrayListMultimap<TagInstance, Range> tagInstancesAndRanges = ArrayListMultimap.create();
tagReferences.forEach(tagReference -> {
tagInstancesAndRanges.put(tagReference.getTagInstance(), tagReference.getRange());
});
Map<String, Vertex> tagNodesById = new HashMap<>();
Set<String> availablePropertyDefIds = new HashSet<>();
for (TagInstance ti : tagInstancesAndRanges.keySet()) {
List<Range> ranges = tagInstancesAndRanges.get(ti);
List<Integer> flatRanges = ranges.stream().sorted().flatMap(range -> Stream.of(range.getStartPoint(), range.getEndPoint())).collect(Collectors.toList());
if (ti.getAuthor() == null) {
ti.setAuthor(user.getIdentifier());
}
String tagsetId = ti.getTagsetId();
String tagId = ti.getTagDefinitionId();
Vertex tagInstanceV = graph.addVertex(nt(TagInstance));
tagInstanceV.property("tagInstanceId", ti.getUuid());
tagInstanceV.property("author", ti.getAuthor());
tagInstanceV.property("timestamp", ti.getTimestamp());
tagInstanceV.property("ranges", flatRanges);
collectionV.addEdge(rt(hasInstance), tagInstanceV);
Vertex tagV = tagNodesById.get(tagId);
GraphTraversalSource g = graph.traversal();
if (tagV == null) {
GraphTraversal<Vertex, Vertex> traversal = g.V().has(nt(ProjectRevision), "revisionHash", revisionHash).outE(rt(hasTagset)).inV().has(nt(Tagset), "tagsetId", tagsetId).outE(rt(hasTag)).inV().has(nt(Tag), "tagId", tagId);
if (traversal.hasNext()) {
tagV = traversal.next();
tagNodesById.put(tagId, tagV);
}
}
if (tagV != null) {
// usually the Tag should always be present,
// because we delete stale Annotations when loading the Collection from git
// if we hit an orphan Annotation at this stage it gets ignored
// until the next sync might bring the corresponding Tag
tagV.addEdge(rt(hasInstance), tagInstanceV);
for (Property property : ti.getUserDefinedProperties()) {
if (availablePropertyDefIds.contains(property.getPropertyDefinitionId()) || g.V(tagV).outE(rt(hasProperty)).inV().has(nt(Property), "uuid", property.getPropertyDefinitionId()).hasNext()) {
Vertex annoPropertyV = graph.addVertex(nt(AnnotationProperty));
annoPropertyV.property("uuid", property.getPropertyDefinitionId());
annoPropertyV.property("values", property.getPropertyValueList());
tagInstanceV.addEdge(rt(hasProperty), annoPropertyV);
availablePropertyDefIds.add(property.getPropertyDefinitionId());
}
}
}
}
}
use of de.catma.document.annotation.TagReference 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.document.annotation.TagReference 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.document.annotation.TagReference in project catma by forTEXT.
the class GitMarkupCollectionHandler method getBothModifiedAnnotationConflict.
private AnnotationConflict getBothModifiedAnnotationConflict(String projectId, String collectionId, String serializedConflictingAnnotation) throws Exception {
String masterVersion = serializedConflictingAnnotation.replaceAll("\\Q<<<<<<< HEAD\\E(\\r\\n|\\r|\\n)", "").replaceAll("\\Q=======\\E(\\r\\n|\\r|\\n|.)*?\\Q>>>>>>> \\E.+?(\\r\\n|\\r|\\n)", "");
String devVersion = serializedConflictingAnnotation.replaceAll("\\Q<<<<<<< HEAD\\E(\\r\\n|\\r|\\n|.)*?\\Q=======\\E(\\r\\n|\\r|\\n)", "").replaceAll("\\Q>>>>>>> \\E.+?(\\r\\n|\\r|\\n)", "");
JsonLdWebAnnotation masterVersionJsonLdWebAnnotation = new SerializationHelper<JsonLdWebAnnotation>().deserialize(masterVersion, JsonLdWebAnnotation.class);
JsonLdWebAnnotation devVersionJsonLdWebAnnotation = new SerializationHelper<JsonLdWebAnnotation>().deserialize(devVersion, JsonLdWebAnnotation.class);
List<TagReference> masterTagReferences = masterVersionJsonLdWebAnnotation.toTagReferenceList(projectId, collectionId);
List<TagReference> devTagReferences = devVersionJsonLdWebAnnotation.toTagReferenceList(projectId, collectionId);
AnnotationConflict annotationConflict = new AnnotationConflict(devTagReferences.get(0).getTagInstance(), devTagReferences, masterTagReferences.get(0).getTagInstance(), masterTagReferences);
return annotationConflict;
}
use of de.catma.document.annotation.TagReference in project catma by forTEXT.
the class GitMarkupCollectionHandler method getDeleteByUsAnnotationConflict.
private AnnotationConflict getDeleteByUsAnnotationConflict(String projectId, String collectionId, String serializedConflictingAnnotation) throws Exception {
JsonLdWebAnnotation devVersionJsonLdWebAnnotation = new SerializationHelper<JsonLdWebAnnotation>().deserialize(serializedConflictingAnnotation, JsonLdWebAnnotation.class);
List<TagReference> devTagReferences = devVersionJsonLdWebAnnotation.toTagReferenceList(projectId, collectionId);
AnnotationConflict annotationConflict = new AnnotationConflict(devTagReferences.get(0).getTagInstance(), devTagReferences, null, Collections.emptyList());
return annotationConflict;
}
Aggregations