Search in sources :

Example 6 with TagReference

use of de.catma.document.annotation.TagReference in project catma by forTEXT.

the class XmlMarkupCollectionSerializationHandler method scanElements.

private void scanElements(StringBuilder contentBuilder, Element element, Stack<String> elementStack, TagManager tagManager, TagLibrary tagLibrary, Map<String, String> namespacePrefixToTagsetIdMap, AnnotationCollection userMarkupCollection, String docId, int docLength) throws Exception {
    int start = contentBuilder.length();
    StringBuilder pathBuilder = new StringBuilder();
    for (int j = 0; j < elementStack.size(); j++) {
        pathBuilder.append("/" + elementStack.get(j));
    }
    String parentPath = pathBuilder.toString();
    elementStack.push(element.getLocalName());
    String path = parentPath + "/" + elementStack.peek();
    String tagName = element.getLocalName();
    String prefix = element.getNamespacePrefix();
    String tagsetId = namespacePrefixToTagsetIdMap.get(prefix);
    if (tagsetId == null) {
        tagsetId = KnownTagsetDefinitionName.DEFAULT_INTRINSIC_XML.asTagsetId();
    }
    TagsetDefinition tagset = tagLibrary.getTagsetDefinition(tagsetId);
    String tagId = idGenerator.generate();
    TagDefinition tagDefinition = tagset.getTagDefinitionsByName(tagName).findFirst().orElse(null);
    String pathPropertyDefId = null;
    if (tagDefinition == null) {
        tagDefinition = new TagDefinition(tagId, elementStack.peek(), // no parent, hierarchy is collected in annotation property
        null, tagsetId);
        tagDefinition.addSystemPropertyDefinition(new PropertyDefinition(idGenerator.generate(PropertyDefinition.SystemPropertyName.catma_displaycolor.name()), PropertyDefinition.SystemPropertyName.catma_displaycolor.name(), Collections.singletonList(ColorConverter.toRGBIntAsString(ColorConverter.randomHex()))));
        tagDefinition.addSystemPropertyDefinition(new PropertyDefinition(idGenerator.generate(PropertyDefinition.SystemPropertyName.catma_markupauthor.name()), PropertyDefinition.SystemPropertyName.catma_markupauthor.name(), Collections.singletonList(author)));
        pathPropertyDefId = idGenerator.generate();
        PropertyDefinition pathDef = new PropertyDefinition(pathPropertyDefId, "path", Collections.emptyList());
        tagDefinition.addUserDefinedPropertyDefinition(pathDef);
        tagManager.addTagDefinition(tagset, tagDefinition);
    } else {
        pathPropertyDefId = tagDefinition.getPropertyDefinition("path").getUuid();
    }
    for (int idx = 0; idx < element.getChildCount(); idx++) {
        Node curChild = element.getChild(idx);
        if (curChild instanceof Text) {
            xmlContentHandler.addTextContent(contentBuilder, element, curChild.getValue());
        } else if (curChild instanceof Element) {
            // descent
            scanElements(contentBuilder, (Element) curChild, elementStack, tagManager, tagLibrary, namespacePrefixToTagsetIdMap, userMarkupCollection, docId, docLength);
        }
    }
    if (element.getChildCount() != 0) {
        xmlContentHandler.addBreak(contentBuilder, element);
    }
    int end = contentBuilder.length();
    Range range = new Range(start, end);
    if (range.isSinglePoint()) {
        int newStart = range.getStartPoint();
        if (newStart > 0) {
            newStart = newStart - 1;
        }
        int newEnd = range.getEndPoint();
        if (newEnd < docLength - 1) {
            newEnd = newEnd + 1;
        }
        range = new Range(newStart, newEnd);
    }
    TagInstance tagInstance = new TagInstance(idGenerator.generate(), tagDefinition.getUuid(), author, ZonedDateTime.now().format(DateTimeFormatter.ofPattern(Version.DATETIMEPATTERN)), tagDefinition.getUserDefinedPropertyDefinitions(), tagDefinition.getTagsetDefinitionUuid());
    for (int i = 0; i < element.getAttributeCount(); i++) {
        PropertyDefinition propertyDefinition = tagDefinition.getPropertyDefinition(element.getAttribute(i).getQualifiedName());
        if (propertyDefinition == null) {
            propertyDefinition = new PropertyDefinition(idGenerator.generate(), element.getAttribute(i).getQualifiedName(), Collections.singleton(element.getAttribute(i).getValue()));
            tagManager.addUserDefinedPropertyDefinition(tagDefinition, propertyDefinition);
        } else if (!propertyDefinition.getPossibleValueList().contains(element.getAttribute(i).getValue())) {
            List<String> newValueList = new ArrayList<>();
            newValueList.addAll(propertyDefinition.getPossibleValueList());
            newValueList.add(element.getAttribute(i).getValue());
            propertyDefinition.setPossibleValueList(newValueList);
        }
        Property property = new Property(propertyDefinition.getUuid(), Collections.singleton(element.getAttribute(i).getValue()));
        tagInstance.addUserDefinedProperty(property);
    }
    Property pathProperty = new Property(pathPropertyDefId, Collections.singletonList(path));
    tagInstance.addUserDefinedProperty(pathProperty);
    TagReference tagReference = new TagReference(tagInstance, docId, range, userMarkupCollection.getId());
    userMarkupCollection.addTagReference(tagReference);
    elementStack.pop();
}
Also used : TagDefinition(de.catma.tag.TagDefinition) Node(nu.xom.Node) Element(nu.xom.Element) Text(nu.xom.Text) Range(de.catma.document.Range) PropertyDefinition(de.catma.tag.PropertyDefinition) TagsetDefinition(de.catma.tag.TagsetDefinition) TagInstance(de.catma.tag.TagInstance) ArrayList(java.util.ArrayList) List(java.util.List) TagReference(de.catma.document.annotation.TagReference) Property(de.catma.tag.Property)

