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, ","));
}
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;
}
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<>());
}
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");
}
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);
}
Aggregations