Search in sources :

Example 6 with KwicProvider

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

the class QueryResultPanel method addPropertiesAsColumnsTagBasedRootItems.

void addPropertiesAsColumnsTagBasedRootItems(QueryResult result) {
    try {
        HashMap<String, QueryResultRowArray> rowsGroupedByTagInstance = new HashMap<String, QueryResultRowArray>();
        for (QueryResultRow row : result) {
            if (row instanceof TagQueryResultRow) {
                TagQueryResultRow tRow = (TagQueryResultRow) row;
                QueryResultRowArray rows = rowsGroupedByTagInstance.get(tRow.getTagInstanceId());
                if (rows == null) {
                    rows = new QueryResultRowArray();
                    rowsGroupedByTagInstance.put(tRow.getTagInstanceId(), rows);
                }
                rows.add(tRow);
                if (tRow.getPropertyName() != null) {
                    propertyNames.add(tRow.getPropertyName());
                }
            }
        }
        for (Map.Entry<String, QueryResultRowArray> entry : rowsGroupedByTagInstance.entrySet()) {
            QueryResultRowArray rows = entry.getValue();
            TagQueryResultRow masterRow = (TagQueryResultRow) rows.get(0);
            KwicProvider kwicProvider = kwicProviderCache.get(masterRow.getSourceDocumentId());
            TagDefinition tagDefinition = project.getTagManager().getTagLibrary().getTagDefinition(masterRow.getTagDefinitionId());
            KwicPropertiesAsColumnsQueryResultRowItem item = new KwicPropertiesAsColumnsQueryResultRowItem(rows, AnnotatedTextProvider.buildAnnotatedText(new ArrayList<>(masterRow.getRanges()), kwicProvider, tagDefinition), AnnotatedTextProvider.buildAnnotatedKeywordInContext(new ArrayList<>(masterRow.getRanges()), kwicProvider, tagDefinition, masterRow.getTagDefinitionPath()), kwicProvider.getSourceDocumentName(), kwicProvider.getSourceDocument().getUserMarkupCollectionReference(masterRow.getMarkupCollectionId()).getName());
            if (!propertiesAsColumnsTagBasedTreeData.contains(item)) {
                propertiesAsColumnsTagBasedTreeData.addItem(null, item);
            }
        }
        tokenCount = propertiesAsColumnsTagBasedTreeData.getRootItems().size();
    } catch (Exception e) {
        ((ErrorHandler) UI.getCurrent()).showAndLogError("error adding query result", e);
    }
}
Also used : TagDefinition(de.catma.tag.TagDefinition) HashMap(java.util.HashMap) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) QueryResultRow(de.catma.queryengine.result.QueryResultRow) ArrayList(java.util.ArrayList) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) KwicProvider(de.catma.indexer.KwicProvider) QueryResultRowArray(de.catma.queryengine.result.QueryResultRowArray) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with KwicProvider

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

the class DoubleTreePanel method setSelectedQueryResultRows.

@Override
public void setSelectedQueryResultRows(Iterable<QueryResultRow> selectedRows) {
    kwics.clear();
    if ((selectedRows.iterator().next() instanceof TagQueryResultRow) && displaySettings.equals(DisplaySetting.GROUPED_BY_TAG)) {
        for (QueryResultRow row : selectedRows) {
            TagQueryResultRow tqrr = (TagQueryResultRow) row;
            String tagPath = tqrr.getTagDefinitionPath().replace("/", "");
            KwicProvider kwicProvider = null;
            try {
                kwicProvider = kwicProviderCache.get(row.getSourceDocumentId());
            } catch (ExecutionException e1) {
                ((CatmaApplication) UI.getCurrent()).showAndLogError("Error visualizing group by tag", e1);
            }
            KeywordInSpanContext kwic = null;
            try {
                kwic = kwicProvider.getKwic(row.getRange(), contextSize);
                KeywordInSpanContext newKwic = new KeywordInSpanContext(tagPath, kwic.getKwic(), kwic.getKwicSourceRange(), kwic.getRelativeKeywordStartPos(), kwic.isRightToLeft(), kwic.getSpanContext());
                kwics.add(newKwic);
            } catch (IOException e) {
                ((CatmaApplication) UI.getCurrent()).showAndLogError("Error visualizing group by tag", e);
            }
        }
        doubleTree.setupFromArrays(kwics, true);
    } else {
        for (QueryResultRow row : selectedRows) {
            KwicProvider kwicProvider = null;
            try {
                kwicProvider = kwicProviderCache.get(row.getSourceDocumentId());
            } catch (ExecutionException e1) {
                ((CatmaApplication) UI.getCurrent()).showAndLogError("Error visualizing selected data", e1);
            }
            KeywordInSpanContext kwic = null;
            try {
                kwic = kwicProvider.getKwic(row.getRange(), contextSize);
            } catch (IOException e) {
                ((CatmaApplication) UI.getCurrent()).showAndLogError("Error visualizing selected data", e);
            }
            kwics.add(kwic);
        }
        doubleTree.setupFromArrays(kwics, true);
    }
}
Also used : TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) QueryResultRow(de.catma.queryengine.result.QueryResultRow) KwicProvider(de.catma.indexer.KwicProvider) KeywordInSpanContext(de.catma.indexer.KeywordInSpanContext) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 8 with KwicProvider

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

