How to render name or company name according to type

0

I am trying to render the contents of a column of a table with name or Social reason depending on 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 happens that you always render Social Reason / Fancy Name.

    
asked by anonymous 19.02.2018 / 13:29

1 answer

-1

There is a more elegant solution to this, which would be solved in its modeling through inheritance / polymorphism, you could model a super class, for example the Person class, having 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 clada 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().

Without ifs, without gambiarras, only good modeling, a good design of the application. Another less elegant option, however that would work perfectly would be to have a getNomeFormatado() method with no inheritance or polymorphism to check inside the method:

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

I hope I have helped.

    
19.02.2018 / 16:08