p:dialog being called by several Buttons in the system

Asked

Viewed 117 times

0

Personal I have a dialog (do primefaces) confirming the ZIP Code number and after the user has entered the residence number in this dialog the fields address, neighborhood, zip code, city and Uf have to be filled in the form. The problem is that this dialog can be called in several places of the system where you have ZIP code to validate, in the same form if you have more than one ZIP code. So how do I get the button to confirm the dialog perform different methods and beans different ?

PS: I am using JSF+PrimeFaces.

Follows the code:

Dialog:

<ui:composition xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
            xmlns="http://www.w3.org/1999/xhtml"
            xmlns:h="http://xmlns.jcp.org/jsf/html"
            xmlns:p="http://primefaces.org/ui">
<p:dialog widgetVar="cepConfirmDlg" modal="true" responsive="true" header="Confirmar Endereço" resizable="false" width="630" closable="false" >
    <p:panelGrid id="panelMensagem" columns="1" layout="grid" class="semBorda" rendered="#{buscaCEPMB.renderizaPanel = 1}">
    </p:panelGrid>>
    <p:panelGrid id="panelConfirma" columns="1" layout="grid" class="semBorda" rendered="#{buscaCEPMB.renderizaPanel = 3}"  >
    <p:panelGrid id="cepConfirmPanel" columns="1" layout="grid" class="semBorda">
    <p:messages/>
    <h:panelGroup>
        <p:outputLabel value="Logradouro: "/><p:outputLabel value="#{buscaCEPMB.viaEndereco.logradouro}" class="labelFine" />
    </h:panelGroup><h:panelGroup>
        <p:outputLabel value="Complemento: "/><p:outputLabel value="#{buscaCEPMB.viaEndereco.complemento}" class="labelFine" />
    </h:panelGroup><h:panelGroup>
        <p:outputLabel value="Bairro: "/><p:outputLabel value="#{buscaCEPMB.viaEndereco.bairro}" class="labelFine" />
    </h:panelGroup><h:panelGroup>
        <p:outputLabel value="   CEP: "/><p:outputLabel value="#{buscaCEPMB.viaEndereco.cep}" class="labelFine" />
    </h:panelGroup><h:panelGroup>
        <p:outputLabel value="Cidade/UF: "/><p:outputLabel value="#{buscaCEPMB.viaEndereco.localidade}/#{buscaCEPMB.viaEndereco.uf}" class="labelFine" />
    </h:panelGroup>
    <p:fieldset>
        <p:panelGrid columns="1" layout="grid" class="semBorda" >
                <p:outputLabel value="Endereço" for="confEndereco"/>
                <p:inputText id="confEndereco" value="#{cepMB.endereco}" maxlength="75" styleClass="Wid90" required="true" requiredMessage="É Obrigatório Informar o Endereço do Imóvel!"/>
                <p:outputLabel value="Nr. e Complemento" for="confNr"/>
                <p:inputText id="confNr" value="#{cepMB.nrComp}" maxlength="25" required="true" requiredMessage="É Obrigatório Informar o Número do Imóvel!"/>
                <p:outputLabel value="Bairro" for="confBairro"/>
                <p:inputText id="confBairro" value="#{cepMB.bairro}" maxlength="25" styleClass="Wid60" required="true" requiredMessage="É Obrigatório Informar o Bairro do Imóvel!"/>
               </p:panelGrid>   
        </p:fieldset>
    </p:panelGrid>
    </p:panelGrid>>

