Search in sources :

Example 1 with TagsetDefinitionImportStatus

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();
}
Also used : CatmaApplication(de.catma.ui.CatmaApplication) GenericUploadDialog(de.catma.ui.dialog.GenericUploadDialog) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) BOMFilterInputStream(de.catma.document.source.contenthandler.BOMFilterInputStream) InputStream(java.io.InputStream) SaveCancelListener(de.catma.ui.dialog.SaveCancelListener) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) TagsetDefinitionImportStatus(de.catma.serialization.TagsetDefinitionImportStatus) BOMFilterInputStream(de.catma.document.source.contenthandler.BOMFilterInputStream)

Example 2 with TagsetDefinitionImportStatus

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;
}
Also used : TagLibrary(de.catma.tag.TagLibrary) TagsetDefinition(de.catma.tag.TagsetDefinition) TagManager(de.catma.tag.TagManager) TeiSerializationHandlerFactory(de.catma.serialization.tei.TeiSerializationHandlerFactory) TeiTagLibrarySerializationHandler(de.catma.serialization.tei.TeiTagLibrarySerializationHandler) TagLibrarySerializationHandler(de.catma.serialization.TagLibrarySerializationHandler) ArrayList(java.util.ArrayList) TagsetDefinitionImportStatus(de.catma.serialization.TagsetDefinitionImportStatus)

Example 3 with TagsetDefinitionImportStatus

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);
}
Also used : TagLibrary(de.catma.tag.TagLibrary) AnnotationCollection(de.catma.document.annotation.AnnotationCollection) TeiTagLibrarySerializationHandler(de.catma.serialization.tei.TeiTagLibrarySerializationHandler) ArrayList(java.util.ArrayList) TeiUserMarkupCollectionDeserializer(de.catma.serialization.tei.TeiUserMarkupCollectionDeserializer) TagsetDefinition(de.catma.tag.TagsetDefinition) TagManager(de.catma.tag.TagManager) TagsetDefinitionImportStatus(de.catma.serialization.TagsetDefinitionImportStatus) Pair(de.catma.util.Pair)

Example 4 with TagsetDefinitionImportStatus

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);
    }
}
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 5 with TagsetDefinitionImportStatus

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);
                }
            }
        }
    }
}
Also used : TagsetDefinition(de.catma.tag.TagsetDefinition) TagDefinition(de.catma.tag.TagDefinition) IOException(java.io.IOException) TagsetDefinitionImportStatus(de.catma.serialization.TagsetDefinitionImportStatus) PropertyDefinition(de.catma.tag.PropertyDefinition) IOException(java.io.IOException) Pair(de.catma.util.Pair)

Aggregations

TagsetDefinitionImportStatus (de.catma.serialization.TagsetDefinitionImportStatus)10 ArrayList (java.util.ArrayList)7 TagsetDefinition (de.catma.tag.TagsetDefinition)6 Pair (de.catma.util.Pair)6 List (java.util.List)6 AnnotationCollection (de.catma.document.annotation.AnnotationCollection)5 TagLibrary (de.catma.tag.TagLibrary)5 TagManager (de.catma.tag.TagManager)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 SourceDocument (de.catma.document.source.SourceDocument)4 PropertyDefinition (de.catma.tag.PropertyDefinition)4 TagDefinition (de.catma.tag.TagDefinition)4 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)3 Multimap (com.google.common.collect.Multimap)3 Label (com.vaadin.ui.Label)3 UI (com.vaadin.ui.UI)3 ProgressListener (de.catma.backgroundservice.ProgressListener)3 TagReference (de.catma.document.annotation.TagReference)3 BOMFilterInputStream (de.catma.document.source.contenthandler.BOMFilterInputStream)3 TeiTagLibrarySerializationHandler (de.catma.serialization.tei.TeiTagLibrarySerializationHandler)3