Search in sources :

Example 36 with SourceDocument

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

the class AnalyzeResourcePanel method initData.

private void initData() {
    documentData = new TreeData<>();
    try {
        Collection<SourceDocument> documents = project.getSourceDocuments();
        documentData.addRootItems(documents.stream().map(document -> new DocumentDataItem(document)));
        for (DocumentTreeItem documentDataItem : documentData.getRootItems()) {
            for (AnnotationCollectionReference umcRef : ((DocumentDataItem) documentDataItem).getDocument().getUserMarkupCollectionRefs()) {
                documentData.addItem(documentDataItem, new CollectionDataItem(umcRef, project.hasPermission(project.getRoleForCollection(umcRef.getId()), RBACPermission.COLLECTION_WRITE)));
            }
        }
        documentTree.setDataProvider(new TreeDataProvider<>(documentData));
        Collection<SourceDocument> selectedDocuments = corpus.getSourceDocuments();
        Collection<AnnotationCollectionReference> selectedCollections = corpus.getUserMarkupCollectionRefs();
        documentData.getRootItems().stream().filter(documentItem -> selectedDocuments.contains(((DocumentDataItem) documentItem).getDocument())).forEach(documentTree::select);
        for (DocumentTreeItem documentDataItem : documentData.getRootItems()) {
            List<DocumentTreeItem> collectionItems = documentData.getChildren(documentDataItem);
            for (DocumentTreeItem oneCollection : collectionItems) {
                if (selectedCollections.contains(((CollectionDataItem) oneCollection).getCollectionRef())) {
                    documentTree.getSelectionModel().select(oneCollection);
                }
            }
        }
        documentTree.expand(documentData.getRootItems());
    } catch (Exception e) {
        ((ErrorHandler) UI.getCurrent()).showAndLogError("error loading Project data", e);
    }
}
Also used : SelectionListener(com.vaadin.event.selection.SelectionListener) VerticalLayout(com.vaadin.ui.VerticalLayout) SelectionEvent(com.vaadin.event.selection.SelectionEvent) UI(com.vaadin.ui.UI) ActionGridComponent(de.catma.ui.component.actiongrid.ActionGridComponent) HashSet(java.util.HashSet) EventBus(com.google.common.eventbus.EventBus) Notification(com.vaadin.ui.Notification) ErrorHandler(de.catma.ui.module.main.ErrorHandler) Label(com.vaadin.ui.Label) TreeDataProvider(com.vaadin.data.provider.TreeDataProvider) DocumentChangeEvent(de.catma.project.event.DocumentChangeEvent) TreeGridFactory(de.catma.ui.component.TreeGridFactory) Subscribe(com.google.common.eventbus.Subscribe) SelectionModel(com.vaadin.data.SelectionModel) TreeData(com.vaadin.data.TreeData) AnnotationCollectionReference(de.catma.document.annotation.AnnotationCollectionReference) Project(de.catma.project.Project) Collection(java.util.Collection) Set(java.util.Set) TreeGrid(com.vaadin.ui.TreeGrid) SourceDocument(de.catma.document.source.SourceDocument) ProjectReadyEvent(de.catma.project.event.ProjectReadyEvent) Collectors(java.util.stream.Collectors) MarginInfo(com.vaadin.shared.ui.MarginInfo) ItemClick(com.vaadin.ui.Grid.ItemClick) List(java.util.List) Type(com.vaadin.ui.Notification.Type) ChangeType(de.catma.project.event.ChangeType) Corpus(de.catma.document.corpus.Corpus) Optional(java.util.Optional) HtmlRenderer(com.vaadin.ui.renderers.HtmlRenderer) CollectionChangeEvent(de.catma.project.event.CollectionChangeEvent) Collections(java.util.Collections) RBACPermission(de.catma.rbac.RBACPermission) Grid(com.vaadin.ui.Grid) SourceDocument(de.catma.document.source.SourceDocument) AnnotationCollectionReference(de.catma.document.annotation.AnnotationCollectionReference)

Example 37 with SourceDocument

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

the class CollectionSelectionStep method initData.

