Search in sources :

Example 1 with ResultField

use of org.eol.globi.server.util.ResultField in project eol-globi-data by jhpoelen.

the class CypherReturnClauseBuilder method appendReturnFields.

private static void appendReturnFields(StringBuilder query, List<ResultField> fields, Map<ResultField, String> selectors) {
    List<String> returnFields = new ArrayList<String>();
    for (ResultField field : fields) {
        returnFields.add(selectors.get(field) + " as " + field);
    }
    query.append(StringUtils.join(returnFields, ","));
}
Also used : ArrayList(java.util.ArrayList) ResultField(org.eol.globi.server.util.ResultField)

Example 2 with ResultField

use of org.eol.globi.server.util.ResultField in project eol-globi-data by jhpoelen.

the class TaxonSearchImpl method taxonLinks.

@RequestMapping(value = "/taxonLinks/{taxonPath}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
@ResponseBody
public Collection<String> taxonLinks(@PathVariable("taxonPath") final String taxonPath, HttpServletRequest request) throws IOException {
    final String pathQuery = CypherQueryBuilder.lucenePathQuery(Collections.singletonList(taxonPath), true);
    StringBuilder query = new StringBuilder("START someTaxon = node:taxons({pathQuery}) ");
    Map<ResultField, String> selectors = new HashMap<ResultField, String>() {

        {
            put(ResultField.TAXON_EXTERNAL_ID, "externalId");
            put(ResultField.TAXON_EXTERNAL_URL, "externalUrl");
        }
    };
    ResultField[] returnFieldsCloseMatches = new ResultField[] { ResultField.TAXON_EXTERNAL_ID, ResultField.TAXON_EXTERNAL_URL };
    List<String> requestedFields = new ArrayList<String>();
    if (request != null) {
        requestedFields.addAll(CypherQueryBuilder.collectRequestedFields(request.getParameterMap()));
    }
    query.append(" MATCH someTaxon-[:SAME_AS*0..1]->taxon WHERE has(taxon.externalId) WITH DISTINCT(taxon.externalId) as externalId, taxon.externalUrl? as externalUrl ");
    CypherReturnClauseBuilder.appendReturnClauseDistinctz(query, CypherReturnClauseBuilder.actualReturnFields(requestedFields, Arrays.asList(returnFieldsCloseMatches), selectors.keySet()), selectors);
    final CypherQuery query1 = new CypherQuery(query.toString(), new HashMap() {

        {
            put("pathQuery", pathQuery);
        }
    });
    final CypherQuery pagedQuery = CypherQueryBuilder.createPagedQuery(request, query1, 15);
    final String response = CypherUtil.executeRemote(pagedQuery);
    JsonNode node = new ObjectMapper().readTree(response);
    JsonNode dataNode = node.get("data");
    Collection<String> links = new HashSet<>();
    if (dataNode != null) {
        for (JsonNode jsonNode : dataNode) {
            if (jsonNode.isArray() && jsonNode.size() > 1) {
                String externalId = jsonNode.get(0).asText();
                String externalUrl = jsonNode.get(1).asText();
                String resolvedUrl = ExternalIdUtil.urlForExternalId(externalId);
                if (StringUtils.isNotBlank(resolvedUrl)) {
                    links.add(resolvedUrl);
                } else if (StringUtils.isNotBlank(externalUrl)) {
                    links.add(externalUrl);
                }
            }
        }
    }
    return links;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonNode(org.codehaus.jackson.JsonNode) CypherQuery(org.eol.globi.util.CypherQuery) ResultField(org.eol.globi.server.util.ResultField) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) HashSet(java.util.HashSet) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with ResultField

use of org.eol.globi.server.util.ResultField in project eol-globi-data by jhpoelen.

the class CypherQueryBuilder method createDistinctTaxaInLocationQuery.

public static CypherQuery createDistinctTaxaInLocationQuery(Map<String, String[]> params) {
    StringBuilder builder = new StringBuilder();
    List<String> interactionTypes = collectParamValues(params, ParamName.INTERACTION_TYPE);
    if (RequestHelper.isSpatialSearch(params)) {
        appendSpatialStartWhereWith(params, builder);
    } else {
        builder.append("START taxon = node:taxons('*:*') ");
    }
    if (RequestHelper.isSpatialSearch(params)) {
        builder.append("MATCH taxon<-[:CLASSIFIED_AS]-specimen-[:COLLECTED_AT]->loc");
        if (!interactionTypes.isEmpty()) {
            builder.append(", taxon-[:");
            builder.append(createInteractionTypeSelector(interactionTypes));
            builder.append("]->otherTaxon ");
        } else {
            builder.append(" ");
        }
    } else {
        if (!interactionTypes.isEmpty()) {
            builder.append("MATCH taxon-[:");
            builder.append(createInteractionTypeSelector(interactionTypes));
            builder.append("]->otherTaxon ");
        }
    }
    List<String> fields = collectRequestedFields(params);
    List<ResultField> returnFields = CypherReturnClauseBuilder.actualReturnFields(fields, TAXON_FIELDS, TAXON_FIELDS);
    for (int i = 0; i < returnFields.size(); i++) {
        ResultField fieldName = returnFields.get(i);
        if (i == 0) {
            builder.append("RETURN distinct(").append(FIELD_MAP.get(fieldName)).append(") as ").append(fieldName);
        } else {
            builder.append(", ").append(FIELD_MAP.get(fieldName)).append(" as ").append(fieldName);
        }
    }
    return new CypherQuery(builder.toString(), new HashMap<>());
}
Also used : CypherQuery(org.eol.globi.util.CypherQuery) ResultField(org.eol.globi.server.util.ResultField)

Example 4 with ResultField

use of org.eol.globi.server.util.ResultField in project eol-globi-data by jhpoelen.

the class SchemaController method csvFields.

protected String csvFields() {
    List<String> fields = new ArrayList<String>();
    fields.add("name,description");
    for (ResultField resultField : ResultField.values()) {
        fields.add("" + resultField.getLabel() + "," + resultField.getDescription() + "");
    }
    return StringUtils.join(fields, "\n");
}
Also used : ResultField(org.eol.globi.server.util.ResultField)

Example 5 with ResultField

use of org.eol.globi.server.util.ResultField in project eol-globi-data by jhpoelen.

the class TaxonSearchImpl method findCloseMatchesForCommonAndScientificNames.

@RequestMapping(value = "/findCloseMatchesForTaxon/{taxonName}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
@ResponseBody
public CypherQuery findCloseMatchesForCommonAndScientificNames(@PathVariable("taxonName") final String taxonName, HttpServletRequest request) throws IOException {
    StringBuilder exactQuery = new StringBuilder("START taxon = node:taxons(name = {taxonName}) ");
    String luceneQuery = buildLuceneQuery(taxonName, "name");
    StringBuilder fuzzyQuery = new StringBuilder("START taxon = node:taxonNameSuggestions('" + luceneQuery + "') ");
    Map<ResultField, String> selectors = new HashMap<ResultField, String>() {

        {
            put(TAXON_NAME, "taxon.name");
            put(TAXON_COMMON_NAMES, "taxon.commonNames");
            put(TAXON_PATH, "taxon.path");
            put(TAXON_EXTERNAL_ID, "taxon.externalId");
            put(TAXON_PATH_IDS, "taxon.pathIds");
            put(TAXON_PATH_RANKS, "taxon.pathNames");
        }
    };
    ResultField[] returnFieldsCloseMatches = new ResultField[] { TAXON_NAME, TAXON_COMMON_NAMES, TAXON_PATH, TAXON_PATH_IDS };
    List<String> requestedFields = new ArrayList<String>();
    if (request != null) {
        requestedFields.addAll(CypherQueryBuilder.collectRequestedFields(request.getParameterMap()));
    }
    CypherReturnClauseBuilder.appendReturnClauseDistinctz(exactQuery, CypherReturnClauseBuilder.actualReturnFields(requestedFields, Arrays.asList(returnFieldsCloseMatches), selectors.keySet()), selectors);
    CypherReturnClauseBuilder.appendReturnClauseDistinctz(fuzzyQuery, CypherReturnClauseBuilder.actualReturnFields(requestedFields, Arrays.asList(returnFieldsCloseMatches), selectors.keySet()), selectors);
    return CypherQueryBuilder.createPagedQuery(request, new CypherQuery(exactQuery + " LIMIT 1 UNION " + fuzzyQuery.toString(), new TreeMap<String, String>() {

        {
            put("taxonName", taxonName);
        }
    }, CypherUtil.CYPHER_VERSION_2_3), 30);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CypherQuery(org.eol.globi.util.CypherQuery) ResultField(org.eol.globi.server.util.ResultField) TreeMap(java.util.TreeMap) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

ResultField (org.eol.globi.server.util.ResultField)5 ArrayList (java.util.ArrayList)3 CypherQuery (org.eol.globi.util.CypherQuery)3 HashMap (java.util.HashMap)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 HashSet (java.util.HashSet)1 TreeMap (java.util.TreeMap)1 JsonNode (org.codehaus.jackson.JsonNode)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1