Search in sources :

Example 1 with KwicProvider

use of de.catma.indexer.KwicProvider in project catma by forTEXT.

the class CSVExportGroupedStreamSource method getStream.

@Override
public InputStream getStream() {
    final QueryResult queryResult = queryResultSupplier.get();
    final Set<String> documentIds = new TreeSet<String>();
    final Multimap<String, String> collectionIdByDocumentId = ArrayListMultimap.create();
    final Table<String, String, Integer> groupings = HashBasedTable.create();
    for (QueryResultRow row : queryResult) {
        String group = row.getPhrase();
        if (groupByTagSupplier.get()) {
            if (row instanceof TagQueryResultRow) {
                group = ((TagQueryResultRow) row).getTagDefinitionPath();
            } else {
                group = TagQueryResultRowItem.getNoTagAvailableKey();
            }
        }
        groupings.put(group, "Total", getValue(groupings, group, "Total") + 1);
        groupings.put(group, row.getSourceDocumentId(), getValue(groupings, group, row.getSourceDocumentId()) + 1);
        documentIds.add(row.getSourceDocumentId());
        if (row instanceof TagQueryResultRow) {
            collectionIdByDocumentId.put(row.getSourceDocumentId(), ((TagQueryResultRow) row).getMarkupCollectionId());
            groupings.put(group, ((TagQueryResultRow) row).getMarkupCollectionId(), getValue(groupings, group, ((TagQueryResultRow) row).getMarkupCollectionId()) + 1);
        }
    }
    final PipedInputStream in = new PipedInputStream();
    final UI ui = UI.getCurrent();
    backgroundServiceProvider.submit("csv-export", new DefaultProgressCallable<Void>() {

        @Override
        public Void call() throws Exception {
            PipedOutputStream out = new PipedOutputStream(in);
            OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
            ArrayList<String> header = new ArrayList<>();
            header.add("Group");
            header.add("Total");
            for (String documentId : documentIds) {
                KwicProvider kwicProvider = kwicProviderCache.get(documentId);
                header.add(kwicProvider.getSourceDocumentName() + " (" + documentId + ")");
                for (String collectionId : new TreeSet<String>(collectionIdByDocumentId.get(documentId))) {
                    header.add(kwicProvider.getSourceDocument().getUserMarkupCollectionReference(collectionId).toString() + " (" + collectionId + ")");
                }
            }
            try (CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.EXCEL.withDelimiter(';').withHeader(header.toArray(new String[] {})))) {
                for (String group : new TreeSet<String>(groupings.rowKeySet())) {
                    csvPrinter.print(group);
                    csvPrinter.print(groupings.get(group, "Total"));
                    for (String documentId : documentIds) {
                        csvPrinter.print(groupings.get(group, documentId));
                        for (String collectionId : new TreeSet<String>(collectionIdByDocumentId.get(documentId))) {
                            csvPrinter.print(groupings.get(group, collectionId));
                        }
                    }
                    csvPrinter.println();
                }
                csvPrinter.flush();
            }
            // intended
            return null;
        }
    }, new ExecutionListener<Void>() {

        @Override
        public void done(Void result) {
        // noop
        }

        @Override
        public void error(Throwable t) {
            ((ErrorHandler) ui).showAndLogError("Error export data to CSV!", t);
        }
    });
    // waiting on the background thread to send data to the pipe
    int tries = 100;
    try {
        while (!(in.available() > 0) && tries > 0) {
            Thread.sleep(10);
            tries--;
        }
    } catch (IOException | InterruptedException e) {
        Logger.getLogger(getClass().getName()).log(Level.WARNING, "Error while waiting on CSV export!", e);
    }
    return in;
}
Also used : TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) QueryResultRow(de.catma.queryengine.result.QueryResultRow) ArrayList(java.util.ArrayList) PipedOutputStream(java.io.PipedOutputStream) CSVPrinter(org.apache.commons.csv.CSVPrinter) QueryResult(de.catma.queryengine.result.QueryResult) UI(com.vaadin.ui.UI) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) TreeSet(java.util.TreeSet) KwicProvider(de.catma.indexer.KwicProvider) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) IOException(java.io.IOException) OutputStreamWriter(java.io.OutputStreamWriter)

Example 2 with KwicProvider

use of de.catma.indexer.KwicProvider in project catma by forTEXT.

the class CollectionQueryResultRowItem method addChildRowItems.

@Override
public void addChildRowItems(TreeData<QueryResultRowItem> treeData, LoadingCache<String, KwicProvider> kwicProviderCache) {
    try {
        for (QueryResultRow row : getRows()) {
            if (row instanceof TagQueryResultRow) {
                TagQueryResultRow tRow = (TagQueryResultRow) row;
                KwicProvider kwicProvider = kwicProviderCache.get(row.getSourceDocumentId());
                TagDefinition tagDefinition = project.getTagManager().getTagLibrary().getTagDefinition(tRow.getTagDefinitionId());
                KwicQueryResultRowItem item = new KwicQueryResultRowItem(tRow, AnnotatedTextProvider.buildAnnotatedText(new ArrayList<>(tRow.getRanges()), kwicProvider, tagDefinition), AnnotatedTextProvider.buildAnnotatedKeywordInContext(new ArrayList<>(tRow.getRanges()), kwicProvider, tagDefinition, tRow.getTagDefinitionPath()), true);
                if (!treeData.contains(item)) {
                    treeData.addItem(this, item);
                }
            }
        }
    } catch (Exception e) {
        ((ErrorHandler) UI.getCurrent()).showAndLogError("error displaying annotated kwic query results", e);
    }
}
Also used : TagDefinition(de.catma.tag.TagDefinition) QueryResultRow(de.catma.queryengine.result.QueryResultRow) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) KwicProvider(de.catma.indexer.KwicProvider) ArrayList(java.util.ArrayList)

