Search in sources :

Example 1 with ContentInfoSet

use of de.catma.document.source.ContentInfoSet in project catma by forTEXT.

the class TagsetXMLExportStreamSource method getStream.

@Override
public InputStream getStream() {
    final UI ui = UI.getCurrent();
    Set<TagsetDefinition> tagsets = tagsetsSupplier.get();
    Project project = projectSupplier.get();
    if (tagsets != null && !tagsets.isEmpty()) {
        TeiDocumentFactory teiDocumentFactory = new TeiDocumentFactory();
        try {
            final TeiDocument teiDocument = teiDocumentFactory.createEmptyDocument(null);
            final TeiTagLibrarySerializer teiTagSerializer = new TeiTagLibrarySerializer(teiDocument);
            final TagManager tagManager = new TagManager(new TagLibrary());
            tagsets.forEach(tagset -> tagManager.addTagsetDefinition(tagset));
            final ContentInfoSet contentInfoSet = new ContentInfoSet(project.getUser().toString(), project.getDescription(), project.getUser().toString(), project.getName());
            teiDocument.getTeiHeader().setValues(contentInfoSet);
            teiTagSerializer.serialize(tagManager.getTagLibrary());
            File tempFile = File.createTempFile(new IDGenerator().generate() + "_TagLibrary_Export", "xml");
            try (FileOutputStream fos = new FileOutputStream(tempFile)) {
                teiDocument.printXmlDocument(fos);
            }
            return new FileInputStream(tempFile);
        } catch (Exception e) {
            ((ErrorHandler) ui).showAndLogError("Error exporting Tagsets to XML!", e);
        }
    }
    return null;
}
Also used : TagLibrary(de.catma.tag.TagLibrary) TeiDocumentFactory(de.catma.serialization.tei.TeiDocumentFactory) TeiTagLibrarySerializer(de.catma.serialization.tei.TeiTagLibrarySerializer) FileInputStream(java.io.FileInputStream) TagsetDefinition(de.catma.tag.TagsetDefinition) Project(de.catma.project.Project) TagManager(de.catma.tag.TagManager) ContentInfoSet(de.catma.document.source.ContentInfoSet) UI(com.vaadin.ui.UI) FileOutputStream(java.io.FileOutputStream) File(java.io.File) IDGenerator(de.catma.util.IDGenerator) TeiDocument(de.catma.serialization.tei.TeiDocument)

Example 2 with ContentInfoSet

