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();
}
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;
}
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);
}
}
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);
}
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;
}
Aggregations