Search in sources :

Example 1 with ApiException

use of org.corpus_tools.annis.ApiException in project ANNIS by korpling.

the class Helper method getCorpusConfig.

/**
 * Loads the corpus config of a specific corpus.
 *
 * @param corpus The name of the corpus, for which the config is fetched.
 * @return A {@link CorpusConfig} object, which wraps a {@link Properties} object. This Properties
 *         object stores the corpus configuration as simple key-value pairs.
 */
public static CorpusConfiguration getCorpusConfig(final String corpus, final UI ui) {
    if (corpus == null || corpus.isEmpty()) {
        Notification.show("no corpus is selected", "please select at least one corpus and execute query again", Notification.Type.WARNING_MESSAGE);
        return null;
    }
    CorpusConfiguration corpusConfig = new CorpusConfiguration();
    final CorporaApi api = new CorporaApi(getClient(ui));
    try {
        corpusConfig = api.corpusConfiguration(corpus);
    } catch (final ApiException ex) {
        ui.access(() -> ExceptionDialog.show(ex, ERROR_MESSAGE_CORPUS_PROPS_HEADER, ui));
    }
    return corpusConfig;
}
Also used : CorpusConfiguration(org.corpus_tools.annis.api.model.CorpusConfiguration) CorporaApi(org.corpus_tools.annis.api.CorporaApi) ApiException(org.corpus_tools.annis.ApiException)

Example 2 with ApiException

use of org.corpus_tools.annis.ApiException in project ANNIS by korpling.

the class Helper method getMetaDataDoc.

/**
 * Retrieve the meta data for a given document of a corpus.
 *
 * @param toplevelCorpusName specifies the toplevel corpus
 * @param documentName specifies the document.
 * @return returns only the meta data for a single document.
 */
