Check and edit a List

Asked

Viewed 253 times

4

I own a List:

private List<Geracao> lista;

This list will be filled with data from a database:

lista = dao.findAll();

The method findAll() :

@SuppressWarnings("unchecked")
    @Override
    public List<Geracao> findAll() throws Exception {
        log.info("Encontrando todas as Gerações");
        return em.createQuery("from Geracao").getResultList();
    }

These data after being completed in lista will go to the screen (JSF). I would like to make a formatting in the text before going to the screen, example: I have an attribute called nome in class Geracao. How can I take the amount that will come from the bank, edit and then play to the lista ?

My ultimate goal is that in the text that will come from the database before going to the screen, make a check if there is a word x. If there is x in the text, x will be formatted, example as in bold before going to the screen. Ex:

texto = texto.replace(palavra, "<b>"+palavra+"</b>");
  • 1

    What kind of formatting? Are you looking to apply some business rule (for example, putting the last name in front of the name)? Or you are wanting to do something purely cosmetic (for example, limit the quandity of characters). The answer is important because it defines the best component (business class, bean Managed, etc.) is most suitable for handling

  • @Anthonyaccioly edited the question with the information.

  • Man, it particularly seems to me that what you want to do should be done in the view, so I would do in direct JSF: <h:outputText value="#{fn:replace('palavra', ')', '<b>palavra</b>')}" />

  • 1

    @Sorack the problem is that it is not the same word, and depending on the word will receive different formatting, in my case I will do a check along with a list coming from Enum.

  • Oh got it @Douglas. The best thing seems to me is to do it in the bean, but I think Giovane’s answer should fall like a glove to you

  • Related questions: http://answall.com/q/165766/132 and http://answall.com/q/165762/132

Show 1 more comment

2 answers

2


You can try to do that:

final String palavraChave = "palavra";
final String trocaPalavraChave = "<b>palavra</b>";

List<Geracao> lista = dao.findAll().stream().forEach(geracao -> {
  if(geracao.getNome().contains(palavraChave)) {
    geracao.setNome(geracao.getNome().replaceAll(palavra, trocaPalavraChave));
  }
});

To access external variables to .stream().forEach() they must be immutable, final.


UPDATE

The above code was made based on the Java8 API, if you are using a lower version you can use the foreach basic:

final String palavraChave = "palavra";
final String trocaPalavraChave = "<b>palavra</b>";

List<Geracao> lista = dao.findAll();
for(Geracao geracao : lista) {
  if(geracao.getNome().contains(palavraChave)) {
    geracao.setNome(geracao.getNome().replaceAll(palavra, trocaPalavraChave));
  }
}

UPDATE

As I mentioned in the comment, you can treat several rules within the same for, example:

List<Geracao> lista = dao.findAll();
for(Geracao geracao : lista) {
  regraDeNome(geracao);
  regraXYZ(geracao);
}

private void regraDeNome(Geracao geracao) {
  final String palavraChave = "palavra";
  final String trocaPalavraChave = "<b>palavra</b>";

  if(geracao.getNome().contains(palavraChave)) {
    geracao.setNome(geracao.getNome().replaceAll(palavra, trocaPalavraChave));
  }
}
private void regraXYZ(Geracao geracao) {
  // tratamento da regra
}
  • .stream(). foreach are List methods ?

  • Yes, it is part of the Java8 API

  • Giovane, I’ll bother you again... I don’t only have the attribute name to check, there are others, I will have to do a for each attribute ?

  • 1

    No, leave it inside for. Think that inside the for are all the instructions that must be executed for each element of the list, so you can by all the necessary validations for each element within the same for. Perhaps a good practice not to get a very dense code and full of business rule you can create rule validation methods where you treat each rule in a separate method. I will illustrate this.

  • Show, thank you very much.

1

You can use the datatable rowStyleClass and create a method to format or treat words.

<p:dataTable value="#{ManagedBeam.lista}" var="item"
rowStyleClass="#{ManagedBean.tratarPalavra(item)}"

and in the method:

public String tratarPalavra(Geracao g) {
    // lógica e tratamento...
    return String;
}
  • Very good, I’m just finding it strange is that the formatting is not going to screen, but in debug I saw that is doing everything I want.

  • you can create in the project’s css a standard format... example: in CSS . red { background-color:red; } and return in the Beam method the "red" string; ?

  • This datatable attribute expects return of a css class ?

  • Exact! Anything in the documentation of the first faces has well explained. Got there?

  • Not because it formats everything, and I want formatting only in specific words :/

  • tried instead of using <b> to use <Strong>??

  • Giovani’s solution met, but thank you.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.