Example 7 with TagReference

use of de.catma.document.annotation.TagReference in project catma by forTEXT.

the class AnnotationConflict method createCombinedTagInstance.

private List<TagReference> createCombinedTagInstance() throws Exception {
    TagInstance combinedInstance = new TagInstance(masterTagInstance.getUuid(), masterTagInstance.getTagDefinitionId(), masterTagInstance.getAuthor(), masterTagInstance.getTimestamp(), Collections.emptySet(), masterTagInstance.getTagsetId());
    for (Property property : masterTagInstance.getUserDefinedProperties()) {
        HashSet<String> values = new HashSet<>();
        values.addAll(property.getPropertyValueList());
        Property devUserDefinedProperty = devTagInstance.getUserDefinedPropetyByUuid(property.getPropertyDefinitionId());
        if (devUserDefinedProperty != null) {
            values.addAll(devUserDefinedProperty.getPropertyValueList());
        }
        combinedInstance.addUserDefinedProperty(new Property(property.getPropertyDefinitionId(), values));
    }
    List<TagReference> references = new ArrayList<>();
    for (TagReference tagReference : masterTagReferences) {
        references.add(new TagReference(combinedInstance, tagReference.getTarget().toString(), tagReference.getRange(), tagReference.getUserMarkupCollectionUuid()));
    }
    return references;
}
Also used : TagInstance(de.catma.tag.TagInstance) ArrayList(java.util.ArrayList) TagReference(de.catma.document.annotation.TagReference) Property(de.catma.tag.Property) HashSet(java.util.HashSet)

Example 8 with TagReference

use of de.catma.document.annotation.TagReference in project catma by forTEXT.

the class GraphWorktreeProject method importCollection.

