Search in sources :

Example 1 with GitMarkupCollectionHeader

use of de.catma.repository.git.serialization.models.GitMarkupCollectionHeader 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 2 with GitMarkupCollectionHeader

use of de.catma.repository.git.serialization.models.GitMarkupCollectionHeader in project catma by forTEXT.

the class GitMarkupCollectionHandler method create.

/**
 * Creates a new markup collection.
 * <p>
 * NB: You probably don't want to call this method directly (it doesn't create the submodule in the project root
 * repo). Instead call the <code>createMarkupCollection</code> method of the {@link GitProjectManager} class.
 *
 * @param projectId the ID of the project within which the new markup collection must be created
 * @param collectionId the ID of the new collection
 * @param name the name of the new markup collection
 * @param description the description of the new markup collection
 * @param sourceDocumentId the ID of the source document to which the new markup collection relates
 * @param sourceDocumentVersion the version of the source document to which the new markup collection relates
 * @return the Collection's revisionHash
 * @throws IOException if an error occurs while creating the markup collection
 */
public String create(String projectId, String collectionId, String name, String description, String sourceDocumentId, String sourceDocumentVersion) throws IOException {
    try (ILocalGitRepositoryManager localGitRepoManager = this.localGitRepositoryManager) {
        // create the remote markup collection repository
        CreateRepositoryResponse createRepositoryResponse = this.remoteGitServerManager.createRepository(collectionId, collectionId, projectId);
        // clone the repository locally
        localGitRepoManager.clone(projectId, createRepositoryResponse.repositoryHttpUrl, null, credentialsProvider);
        // write header.json into the local repo
        File targetHeaderFile = new File(localGitRepoManager.getRepositoryWorkTree(), HEADER_FILE_NAME);
        GitMarkupCollectionHeader header = new GitMarkupCollectionHeader(name, description, sourceDocumentId, sourceDocumentVersion);
        String serializedHeader = new SerializationHelper<GitMarkupCollectionHeader>().serialize(header);
        return localGitRepoManager.addAndCommit(targetHeaderFile, serializedHeader.getBytes(StandardCharsets.UTF_8), String.format("Added Collection %1$s with ID %2$s", name, collectionId), remoteGitServerManager.getUsername(), remoteGitServerManager.getEmail());
    }
}
Also used : ILocalGitRepositoryManager(de.catma.repository.git.interfaces.ILocalGitRepositoryManager) File(java.io.File) GitMarkupCollectionHeader(de.catma.repository.git.serialization.models.GitMarkupCollectionHeader)

Example 3 with GitMarkupCollectionHeader

use of de.catma.repository.git.serialization.models.GitMarkupCollectionHeader 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 GitMarkupCollectionHeader

use of de.catma.repository.git.serialization.models.GitMarkupCollectionHeader 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 GitMarkupCollectionHeader

use of de.catma.repository.git.serialization.models.GitMarkupCollectionHeader 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

GitMarkupCollectionHeader (de.catma.repository.git.serialization.models.GitMarkupCollectionHeader)7 ILocalGitRepositoryManager (de.catma.repository.git.interfaces.ILocalGitRepositoryManager)6 File (java.io.File)6 ContentInfoSet (de.catma.document.source.ContentInfoSet)5 SerializationHelper (de.catma.repository.git.serialization.SerializationHelper)2 AnnotationCollection (de.catma.document.annotation.AnnotationCollection)1 AnnotationCollectionReference (de.catma.document.annotation.AnnotationCollectionReference)1 TagReference (de.catma.document.annotation.TagReference)1 AnnotationConflict (de.catma.project.conflict.AnnotationConflict)1 CollectionConflict (de.catma.project.conflict.CollectionConflict)1 JsonLdWebAnnotation (de.catma.repository.git.serialization.models.json_ld.JsonLdWebAnnotation)1 Property (de.catma.tag.Property)1 TagInstance (de.catma.tag.TagInstance)1 TagsetDefinition (de.catma.tag.TagsetDefinition)1 HashSet (java.util.HashSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Status (org.eclipse.jgit.api.Status)1 StageState (org.eclipse.jgit.lib.IndexDiff.StageState)1