A canvas form with include dialog and with 2 buttons to search for zip code (I need to call and confirm the zip code at the home address as in commercial:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://xmlns.jcp.org/jsf/html"
                xmlns:f="http://xmlns.jcp.org/jsf/core"
                xmlns:p="http://primefaces.org/ui"
                template="/WEB-INF/template.xhtml"
                xmlns:oz="http://xmlns.jcp.org/jsf/composite/ozelo/componentes"
                xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">

    <ui:define name="content">
 <p:panel id="formCliPF">
    <p:panelGrid columns="2" layout="grid" class="semBorda" columnClasses="ui-grid-col-1, ui-grid-col-11">
           <div class="SoftBlueBack DispInlBlock" style="padding:20px 10px;">
                <i style="margin: 0 auto;" class="oz-PFM Blue Fs100"></i>
                <p></p>
                <p:rating stars="5" cancel="false" value="#{clienteMB.cliSel.star}" />
           </div>
        <p:panelGrid columns="1" layout="grid" class="semBorda">
      <p:outputLabel for="apelido" value="Apelido:" />
      <p:inputText id="apelido" value="#{clienteMB.cliSel.apelido}" maxlength="25"/>
      <p:outputLabel for="nome" value="Nome:" />
      <p:inputText id="nome" value="#{clienteMB.cliSel.nome}" maxlength="120" styleClass="Wid100" requiredMessage="Nome do Cliente É Obrigatório!"/>
          </p:panelGrid>
    <p:outputLabel/>   
    <p:panelGrid columns="1" layout="grid" class="semBorda">
    <p:panelGrid columns="3" layout="grid" class="semBorda">
      <p:outputLabel for="rg" value="RG:" />
        <p:outputLabel for="cpf" value="CPF:" />
        <p:outputLabel/>
      <p:inputText id="rg" value="#{clienteMB.cliSel.rg}" />
        <p:inputMask id="cpf" value="#{clienteMB.cliSel.cpf}" mask="999.999.999-99" />
    </p:panelGrid>
        <p:panelGrid columns="2" layout="grid" class="semBorda">
        <p:outputLabel for="sexo" value="Sexo:" />
        <p:outputLabel for="dtNasc" value="Data Nascimento:"/>
        <p:selectOneMenu id="sexo" value="#{clienteMB.cliSel.sexo}" >
        <f:selectItem itemLabel="Fem." itemValue="F" />
        <f:selectItem itemLabel="Masc." itemValue="M" />
        </p:selectOneMenu>
        <p:calendar id="dtNasc" locale="pt" navigator="true" pattern="dd/MM/yyyy" value="#{clienteMB.cliSel.dtNasc}"/>
        </p:panelGrid>
        <p:fieldset>
        <p:panelGrid columns="4" layout="grid" class="semBorda"> 
            <p:outputLabel value="Telefones:" /><p:outputLabel/><p:outputLabel/><p:outputLabel/>
        <p:inputText value="#{clienteMB.cliSel.tel1}" styleClass="telMask" />
        <p:inputText value="#{clienteMB.cliSel.tel2}" styleClass="telMask" />
        <p:inputText value="#{clienteMB.cliSel.tel3}" styleClass="telMask" />
        <p:inputText value="#{clienteMB.cliSel.tel4}" styleClass="telMask" />
        </p:panelGrid>
        <p:panelGrid columns="3" layout="grid" class="semBorda" columnClasses="ui-grid-col-3, ui-grid-col-3, ui-grid-col-6">
        <p:outputLabel for="telcial" value="Tel. Cial.:" />
        <p:outputLabel for="nextel" value="Nextel:" />
        <p:outputLabel for="email" value="e-Mail:" />
        <p:inputText id="telcial" value="#{clienteMB.cliSel.cel}" styleClass="telMask" />
        <p:inputText id="nextel" value="#{clienteMB.cliSel.nextel}" />
        <p:inputText id="email" value="#{clienteMB.cliSel.email}" styleClass="Wid90"/>
        </p:panelGrid>
        </p:fieldset>
        <p:outputLabel for="endereco" value="Endereço:" />
       <p:panel id="endRes">
        <p:inputText id="endereco" value="#{clienteMB.cliSel.endereco}" maxlength="100" styleClass="Wid90"/>
       <p:panelGrid columns="2" layout="grid" class="semBorda"> 
        <p:outputLabel for="bairro" value="Bairro:" />
        <p:outputLabel for="cep" value="CEP:" />
        <p:inputText id="bairro" value="#{clienteMB.cliSel.bairro}" maxlength="25" styleClass="Wid90"/>
        <h:panelGroup>
        <p:inputMask id="cep" value="#{clienteMB.cliSel.cep}" mask="99999-999" /><p:spacer width="5"/>

        <p:commandButton icon="fa fa-envelope Fs16 White" value="cep" process="endRes"  action="#{clienteMB.buscaCEPRes()}" update="endRes growl" />
        </h:panelGroup>
       </p:panelGrid>
        <p:panelGrid columns="2" layout="grid" class="semBorda" columnClasses="ui-grid-col-10, ui-grid-col-2">
        <p:outputLabel for="cidade" value="Cidade:" />
        <p:outputLabel for="uf" value="UF:" />
        <p:inputText id="cidade" value="#{clienteMB.cliSel.cidade}" styleClass="Wid90"/>
        <p:selectOneMenu id="uf" value="#{clienteMB.cliSel.uf}" >
            <f:selectItems value="#{clienteMB.ufs}"  />
        </p:selectOneMenu>
        </p:panelGrid>
       </p:panel>
        <p:fieldset id="endCom">
    <p:outputLabel for="empresa" value="Empresa Nome:"/>
    <p:inputText id="empresa" value="#{clienteMB.cliFinSel.empresaNome}" maxlength="60" styleClass="Wid90"/>
    <p:outputLabel for="empresaEnd" value="Endereço:"/>
    <p:inputText id="empresaEnd" value="#{clienteMB.cliFinSel.empresaEnd}" maxlength="100" styleClass="Wid90"/>
    <p:panelGrid columns="2" layout="grid" class="semBorda"> 
    <p:outputLabel for="empresaBai" value="Bairro:"/>
    <p:outputLabel for="empresaCep" value="CEP:" />
    <p:inputText id="empresaBai" value="#{clienteMB.cliFinSel.empresaBai}" maxlength="25" styleClass="Wid90"/>
    <h:panelGroup><p:inputMask id="empresaCep" value="#{clienteMB.cliFinSel.empresaCEP}" mask="99999-999" /><p:spacer width="5"/>
      <p:commandButton icon="fa fa-envelope Fs16 White" value="cep" process="endCom"  action="#{clienteMB.buscaCEPCom()}" update="endCom growl" /></h:panelGroup>
    </p:panelGrid>
    <p:panelGrid columns="2" layout="grid" class="semBorda" columnClasses="ui-grid-col-10, ui-grid-col-2">
    <p:outputLabel for="empresaCid" value="Cidade:"/>
    <p:outputLabel for="empresaUf" value="UF:"/>
    <p:inputText id="empresaCid" value="#{clienteMB.cliFinSel.empresaCidade}" maxlength="50" styleClass="Wid90"/>
    <p:selectOneMenu id="empresaUf" value="#{clienteMB.cliFinSel.empresaUF}" >
      <f:selectItems value="#{clienteMB.ufs}"  />
    </p:selectOneMenu>
    </p:panelGrid>
    <p:panelGrid columns="3" layout="grid" class="semBorda" columnClasses="ui-grid-col-4, ui-grid-col-3, ui-col-4">
    <p:outputLabel for="empresaTel" value="Telefone:"/>
    <p:inputText id="empresaTel" value="#{clienteMB.cliSel.cel}" styleClass="telMask" />
    </p:panelGrid>
    </p:fieldset>   

    </p:panelGrid>
    </p:panelGrid>
    </div>  
        <p:messages />
  <p:commandButton value="Cancelar" icon="fa fa-times Fs16 White" styleClass="Fright RedButton" action="#{clienteMB.doCancela()}" />
  <p:commandButton value="Salvar" icon="fa fa-check Fs16 White" class="Fright" action="#{clienteMB.doAddCli}" process="formCliPF" update="formCliPF" />
 </p:panel>
    <ui:include src="../cepConfirma.xhtml"/>
</ui:define>
</ui:composition>

The ClienteMB that controls the form and calls the CepMB that displays the dialog:

public void buscaCEPRes() {
     cepMB.confirmaCEP(cliSel.getCep(), cliSel.getEndereco(), cliSel.getCidade(), cliSel.getUf());
}

public void buscaCEPCom() {
     cepMB.confirmaCEP(cliSel.getEmpCep(), cliSel.getEmpEndereco(), cliSel.getEmpCidade(), cliSel.getEmpUf());
}

The CepMB controls the dialog with the zip search:

    public void confirmaCEP(String nrCEP, String valEndereco, String valCidade, String valUf) throws IOException,IllegalArgumentException {
        ViaCEPClient client = new ViaCEPClient();
        viaEndereco = null;
        listaViaEndereco = null;
        okCEP = false;
try{
        if (StringUtils.isNotBlank(nrCEP)){   
           viaEndereco = client.getEndereco(nrCEP);
if (endereco != null) {carregaEnderecoParaConfirma();
}else{createFacesErrorMessage("CEP Inválido", "Não Foi Possível Localizar CEP!");}
   }else{ if (!StringUtils.isNotBlank(valCidade)||!StringUtils.isNotBlank(valUf)){
    createFacesErrorMessage("Dados Inválidos", "É Necessário Informar no Mínimo a Cidade e o Estado!");
}else{
     listaViaEndereco = client.getEnderecos(valUf, valCidade, valEndereco);
  if (listaViaEndereco != null) {carregaListaEnderecoParaConfirma();
}else{createFacesErrorMessage("Endereço Inválido", "Não Foi Possível Localizar CEP!");}
    }
}
 }catch (IOException e){
   createFacesErrorMessage("Erro I/O", "Não Foi Possível Conectar com o Servidor de CEP!");  
 }catch (IllegalArgumentException e){
        createFacesErrorMessage("CEP Inválido", e.getMessage());
 }


    }

public void carregaEnderecoParaConfirma(){
    endereco = viaEndereco.getLogradouro();
    bairro = viaEndereco.getBairro();
    nrComp = "";
    RequestContext.getCurrentInstance().update("form:cepConfirmPanel");
    RequestContext.getCurrentInstance().execute("PF('cepConfirmDlg').show();");
    }    

The method of the Confirm dialog button did not do.

  • Hello Ozelo, could post a MVCE demonstrating the problem? I got the impression that you really need is a Composite Component for these Buttons where you can indicate the action and other details of this dialog zip code. But you can only get an idea with an example code.

  • Will the dialog be called by different bean or will different Bean be processed during your call? If it is the first case can be solved using polymorphism.

  • Anthony, the buttons that call the dialog that changes and not the Confirm button in the dialog, so I don’t think Composite Component would apply to that. Giuliana, when you talk about polymorphism in this case would pass which client.endereco, client.bairro, client.cep... to the Cepmb?

  • Look at another case: suppose I have a dialog with a datatable on it listing all my clients and I select a client to be assigned to a given field. Once this dialog is called to assign to the Customer the Order of Sale, another screen calls to assign the Customer for indication registration, other times for several other attributes Client of the system. How will I get the Confirm button of this "know" dialog to which attribute I want to reference?

1 answer

0

I do not believe it is the best alternative, because it can disturb and very much the maintenance, since you start to play a little of the programming for the View.

But vc can pass an object as a method parameter, or even set a value in the Bean before performing the action. In both cases, you can build an Object from the pattern Command and the execution shall be left to that object. With this, even if the Bean is the same, Command will already be ready to execute the best method for each area.

Browser other questions tagged

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