public void importCollection(List<TagsetDefinitionImportStatus> tagsetDefinitionImportStatusList, AnnotationCollection importAnnotationCollection) throws IOException {
    // if updates to existing Tagsets are needed, update only the Tags
    // that are actually referenced in the Collection
    Set<String> tagDefinitionIds = importAnnotationCollection.getTagReferences().stream().map(tagRef -> tagRef.getTagDefinitionId()).collect(Collectors.toSet());
    for (TagsetDefinitionImportStatus tagsetDefinitionImportStatus : tagsetDefinitionImportStatusList) {
        tagsetDefinitionImportStatus.setUpdateFilter(tagDefinitionIds);
    }
    importTagsets(tagsetDefinitionImportStatusList);
    importAnnotationCollection.setTagLibrary(tagManager.getTagLibrary());
    try {
        SourceDocument sourceDocument = getSourceDocument(importAnnotationCollection.getSourceDocumentId());
        String umcRevisionHash = gitProjectHandler.createMarkupCollection(importAnnotationCollection.getId(), importAnnotationCollection.getName(), // description
        importAnnotationCollection.getContentInfoSet().getDescription(), importAnnotationCollection.getSourceDocumentId(), importAnnotationCollection.getSourceDocumentRevisionHash());
        String oldRootRevisionHash = this.rootRevisionHash;
        this.rootRevisionHash = gitProjectHandler.getRootRevisionHash();
        graphProjectHandler.addCollection(rootRevisionHash, importAnnotationCollection.getId(), importAnnotationCollection.getName(), umcRevisionHash, sourceDocument, tagManager.getTagLibrary(), oldRootRevisionHash);
        AnnotationCollectionReference annotationCollectionReference = sourceDocument.getUserMarkupCollectionReference(importAnnotationCollection.getId());
        eventBus.post(new CollectionChangeEvent(annotationCollectionReference, sourceDocument, ChangeType.CREATED));
        AnnotationCollection createdAnnotationCollection = getUserMarkupCollection(annotationCollectionReference);
        createdAnnotationCollection.addTagReferences(importAnnotationCollection.getTagReferences());
        ArrayListMultimap<String, TagReference> tagReferencesByTagInstanceId = ArrayListMultimap.create();
        importAnnotationCollection.getTagReferences().stream().forEach(tagReference -> tagReferencesByTagInstanceId.put(tagReference.getTagInstanceId(), tagReference));
        for (String tagInstanceId : tagReferencesByTagInstanceId.keySet()) {
            update(createdAnnotationCollection, tagReferencesByTagInstanceId.get(tagInstanceId));
        }
        commitChanges(String.format("Imported Annotations from Collection %1$s with ID %2$s", createdAnnotationCollection.getName(), createdAnnotationCollection.getId()));
    } catch (Exception e) {
        throw new IOException(String.format("Import of Collection %1$s failed! The import has been aborted.", importAnnotationCollection.getName()), e);
    }
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Reply(de.catma.document.comment.Reply) LoadingCache(com.google.common.cache.LoadingCache) Status(org.eclipse.jgit.api.Status) UI(com.vaadin.ui.UI) IndexedProject(de.catma.indexer.IndexedProject) TermExtractor(de.catma.indexer.TermExtractor) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInfoProvider(de.catma.repository.git.graph.FileInfoProvider) ErrorHandler(de.catma.ui.module.main.ErrorHandler) Locale(java.util.Locale) Map(java.util.Map) DocumentChangeEvent(de.catma.project.event.DocumentChangeEvent) URI(java.net.URI) Path(java.nio.file.Path) CommitInfo(de.catma.project.CommitInfo) CATMAPropertyKey(de.catma.properties.CATMAPropertyKey) ExecutionListener(de.catma.backgroundservice.ExecutionListener) Collection(java.util.Collection) Set(java.util.Set) TagInstance(de.catma.tag.TagInstance) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) ProjectReference(de.catma.project.ProjectReference) TagReference(de.catma.document.annotation.TagReference) CacheLoader(com.google.common.cache.CacheLoader) List(java.util.List) PropertyChangeListener(java.beans.PropertyChangeListener) TPGraphProjectHandler(de.catma.repository.git.graph.tp.TPGraphProjectHandler) TagDefinition(de.catma.tag.TagDefinition) CacheBuilder(com.google.common.cache.CacheBuilder) ContentInfoSet(de.catma.document.source.ContentInfoSet) CollectionChangeEvent(de.catma.project.event.CollectionChangeEvent) RBACPermission(de.catma.rbac.RBACPermission) StatusPrinter(de.catma.repository.git.managers.StatusPrinter) PropertyDefinition(de.catma.tag.PropertyDefinition) TagManager(de.catma.tag.TagManager) CommentProvider(de.catma.repository.git.graph.CommentProvider) RBACRole(de.catma.rbac.RBACRole) MediaType(org.apache.tika.mime.MediaType) Multimap(com.google.common.collect.Multimap) Function(java.util.function.Function) User(de.catma.user.User) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) EventBus(com.google.common.eventbus.EventBus) CommentChangeEvent(de.catma.project.event.CommentChangeEvent) Comment(de.catma.document.comment.Comment) Charset(java.nio.charset.Charset) TeiTagLibrarySerializationHandler(de.catma.serialization.tei.TeiTagLibrarySerializationHandler) Member(de.catma.user.Member) StandardContentHandler(de.catma.document.source.contenthandler.StandardContentHandler) TagsetDefinition(de.catma.tag.TagsetDefinition) Pair(de.catma.util.Pair) IDGenerator(de.catma.util.IDGenerator) RBACSubject(de.catma.rbac.RBACSubject) TagLibrary(de.catma.tag.TagLibrary) PropertyChangeEvent(java.beans.PropertyChangeEvent) OpenProjectListener(de.catma.project.OpenProjectListener) TeiSerializationHandlerFactory(de.catma.serialization.tei.TeiSerializationHandlerFactory) RemovalNotification(com.google.common.cache.RemovalNotification) ProgressListener(de.catma.backgroundservice.ProgressListener) Property(de.catma.tag.Property) AnnotationCollectionReference(de.catma.document.annotation.AnnotationCollectionReference) TermInfo(de.catma.indexer.TermInfo) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SourceDocument(de.catma.document.source.SourceDocument) AnnotationCollection(de.catma.document.annotation.AnnotationCollection) File(java.io.File) TagsetDefinitionImportStatus(de.catma.serialization.TagsetDefinitionImportStatus) TagManagerEvent(de.catma.tag.TagManager.TagManagerEvent) TagLibrarySerializationHandler(de.catma.serialization.TagLibrarySerializationHandler) Indexer(de.catma.indexer.Indexer) ChangeType(de.catma.project.event.ChangeType) GraphProjectHandler(de.catma.repository.git.graph.GraphProjectHandler) ReplyChangeEvent(de.catma.project.event.ReplyChangeEvent) Paths(java.nio.file.Paths) PropertyChangeSupport(java.beans.PropertyChangeSupport) RemovalListener(com.google.common.cache.RemovalListener) BackgroundService(de.catma.backgroundservice.BackgroundService) InputStream(java.io.InputStream) TeiUserMarkupCollectionDeserializer(de.catma.serialization.tei.TeiUserMarkupCollectionDeserializer) AnnotationCollection(de.catma.document.annotation.AnnotationCollection) SourceDocument(de.catma.document.source.SourceDocument) AnnotationCollectionReference(de.catma.document.annotation.AnnotationCollectionReference) CollectionChangeEvent(de.catma.project.event.CollectionChangeEvent) TagReference(de.catma.document.annotation.TagReference) IOException(java.io.IOException) TagsetDefinitionImportStatus(de.catma.serialization.TagsetDefinitionImportStatus) IOException(java.io.IOException)

