Created
September 27, 2019 14:18
-
-
Save davidtruchet/4d7e7e2072e23da202b23189ebf25de1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public PatientPlatformSearchResult<StudyModel> findStudies(SearchFilter filter, ResourceResolver resourceResolver) { | |
Map<String, String> predicateGroupMap = new HashMap<>(); | |
predicateGroupMap.put("type", "cq:PageContent"); | |
predicateGroupMap.put("path", filter.getHomePagePath()); | |
predicateGroupMap.put("property", "sling:resourceType"); | |
predicateGroupMap.put("property.value", ResourceType.CLINICAL_TRIAL.getResourceType()); | |
if (filter.getResultsonly()) { | |
predicateGroupMap.put(TAG_ID_PREDICATE, WITH_RESULTS_TAG_ID); | |
} | |
if (CollectionUtils.isNotEmpty(filter.getStatus())) { | |
for (int i = 0; i < filter.getStatus().size(); i++) { | |
predicateGroupMap.put("11_tagid." + (i + 1) + "_value", STATUS_TAG_ID + "/" + filter.getStatus().get(i)); | |
} | |
} | |
// If there is not status selected, filter only by valid statuses | |
else if (CollectionUtils.isNotEmpty(filter.getAllStatus())) { | |
for (int i = 0; i < filter.getAllStatus().size(); i++) { | |
predicateGroupMap.put("22_tagid." + (i + 1) + "_value", STATUS_TAG_ID + "/" + filter.getAllStatus().get(i)); | |
} | |
} | |
predicateGroupMap.put("group.p.or", "true"); | |
String title = filter.getTitle(); | |
if (StringUtils.isNotEmpty(title)) { | |
predicateGroupMap.put("group.1_group.p.or", "true"); | |
predicateGroupMap.put("group.1_group.1_fulltext", title + "~"); | |
predicateGroupMap.put("group.1_group.1_fulltext.relPath", "'jcr:title'"); | |
predicateGroupMap.put("group.1_group.2_fulltext", title + "~"); | |
predicateGroupMap.put("group.1_group.2_fulltext.relPath", "'expertTitle'"); | |
} | |
String id = filter.getId(); | |
if (StringUtils.isNotEmpty(id)) { | |
predicateGroupMap.put("group.1_group.p.or", "true"); | |
predicateGroupMap.put("group.1_group.3_property", "nctId"); | |
predicateGroupMap.put("group.1_group.3_property.value", id); | |
predicateGroupMap.put("group.1_group.4_property", "orgId"); | |
predicateGroupMap.put("group.1_group.4_property.value", id); | |
predicateGroupMap.put("group.1_group.5_property", "secondaryId"); | |
predicateGroupMap.put("group.1_group.5_property.value", id); | |
} | |
if (StringUtils.isNotEmpty(filter.getTitle()) && StringUtils.isEmpty(filter.getMedicalCondition())) { | |
// Limit the search to the medical conditions tags | |
predicateGroupMap.put("group.1_group.tagsearch", filter.getTitle()); | |
predicateGroupMap.put("group.1_group.tagsearch.lang", filter.getLocale()); | |
} else if (StringUtils.isNotEmpty(filter.getMedicalCondition())) { | |
predicateGroupMap.put("33_tagid.1_value", MEDICAL_CONDITIONS_TAG_ID + "/" + filter.getMedicalCondition()); | |
} | |
if (StringUtils.isNotEmpty(filter.getCountry()) || StringUtils.isNotEmpty(filter.getCity()) || | |
StringUtils.isNotEmpty(filter.getZip())) { | |
StringBuilder locationName = new StringBuilder(); | |
locationName.append("\"").append(LOCATIONS_TAG_ID).append("\"").append(" "); | |
locationName.append("\""); | |
if (StringUtils.isNotEmpty(filter.getCountry())) { | |
locationName.append(filter.getCountry()); | |
locationName.append("/"); | |
} | |
if (StringUtils.isNotEmpty(filter.getCity())) { | |
locationName.append(filter.getCity()); | |
locationName.append("/"); | |
} | |
if (StringUtils.isNotEmpty(filter.getCity())) { | |
locationName.append(filter.getZip()); | |
} | |
locationName.append("\""); | |
predicateGroupMap.put("1_fulltext", locationName.toString()); | |
predicateGroupMap.put("1_fulltext.relPath", "'cq:tags'"); | |
} | |
predicateGroupMap.put("80_orderby", "@statusRanking"); | |
predicateGroupMap.put("81_orderby", "@patientRanking"); | |
predicateGroupMap.put("82_orderby", "@jcr:score"); | |
predicateGroupMap.put("p.facets", "true"); | |
return getResults(predicateGroupMap, resourceResolver, filter, StudyModel.class); | |
} | |
private <T> PatientPlatformSearchResult<T> getResults(Map<String, String> predicateMap, ResourceResolver resourceResolver, SearchFilter filter, @Nonnull Class<T> classOfT) { | |
Query query = queryBuilder.createQuery(PredicateGroup.create(predicateMap), resourceResolver.adaptTo(Session.class)); | |
long limit = DEFAULT_RESULT_SIZE_LIMIT; | |
if (filter != null && filter.getLimit() != null) { | |
limit = filter.getLimit(); | |
} | |
long offset = DEFAULT_RESULT_OFFSET; | |
if (filter != null && filter.getOffset() != null) { | |
offset = filter.getOffset(); | |
} | |
query.setStart(offset); | |
query.setHitsPerPage(limit); | |
final SearchResult result = query.getResult(); | |
List<T> results = new ArrayList<>(); | |
for (Hit hit : result.getHits()) { | |
try { | |
results.add(hit.getResource().adaptTo(classOfT)); | |
} catch (RepositoryException e) { | |
logger.error("Error in retrieving result element", e); | |
} | |
} | |
// Add facets to the result | |
PatientPlatformSearchResult<T> ppResults = new PatientPlatformSearchResult<>(); | |
try { | |
Map<String, Facet> facets = result.getFacets(); | |
facets.keySet().forEach(key -> { | |
Facet cqFacet = facets.get(key); | |
if (StringUtils.contains(key, TAG_ID_PREDICATE)) { | |
cqFacet.getBuckets().forEach(bucket -> { | |
// Process locations | |
if (StringUtils.startsWith(bucket.getValue(), LOCATIONS_TAG_ID)) { | |
ppResults.addFacetEntry(FacetNames.LOCATIONS, bucket.getValue(), bucket.getCount()); | |
} | |
// Process with-results items | |
else if (StringUtils.startsWith(bucket.getValue(), WITH_RESULTS_TAG_ID)) { | |
ppResults.addFacetEntry(FacetNames.HAS_RESULTS, bucket.getValue(), bucket.getCount()); | |
} | |
}); | |
} | |
}); | |
} catch (RepositoryException e) { | |
logger.warn("Could not obtain facets"); | |
} | |
ppResults.addResults(results); | |
ppResults.setCount(result.getTotalMatches()); | |
return ppResults; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment