p: dialog being called by several buttons in the system

0

Personnel I have a dialog (of primefaces ) that confirms the number of CEP and after the user to inform the number of the residence in this dialog the fields address, neighborhood, cep, 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 they have CEP to validate, in the same form if they have more than one CEP. So how do I get the Confirm button from dialog to execute different methods and beans different?

PS: I'm using JSF+PrimeFaces .

Follow 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 form screen with include of the dialog and with 2 buttons for the ZIP code search (I need to call and confirm the zip code in the home address as in the commercial one:

<?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 shows 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 it.

    
asked by anonymous 01.09.2016 / 06:10

1 answer

0

I do not think it's the best alternative, because it can mess up a lot of maintenance, since you start playing some programming for View.

But you can pass an object as a method parameter, or even set a value in the Bean before executing the action. In both cases, you can construct an Object of the standard Command and execution is borne by that object. With this, even if the Bean is the same, Command will already be prepared to execute the best method for each area.

    
02.09.2016 / 22:03