private void initData() throws Exception {
    documentData = new TreeData<>();
    @SuppressWarnings("unchecked") Set<String> documentIds = (Set<String>) context.get(AnnotationWizardContextKey.DOCUMENTIDS);
    for (String documentId : documentIds) {
        SourceDocument srcDoc = project.getSourceDocument(documentId);
        DocumentResource docResource = new DocumentResource(srcDoc, project.getProjectId(), project.hasPermission(project.getRoleForDocument(srcDoc.getUuid()), RBACPermission.DOCUMENT_WRITE));
        if (project.hasPermission(project.getRoleForDocument(srcDoc.getUuid()), RBACPermission.DOCUMENT_READ)) {
            documentData.addItem(null, docResource);
            List<AnnotationCollectionReference> collections = srcDoc.getUserMarkupCollectionRefs();
            List<Resource> readableCollectionResources = collections.stream().map(collectionRef -> (Resource) new CollectionResource(collectionRef, project.getProjectId(), project.hasPermission(project.getRoleForCollection(collectionRef.getId()), RBACPermission.COLLECTION_WRITE))).filter(colRes -> project.hasPermission(project.getRoleForCollection(colRes.getResourceId()), RBACPermission.COLLECTION_WRITE)).collect(Collectors.toList());
            if (!collections.isEmpty()) {
                documentData.addItems(docResource, readableCollectionResources);
            } else {
                // TODO: improve message
                Notification.show("Info", String.format("You do not have a writable Collection available for Document %1$s", srcDoc.toString()), Type.HUMANIZED_MESSAGE);
            }
        }
    }
    documentDataProvider = new TreeDataProvider<Resource>(documentData);
    documentGrid.setDataProvider(documentDataProvider);
}
Also used : Resource(de.catma.ui.module.project.Resource) StepChangeListener(de.catma.ui.dialog.wizard.StepChangeListener) VerticalLayout(com.vaadin.ui.VerticalLayout) UI(com.vaadin.ui.UI) HashMap(java.util.HashMap) WizardContext(de.catma.ui.dialog.wizard.WizardContext) ActionGridComponent(de.catma.ui.component.actiongrid.ActionGridComponent) ProgressStep(de.catma.ui.dialog.wizard.ProgressStep) Function(java.util.function.Function) HashSet(java.util.HashSet) EventBus(com.google.common.eventbus.EventBus) CollectionResource(de.catma.ui.module.project.CollectionResource) Notification(com.vaadin.ui.Notification) ErrorHandler(de.catma.ui.module.main.ErrorHandler) Map(java.util.Map) Label(com.vaadin.ui.Label) TreeDataProvider(com.vaadin.data.provider.TreeDataProvider) TreeGridFactory(de.catma.ui.component.TreeGridFactory) Subscribe(com.google.common.eventbus.Subscribe) SaveCancelListener(de.catma.ui.dialog.SaveCancelListener) DocumentResource(de.catma.ui.module.project.DocumentResource) TreeData(com.vaadin.data.TreeData) AnnotationCollectionReference(de.catma.document.annotation.AnnotationCollectionReference) Project(de.catma.project.Project) Set(java.util.Set) ProgressStepFactory(de.catma.ui.dialog.wizard.ProgressStepFactory) TreeGrid(com.vaadin.ui.TreeGrid) SourceDocument(de.catma.document.source.SourceDocument) Collectors(java.util.stream.Collectors) ContextMenu(com.vaadin.contextmenu.ContextMenu) List(java.util.List) Type(com.vaadin.ui.Notification.Type) ChangeType(de.catma.project.event.ChangeType) SerializablePredicate(com.vaadin.server.SerializablePredicate) HierarchicalQuery(com.vaadin.data.provider.HierarchicalQuery) HtmlRenderer(com.vaadin.ui.renderers.HtmlRenderer) SingleTextInputDialog(de.catma.ui.dialog.SingleTextInputDialog) WizardStep(de.catma.ui.dialog.wizard.WizardStep) CollectionChangeEvent(de.catma.project.event.CollectionChangeEvent) SelectionMode(com.vaadin.ui.Grid.SelectionMode) Collections(java.util.Collections) RBACPermission(de.catma.rbac.RBACPermission) CollectionResource(de.catma.ui.module.project.CollectionResource) HashSet(java.util.HashSet) Set(java.util.Set) SourceDocument(de.catma.document.source.SourceDocument) Resource(de.catma.ui.module.project.Resource) CollectionResource(de.catma.ui.module.project.CollectionResource) DocumentResource(de.catma.ui.module.project.DocumentResource) AnnotationCollectionReference(de.catma.document.annotation.AnnotationCollectionReference) DocumentResource(de.catma.ui.module.project.DocumentResource)