Example 3 with KwicProvider

use of de.catma.indexer.KwicProvider in project catma by forTEXT.

the class ConflictedProjectView method showNextAnnotationConflict.

private void showNextAnnotationConflict() throws IOException {
    if (annotationConflictIterator.hasNext()) {
        SourceDocument document = documents.stream().filter(doc -> doc.getUuid().equals(currentCollectionConflict.getDocumentId())).findFirst().get();
        KwicProvider kwicProvider = new KwicProvider(document);
        AnnotationConflict annotationConflict = annotationConflictIterator.next();
        AnnotationConflictView annotationConflictView = new AnnotationConflictView(annotationConflict, currentCollectionConflict, tagManager, kwicProvider, () -> showNextConflict());
        mainPanel.removeAllComponents();
        mainPanel.addComponent(annotationConflictView);
    } else {
        showNextConflict();
    }
}
Also used : SourceDocument(de.catma.document.source.SourceDocument) KwicProvider(de.catma.indexer.KwicProvider)

Example 4 with KwicProvider

use of de.catma.indexer.KwicProvider in project catma by forTEXT.

the class AnnotationDetailsPanel method setDocument.

public void setDocument(SourceDocument document) throws IOException {
    this.kwicProvider = new KwicProvider(document);
    handleClearSelected();
}
Also used : KwicProvider(de.catma.indexer.KwicProvider)

Example 5 with KwicProvider

use of de.catma.indexer.KwicProvider in project catma by forTEXT.

the class QueryResultPanel method addFlatTagBasedRootItems.

void addFlatTagBasedRootItems(QueryResult result) {
    try {
        for (QueryResultRow row : result) {
            if (row instanceof TagQueryResultRow) {
                if (((TagQueryResultRow) row).getPropertyDefinitionId() != null) {
                    resultContainsProperties = true;
                }
                TagQueryResultRow tRow = (TagQueryResultRow) row;
                KwicProvider kwicProvider = kwicProviderCache.get(row.getSourceDocumentId());
                TagDefinition tagDefinition = project.getTagManager().getTagLibrary().getTagDefinition(tRow.getTagDefinitionId());
                KwicQueryResultRowItem item = new KwicQueryResultRowItem(tRow, AnnotatedTextProvider.buildAnnotatedText(new ArrayList<>(tRow.getRanges()), kwicProvider, tagDefinition), AnnotatedTextProvider.buildAnnotatedKeywordInContext(new ArrayList<>(tRow.getRanges()), kwicProvider, tagDefinition, tRow.getTagDefinitionPath()), kwicProvider.getSourceDocumentName(), kwicProvider.getSourceDocument().getUserMarkupCollectionReference(tRow.getMarkupCollectionId()).getName(), true);
                if (!flatTagBasedTreeData.contains(item)) {
                    flatTagBasedTreeData.addItem(null, item);
                }
            }
        }
        tokenCount = flatTagBasedTreeData.getRootItems().size();
    } catch (Exception e) {
        ((ErrorHandler) UI.getCurrent()).showAndLogError("error adding query result", e);
    }
}
Also used : TagDefinition(de.catma.tag.TagDefinition) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) QueryResultRow(de.catma.queryengine.result.QueryResultRow) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) KwicProvider(de.catma.indexer.KwicProvider) ArrayList(java.util.ArrayList)

Aggregations

KwicProvider (de.catma.indexer.KwicProvider)8 QueryResultRow (de.catma.queryengine.result.QueryResultRow)6 TagQueryResultRow (de.catma.queryengine.result.TagQueryResultRow)6 ArrayList (java.util.ArrayList)4 TagDefinition (de.catma.tag.TagDefinition)3 UI (com.vaadin.ui.UI)2 KeywordInSpanContext (de.catma.indexer.KeywordInSpanContext)2 QueryResult (de.catma.queryengine.result.QueryResult)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2 PipedInputStream (java.io.PipedInputStream)2 PipedOutputStream (java.io.PipedOutputStream)2 TreeSet (java.util.TreeSet)2 CSVPrinter (org.apache.commons.csv.CSVPrinter)2 CacheLoader (com.google.common.cache.CacheLoader)1 LoadingCache (com.google.common.cache.LoadingCache)1 Range (de.catma.document.Range)1 SourceDocument (de.catma.document.source.SourceDocument)1 QueryResultRowArray (de.catma.queryengine.result.QueryResultRowArray)1 HashMap (java.util.HashMap)1