Example 9 with TagReference

use of de.catma.document.annotation.TagReference in project catma by forTEXT.

the class TPGraphProjectHandler method removeTagReferences.

@Override
public void removeTagReferences(String rootRevisionHash, AnnotationCollection collection, List<TagReference> tagReferences) throws Exception {
    logger.info("Removing " + tagReferences.size() + " references, graph size: " + graph);
    Set<String> tagInstanceIds = tagReferences.stream().map(tr -> tr.getTagInstanceId()).collect(Collectors.toSet());
    GraphTraversalSource g = graph.traversal();
    g.V().has(nt(ProjectRevision), "revisionHash", rootRevisionHash).outE(rt(hasDocument)).inV().has(nt(SourceDocument), "documentId", collection.getSourceDocumentId()).outE(rt(hasCollection)).inV().has(nt(MarkupCollection), "collectionId", collection.getId()).outE(rt(hasInstance)).inV().has(nt(TagInstance), "tagInstanceId", P.within(tagInstanceIds)).store("instances").outE(rt(hasProperty)).inV().drop().cap("instances").unfold().drop().iterate();
    logger.info("Finished removing " + tagReferences.size() + " references, graph size: " + graph);
}
Also used : Term(de.catma.repository.git.graph.NodeType.Term) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Graph(org.apache.tinkerpop.gremlin.structure.Graph) RelationType.rt(de.catma.repository.git.graph.RelationType.rt) TinkerGraph(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph) MarkupCollection(de.catma.repository.git.graph.NodeType.MarkupCollection) Tagset(de.catma.repository.git.graph.NodeType.Tagset) HashMultimap(com.google.common.collect.HashMultimap) FileInfoProvider(de.catma.repository.git.graph.FileInfoProvider) Map(java.util.Map) TagInstance(de.catma.repository.git.graph.NodeType.TagInstance) RelationType.hasProperty(de.catma.repository.git.graph.RelationType.hasProperty) Path(java.nio.file.Path) P(org.apache.tinkerpop.gremlin.process.traversal.P) RelationType.hasInstance(de.catma.repository.git.graph.RelationType.hasInstance) Tag(de.catma.repository.git.graph.NodeType.Tag) ExecutionListener(de.catma.backgroundservice.ExecutionListener) Property(de.catma.repository.git.graph.NodeType.Property) Collection(java.util.Collection) Set(java.util.Set) TagInstance(de.catma.tag.TagInstance) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) ProjectReference(de.catma.project.ProjectReference) TagReference(de.catma.document.annotation.TagReference) ProjectRevision(de.catma.repository.git.graph.NodeType.ProjectRevision) List(java.util.List) TagDefinition(de.catma.tag.TagDefinition) RelationType.hasPosition(de.catma.repository.git.graph.RelationType.hasPosition) RelationType.isPartOf(de.catma.repository.git.graph.RelationType.isPartOf) ContentInfoSet(de.catma.document.source.ContentInfoSet) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) PropertyDefinition(de.catma.tag.PropertyDefinition) TagManager(de.catma.tag.TagManager) CommentProvider(de.catma.repository.git.graph.CommentProvider) Multimap(com.google.common.collect.Multimap) Supplier(java.util.function.Supplier) User(de.catma.user.User) SourceDocument(de.catma.repository.git.graph.NodeType.SourceDocument) NodeType.nt(de.catma.repository.git.graph.NodeType.nt) TagsetDefinition(de.catma.tag.TagsetDefinition) Pair(de.catma.util.Pair) AnnotationProperty(de.catma.repository.git.graph.NodeType.AnnotationProperty) TagLibrary(de.catma.tag.TagLibrary) ProgressListener(de.catma.backgroundservice.ProgressListener) RelationType.hasTagset(de.catma.repository.git.graph.RelationType.hasTagset) Property(de.catma.tag.Property) RelationType.hasDocument(de.catma.repository.git.graph.RelationType.hasDocument) AnnotationCollectionReference(de.catma.document.annotation.AnnotationCollectionReference) RelationType.hasTag(de.catma.repository.git.graph.RelationType.hasTag) SourceDocument(de.catma.document.source.SourceDocument) AnnotationCollection(de.catma.document.annotation.AnnotationCollection) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Position(de.catma.repository.git.graph.NodeType.Position) Indexer(de.catma.indexer.Indexer) GraphProjectHandler(de.catma.repository.git.graph.GraphProjectHandler) RelationType.hasCollection(de.catma.repository.git.graph.RelationType.hasCollection) BackgroundService(de.catma.backgroundservice.BackgroundService) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)