the class CSVExportFlatStreamSource method getStream.

@Override
public InputStream getStream() {
    final QueryResult queryResult = queryResultSupplier.get();
    final PipedInputStream in = new PipedInputStream();
    final UI ui = UI.getCurrent();
    final Lock lock = new ReentrantLock();
    final Condition sending = lock.newCondition();
    lock.lock();
    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");
            LoadingCache<String, String> colorCache = CacheBuilder.newBuilder().build(new CacheLoader<String, String>() {

                @Override
                public String load(String tagDefinitionId) throws Exception {
                    return "#" + ColorConverter.toHex(project.getTagManager().getTagLibrary().getTagDefinition(tagDefinitionId).getColor());
                }
            });
            try (CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.EXCEL.withDelimiter(';'))) {
                for (QueryResultRow row : queryResult) {
                    KwicProvider kwicProvider = kwicProviderCache.get(row.getSourceDocumentId());
                    if (row instanceof TagQueryResultRow) {
                        TagQueryResultRow tRow = (TagQueryResultRow) row;
                        List<Range> mergedRanges = Range.mergeRanges(new TreeSet<>((tRow).getRanges()));
                        for (Range range : mergedRanges) {
                            KeywordInSpanContext kwic = kwicProvider.getKwic(range, 5);
                            csvPrinter.printRecord(row.getQueryId().toSerializedString(), row.getSourceDocumentId(), kwicProvider.getSourceDocumentName(), kwicProvider.getDocumentLength(), kwic.getKeyword(), kwic.toString(), range.getStartPoint(), range.getEndPoint(), tRow.getMarkupCollectionId(), kwicProvider.getSourceDocument().getUserMarkupCollectionReference(tRow.getMarkupCollectionId()).toString(), tRow.getTagDefinitionPath(), tRow.getTagDefinitionVersion(), colorCache.get(tRow.getTagDefinitionId()), tRow.getTagInstanceId(), tRow.getPropertyDefinitionId(), tRow.getPropertyName(), tRow.getPropertyValue());
                        }
                    } else {
                        KeywordInSpanContext kwic = kwicProvider.getKwic(row.getRange(), 5);
                        csvPrinter.printRecord(row.getQueryId().toSerializedString(), row.getSourceDocumentId(), kwicProvider.getSourceDocumentName(), kwicProvider.getDocumentLength(), kwic.getKeyword(), kwic.toString(), row.getRange().getStartPoint(), row.getRange().getEndPoint());
                    }
                    csvPrinter.flush();
                    lock.lock();
                    try {
                        sending.signal();
                    } finally {
                        lock.unlock();
                    }
                }
            }
            // 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
    try {
        try {
            sending.await(10, TimeUnit.SECONDS);
        } catch (InterruptedException e1) {
            Logger.getLogger(getClass().getName()).log(Level.WARNING, "Error while waiting on CSV export!", e1);
        }
    } finally {
        lock.unlock();
    }
    return in;
}
Also used : TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) QueryResultRow(de.catma.queryengine.result.QueryResultRow) 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) LoadingCache(com.google.common.cache.LoadingCache) List(java.util.List) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) PipedInputStream(java.io.PipedInputStream) KeywordInSpanContext(de.catma.indexer.KeywordInSpanContext) Range(de.catma.document.Range) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) OutputStreamWriter(java.io.OutputStreamWriter) CacheLoader(com.google.common.cache.CacheLoader)

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