Search in sources :

Example 6 with ILocalGitRepositoryManager

use of de.catma.repository.git.interfaces.ILocalGitRepositoryManager in project catma by forTEXT.

the class GitMarkupCollectionHandlerTest method create.

@Test
public void create() throws Exception {
    try (ILocalGitRepositoryManager jGitRepoManager = new JGitRepoManager(this.catmaProperties.getProperty(RepositoryPropertyKey.GitBasedRepositoryBasePath.name()), this.catmaUser)) {
        this.directoriesToDeleteOnTearDown.add(jGitRepoManager.getRepositoryBasePath());
        GitProjectManager gitProjectHandler = new GitProjectManager(RepositoryPropertyKey.GitBasedRepositoryBasePath.getValue(), UserIdentification.userToMap(this.catmaUser.getIdentifier()));
        String projectId = gitProjectHandler.create("Test CATMA Project", "This is a test CATMA project");
        this.projectsToDeleteOnTearDown.add(projectId);
        // the JGitRepoManager instance should always be in a detached state after GitProjectHandler calls
        // return
        assertFalse(jGitRepoManager.isAttached());
        GitMarkupCollectionHandler gitMarkupCollectionHandler = new GitMarkupCollectionHandler(jGitRepoManager, this.gitLabServerManager);
        String markupCollectionId = gitMarkupCollectionHandler.create(projectId, null, "Test Markup Collection", null, "fakeSourceDocumentId", "fakeSourceDocumentVersion");
        // we don't add the markupCollectionId to this.markupCollectionReposToDeleteOnTearDown as deletion of the
        // project will take care of that for us
        assertNotNull(markupCollectionId);
        assert markupCollectionId.startsWith("CATMA_");
        // the JGitRepoManager instance should always be in a detached state after GitMarkupCollectionHandler
        // calls return
        assertFalse(jGitRepoManager.isAttached());
        File expectedRepoPath = new File(jGitRepoManager.getRepositoryBasePath(), GitMarkupCollectionHandler.getMarkupCollectionRepositoryName(markupCollectionId));
        assert expectedRepoPath.exists();
        assert expectedRepoPath.isDirectory();
        assert Arrays.asList(expectedRepoPath.list()).contains("header.json");
        String expectedSerializedHeader = "" + "{\n" + "\t\"author\":null,\n" + "\t\"description\":null,\n" + "\t\"name\":\"Test Markup Collection\",\n" + "\t\"publisher\":null,\n" + "\t\"sourceDocumentId\":\"fakeSourceDocumentId\",\n" + "\t\"sourceDocumentVersion\":\"fakeSourceDocumentVersion\",\n" + "\t\"tagsets\":{}\n" + "}";
        assertEquals(expectedSerializedHeader.replaceAll("[\n\t]", ""), FileUtils.readFileToString(new File(expectedRepoPath, "header.json"), StandardCharsets.UTF_8));
    }
}
Also used : ILocalGitRepositoryManager(de.catma.repository.git.interfaces.ILocalGitRepositoryManager) JGitRepoManager(de.catma.repository.git.managers.JGitRepoManager) File(java.io.File) GitLabServerManagerTest(de.catma.repository.git.managers.GitLabServerManagerTest) Test(org.junit.Test) JsonLdWebAnnotationTest(de.catma.repository.git.serialization.models.json_ld.JsonLdWebAnnotationTest)

Example 7 with ILocalGitRepositoryManager

use of de.catma.repository.git.interfaces.ILocalGitRepositoryManager in project catma by forTEXT.

the class GitProjectHandlerTest method create.

