Search in sources :

Example 1 with SerializationHelper

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

the class ProjectResourceExportApiRequestHandler method serializeProjectResources.

private String serializeProjectResources() {
    try {
        Export export = new Export();
        for (SourceDocument sourceDocument : project.getSourceDocuments()) {
            ArrayList<AnnotationCollection> annotationCollections = new ArrayList<>();
            for (AnnotationCollectionReference annotationCollectionReference : sourceDocument.getUserMarkupCollectionRefs()) {
                annotationCollections.add(project.getUserMarkupCollection(annotationCollectionReference));
            }
            ArrayList<TagDefinition> tagDefinitions = new ArrayList<>();
            ArrayList<TagReference> tagReferences = new ArrayList<>();
            for (AnnotationCollection annotationCollection : annotationCollections) {
                for (TagsetDefinition tagsetDefinition : annotationCollection.getTagLibrary().getTagsetDefinitions()) {
                    tagDefinitions.addAll(tagsetDefinition.stream().collect(Collectors.toList()));
                }
                tagReferences.addAll(annotationCollection.getTagReferences());
            }
            ExportDocument exportDocument = new ExportDocument(new PreApiSourceDocument(sourceDocument, String.format("%s%s/doc/%s", BASE_URL, handlerPath.substring(1), sourceDocument.getUuid().toLowerCase())), tagDefinitions.stream().map(PreApiTagDefinition::new).collect(Collectors.toList()), tagReferences.stream().map((TagReference tagReference) -> {
                try {
                    return new PreApiAnnotation(tagReference, tagDefinitions.stream().filter(td -> td.getUuid().equals(tagReference.getTagDefinitionId())).findFirst().get(), sourceDocument);
                } catch (IOException e) {
                    logger.log(Level.WARNING, String.format("Error serializing TagReference: %s", tagReference), e);
                    return null;
                }
            }).collect(Collectors.toList()));
            export.addExportDocument(exportDocument);
        }
        return new SerializationHelper<Export>().serialize(export);
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Failed to serialize project resources", e);
        return "{\"error\": \"Failed to serialize project resources, please contact CATMA support\"}";
    }
}
Also used : RequestHandler(com.vaadin.server.RequestHandler) ExportDocument(de.catma.api.pre.serialization.models.ExportDocument) VaadinRequest(com.vaadin.server.VaadinRequest) PreApiAnnotation(de.catma.api.pre.serialization.model_wrappers.PreApiAnnotation) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Export(de.catma.api.pre.serialization.models.Export) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) TagsetDefinition(de.catma.tag.TagsetDefinition) IDGenerator(de.catma.util.IDGenerator) NoSuchElementException(java.util.NoSuchElementException) OutputStream(java.io.OutputStream) PreApiSourceDocument(de.catma.api.pre.serialization.model_wrappers.PreApiSourceDocument) CATMAPropertyKey(de.catma.properties.CATMAPropertyKey) VaadinResponse(com.vaadin.server.VaadinResponse) AnnotationCollectionReference(de.catma.document.annotation.AnnotationCollectionReference) Project(de.catma.project.Project) IOException(java.io.IOException) SourceDocument(de.catma.document.source.SourceDocument) PreApiTagDefinition(de.catma.api.pre.serialization.model_wrappers.PreApiTagDefinition) AnnotationCollection(de.catma.document.annotation.AnnotationCollection) SerializationHelper(de.catma.repository.git.serialization.SerializationHelper) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) TagReference(de.catma.document.annotation.TagReference) VaadinSession(com.vaadin.server.VaadinSession) TagDefinition(de.catma.tag.TagDefinition) PreApiTagDefinition(de.catma.api.pre.serialization.model_wrappers.PreApiTagDefinition) TagDefinition(de.catma.tag.TagDefinition) AnnotationCollection(de.catma.document.annotation.AnnotationCollection) PreApiSourceDocument(de.catma.api.pre.serialization.model_wrappers.PreApiSourceDocument) PreApiSourceDocument(de.catma.api.pre.serialization.model_wrappers.PreApiSourceDocument) SourceDocument(de.catma.document.source.SourceDocument) ArrayList(java.util.ArrayList) AnnotationCollectionReference(de.catma.document.annotation.AnnotationCollectionReference) IOException(java.io.IOException) ExportDocument(de.catma.api.pre.serialization.models.ExportDocument) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) PreApiTagDefinition(de.catma.api.pre.serialization.model_wrappers.PreApiTagDefinition) TagsetDefinition(de.catma.tag.TagsetDefinition) PreApiAnnotation(de.catma.api.pre.serialization.model_wrappers.PreApiAnnotation) Export(de.catma.api.pre.serialization.models.Export) TagReference(de.catma.document.annotation.TagReference)