public static List<SMetaAnnotation> getMetaDataDoc(final String toplevelCorpusName, final String documentName, final UI ui) {
    final List<SMetaAnnotation> result = new ArrayList<>();
    final SearchApi api = new SearchApi(Helper.getClient(ui));
    try {
        // Get the corpus graph and with it the meta data on the corpus/document nodes
        final File graphML = api.subgraphForQuery(toplevelCorpusName, "annis:node_type=\"corpus\" _ident_ annis:doc=/" + AQL_REGEX_VALUE_ESCAPER.escape(documentName) + "/", QueryLanguage.AQL, AnnotationComponentType.PARTOF);
        final SCorpusGraph cg = CorpusGraphMapper.map(graphML);
        for (final SNode n : cg.getNodes()) {
            result.addAll(n.getMetaAnnotations());
        }
    } catch (ApiException | XMLStreamException | IOException ex) {
        ui.access(() -> ExceptionDialog.show(ex, "Could not retrieve metadata for document", ui));
    }
    return result;
}
Also used : SNode(org.corpus_tools.salt.core.SNode) SMetaAnnotation(org.corpus_tools.salt.core.SMetaAnnotation) XMLStreamException(javax.xml.stream.XMLStreamException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SearchApi(org.corpus_tools.annis.api.SearchApi) File(java.io.File) SCorpusGraph(org.corpus_tools.salt.common.SCorpusGraph) ApiException(org.corpus_tools.annis.ApiException)

Example 3 with ApiException

use of org.corpus_tools.annis.ApiException in project ANNIS by korpling.

the class Helper method getMetaAnnotationNames.

public static Set<AnnoKey> getMetaAnnotationNames(final String corpus, final UI ui) throws ApiException {
    final CorporaApi api = new CorporaApi(getClient(ui));
    final SearchApi search = new SearchApi(getClient(ui));
    final List<org.corpus_tools.annis.api.model.Annotation> nodeAnnos = api.nodeAnnotations(corpus, false, true).stream().filter(a -> !Objects.equals(a.getKey().getNs(), "annis") && !Objects.equals(a.getKey().getName(), "tok")).collect(Collectors.toList());
    final Set<AnnoKey> metaAnnos = new HashSet<>();
    // Check for each annotation if its actually a meta-annotation
    for (final org.corpus_tools.annis.api.model.Annotation a : nodeAnnos) {
        String annotationName = getQName(a.getKey());
        if (annotationIsMetadata(corpus, annotationName, search)) {
            metaAnnos.add(a.getKey());
        }
    }
    return metaAnnos;
}
Also used : Arrays(java.util.Arrays) SDominanceRelation(org.corpus_tools.salt.common.SDominanceRelation) SGraph(org.corpus_tools.salt.core.SGraph) StringUtils(org.apache.commons.lang3.StringUtils) Base64(org.apache.commons.codec.binary.Base64) InetAddress(java.net.InetAddress) JsonValue(elemental.json.JsonValue) DataSourceSequence(org.corpus_tools.salt.util.DataSourceSequence) AnnoKey(org.corpus_tools.annis.api.model.AnnoKey) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) CorpusConfigurationContext(org.corpus_tools.annis.api.model.CorpusConfigurationContext) STextualRelation(org.corpus_tools.salt.common.STextualRelation) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) SRelation(org.corpus_tools.salt.core.SRelation) SSpan(org.corpus_tools.salt.common.SSpan) Range(com.google.common.collect.Range) Set(java.util.Set) ApiClient(org.corpus_tools.annis.ApiClient) FEAT_MATCHEDNODE(org.corpus_tools.annis.gui.objects.AnnisConstants.FEAT_MATCHEDNODE) ComparisonChain(com.google.common.collect.ComparisonChain) StandardCharsets(java.nio.charset.StandardCharsets) AnnisConstants(org.corpus_tools.annis.gui.objects.AnnisConstants) Stream(java.util.stream.Stream) SALT_TYPE(org.corpus_tools.salt.SALT_TYPE) SDocumentGraph(org.corpus_tools.salt.common.SDocumentGraph) OAuth2User(org.springframework.security.oauth2.core.user.OAuth2User) SCorpusGraph(org.corpus_tools.salt.common.SCorpusGraph) SAnnotation(org.corpus_tools.salt.core.SAnnotation) Joiner(com.google.common.base.Joiner) QueryLanguage(org.corpus_tools.annis.api.model.QueryLanguage) CorpusConfiguration(org.corpus_tools.annis.api.model.CorpusConfiguration) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) LinkedHashSet(java.util.LinkedHashSet) Properties(java.util.Properties) Files(java.nio.file.Files) GraphTraverseHandler(org.corpus_tools.salt.core.GraphTraverseHandler) SaltProject(org.corpus_tools.salt.common.SaltProject) IOException(java.io.IOException) MultiPartEmail(org.apache.commons.mail.MultiPartEmail) UnknownHostException(java.net.UnknownHostException) File(java.io.File) ANNIS_NS(org.corpus_tools.annis.gui.objects.AnnisConstants.ANNIS_NS) SCorpus(org.corpus_tools.salt.common.SCorpus) TreeMap(java.util.TreeMap) ApiException(org.corpus_tools.annis.ApiException) CorpusGraphMapper(org.corpus_tools.annis.gui.graphml.CorpusGraphMapper) CorpusConfigurationView(org.corpus_tools.annis.api.model.CorpusConfigurationView) AnnotationComponentType(org.corpus_tools.annis.api.model.AnnotationComponentType) STextualDS(org.corpus_tools.salt.common.STextualDS) JsonCodec(com.vaadin.server.JsonCodec) URLDecoder(java.net.URLDecoder) URISyntaxException(java.net.URISyntaxException) UI(com.vaadin.ui.UI) LoggerFactory(org.slf4j.LoggerFactory) ExceptionDialog(org.corpus_tools.annis.gui.components.ExceptionDialog) Match(org.corpus_tools.annis.gui.objects.Match) OrderEnum(org.corpus_tools.annis.api.model.FindQuery.OrderEnum) URI(java.net.URI) CorporaApi(org.corpus_tools.annis.api.CorporaApi) Escaper(com.google.common.escape.Escaper) Collection(java.util.Collection) SAnnotationContainer(org.corpus_tools.salt.core.SAnnotationContainer) Collectors(java.util.stream.Collectors) Label(org.corpus_tools.salt.graph.Label) Objects(java.util.Objects) List(java.util.List) SecurityContext(org.springframework.security.core.context.SecurityContext) Component(org.corpus_tools.annis.api.model.Component) SaltFactory(org.corpus_tools.salt.SaltFactory) EmailException(org.apache.commons.mail.EmailException) Optional(java.util.Optional) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) Pattern(java.util.regex.Pattern) Authentication(org.springframework.security.core.Authentication) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SToken(org.corpus_tools.salt.common.SToken) SLayer(org.corpus_tools.salt.core.SLayer) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) SNode(org.corpus_tools.salt.core.SNode) SMetaAnnotation(org.corpus_tools.salt.core.SMetaAnnotation) SSpanningRelation(org.corpus_tools.salt.common.SSpanningRelation) HashSet(java.util.HashSet) FindQuery(org.corpus_tools.annis.api.model.FindQuery) GRAPH_TRAVERSE_TYPE(org.corpus_tools.salt.core.SGraph.GRAPH_TRAVERSE_TYPE) SOrderRelation(org.corpus_tools.salt.common.SOrderRelation) Notification(com.vaadin.ui.Notification) Escapers(com.google.common.escape.Escapers) LinkedList(java.util.LinkedList) SecurityConfiguration(org.corpus_tools.annis.gui.security.SecurityConfiguration) SDocument(org.corpus_tools.salt.common.SDocument) SearchApi(org.corpus_tools.annis.api.SearchApi) StandardClaimNames(org.springframework.security.oauth2.core.oidc.StandardClaimNames) SFeature(org.corpus_tools.salt.core.SFeature) Comparator(java.util.Comparator) CorporaApi(org.corpus_tools.annis.api.CorporaApi) SAnnotation(org.corpus_tools.salt.core.SAnnotation) SMetaAnnotation(org.corpus_tools.salt.core.SMetaAnnotation) AnnoKey(org.corpus_tools.annis.api.model.AnnoKey) SearchApi(org.corpus_tools.annis.api.SearchApi) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 4 with ApiException

