Example usage for org.apache.commons.lang3 StringUtils substringBefore

List of usage examples for org.apache.commons.lang3 StringUtils substringBefore

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils substringBefore.

Prototype

public static String substringBefore(final String str, final String separator) 

Source Link

Document

Gets the substring before the first occurrence of a separator.

Usage

From source file:org.phenotips.solr.HPOScriptService.java

/**
 * Get the HPO IDs of the specified phenotype and all its ancestors.
 *
 * @param id the HPO identifier to search for, in the {@code HP:1234567} format
 * @return the full set of ancestors-or-self IDs, or an empty set if the requested ID was not found in the index
 *//*w w w  . j  a v a2 s  .  c  om*/
@SuppressWarnings("unchecked")
public Set<String> getAllAncestorsAndSelfIDs(final String id) {
    Set<String> results = new HashSet<String>();
    Queue<SolrDocument> nodes = new LinkedList<SolrDocument>();
    SolrDocument crt = this.get(id);
    if (crt == null) {
        return results;
    }
    nodes.add(crt);
    while (!nodes.isEmpty()) {
        crt = nodes.poll();
        results.add(String.valueOf(crt.get(ID_FIELD_NAME)));
        Object rawParents = crt.get("is_a");
        if (rawParents == null) {
            continue;
        }
        List<String> parents;
        if (rawParents instanceof String) {
            parents = Collections.singletonList(String.valueOf(rawParents));
        } else {
            parents = (List<String>) rawParents;
        }
        for (String pid : parents) {
            nodes.add(this.get(StringUtils.substringBefore(pid, " ")));
        }
    }
    return results;
}

From source file:org.phenotips.vocabulary.internal.DefaultVocabularyManager.java

/**
 * Finds the owner vocabulary given a term identifier. The vocabulary is identified by the term ID prefix, for
 * example {@code HP} in {@code HP:0002066}.
 *
 * @param termId the term identifier to process
 * @return the owner vocabulary, or {@code null} if the term doesn't belong to a known vocabulary
 *///  w w w . j a va 2s  .  c  o  m
private Vocabulary getVocabularyForTerm(String termId) {
    String vocabularyId = StringUtils.substringBefore(termId, ":");
    if (StringUtils.isNotBlank(vocabularyId)) {
        return this.aliasVocabularies.get(vocabularyId);
    }
    return null;
}

From source file:org.phenotips.vocabulary.internal.solr.EthnicityOntology.java

private SolrQuery addDynamicQueryParameters(String originalQuery, Integer rows, String sort, String customFq,
        boolean isId, SolrQuery query) {
    String queryString = originalQuery.trim();
    String escapedQuery = ClientUtils.escapeQueryChars(queryString);
    if (isId) {//from  www  .j  a v a  2 s .  co  m
        query.setFilterQueries(new MessageFormat("id:{0}").format(new String[] { escapedQuery }));
    }
    query.setQuery(escapedQuery);
    query.set(SpellingParams.SPELLCHECK_Q, queryString);
    query.setRows(rows);
    if (StringUtils.isNotBlank(sort)) {
        for (String sortItem : sort.split("\\s*,\\s*")) {
            query.addSort(StringUtils.substringBefore(sortItem, " "),
                    sortItem.endsWith(" desc") || sortItem.startsWith("-") ? ORDER.desc : ORDER.asc);
        }
    }
    return query;
}

From source file:org.phenotips.vocabulary.internal.solr.HumanPhenotypeOntology.java

private SolrQuery addDynamicQueryParameters(String originalQuery, Integer rows, String sort, String customFq,
        boolean isId, SolrQuery query) {
    String queryString = originalQuery.trim();
    String escapedQuery = ClientUtils.escapeQueryChars(queryString);
    if (isId) {/*from  w ww . ja v a 2  s.  com*/
        query.setFilterQueries(StringUtils.defaultIfBlank(customFq,
                new MessageFormat("id:{0} alt_id:{0}").format(new String[] { escapedQuery })));
    } else {
        query.setFilterQueries(StringUtils.defaultIfBlank(customFq, "term_category:HP\\:0000118"));
    }
    query.setQuery(escapedQuery);
    query.set(SpellingParams.SPELLCHECK_Q, queryString);
    query.setRows(rows);
    if (StringUtils.isNotBlank(sort)) {
        for (String sortItem : sort.split("\\s*,\\s*")) {
            query.addSort(StringUtils.substringBefore(sortItem, " "),
                    sortItem.endsWith(" desc") || sortItem.startsWith("-") ? ORDER.desc : ORDER.asc);
        }
    }
    return query;
}