Example 10 with TagReference

use of de.catma.document.annotation.TagReference in project catma by forTEXT.

the class TPGraphProjectHandler method getTagReferencesByCollectionId.

@Override
public Multimap<String, TagReference> getTagReferencesByCollectionId(String rootRevisionHash, PropertyDefinition propertyDefinition, TagDefinition tag) throws Exception {
    Multimap<String, TagReference> result = ArrayListMultimap.create();
    GraphTraversalSource g = graph.traversal();
    g.V().has(nt(ProjectRevision), "revisionHash", rootRevisionHash).outE(rt(hasDocument)).inV().hasLabel(nt(SourceDocument)).outE(rt(hasCollection)).inV().hasLabel(nt(MarkupCollection)).properties("collection").map(prop -> (AnnotationCollection) prop.get().orElse(null)).toList().forEach(collection -> result.putAll(collection.getId(), collection.getTagReferences(tag)));
    return result;
}
Also used : GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) TagReference(de.catma.document.annotation.TagReference)

Aggregations

TagReference (de.catma.document.annotation.TagReference)25 ArrayList (java.util.ArrayList)16 TagInstance (de.catma.tag.TagInstance)15 Property (de.catma.tag.Property)12 TagDefinition (de.catma.tag.TagDefinition)12 List (java.util.List)12 AnnotationCollection (de.catma.document.annotation.AnnotationCollection)11 TagsetDefinition (de.catma.tag.TagsetDefinition)10 AnnotationCollectionReference (de.catma.document.annotation.AnnotationCollectionReference)8 SourceDocument (de.catma.document.source.SourceDocument)8 Logger (java.util.logging.Logger)8 PropertyDefinition (de.catma.tag.PropertyDefinition)7 TagLibrary (de.catma.tag.TagLibrary)7 IOException (java.io.IOException)7 Level (java.util.logging.Level)7 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)6 Range (de.catma.document.Range)6 User (de.catma.user.User)6 File (java.io.File)6 Multimap (com.google.common.collect.Multimap)5