use of org.corpus_tools.annis.ApiException in project ANNIS by korpling.

the class QueryController method executeSearch.

/**
 * Executes a query.
 *
 * @param replaceOldTab
 * @param freshQuery If true the offset and the selected matches are reset before executing the
 *        query.
 */
public void executeSearch(boolean replaceOldTab, boolean freshQuery) {
    UI ui = UI.getCurrent();
    if (freshQuery && ui instanceof AnnisUI) {
        getState().getOffset().setValue(0l);
        getState().getSelectedMatches().setValue(new TreeSet<Long>());
        // get the value for the visible segmentation from the configured context
        Collection<String> selectedCorpora = getState().getSelectedCorpora();
        CorpusConfiguration config = new CorpusConfiguration();
        if (selectedCorpora != null && !selectedCorpora.isEmpty()) {
            config = ((AnnisUI) ui).getCorpusConfigWithCache(selectedCorpora.iterator().next());
        }
        if (config.getView() != null) {
            String configVal = config.getView().getBaseTextSegmentation();
            if ("".equals(configVal) || "tok".equals(configVal)) {
                configVal = null;
            }
            getState().getVisibleBaseText().setValue(configVal);
        }
    }
    // construct a query from the current properties
    DisplayedResultQuery displayedQuery = getSearchQuery();
    searchView.getControlPanel().getQueryPanel().setStatus("Searching...");
    cancelSearch();
    // cleanup resources
    VaadinSession session = VaadinSession.getCurrent();
    session.setAttribute(IFrameResourceMap.class, new IFrameResourceMap());
    if (session.getAttribute(MediaController.class) != null) {
        session.getAttribute(MediaController.class).clearMediaPlayers();
    }
    searchView.updateFragment(displayedQuery);
    if (displayedQuery.getCorpora() == null || displayedQuery.getCorpora().isEmpty()) {
        searchView.getControlPanel().getQueryPanel().setStatus("No corpus selected");
        searchView.getControlPanel().getQueryPanel().setCountIndicatorEnabled(false);
        Notification.show("Please select a corpus", Notification.Type.WARNING_MESSAGE);
        return;
    }
    if ("".equals(displayedQuery.getQuery())) {
        searchView.getControlPanel().getQueryPanel().setStatus("Empty query");
        searchView.getControlPanel().getQueryPanel().setCountIndicatorEnabled(false);
        Notification.show("Empty query", Notification.Type.WARNING_MESSAGE);
        return;
    }
    checkQuirksMode(displayedQuery);
    addHistoryEntry(displayedQuery);
    // 
    // begin execute match fetching
    // 
    ResultViewPanel oldPanel = searchView.getLastSelectedResultView();
    if (replaceOldTab) {
        // remove old panel from view
        searchView.closeTab(oldPanel);
    }
    if (ui instanceof AnnisUI) {
        AnnisUI annisUI = (AnnisUI) ui;
        ResultViewPanel newResultView = new ResultViewPanel(annisUI, displayedQuery);
        newResultView.getPaging().addCallback(new SpecificPagingCallback(annisUI, searchView, newResultView, displayedQuery));
        TabSheet.Tab newTab;
        List<ResultViewPanel> existingResultPanels = getResultPanels();
        String caption = existingResultPanels.isEmpty() ? "Query Result" : "Query Result #" + (existingResultPanels.size() + 1);
        newTab = searchView.getMainTab().addTab(newResultView, caption);
        newTab.setClosable(true);
        newTab.setIcon(FontAwesome.SEARCH);
        searchView.getMainTab().setSelectedTab(newResultView);
        searchView.notifiyQueryStarted();
        Background.run(new ResultFetchJob(displayedQuery, newResultView, annisUI));
        // 
        // end execute match fetching
        // 
        // 
        // begin execute count
        // 
        // start count query
        searchView.getControlPanel().getQueryPanel().setCountIndicatorEnabled(true);
        SearchApi api = new SearchApi(Helper.getClient(ui));
        CountQuery countQuery = new CountQuery();
        countQuery.setCorpora(new LinkedList<>(displayedQuery.getCorpora()));
        countQuery.setQuery(displayedQuery.getQuery());
        countQuery.setQueryLanguage(displayedQuery.getApiQueryLanguage());
        try {
            Call call = api.countAsync(countQuery, new CountCallback(newResultView, displayedQuery.getLimit(), annisUI));
            state.getExecutedCalls().put(QueryUIState.QueryType.COUNT, call);
        } catch (ApiException ex) {
            ExceptionDialog.show(ex, ui);
        }
    }
}
Also used : Call(okhttp3.Call) MediaController(org.corpus_tools.annis.gui.media.MediaController) CountQuery(org.corpus_tools.annis.api.model.CountQuery) CorpusConfiguration(org.corpus_tools.annis.api.model.CorpusConfiguration) VaadinSession(com.vaadin.server.VaadinSession) ResultViewPanel(org.corpus_tools.annis.gui.resultview.ResultViewPanel) SingleResultFetchJob(org.corpus_tools.annis.gui.resultfetch.SingleResultFetchJob) ResultFetchJob(org.corpus_tools.annis.gui.resultfetch.ResultFetchJob) CountCallback(org.corpus_tools.annis.gui.controller.CountCallback) UI(com.vaadin.ui.UI) TabSheet(com.vaadin.ui.TabSheet) IFrameResourceMap(org.corpus_tools.annis.gui.visualizers.IFrameResourceMap) DisplayedResultQuery(org.corpus_tools.annis.gui.objects.DisplayedResultQuery) SpecificPagingCallback(org.corpus_tools.annis.gui.controller.SpecificPagingCallback) SearchApi(org.corpus_tools.annis.api.SearchApi) ApiException(org.corpus_tools.annis.ApiException)

