How to render name or social reason according to type

Asked

Viewed 30 times

0

I am trying to render the contents of a column of a table with name or Social reason depending on the type

<f:facet name="header"> #{contato.tipoCadastro.codigo eq '1' ? 'Nome' : 'Razão Social/Nome Fantasia'}</f:facet>

The code is of type String.

It turns out that it always renders Social Reason/Fantasy Name.

1 answer

-1

There is a more elegant solution to this, which would be solved in your modeling through inheritance/polymorphism, you could model a super class, for example the class Person, taking as subclasses (inheritance) PessoaFisica and PessoaJuridica, in the superclass you could have the method getNomeFormatado, or getDescricao for example, returning the correct text, properly formatted to clade object by type... this is the concept of polymorphism.

Pessoa p1 = new PessoaFisica(); // retorna em getNomeFormatado() o valor de getNome().
Pessoa p2 = new PessoaJuridica(); // retorna o valor de getNomeFormatado() o valor de getRazaoSocial().

No ifs, no gambiarras, just good modeling, good application design. Another less elegant option, however, that would work perfectly would be to have a method getNomeFormatado() no inheritance or polymorphism within the method:

contato.tipoCadastro.codigo == '1' ? contato.getNome() : contato.getRazaoSocial();

I hope I’ve helped.

Browser other questions tagged

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