Example 2 with SerializationHelper

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

the class GitlabManagerRestricted method getCommentReplies.

@Override
public List<Reply> getCommentReplies(String projectId, Comment comment) throws IOException {
    String resourceId = comment.getDocumentId();
    String projectPath = projectId + "/" + resourceId;
    NotesApi notesApi = restrictedGitLabApi.getNotesApi();
    List<Reply> result = new ArrayList<Reply>();
    try {
        List<Note> notes = notesApi.getIssueNotes(projectPath, comment.getIid());
        for (Note note : notes.stream().filter(n -> !n.getSystem()).collect(Collectors.toList())) {
            // filter system notes
            String noteBody = note.getBody();
            Reply reply = null;
            try {
                reply = new SerializationHelper<Reply>().deserialize(noteBody, Reply.class);
                reply.setCommentUuid(comment.getUuid());
                reply.setId(note.getId());
                reply.setUserId(note.getAuthor().getId());
                reply.setUsername(note.getAuthor().getName());
            } catch (Exception e) {
                logger.log(Level.SEVERE, String.format("Error deserializing Reply #%1$d %2$s", note.getId(), noteBody), e);
                IDGenerator idGenerator = new IDGenerator();
                reply = new Reply(idGenerator.generate(), noteBody, note.getAuthor().getUsername(), note.getAuthor().getId(), comment.getUuid(), note.getId());
            }
            result.add(reply);
        }
        comment.setReplies(result);
        return result;
    } catch (GitLabApiException e) {
        throw new IOException(String.format("Failed to retrieve Replies for Comment %1$s %2$d for resource %3$s in group %4$s!", comment.getUuid(), comment.getIid(), resourceId, projectId), e);
    }
}
Also used : NotesApi(org.gitlab4j.api.NotesApi) JsonObject(com.google.gson.JsonObject) Reply(de.catma.document.comment.Reply) AccessLevel(org.gitlab4j.api.models.AccessLevel) ProjectFilter(org.gitlab4j.api.models.ProjectFilter) StringUtils(org.apache.commons.lang3.StringUtils) Author(org.gitlab4j.api.models.Author) IssuesApi(org.gitlab4j.api.IssuesApi) ChangeUserAttributeEvent(de.catma.ui.events.ChangeUserAttributeEvent) Map(java.util.Map) Group(org.gitlab4j.api.models.Group) IssueState(org.gitlab4j.api.Constants.IssueState) GroupApi(org.gitlab4j.api.GroupApi) Visibility(org.gitlab4j.api.models.Visibility) CATMAPropertyKey(de.catma.properties.CATMAPropertyKey) IGitUserInformation(de.catma.repository.git.interfaces.IGitUserInformation) GitMember(de.catma.repository.git.GitMember) Pager(org.gitlab4j.api.Pager) GitlabUtils(de.catma.repository.git.GitlabUtils) Set(java.util.Set) Logger(java.util.logging.Logger) SerializationHelper(de.catma.repository.git.serialization.SerializationHelper) Collectors(java.util.stream.Collectors) ProjectReference(de.catma.project.ProjectReference) Objects(java.util.Objects) List(java.util.List) ProjectApi(org.gitlab4j.api.ProjectApi) Optional(java.util.Optional) GitLabApiException(org.gitlab4j.api.GitLabApiException) Status(org.gitlab4j.api.models.ImportStatus.Status) CacheBuilder(com.google.common.cache.CacheBuilder) GitLabApi(org.gitlab4j.api.GitLabApi) RBACPermission(de.catma.rbac.RBACPermission) Permissions(org.gitlab4j.api.models.Permissions) GroupFilter(org.gitlab4j.api.models.GroupFilter) Namespace(org.gitlab4j.api.models.Namespace) HashMap(java.util.HashMap) RBACRole(de.catma.rbac.RBACRole) JsonParser(com.google.gson.JsonParser) User(de.catma.user.User) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) EventBus(com.google.common.eventbus.EventBus) IRemoteGitManagerRestricted(de.catma.repository.git.interfaces.IRemoteGitManagerRestricted) Comment(de.catma.document.comment.Comment) ForkStatus(de.catma.project.ForkStatus) GitProjectManager(de.catma.repository.git.GitProjectManager) Note(org.gitlab4j.api.models.Note) IDGenerator(de.catma.util.IDGenerator) Subscribe(com.google.common.eventbus.Subscribe) CreateRepositoryResponse(de.catma.repository.git.CreateRepositoryResponse) Issue(org.gitlab4j.api.models.Issue) IOException(java.io.IOException) Project(org.gitlab4j.api.models.Project) Maps(com.google.common.collect.Maps) IssueFilter(org.gitlab4j.api.models.IssueFilter) TimeUnit(java.util.concurrent.TimeUnit) Member(org.gitlab4j.api.models.Member) Cache(com.google.common.cache.Cache) Collections(java.util.Collections) BackgroundService(de.catma.backgroundservice.BackgroundService) GitUser(de.catma.repository.git.GitUser) ArrayList(java.util.ArrayList) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException) SerializationHelper(de.catma.repository.git.serialization.SerializationHelper) Note(org.gitlab4j.api.models.Note) Reply(de.catma.document.comment.Reply) IDGenerator(de.catma.util.IDGenerator) NotesApi(org.gitlab4j.api.NotesApi)

