use of de.catma.serialization.TagsetDefinitionImportStatus in project catma by forTEXT.
the class ProjectView method handleImportTagsetsRequest.
private void handleImportTagsetsRequest() {
GenericUploadDialog uploadDialog = new GenericUploadDialog("Upload a Tag Library with one or more Tagsets:", new SaveCancelListener<byte[]>() {
public void savePressed(byte[] result) {
InputStream is = new ByteArrayInputStream(result);
try {
if (BOMFilterInputStream.hasBOM(result)) {
is = new BOMFilterInputStream(is, // $NON-NLS-1$
Charset.forName("UTF-8"));
}
List<TagsetDefinitionImportStatus> tagsetDefinitionImportStatusList = project.loadTagLibrary(is);
TagsetImportDialog tagsetImportDialog = new TagsetImportDialog(tagsetDefinitionImportStatusList, new SaveCancelListener<List<TagsetDefinitionImportStatus>>() {
@Override
public void savePressed(List<TagsetDefinitionImportStatus> result) {
try {
project.importTagsets(result);
} catch (IOException e) {
((CatmaApplication) UI.getCurrent()).showAndLogError("Error importing Tagsets", e);
}
}
});
tagsetImportDialog.show();
} catch (IOException e) {
((CatmaApplication) UI.getCurrent()).showAndLogError("Error loading external Tagsets", e);
} finally {
CloseSafe.close(is);
}
}
});
uploadDialog.show();
}
use of de.catma.serialization.TagsetDefinitionImportStatus in project catma by forTEXT.
the class GraphWorktreeProject method loadTagLibrary.
@Override
public List<TagsetDefinitionImportStatus> loadTagLibrary(InputStream inputStream) throws IOException {
TeiSerializationHandlerFactory factory = new TeiSerializationHandlerFactory();
factory.setTagManager(new TagManager(new TagLibrary()));
TagLibrarySerializationHandler tagLibrarySerializationHandler = factory.getTagLibrarySerializationHandler();
TagLibrary importedLibrary = tagLibrarySerializationHandler.deserialize(null, inputStream);
List<String> resourceIds = gitProjectHandler.getResourceIds();
List<TagsetDefinitionImportStatus> tagsetDefinitionImportStatusList = new ArrayList<>();
for (TagsetDefinition tagset : importedLibrary) {
boolean inProjectHistory = resourceIds.contains(tagset.getUuid());
boolean current = inProjectHistory && (getTagManager().getTagLibrary().getTagsetDefinition(tagset.getUuid()) != null);
tagsetDefinitionImportStatusList.add(new TagsetDefinitionImportStatus(tagset, inProjectHistory, current));
}
return tagsetDefinitionImportStatusList;
}
use of de.catma.serialization.TagsetDefinitionImportStatus in project catma by forTEXT.
the class GraphWorktreeProject method loadAnnotationCollection.
public Pair<AnnotationCollection, List<TagsetDefinitionImportStatus>> loadAnnotationCollection(InputStream inputStream, SourceDocument document) throws IOException {
TagManager tagManager = new TagManager(new TagLibrary());
TeiTagLibrarySerializationHandler tagLibrarySerializationHandler = new TeiTagLibrarySerializationHandler(tagManager);
TagLibrary importedLibrary = tagLibrarySerializationHandler.deserialize(null, inputStream);
List<String> resourceIds = gitProjectHandler.getResourceIds();
List<TagsetDefinitionImportStatus> tagsetDefinitionImportStatusList = new ArrayList<>();
for (TagsetDefinition tagset : importedLibrary) {
boolean inProjectHistory = resourceIds.contains(tagset.getUuid());
boolean current = inProjectHistory && (getTagManager().getTagLibrary().getTagsetDefinition(tagset.getUuid()) != null);
tagsetDefinitionImportStatusList.add(new TagsetDefinitionImportStatus(tagset, inProjectHistory, current));
}
String collectionId = idGenerator.generate();
TeiUserMarkupCollectionDeserializer deserializer = new TeiUserMarkupCollectionDeserializer(tagLibrarySerializationHandler.getTeiDocument(), tagManager.getTagLibrary(), collectionId);
AnnotationCollection annotationCollection = new AnnotationCollection(collectionId, tagLibrarySerializationHandler.getTeiDocument().getContentInfoSet(), tagManager.getTagLibrary(), deserializer.getTagReferences(), document.getUuid(), document.getRevisionHash());
return new Pair<>(annotationCollection, tagsetDefinitionImportStatusList);
}
use of de.catma.serialization.TagsetDefinitionImportStatus 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.serialization.TagsetDefinitionImportStatus in project catma by forTEXT.
the class GraphWorktreeProject method importTagsets.
@Override
public void importTagsets(List<TagsetDefinitionImportStatus> tagsetDefinitionImportStatusList) throws IOException {
for (TagsetDefinitionImportStatus tagsetDefinitionImportStatus : tagsetDefinitionImportStatusList) {
if (tagsetDefinitionImportStatus.isDoImport()) {
TagsetDefinition tagset = tagsetDefinitionImportStatus.getTagset();
// new Tagset
if (!tagsetDefinitionImportStatus.isInProjectHistory()) {
try {
tagManagerListenersEnabled = false;
try {
addTagsetDefinition(tagset);
tagManager.addTagsetDefinition(tagset);
} catch (Exception e) {
throw new IOException(String.format("Import of Tagset %1$s failed! The import has been aborted.", tagset.getName()), e);
}
} finally {
tagManagerListenersEnabled = true;
}
for (TagDefinition tag : tagset.getRootTagDefinitions()) {
tagManager.addTagDefinition(tagset, tag);
importTagHierarchy(tag, tagset, tagset);
}
} else // removed, but exists in version history
if (!tagsetDefinitionImportStatus.isCurrent()) {
String oldRootRevisionHash = this.rootRevisionHash;
Pair<TagsetDefinition, String> result = gitProjectHandler.cloneAndAddTagset(tagset.getUuid(), tagset.getName(), String.format("Re-Added Tagset %1$s with ID %2$s", tagset.getName(), tagset.getUuid()));
TagsetDefinition oldTagset = result.getFirst();
this.rootRevisionHash = result.getSecond();
// remove old Tags
for (TagDefinition tagDefinition : oldTagset.getRootTagDefinitions()) {
gitProjectHandler.removeTag(tagDefinition);
oldTagset.remove(tagDefinition);
}
try {
// add empty Tagset
graphProjectHandler.addTagset(this.rootRevisionHash, oldTagset, oldRootRevisionHash);
try {
tagManagerListenersEnabled = false;
tagManager.addTagsetDefinition(tagset);
} finally {
tagManagerListenersEnabled = true;
}
// add imported Tags
for (TagDefinition tag : tagset.getRootTagDefinitions()) {
tagManager.addTagDefinition(oldTagset, tag);
importTagHierarchy(tag, tagset, oldTagset);
}
// update meta data
oldTagset.setName(tagset.getName());
updateTagsetDefinition(oldTagset);
} catch (Exception e) {
throw new IOException(String.format("Import of Tagset %1$s failed! The import has been aborted.", tagset.getName()), e);
}
} else // exists already in project
{
try {
TagsetDefinition existingTagset = getTagManager().getTagLibrary().getTagsetDefinition(tagset.getUuid());
for (TagDefinition incomingTag : tagset) {
if (existingTagset.hasTagDefinition(incomingTag.getUuid())) {
if (tagsetDefinitionImportStatus.passesUpdateFilter(incomingTag.getUuid())) {
TagDefinition existingTag = existingTagset.getTagDefinition(incomingTag.getUuid());
for (PropertyDefinition incomingPropertyDef : incomingTag.getUserDefinedPropertyDefinitions()) {
PropertyDefinition existingPropertyDef = existingTag.getPropertyDefinitionByUuid(incomingPropertyDef.getUuid());
if (existingPropertyDef != null) {
for (String value : incomingPropertyDef.getPossibleValueList()) {
if (!existingPropertyDef.getPossibleValueList().contains(value)) {
existingPropertyDef.addValue(value);
}
}
existingPropertyDef.setName(incomingPropertyDef.getName());
updatePropertyDefinition(existingPropertyDef, existingTag);
} else {
existingTag.addUserDefinedPropertyDefinition(incomingPropertyDef);
}
existingTag.setName(incomingTag.getName());
existingTag.setColor(incomingTag.getColor());
updateTagDefinition(existingTag, existingTagset);
}
}
} else {
getTagManager().addTagDefinition(existingTagset, incomingTag);
}
}
existingTagset.setName(tagset.getName());
updateTagsetDefinition(existingTagset);
} catch (Exception e) {
throw new IOException(String.format("Import of Tagset %1$s failed! The import has been aborted.", tagset.getName()), e);
}
}
}
}
}
Aggregations