Skip to content

Commit 68076d3

Browse files
vladakahornace
authored andcommitted
adress Sonar issues in Suggestion
1 parent edf8e58 commit 68076d3

File tree

3 files changed

+57
-21
lines changed

3 files changed

+57
-21
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/web/SearchHelper.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ public List<Suggestion> getSuggestions() {
542542
IndexReader ir = null;
543543
Term t;
544544
for (String proj : name) {
545-
Suggestion s = new Suggestion(proj);
545+
Suggestion suggestion = new Suggestion(proj);
546546
try {
547547
if (!closeOnDestroy) {
548548
SuperIndexSearcher searcher = RuntimeEnvironment.getInstance().getIndexSearcher(proj);
@@ -556,30 +556,28 @@ public List<Suggestion> getSuggestions() {
556556
&& !builder.getFreetext().isEmpty()) {
557557
t = new Term(QueryBuilder.FULL, builder.getFreetext());
558558
getSuggestion(t, ir, dummy);
559-
s.freetext = dummy.toArray(new String[0]);
559+
suggestion.setFreetext(dummy.toArray(new String[0]));
560560
dummy.clear();
561561
}
562562
if (builder.getRefs() != null && !builder.getRefs().isEmpty()) {
563563
t = new Term(QueryBuilder.REFS, builder.getRefs());
564564
getSuggestion(t, ir, dummy);
565-
s.refs = dummy.toArray(new String[0]);
565+
suggestion.setRefs(dummy.toArray(new String[0]));
566566
dummy.clear();
567567
}
568568
if (builder.getDefs() != null && !builder.getDefs().isEmpty()) {
569569
t = new Term(QueryBuilder.DEFS, builder.getDefs());
570570
getSuggestion(t, ir, dummy);
571-
s.defs = dummy.toArray(new String[0]);
571+
suggestion.setDefs(dummy.toArray(new String[0]));
572572
dummy.clear();
573573
}
574574
//TODO suggest also for path and history?
575-
if ((s.freetext != null && s.freetext.length > 0)
576-
|| (s.defs != null && s.defs.length > 0)
577-
|| (s.refs != null && s.refs.length > 0)) {
578-
res.add(s);
575+
if (suggestion.isUsable()) {
576+
res.add(suggestion);
579577
}
580578
} catch (IOException e) {
581-
LOGGER.log(Level.WARNING, "Got exception while getting "
582-
+ "spelling suggestions for project " + proj + ":", e);
579+
LOGGER.log(Level.WARNING,
580+
String.format("Got exception while getting spelling suggestions for project %s:", proj), e);
583581
} finally {
584582
if (ir != null && closeOnDestroy) {
585583
try {

opengrok-indexer/src/main/java/org/opengrok/indexer/web/Suggestion.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
/*
21+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2122
* Copyright (c) 2011, Jens Elkner.
2223
*/
2324
package org.opengrok.indexer.web;
@@ -30,13 +31,13 @@
3031
public class Suggestion {
3132

3233
/** index name. */
33-
public String name;
34+
private final String name;
3435
/** freetext search suggestions. */
35-
public String[] freetext;
36+
private String[] freetext;
3637
/** references search suggestions. */
37-
public String[] refs;
38+
private String[] refs;
3839
/** definitions search suggestions. */
39-
public String[] defs;
40+
private String[] defs;
4041

4142
/**
4243
* Create a new suggestion.
@@ -45,4 +46,41 @@ public class Suggestion {
4546
public Suggestion(String name) {
4647
this.name = name;
4748
}
49+
50+
public String getName() {
51+
return name;
52+
}
53+
54+
public String[] getFreetext() {
55+
return freetext;
56+
}
57+
58+
public String[] getRefs() {
59+
return refs;
60+
}
61+
62+
public String[] getDefs() {
63+
return defs;
64+
}
65+
66+
public void setFreetext(String[] freetext) {
67+
this.freetext = freetext;
68+
}
69+
70+
public void setRefs(String[] refs) {
71+
this.refs = refs;
72+
}
73+
74+
public void setDefs(String[] defs) {
75+
this.defs = defs;
76+
}
77+
78+
/**
79+
* @return true if at least one of the properties has some content, false otherwise
80+
*/
81+
public boolean isUsable() {
82+
return (freetext != null && freetext.length > 0)
83+
|| (defs != null && defs.length > 0)
84+
|| (refs != null && refs.length > 0);
85+
}
4886
}

opengrok-web/src/main/webapp/search.jsp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,23 +169,23 @@ include file="menu.jspf"
169169
} else if (searchHelper.hits.length == 0) {
170170
List<Suggestion> hints = searchHelper.getSuggestions();
171171
for (Suggestion hint : hints) {
172-
%><p class="suggestions"><font color="#cc0000">Did you mean (for <%= hint.name %>)</font>:<%
173-
if (hint.freetext!=null) {
174-
for (String word : hint.freetext) {
172+
%><p class="suggestions"><font color="#cc0000">Did you mean (for <%= hint.getName() %>)</font>:<%
173+
if (hint.getFreetext() != null) {
174+
for (String word : hint.getFreetext()) {
175175
%> <a href="search?<%= QueryParameters.FULL_SEARCH_PARAM_EQ %>
176176
<%= Util.URIEncode(QueryParser.escape(word)) %>"><%=
177177
Util.htmlize(word) %></a> &nbsp; <%
178178
}
179179
}
180-
if (hint.refs!=null) {
181-
for (String word : hint.refs) {
180+
if (hint.getRefs() != null) {
181+
for (String word : hint.getRefs()) {
182182
%> <a href="search?<%= QueryParameters.REFS_SEARCH_PARAM_EQ %>
183183
<%= Util.URIEncode(QueryParser.escape(word)) %>"><%=
184184
Util.htmlize(word) %></a> &nbsp; <%
185185
}
186186
}
187-
if (hint.defs!=null) {
188-
for (String word : hint.defs) {
187+
if (hint.getDefs() != null) {
188+
for (String word : hint.getDefs()) {
189189
%> <a href="search?<%= QueryParameters.DEFS_SEARCH_PARAM_EQ %>
190190
<%= Util.URIEncode(QueryParser.escape(word)) %>"><%=
191191
Util.htmlize(word) %></a> &nbsp; <%

0 commit comments

Comments
 (0)