@Test
public void create() throws Exception {
    try (ILocalGitRepositoryManager jGitRepoManager = new JGitRepoManager(CATMAPropertyKey.GitBasedRepositoryBasePath.getValue(), gitlabManagerRestricted.getUser())) {
        directoriesToDeleteOnTearDown.add(jGitRepoManager.getRepositoryBasePath());
        BackgroundService mockBackgroundService = mock(BackgroundService.class);
        EventBus mockEventBus = mock(EventBus.class);
        GitProjectManager gitProjectManager = new GitProjectManager(CATMAPropertyKey.GitBasedRepositoryBasePath.getValue(), gitlabManagerRestricted, // noop deletion handler
        (projectId) -> {
        }, mockBackgroundService, mockEventBus);
        String projectId = gitProjectManager.create("Test CATMA Project", "This is a test CATMA project");
        // we don't add the projectId to projectsToDeleteOnTearDown as deletion of the user will take care of that for us
        assertNotNull(projectId);
        assert projectId.startsWith("CATMA_");
        // the JGitRepoManager instance should always be in a detached state after GitProjectManager calls return
        assertFalse(jGitRepoManager.isAttached());
        String expectedRootRepositoryName = GitProjectManager.getProjectRootRepositoryName(projectId);
        File expectedRootRepositoryPath = Paths.get(jGitRepoManager.getRepositoryBasePath().getPath(), projectId, expectedRootRepositoryName).toFile();
        assert expectedRootRepositoryPath.exists();
        assert expectedRootRepositoryPath.isDirectory();
    }
}
Also used : BackgroundService(de.catma.backgroundservice.BackgroundService) ILocalGitRepositoryManager(de.catma.repository.git.interfaces.ILocalGitRepositoryManager) JGitRepoManager(de.catma.repository.git.managers.JGitRepoManager) EventBus(com.google.common.eventbus.EventBus) File(java.io.File) GitLabServerManagerTest(de.catma.repository.git.managers.GitLabServerManagerTest) Test(org.junit.jupiter.api.Test)

Example 8 with ILocalGitRepositoryManager

use of de.catma.repository.git.interfaces.ILocalGitRepositoryManager in project catma by forTEXT.

the class GitTagsetHandlerTest method delete.

@Test
public void delete() throws Exception {
    try (ILocalGitRepositoryManager jGitRepoManager = new JGitRepoManager(this.catmaProperties.getProperty(RepositoryPropertyKey.GitBasedRepositoryBasePath.name()), this.catmaUser)) {
        GitTagsetHandler gitTagsetHandler = new GitTagsetHandler(jGitRepoManager, this.gitLabServerManager);
        thrown.expect(IOException.class);
        thrown.expectMessage("Not implemented");
        gitTagsetHandler.delete("fakeProjectId", "fakeTagsetId");
    }
}
Also used : ILocalGitRepositoryManager(de.catma.repository.git.interfaces.ILocalGitRepositoryManager) JGitRepoManager(de.catma.repository.git.managers.JGitRepoManager) GitLabServerManagerTest(de.catma.repository.git.managers.GitLabServerManagerTest) Test(org.junit.Test) JsonLdWebAnnotationTest(de.catma.repository.git.serialization.models.json_ld.JsonLdWebAnnotationTest)

Example 9 with ILocalGitRepositoryManager

use of de.catma.repository.git.interfaces.ILocalGitRepositoryManager in project catma by forTEXT.

the class GitTagsetHandlerTest method createTagDefinitionWithParent.