Example 3 with SerializationHelper

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

the class GitMarkupCollectionHandler method openTagReferences.

private ArrayList<TagReference> openTagReferences(String projectId, String markupCollectionId, String collectionName, File parentDirectory, ProgressListener progressListener, AtomicInteger counter) throws Exception {
    ArrayList<TagReference> tagReferences = new ArrayList<>();
    List<String> contents = Arrays.asList(parentDirectory.list());
    for (String item : contents) {
        File target = new File(parentDirectory, item);
        // if it is a directory, recurse into it adding results to the current tagReferences list
        if (target.isDirectory() && !target.getName().equalsIgnoreCase(".git")) {
            tagReferences.addAll(this.openTagReferences(projectId, markupCollectionId, collectionName, target, progressListener, counter));
        } else // if item is <CATMA_UUID>.json, read it into a list of TagReference objects
        if (target.isFile() && isTagInstanceFilename(target.getName())) {
            counter.incrementAndGet();
            if (counter.intValue() % 1000 == 0) {
                progressListener.setProgress("Loading Annotations %1$s %2$d", collectionName, counter.intValue());
            }
            String serialized = readFileToString(target, StandardCharsets.UTF_8);
            JsonLdWebAnnotation jsonLdWebAnnotation = new SerializationHelper<JsonLdWebAnnotation>().deserialize(serialized, JsonLdWebAnnotation.class);
            tagReferences.addAll(jsonLdWebAnnotation.toTagReferenceList(projectId, markupCollectionId));
        }
    }
    return tagReferences;
}
Also used : SerializationHelper(de.catma.repository.git.serialization.SerializationHelper) ArrayList(java.util.ArrayList) TagReference(de.catma.document.annotation.TagReference) JsonLdWebAnnotation(de.catma.repository.git.serialization.models.json_ld.JsonLdWebAnnotation) File(java.io.File)

Example 4 with SerializationHelper

use of de.catma.repository.git.serialization.SerializationHelper 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 5 with SerializationHelper

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

SerializationHelper (de.catma.repository.git.serialization.SerializationHelper)9 File (java.io.File)7 ILocalGitRepositoryManager (de.catma.repository.git.interfaces.ILocalGitRepositoryManager)6 GitTagsetHeader (de.catma.repository.git.serialization.models.GitTagsetHeader)3 ArrayList (java.util.ArrayList)3 Status (org.eclipse.jgit.api.Status)3 TagReference (de.catma.document.annotation.TagReference)2 ContentInfoSet (de.catma.document.source.ContentInfoSet)2 CATMAPropertyKey (de.catma.properties.CATMAPropertyKey)2 GitMarkupCollectionHeader (de.catma.repository.git.serialization.models.GitMarkupCollectionHeader)2 IDGenerator (de.catma.util.IDGenerator)2 IOException (java.io.IOException)2 Level (java.util.logging.Level)2 Logger (java.util.logging.Logger)2 Collectors (java.util.stream.Collectors)2 StageState (org.eclipse.jgit.lib.IndexDiff.StageState)2 Cache (com.google.common.cache.Cache)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 Maps (com.google.common.collect.Maps)1 EventBus (com.google.common.eventbus.EventBus)1