Example 38 with SourceDocument

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

the class CommentMessageListener method uiOnMessage.

@Override
public void uiOnMessage(Message<CommentMessage> message) {
    boolean autoShowcomments = (boolean) cbAutoShowComments.getData();
    if (!autoShowcomments) {
        return;
    }
    try {
        CommentMessage commentMessage = message.getMessageObject();
        final String documentId = commentMessage.getDocumentId();
        final boolean replyMessage = commentMessage.isReplyMessage();
        final int senderId = commentMessage.getSenderId();
        final boolean deleted = commentMessage.isDeleted();
        final SourceDocument document = documentSupplier.get();
        if ((document != null) && document.getUuid().equals(documentId)) {
            User user = project.getUser();
            Integer receiverId = user.getUserId();
            if (!receiverId.equals(senderId)) {
                final Comment comment = commentMessage.toComment();
                Optional<Comment> optionalExistingComment = this.comments.stream().filter(c -> c.getUuid().equals(comment.getUuid())).findFirst();
                if (replyMessage) {
                    if (optionalExistingComment.isPresent()) {
                        Comment existingComment = optionalExistingComment.get();
                        Reply reply = commentMessage.toReply();
                        Reply existingReply = existingComment.getReply(reply.getUuid());
                        if (existingReply != null) {
                            if (deleted) {
                                existingComment.removeReply(existingReply);
                                tagger.removeReply(existingComment, existingReply);
                            } else {
                                existingReply.setBody(reply.getBody());
                                tagger.updateReply(existingComment, existingReply);
                            }
                        } else {
                            existingComment.addReply(reply);
                            tagger.addReply(existingComment, reply);
                        }
                    }
                } else {
                    if (deleted) {
                        optionalExistingComment.ifPresent(existingComment -> {
                            this.comments.remove(existingComment);
                            tagger.removeComment(existingComment);
                        });
                    } else {
                        if (optionalExistingComment.isPresent()) {
                            Comment existingComment = optionalExistingComment.get();
                            existingComment.setBody(comment.getBody());
                            tagger.updateComment(existingComment);
                        } else {
                            comments.add(comment);
                            tagger.addComment(comment);
                        }
                    }
                }
                getUi().push();
            }
        }
    } catch (Exception e) {
        logger.log(Level.WARNING, "error processing an incoming Comment", e);
    }
}
Also used : Reply(de.catma.document.comment.Reply) Project(de.catma.project.Project) UI(com.vaadin.ui.UI) SourceDocument(de.catma.document.source.SourceDocument) Logger(java.util.logging.Logger) CommentMessage(de.catma.ui.events.CommentMessage) Supplier(java.util.function.Supplier) UIMessageListener(de.catma.ui.UIMessageListener) User(de.catma.user.User) Level(java.util.logging.Level) List(java.util.List) Comment(de.catma.document.comment.Comment) Message(com.hazelcast.core.Message) Optional(java.util.Optional) IconButton(de.catma.ui.component.IconButton) Comment(de.catma.document.comment.Comment) User(de.catma.user.User) CommentMessage(de.catma.ui.events.CommentMessage) SourceDocument(de.catma.document.source.SourceDocument) Reply(de.catma.document.comment.Reply)

Example 39 with SourceDocument

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

the class AnalyzeResourcePanel method handleCollectionChanged.