@Test
public void createTagDefinitionWithParent() throws Exception {
    try (ILocalGitRepositoryManager jGitRepoManager = new JGitRepoManager(this.catmaProperties.getProperty(RepositoryPropertyKey.GitBasedRepositoryBasePath.name()), this.catmaUser)) {
        this.directoriesToDeleteOnTearDown.add(jGitRepoManager.getRepositoryBasePath());
        // create a project
        GitProjectManager gitProjectManager = new GitProjectManager(RepositoryPropertyKey.GitBasedRepositoryBasePath.getValue(), UserIdentification.userToMap(this.catmaUser.getIdentifier()));
        String projectId = gitProjectManager.create("Test CATMA Project for Tagset", "This is a test CATMA project");
        this.projectsToDeleteOnTearDown.add(projectId);
        GitProjectHandler gitProjectHandler = new GitProjectHandler(null, projectId, jGitRepoManager, gitLabServerManager);
        // create a tagset
        String tagsetId = gitProjectHandler.createTagset(null, "Test Tagset", null);
        // we don't add the tagsetId to this.tagsetReposToDeleteOnTearDown as deletion of the project will take
        // care of that for us
        // create a TagDefinition object that has a (fake) parent
        String tagDefinitionId = this.idGenerator.generate();
        String parentTagDefinitionId = this.idGenerator.generate();
        Version tagDefinitionVersion = new Version();
        TagDefinition tagDefinition = new TagDefinition(null, tagDefinitionId, "FakeTagDefinitionName", tagDefinitionVersion, null, parentTagDefinitionId);
        // call createTagDefinition
        GitTagsetHandler gitTagsetHandler = new GitTagsetHandler(jGitRepoManager, this.gitLabServerManager);
        String returnedTagDefinitionId = gitTagsetHandler.createOrUpdateTagDefinition(projectId, tagsetId, tagDefinition);
        assertNotNull(returnedTagDefinitionId);
        assert returnedTagDefinitionId.startsWith("CATMA_");
        // the JGitRepoManager instance should always be in a detached state after GitTagsetHandler calls return
        assertFalse(jGitRepoManager.isAttached());
        assertEquals(tagDefinitionId, returnedTagDefinitionId);
        String projectRootRepositoryName = GitProjectManager.getProjectRootRepositoryName(projectId);
        File expectedTagDefinitionPath = Paths.get(jGitRepoManager.getRepositoryBasePath().toString(), projectRootRepositoryName, GitProjectHandler.TAGSET_SUBMODULES_DIRECTORY_NAME, tagsetId, parentTagDefinitionId, tagDefinition.getUuid()).toFile();
        assert expectedTagDefinitionPath.exists() : "Directory does not exist";
        assert expectedTagDefinitionPath.isDirectory() : "Path is not a directory";
        assert Arrays.asList(expectedTagDefinitionPath.list()).contains("propertydefs.json");
        GitTagDefinition expectedGitTagDefinition = new GitTagDefinition(tagDefinition);
        String actualSerializedGitTagDefinition = FileUtils.readFileToString(new File(expectedTagDefinitionPath, "propertydefs.json"), StandardCharsets.UTF_8);
        GitTagDefinition actualGitTagDefinition = new SerializationHelper<GitTagDefinition>().deserialize(actualSerializedGitTagDefinition, GitTagDefinition.class);
        assertEquals(expectedGitTagDefinition.getTagsetDefinitionUuid(), actualGitTagDefinition.getTagsetDefinitionUuid());
        assertEquals(expectedGitTagDefinition.getParentUuid(), actualGitTagDefinition.getParentUuid());
        assertEquals(expectedGitTagDefinition.getUuid(), actualGitTagDefinition.getUuid());
        assertEquals(expectedGitTagDefinition.getName(), actualGitTagDefinition.getName());
    // TODO: assert tag definition
    }
}
Also used : GitTagDefinition(de.catma.repository.git.serialization.model_wrappers.GitTagDefinition) TagDefinition(de.catma.tag.TagDefinition) GitTagDefinition(de.catma.repository.git.serialization.model_wrappers.GitTagDefinition) ILocalGitRepositoryManager(de.catma.repository.git.interfaces.ILocalGitRepositoryManager) Version(de.catma.tag.Version) JGitRepoManager(de.catma.repository.git.managers.JGitRepoManager) File(java.io.File) GitLabServerManagerTest(de.catma.repository.git.managers.GitLabServerManagerTest) Test(org.junit.Test) JsonLdWebAnnotationTest(de.catma.repository.git.serialization.models.json_ld.JsonLdWebAnnotationTest)

Example 10 with ILocalGitRepositoryManager

use of de.catma.repository.git.interfaces.ILocalGitRepositoryManager 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)

Aggregations

ILocalGitRepositoryManager (de.catma.repository.git.interfaces.ILocalGitRepositoryManager)107 File (java.io.File)56 JGitRepoManager (de.catma.repository.git.managers.JGitRepoManager)22 MergeResult (org.eclipse.jgit.api.MergeResult)19 ContentInfoSet (de.catma.document.source.ContentInfoSet)18 Test (org.junit.Test)18 Path (java.nio.file.Path)15 AnnotationCollectionReference (de.catma.document.annotation.AnnotationCollectionReference)13 GitLabServerManagerTest (de.catma.repository.git.managers.GitLabServerManagerTest)13 JsonLdWebAnnotation (de.catma.repository.git.serialization.models.json_ld.JsonLdWebAnnotation)13 SourceDocument (de.catma.document.source.SourceDocument)11 SourceDocumentInfo (de.catma.document.source.SourceDocumentInfo)11 IDGenerator (de.catma.util.IDGenerator)11 AnnotationCollection (de.catma.document.annotation.AnnotationCollection)10 TagReference (de.catma.document.annotation.TagReference)10 RBACRole (de.catma.rbac.RBACRole)10 Pair (de.catma.util.Pair)10 ProgressListener (de.catma.backgroundservice.ProgressListener)9 Comment (de.catma.document.comment.Comment)9 Reply (de.catma.document.comment.Reply)9