type Enum field can be used for comparison?

Asked

Viewed 68 times

0

I own an enum field:

public enum StatusImpressaoEnum {
PREVISAO(0  ,"Previsão"),
EMELABORACAO(1, "Em Elaboração"),
AGUARDANDOREPASSE(2, "Aguardando Repasse"),
REPASSERECEBIDO(3,  "Repasse Recebido"),
ATENDIDO(4  ,"Atendido"),
SOLICITACAORECUSADA(5,  "Solicitação Recusada"),
ARQUIVADO(6 ,"Arquivado"),
CANCELADO(7,"Cancelado");

private final Integer codigo;
private final String descricao;

private StatusImpressaoEnum(Integer codigo, String descricao) {
    this.codigo = codigo;
    this.descricao = descricao;
}

public Integer getCodigo() {
    return codigo;
}

public String getDescricao() {
    return descricao;
}

public static StatusImpressaoEnum obtemPorCodigo(int valor) {
    for (StatusImpressaoEnum enumerador : StatusImpressaoEnum.values()) {
        if (enumerador.getCodigo() == valor) {
            return enumerador;
        }
    }
    return null;
 }

}

and I want to do the following :

rendered="#{Peg.status.Description != 'Cancelled'}">

Since the status is of this Enum type. At first this command is doing nothing or is continuing to render with Canceled status.

I put it in an oputput to see the result :

And I got the mistake :

com.sun.faces.facelets.el.Tagmethodexpression cannot be cast to [Ljava.lang.Object;

View code :

<rich:dataTable value="#{solicitacaoImpressaoBean.solicitacoesImpressao}" var="peg" rows="15" rowClasses="linhaPar, linhaImpar" rendered="#{not empty solicitacaoImpressaoBean.solicitacoesImpressao}">
    <rich:column style="text-align:left;width:50px;">
        <f:facet name="header">
            <a4j:commandLink value="Código"  render="@this" />
        </f:facet>
        <h:outputText value="#{peg.codigo}" />
    </rich:column>

    <rich:column style="text-align:left;width:655px;">
        <f:facet name="header">
            <a4j:commandLink value="Solicitação"  render="@this" />
        </f:facet>

        <h:outputText value="Ano: "  styleClass="nomeCampo" />  
        <h:outputText value="#{peg.anoPrevisao}" />
        <br></br>
        <h:outputText value="Pesquisa: "  styleClass="nomeCampo" /> 
        <h:outputText value="#{peg.pesquisa.descricao}" />
        <br></br>
        <h:outputText value="Título: "  styleClass="nomeCampo" />   
        <h:outputText value="#{peg.titulo.descricao}" />
        <br></br>
        <h:outputText value="Categoria: "  styleClass="nomeCampo" />    
        <h:outputText value="#{peg.categoria.descricao}" />
        <br></br>
        <h:outputText value="Unidade: "  styleClass="nomeCampo" />  
        <h:outputText value="#{peg.setor.codigo} - #{peg.setor.nomeSetor}" />
        <br></br>
        <h:outputText value="Tiragem: "  styleClass="nomeCampo" />  
        <h:outputText value="#{peg.tiragem}" />
        <br></br>
        <h:outputText value="Valor Unitário do item:"  styleClass="nomeCampo" />
        <h:outputText value="#{peg.valorUnitario}"  >
         <f:convertNumber currencySymbol="R$" minFractionDigits="2" type="currency"/> 
         <!-- //Você define qual o simbolo da moeda e quantas casas decimais vão ter. -->
        </h:outputText>
        <br></br>
        <h:outputText value="Observação Arquivo: "  styleClass="nomeCampo" />   
        <h:outputText value="#{peg.observacaoArquivos}" />
        <br></br>
        <h:outputText value="Observação Lista Entrega: "  styleClass="nomeCampo" /> 
        <h:outputText value="#{peg.obsListaEntrega}" />
        <h:outputText value="#{peg.status}" converter="statusImpressaoEnumConverter" />
        <br></br>

    </rich:column>

    <rich:column style="text-align:center;width:70px;">
        <f:facet name="header">Ação</f:facet>

        <a4j:commandLink action="#{solicitacaoImpressaoBean.abrirConsulta(peg.codigo)}"  render="@form">
            <h:graphicImage value="resources/img/lupa.png" styleClass="espacoBotao" />
            <rich:tooltip layout="block" followMouse="true" value="Consultar Peg #{peg.pesquisa.descricao}" />
        </a4j:commandLink>

        <a4j:commandLink action="#{solicitacaoImpressaoBean.abrirEdicao(peg.codigo)}" render="@form"
            rendered="#{peg.status.descricao != 'Cancelado'}">
            <h:graphicImage value="resources/img/editar.png" styleClass="espacoBotao" />

            <rich:tooltip layout="block" followMouse="true" value="Editar Peg #{peg.pesquisa.descricao}" />
        </a4j:commandLink>

        <a4j:commandLink action="#{solicitacaoImpressaoBean.abrirPopupCancelarSolicitacao(peg.codigo)}" rendered="#{peg.status.descricao != 'Cancelado'}" render="@form" >
            <h:graphicImage value="resources/img/cancelar.jpg" alt="Cancelar Solicitação" styleClass="espacoBotao" />               
            <rich:tooltip layout="block" followMouse="false" value="Cancelar Solicitação"/>
        </a4j:commandLink>
    </rich:column>

    <f:facet name="footer">
        <rich:dataScroller execute="@form" fastStep="15" />
    </f:facet>
</rich:dataTable>

Stacktrace

Caused by: java.lang.ClassCastException: com.sun.faces.facelets.el.TagMethodExpression cannot be cast to [Ljava.lang.Object;
at javax.faces.component.AttachedObjectListHolder.restoreState(AttachedObjectListHolder.java:148)
at javax.faces.component.UIComponentBase.restoreState(UIComponentBase.java:1612)
at javax.faces.component.UIOutput.restoreState(UIOutput.java:286)
at javax.faces.component.UIInput.restoreState(UIInput.java:1420)
at com.sun.faces.application.view.FaceletPartialStateManagementStrategy$2.visit(FaceletPartialStateManagementStrategy.java:379)
... 43 more
  • The front end gets paid peg.status? Try to print it on the screen.

  • When placing in an output does not show the screen but an error screen with the message com.sun.faces.facelets.el.Tagmethodexpression cannot be cast to [Ljava.lang.Object;

  • I tried using a convert <h:outputText value="#{Peg.status}" convert="statusImpressaoEnumConverter" /> but gave the same error.

  • I think q have little information to define why of the error. Put the entire stacktrace error, please.

1 answer

0


You can do a method within the Enum class itself by asking it this way

public boolean isCancelado(){
 return this.descricao.equals("Cancelado");
}

And then just make the call on the page

<tag rendered="#{peg.status.isCancelado()}" />

Browser other questions tagged

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