Skip to content

Commit 892068d

Browse files
committed
Refactor group population logic in TurSolrResultProcessor
Split the group population logic into smaller methods for better readability and maintainability. Added null and empty checks for group responses and group values to prevent potential NullPointerExceptions.
1 parent a51dd47 commit 892068d

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

turing-app/src/main/java/com/viglet/turing/solr/TurSolrResultProcessor.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,34 @@ private void processGroups(SolrQuery query, TurSolrInstance turSolrInstance,
9999

100100
private void populateGroupsFromResponse(QueryResponse queryResponse, TurSNSite turSNSite,
101101
TurSolrQueryContext queryContext, List<TurSEGroup> turSEGroups) {
102-
queryResponse.getGroupResponse().getValues()
103-
.forEach(groupCommand -> groupCommand.getValues()
104-
.forEach(group -> Optional.ofNullable(group.getGroupValue())
105-
.ifPresent(g -> turSEGroups
106-
.add(setTurSEGroup(turSNSite,
107-
queryContext,
108-
queryResponse,
109-
group)))));
102+
var groupResponse = queryResponse.getGroupResponse();
103+
if (groupResponse == null || CollectionUtils.isEmpty(groupResponse.getValues())) {
104+
return;
105+
}
106+
107+
for (GroupCommand groupCommand : groupResponse.getValues()) {
108+
addGroupsFromCommand(turSNSite, queryContext, queryResponse, turSEGroups, groupCommand);
109+
}
110+
}
111+
112+
private void addGroupsFromCommand(TurSNSite turSNSite, TurSolrQueryContext queryContext,
113+
QueryResponse queryResponse, List<TurSEGroup> turSEGroups, GroupCommand groupCommand) {
114+
if (CollectionUtils.isEmpty(groupCommand.getValues())) {
115+
return;
116+
}
117+
118+
for (Group group : groupCommand.getValues()) {
119+
addGroupIfPresent(turSNSite, queryContext, queryResponse, turSEGroups, group);
120+
}
121+
}
122+
123+
private void addGroupIfPresent(TurSNSite turSNSite, TurSolrQueryContext queryContext,
124+
QueryResponse queryResponse, List<TurSEGroup> turSEGroups, Group group) {
125+
if (group.getGroupValue() == null) {
126+
return;
127+
}
128+
129+
turSEGroups.add(setTurSEGroup(turSNSite, queryContext, queryResponse, group));
110130
}
111131

112132
private boolean shouldApplyWildcardQuery(TurSNSite turSNSite, SolrQuery query) {

0 commit comments

Comments
 (0)