use of de.catma.document.source.ContentInfoSet 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;
    }
}
Also used : AnnotationCollection(de.catma.document.annotation.AnnotationCollection) ILocalGitRepositoryManager(de.catma.repository.git.interfaces.ILocalGitRepositoryManager) JsonLdWebAnnotation(de.catma.repository.git.serialization.models.json_ld.JsonLdWebAnnotation) GitMarkupCollectionHeader(de.catma.repository.git.serialization.models.GitMarkupCollectionHeader) TagsetDefinition(de.catma.tag.TagsetDefinition) ContentInfoSet(de.catma.document.source.ContentInfoSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TagInstance(de.catma.tag.TagInstance) TagReference(de.catma.document.annotation.TagReference) File(java.io.File) Property(de.catma.tag.Property) HashSet(java.util.HashSet)

Example 3 with ContentInfoSet

use of de.catma.document.source.ContentInfoSet in project catma by forTEXT.

the class GitMarkupCollectionHandler method getCollectionConflict.

public CollectionConflict getCollectionConflict(String projectId, String collectionId) throws Exception {
    try (ILocalGitRepositoryManager localGitRepoManager = this.localGitRepositoryManager) {
        String projectRootRepositoryName = GitProjectManager.getProjectRootRepositoryName(projectId);
        localGitRepoManager.open(projectId, projectRootRepositoryName);
        String collectionSubmoduleRelDir = GitProjectHandler.ANNOTATION_COLLECTION_SUBMODULES_DIRECTORY_NAME + "/" + collectionId;
        File collectionSubmoduleAbsPath = new File(localGitRepoManager.getRepositoryWorkTree().toString(), collectionSubmoduleRelDir);
        localGitRepoManager.detach();
        String collectionGitRepositoryName = projectRootRepositoryName + "/" + collectionSubmoduleRelDir;
        localGitRepoManager.open(projectId, collectionGitRepositoryName);
        Status status = localGitRepoManager.getStatus();
        File collectionHeaderFile = new File(collectionSubmoduleAbsPath, HEADER_FILE_NAME);
        String serializedCollectionHeaderFile = FileUtils.readFileToString(collectionHeaderFile, StandardCharsets.UTF_8);
        CollectionConflict collectionConflict;
        if (status.getConflictingStageState().containsKey(HEADER_FILE_NAME)) {
            GitMarkupCollectionHeader gitCollectionHeader = resolveCollectionHeaderConflict(serializedCollectionHeaderFile, status.getConflictingStageState().get(HEADER_FILE_NAME));
            serializedCollectionHeaderFile = new SerializationHelper<GitMarkupCollectionHeader>().serialize(gitCollectionHeader);
            localGitRepoManager.add(collectionHeaderFile.getAbsoluteFile(), serializedCollectionHeaderFile.getBytes(StandardCharsets.UTF_8));
            ContentInfoSet contentInfoSet = new ContentInfoSet(gitCollectionHeader.getAuthor(), gitCollectionHeader.getDescription(), gitCollectionHeader.getPublisher(), gitCollectionHeader.getName());
            collectionConflict = new CollectionConflict(projectId, collectionId, contentInfoSet, gitCollectionHeader.getSourceDocumentId());
            collectionConflict.setHeaderConflict(true);
            status = localGitRepoManager.getStatus();
        } else {
            GitMarkupCollectionHeader gitCollectionHeader = new SerializationHelper<GitMarkupCollectionHeader>().deserialize(serializedCollectionHeaderFile, GitMarkupCollectionHeader.class);
            ContentInfoSet contentInfoSet = new ContentInfoSet(gitCollectionHeader.getAuthor(), gitCollectionHeader.getDescription(), gitCollectionHeader.getPublisher(), gitCollectionHeader.getName());
            collectionConflict = new CollectionConflict(projectId, collectionId, contentInfoSet, gitCollectionHeader.getSourceDocumentId());
        }
        for (Entry<String, StageState> entry : status.getConflictingStageState().entrySet()) {
            String relativeAnnotationPathname = entry.getKey();
            String absAnnotationPathname = collectionSubmoduleAbsPath + "/" + relativeAnnotationPathname;
            StageState stageState = entry.getValue();
            switch(stageState) {
                case BOTH_MODIFIED:
                    {
                        String serializedConflictingAnnotation = FileUtils.readFileToString(new File(absAnnotationPathname), StandardCharsets.UTF_8);
                        AnnotationConflict annotationConflict = getBothModifiedAnnotationConflict(projectId, collectionId, serializedConflictingAnnotation);
                        collectionConflict.addAnnotationConflict(annotationConflict);
                        break;
                    }
                case DELETED_BY_THEM:
                    {
                        // them is the user on the dev branch here
                        // in this case the file comes from us (the team on the master branch)
                        String serializedConflictingAnnotation = FileUtils.readFileToString(new File(absAnnotationPathname), StandardCharsets.UTF_8);
                        AnnotationConflict annotationConflict = getDeleteByThemAnnotationConflict(projectId, collectionId, serializedConflictingAnnotation);
                        collectionConflict.addAnnotationConflict(annotationConflict);
                        break;
                    }
                case DELETED_BY_US:
                    {
                        // us is the team on the master branch here
                        // in this case the file comes from them (the user on the dev branch)
                        String serializedConflictingAnnotation = FileUtils.readFileToString(new File(absAnnotationPathname), StandardCharsets.UTF_8);
                        AnnotationConflict annotationConflict = getDeleteByUsAnnotationConflict(projectId, collectionId, serializedConflictingAnnotation);
                        collectionConflict.addAnnotationConflict(annotationConflict);
                        break;
                    }
                default:
                    {
                        // TODO:
                        System.out.println("not handled");
                    }
            }
        }
        return collectionConflict;
    }
}
Also used : Status(org.eclipse.jgit.api.Status) SerializationHelper(de.catma.repository.git.serialization.SerializationHelper) AnnotationConflict(de.catma.project.conflict.AnnotationConflict) ContentInfoSet(de.catma.document.source.ContentInfoSet) StageState(org.eclipse.jgit.lib.IndexDiff.StageState) ILocalGitRepositoryManager(de.catma.repository.git.interfaces.ILocalGitRepositoryManager) File(java.io.File) GitMarkupCollectionHeader(de.catma.repository.git.serialization.models.GitMarkupCollectionHeader) CollectionConflict(de.catma.project.conflict.CollectionConflict)

Example 4 with ContentInfoSet

use of de.catma.document.source.ContentInfoSet in project catma by forTEXT.

the class GitMarkupCollectionHandler method getCollectionReference.

public AnnotationCollectionReference getCollectionReference(String projectId, String markupCollectionId) throws Exception {
    try (ILocalGitRepositoryManager localGitRepoManager = this.localGitRepositoryManager) {
        String projectRootRepositoryName = GitProjectManager.getProjectRootRepositoryName(projectId);
        localGitRepoManager.open(projectId, projectRootRepositoryName);
        String markupCollectionSubmoduleRelDir = String.format("%s/%s", GitProjectHandler.ANNOTATION_COLLECTION_SUBMODULES_DIRECTORY_NAME, markupCollectionId);
        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());
        return new AnnotationCollectionReference(markupCollectionId, markupCollectionRevisionHash, contentInfoSet, markupCollectionHeader.getSourceDocumentId(), markupCollectionHeader.getSourceDocumentVersion());
    }
}
Also used : ContentInfoSet(de.catma.document.source.ContentInfoSet) ILocalGitRepositoryManager(de.catma.repository.git.interfaces.ILocalGitRepositoryManager) AnnotationCollectionReference(de.catma.document.annotation.AnnotationCollectionReference) File(java.io.File) GitMarkupCollectionHeader(de.catma.repository.git.serialization.models.GitMarkupCollectionHeader)

Example 5 with ContentInfoSet

use of de.catma.document.source.ContentInfoSet in project catma by forTEXT.

the class GitMarkupCollectionHandler method updateCollection.

public String updateCollection(String projectId, AnnotationCollectionReference collectionRef) throws Exception {
    try (ILocalGitRepositoryManager localGitRepoManager = this.localGitRepositoryManager) {
        String projectRootRepositoryName = GitProjectManager.getProjectRootRepositoryName(projectId);
        String collectionGitRepositoryName = projectRootRepositoryName + "/" + GitProjectHandler.ANNOTATION_COLLECTION_SUBMODULES_DIRECTORY_NAME + "/" + collectionRef.getId();
        localGitRepoManager.open(projectId, collectionGitRepositoryName);
        ContentInfoSet contentInfoSet = collectionRef.getContentInfoSet();
        File targetHeaderFile = new File(localGitRepoManager.getRepositoryWorkTree(), HEADER_FILE_NAME);
        GitMarkupCollectionHeader header = new GitMarkupCollectionHeader(contentInfoSet.getTitle(), contentInfoSet.getDescription(), collectionRef.getSourceDocumentId(), collectionRef.getSourceDocumentRevisiohHash());
        SerializationHelper<GitMarkupCollectionHeader> serializationHelper = new SerializationHelper<>();
        String serializedHeader = serializationHelper.serialize(header);
        localGitRepoManager.add(targetHeaderFile, // TODO: why are we doing this and then calling addAndCommit?
        serializedHeader.getBytes(StandardCharsets.UTF_8));
        String collectionRevision = localGitRepoManager.addAndCommit(targetHeaderFile, serializedHeader.getBytes(StandardCharsets.UTF_8), String.format("Updated metadata of Collection %1$s with ID %2$s", collectionRef.getName(), collectionRef.getId()), remoteGitServerManager.getUsername(), remoteGitServerManager.getEmail());
        return collectionRevision;
    }
}
Also used : SerializationHelper(de.catma.repository.git.serialization.SerializationHelper) ContentInfoSet(de.catma.document.source.ContentInfoSet) ILocalGitRepositoryManager(de.catma.repository.git.interfaces.ILocalGitRepositoryManager) File(java.io.File) GitMarkupCollectionHeader(de.catma.repository.git.serialization.models.GitMarkupCollectionHeader)

Aggregations

ContentInfoSet (de.catma.document.source.ContentInfoSet)17 File (java.io.File)12 ILocalGitRepositoryManager (de.catma.repository.git.interfaces.ILocalGitRepositoryManager)9 IndexInfoSet (de.catma.document.source.IndexInfoSet)6 SourceDocumentInfo (de.catma.document.source.SourceDocumentInfo)6 TechInfoSet (de.catma.document.source.TechInfoSet)6 IDGenerator (de.catma.util.IDGenerator)6 FileInputStream (java.io.FileInputStream)6 AnnotationCollection (de.catma.document.annotation.AnnotationCollection)4 JGitRepoManager (de.catma.repository.git.managers.JGitRepoManager)4 GitMarkupCollectionHeader (de.catma.repository.git.serialization.models.GitMarkupCollectionHeader)4 TagsetDefinition (de.catma.tag.TagsetDefinition)4 EventBus (com.google.common.eventbus.EventBus)3 BackgroundService (de.catma.backgroundservice.BackgroundService)3 SourceDocument (de.catma.document.source.SourceDocument)3 TermExtractor (de.catma.indexer.TermExtractor)3 GitLabServerManagerTest (de.catma.repository.git.managers.GitLabServerManagerTest)3 Property (de.catma.tag.Property)3 TagInstance (de.catma.tag.TagInstance)3 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)3