@SuppressWarnings("unchecked")
@Subscribe
public void handleCollectionChanged(CollectionChangeEvent collectionChangeEvent) {
    if (collectionChangeEvent.getChangeType().equals(ChangeType.CREATED)) {
        SourceDocument document = collectionChangeEvent.getDocument();
        AnnotationCollectionReference collectionReference = collectionChangeEvent.getCollectionReference();
        CollectionDataItem collectionDataItem = new CollectionDataItem(collectionReference, project.hasPermission(project.getRoleForCollection(collectionReference.getId()), RBACPermission.COLLECTION_WRITE));
        documentData.getRootItems().stream().filter(item -> ((DocumentDataItem) item).getDocument().equals(document)).findAny().ifPresent(documentDataItem -> {
            documentData.addItem(documentDataItem, collectionDataItem);
            documentTree.getDataProvider().refreshAll();
        });
        if (isAttached()) {
            documentTree.expand(documentData.getParent(collectionDataItem));
            Notification.show("Info", String.format("Collection %1$s has been created!", collectionReference.toString()), Type.TRAY_NOTIFICATION);
        }
    } else if (collectionChangeEvent.getChangeType().equals(ChangeType.DELETED)) {
        Optional<DocumentTreeItem> optionalDocResource = documentData.getRootItems().stream().filter(item -> ((DocumentDataItem) item).getDocument().equals(collectionChangeEvent.getDocument())).findAny();
        if (optionalDocResource.isPresent()) {
            Optional<DocumentTreeItem> optionalCollectionResource = documentData.getChildren(optionalDocResource.get()).stream().filter(item -> ((CollectionDataItem) item).getCollectionRef().equals(collectionChangeEvent.getCollectionReference())).findAny();
            if (optionalCollectionResource.isPresent()) {
                DocumentTreeItem collectionItem = optionalCollectionResource.get();
                documentData.removeItem(collectionItem);
                documentTree.getDataProvider().refreshAll();
                // selections needs manual update...
                ((SelectionModel.Multi<DocumentTreeItem>) documentTree.getSelectionModel()).updateSelection(Collections.emptySet(), Collections.singleton(collectionItem));
            }
            corpusChangedListener.corpusChanged();
        }
    } else {
        documentTree.getDataProvider().refreshAll();
        corpusChangedListener.corpusChanged();
    }
}
Also used : Optional(java.util.Optional) SourceDocument(de.catma.document.source.SourceDocument) AnnotationCollectionReference(de.catma.document.annotation.AnnotationCollectionReference) SelectionModel(com.vaadin.data.SelectionModel) Subscribe(com.google.common.eventbus.Subscribe)

Example 40 with SourceDocument

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

the class ProjectResourceExportApiRequestHandler method handleRequest.

@Override
public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException {
    String requestPath = request.getPathInfo().toLowerCase();
    if (requestPath.equals(handlerPath)) {
        response.setContentType("application/json");
        response.setNoCacheHeaders();
        OutputStream outputStream = response.getOutputStream();
        outputStream.write(serializeProjectResources().getBytes(StandardCharsets.UTF_8));
        return true;
    } else if (requestPath.startsWith(handlerPath + "/doc/")) {
        try {
            String[] requestPathParts = requestPath.split("/");
            String documentUuid = requestPathParts[requestPathParts.length - 1];
            SourceDocument sourceDocument = project.getSourceDocument(documentUuid.toUpperCase());
            response.setContentType("text/plain; charset=UTF-8");
            OutputStream outputStream = response.getOutputStream();
            outputStream.write(sourceDocument.getContent().getBytes(StandardCharsets.UTF_8));
        } catch (UncheckedExecutionException e) {
            if (e.getCause() instanceof NoSuchElementException) {
                response.setStatus(404);
            } else {
                throw e;
            }
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Error handling document request", e);
            response.setStatus(500);
        }
        return true;
    }
    return false;
}
Also used : UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) OutputStream(java.io.OutputStream) PreApiSourceDocument(de.catma.api.pre.serialization.model_wrappers.PreApiSourceDocument) SourceDocument(de.catma.document.source.SourceDocument) NoSuchElementException(java.util.NoSuchElementException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException)

Aggregations

SourceDocument (de.catma.document.source.SourceDocument)48 AnnotationCollectionReference (de.catma.document.annotation.AnnotationCollectionReference)23 Project (de.catma.project.Project)17 AnnotationCollection (de.catma.document.annotation.AnnotationCollection)15 List (java.util.List)15 TagsetDefinition (de.catma.tag.TagsetDefinition)13 IOException (java.io.IOException)13 Collectors (java.util.stream.Collectors)13 UI (com.vaadin.ui.UI)12 IDGenerator (de.catma.util.IDGenerator)12 HashSet (java.util.HashSet)12 ErrorHandler (de.catma.ui.module.main.ErrorHandler)11 EventBus (com.google.common.eventbus.EventBus)10 Subscribe (com.google.common.eventbus.Subscribe)10 CollectionChangeEvent (de.catma.project.event.CollectionChangeEvent)10 TreeDataProvider (com.vaadin.data.provider.TreeDataProvider)9 ChangeType (de.catma.project.event.ChangeType)9 RBACPermission (de.catma.rbac.RBACPermission)9 Pair (de.catma.util.Pair)9 Indexer (de.catma.indexer.Indexer)8