1

I have this part of code which I don't understand correctly. I know that it builds a string out of values from the database. I think it's only the title:

private String createSearchFieldContent(Report report) {
  return createSearchFieldContent(report.getTitle(), report.getDescription());
}

private String createSearchFieldContent(OverrideableStringValue title,
                                        OverrideableStringValue description) {

  StringBuilder builder = new StringBuilder(getValue(title));
  if (StringUtils.isNotBlank(getValue(description))) {
    builder.append(" ").append(getValue(description));
  }
  String searchTerm = StringUtils.replaceAll(builder.toString(), "\n", " ");
  return unaccent(searchTerm);
}

I have another value which I'd like to add to the searchTerm. It's stored in report.getOwner(). How is it possible that I can build a new searchTerm which includes the title, description and owner?

Thanks!

2
  • 1
    I think it's only the title - the description if not blank will also be appended. Add a new parameter to your method and follow the same logic as for description Commented Nov 19, 2019 at 8:32
  • @ScaryWombat thx for clarify Commented Nov 19, 2019 at 9:29

2 Answers 2

0

Try below code :

private String createSearchFieldContent(Report report) {
  return createSearchFieldContent(report.getTitle(), report.getDescription(), report.getOwner());
}

private String createSearchFieldContent(OverrideableStringValue title,
                                        OverrideableStringValue description, OverrideableStringValue owner) {

  StringBuilder builder = new StringBuilder(getValue(title));
  if (StringUtils.isNotBlank(getValue(description))) {
    builder.append(" ").append(getValue(description));
  }
  if (StringUtils.isNotBlank(getValue(owner))) {
    builder.append(" ").append(getValue(owner));
  }
  String searchTerm = StringUtils.replaceAll(builder.toString(), "\n", " ");
  return unaccent(searchTerm);
}

In the above code, we are passing the owner attribute to the createSearchFieldContent method like the other two, then the owner attribute is appended to string builder object just like description.

Sign up to request clarification or add additional context in comments.

2 Comments

Thx, woks for me!
@bhanu Code only answers are frowned upon. Please explain what you are doing.
0

You can assign new data to searchTerm:

searchTerm = StringUtils.replaceAll(builder.toString(), "\n", " ")

becomes

String part2 = "something you want to add"; 
searchTerm = searchTerm + part2;

Or you could use the StringBuilder to append new data to

builder.append("new data")
builder.append("newer data")

This gets added to the StringBuilder which is incorporated in the string value of searchTerm.

2 Comments

why not follow the same logic used for description?
That is also a good option, it depends on personal preference i guess. If there are NullPointerExceptions you should indeed check for value or nulls like they do in the description logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.