Example 5 with ApiException

use of org.corpus_tools.annis.ApiException in project ANNIS by korpling.

the class QueryController method validateQuery.

public void validateQuery() {
    QueryPanel qp = searchView.getControlPanel().getQueryPanel();
    // reset status
    qp.setError(null);
    qp.setNodes(null);
    String query = state.getAql().getValue();
    if (query == null || query.isEmpty()) {
        qp.setStatus("Empty query");
    } else {
        // validate query
        UI ui = UI.getCurrent();
        Background.runWithCallback(() -> {
            SearchApi api = new SearchApi(Helper.getClient(ui));
            return api.nodeDescriptions(query, org.corpus_tools.annis.api.model.QueryLanguage.AQL);
        }, new FutureCallback<List<QueryAttributeDescription>>() {

            @Override
            public void onSuccess(List<QueryAttributeDescription> nodes) {
                qp.setNodes(nodes);
                if (state.getSelectedCorpora() == null || state.getSelectedCorpora().isEmpty()) {
                    qp.setStatus("Please select a corpus from the list below, then click on \"Search\".");
                } else {
                    qp.setStatus("Valid query, click on \"Search\" to start searching.");
                }
            }

            @Override
            public void onFailure(Throwable t) {
                if (t instanceof ApiException) {
                    reportServiceException((ApiException) t, false);
                }
            }
        });
    }
}
Also used : QueryAttributeDescription(org.corpus_tools.annis.api.model.QueryAttributeDescription) UI(com.vaadin.ui.UI) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) QueryPanel(org.corpus_tools.annis.gui.controlpanel.QueryPanel) FrequencyQueryPanel(org.corpus_tools.annis.gui.frequency.FrequencyQueryPanel) SearchApi(org.corpus_tools.annis.api.SearchApi) ApiException(org.corpus_tools.annis.ApiException)

Aggregations

ApiException (org.corpus_tools.annis.ApiException)41 CorporaApi (org.corpus_tools.annis.api.CorporaApi)26 File (java.io.File)15 IOException (java.io.IOException)13 ArrayList (java.util.ArrayList)9 SearchApi (org.corpus_tools.annis.api.SearchApi)9 TreeSet (java.util.TreeSet)8 LinkedList (java.util.LinkedList)7 XMLStreamException (javax.xml.stream.XMLStreamException)7 HashSet (java.util.HashSet)6 SCorpusGraph (org.corpus_tools.salt.common.SCorpusGraph)6 SaltProject (org.corpus_tools.salt.common.SaltProject)6 UI (com.vaadin.ui.UI)5 HashMap (java.util.HashMap)5 CorpusConfiguration (org.corpus_tools.annis.api.model.CorpusConfiguration)5 SDocument (org.corpus_tools.salt.common.SDocument)5 Notification (com.vaadin.ui.Notification)4 FileInputStream (java.io.FileInputStream)4 LinkedHashSet (java.util.LinkedHashSet)4 Annotation (org.corpus_tools.annis.api.model.Annotation)4