From source file:org.phenotips.vocabulary.internal.solr.LazySolrTermSet.java

/**
 * Constructor that provides the list of {@link #identifiers terms identifier} and the {@link #vocabulary owner
 * vocabulary}.// www. j  av a  2s .  co  m
 *
 * @param identifiers the {@link #identifiers identifiers to load}
 * @param vocabulary the {@link #vocabulary owner vocabulary}
 */
public LazySolrTermSet(Collection<Object> identifiers, Vocabulary vocabulary) {
    if (identifiers == null || identifiers.isEmpty()) {
        this.identifiers = Collections.emptySet();
        this.terms = Collections.emptySet();
    } else {
        this.identifiers = new LinkedHashSet<>(identifiers.size());
        for (Object id : identifiers) {
            this.identifiers.add(StringUtils.substringBefore(String.valueOf(id), " "));
        }
    }
    this.vocabulary = vocabulary;
}

From source file:org.phenotips.vocabulary.internal.solr.MendelianInheritanceInMan.java

private SolrQuery addDynamicQueryParameters(String originalQuery, Integer rows, String sort, String customFq,
        SolrQuery query) {//from   w w  w .  j  a  v a  2 s.  c o  m
    String queryString = originalQuery.trim();
    String escapedQuery = ClientUtils.escapeQueryChars(queryString);
    query.setFilterQueries(StringUtils.defaultIfBlank(customFq, "+type:disorder"));
    query.setQuery(escapedQuery);
    query.set(SpellingParams.SPELLCHECK_Q, queryString);
    String lastWord = StringUtils.substringAfterLast(escapedQuery, " ");
    if (StringUtils.isBlank(lastWord)) {
        lastWord = escapedQuery;
    }
    lastWord += "*";
    query.set(DisMaxParams.BQ,
            String.format(
                    "nameSpell:%1$s^20 short_name:%1$s^20 synonymSpell:%1$s^12 text:%1$s^1 textSpell:%1$s^2",
                    lastWord));
    query.setRows(rows);
    if (StringUtils.isNotBlank(sort)) {
        for (String sortItem : sort.split("\\s*,\\s*")) {
            query.addSort(StringUtils.substringBefore(sortItem, " "),
                    sortItem.endsWith(" desc") || sortItem.startsWith("-") ? ORDER.desc : ORDER.asc);
        }
    }
    return query;
}

From source file:org.phenotips.vocabulary.internal.solr.MendelianInheritanceInMan.java

private void parseOmimData(URL sourceUrl) {
    try {/*w  ww.ja  v a2  s  . co  m*/
        Reader in = new InputStreamReader(sourceUrl.openConnection().getInputStream(),
                Charset.forName(ENCODING));
        for (CSVRecord row : CSVFormat.TDF.withCommentMarker('#').parse(in)) {
            // Ignore moved or removed entries
            if ("Caret".equals(row.get(0))) {
                continue;
            }

            SolrInputDocument crtTerm = new SolrInputDocument();
            // set id
            addFieldValue(ID_FIELD, row.get(1), crtTerm);

            // set symbol
            addFieldValue(SYMBOL_FIELD, SYMBOLS.get(row.get(0)), crtTerm);
            // set type (multivalued)
            for (String type : TYPES.get(row.get(0))) {
                addFieldValue(TYPE_FIELD, type, crtTerm);
            }
            // set name
            String name = StringUtils.substringBefore(row.get(2), TITLE_SEPARATOR).trim();
            addFieldValue(NAME_FIELD, name, crtTerm);
            // set short name
            String shortNameString = StringUtils.substringAfter(row.get(2), TITLE_SEPARATOR).trim();
            String[] shortNames = StringUtils.split(shortNameString, TITLE_SEPARATOR);
            for (String shortName : shortNames) {
                addFieldValue(SHORT_NAME_FIELD, shortName.trim(), crtTerm);
            }

            // set synonyms
            setListFieldValue(SYNONYM_FIELD, row.get(3), crtTerm);
            // set included name
            setListFieldValue(INCLUDED_NAME_FIELD, row.get(4), crtTerm);

            this.data.put(String.valueOf(crtTerm.get(ID_FIELD).getFirstValue()), crtTerm);
        }
    } catch (IOException ex) {
        this.logger.warn("Failed to read/parse the OMIM source: {}", ex.getMessage());
    }
}

From source file:org.phenotips.vocabulary.internal.solr.OmimSourceParser.java

private void loadField(String name, String value) {
    if (StringUtils.isAnyBlank(name, value)) {
        return;//from  w ww . j  a  v  a 2 s  . c  o m
    }
    switch (name) {
    case FIELD_MIM_NUMBER:
        this.crtTerm.setField(ID_FIELD, value);
        break;
    case FIELD_TITLE:
        String title = StringUtils.substringBefore(value, TITLE_SEPARATOR).trim();
        String[] synonyms = StringUtils.split(StringUtils.substringAfter(value, TITLE_SEPARATOR),
                TITLE_SEPARATOR);
        this.crtTerm.setField(NAME_FIELD, title);
        for (String synonym : synonyms) {
            this.crtTerm.addField(SYNONYM_FIELD, synonym.trim());
        }
        break;
    case FIELD_TEXT:
        this.crtTerm.addField("def", value);
        break;
    default:
        return;
    }
}

From source file:org.phenotips.vocabulary.internal.solr.OrphanetRareDiseaseOntology.java

/**
 * Extracts property hasDbXref from an {@link RDFNode} and adds it to the {@link SolrInputDocument}.
 *
 * @param doc the Solr input document for an {@link OntClass} of interest
 * @param object the {@link RDFNode} object contained within the {@link OntClass} of interest
 *//*from  ww  w.  jav a2  s .  co m*/
private void extractDbxRef(@Nonnull final SolrInputDocument doc, @Nonnull final RDFNode object) {
    // If the node is not a literal, will throw a {@link LiteralRequiredException}. For ORDO, this is always a
    // literal.
    if (object.isLiteral()) {
        final String externalRef = object.asLiteral().getLexicalForm();
        final String ontology = StringUtils.substringBefore(externalRef, SEPARATOR);
        final String externalId = StringUtils.substringAfter(externalRef, SEPARATOR);
        addMultivaluedField(doc, ontology.toLowerCase() + "_id", externalId);
    }
}

From source file:org.phenotips.vocabulary.internal.solr.OrphanetRareDiseaseOntology.java

/**
 * Adds dynamic solr query parameters to {@code query}, based on the received {@code rawQuery raw query string},
 * {@code rows the maximum number of results to return}, {@code sort the sorting order}, and {@code customFilter a
 * custom filter}.//from w w  w.j a  v  a2s .co  m
 *
 * @param rawQuery unprocessed query string
 * @param rows the maximum number of search items to return
 * @param sort the optional sort parameter
 * @param customFilter custom filter for the results
 * @param query a {@link SolrQuery solr query} object
 * @return the updated {@link SolrQuery solr query} object
 */
private SolrQuery addDynamicQueryParam(@Nonnull final String rawQuery, final Integer rows,
        @Nullable final String sort, @Nullable final String customFilter, @Nonnull SolrQuery query) {
    final String queryString = rawQuery.trim();
    final String escapedQuery = ClientUtils.escapeQueryChars(queryString);
    if (StringUtils.isNotBlank(customFilter)) {
        query.setFilterQueries(customFilter);
    }
    query.setQuery(escapedQuery);
    query.set(SpellingParams.SPELLCHECK_Q, queryString);
    final String lastWord = StringUtils.defaultIfBlank(StringUtils.substringAfterLast(escapedQuery, " "),
            escapedQuery) + "*";
    query.set(DisMaxParams.BQ,
            String.format("nameSpell:%1$s^20 defSpell:%1$s^3 text:%1$s^1 textSpell:%1$s^2", lastWord));
    query.setRows(rows);
    if (StringUtils.isNotBlank(sort)) {
        for (final String sortItem : sort.split("\\s*,\\s*")) {
            query.addSort(StringUtils.substringBefore(sortItem, " "),
                    sortItem.endsWith(" desc") || sortItem.startsWith("-") ? ORDER.desc : ORDER.asc);
        }
    }
    return query;
}