Advertisement
Guest User

CadastroEstabelecimentoBean

a guest
Mar 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.09 KB | None | 0 0
  1. package br.com.rang.social.bean.estabelecimento;
  2.  
  3. import java.io.Serializable;
  4. import java.util.ArrayList;
  5. import java.util.Date;
  6. import java.util.Iterator;
  7. import java.util.List;
  8.  
  9. import javax.annotation.PostConstruct;
  10. import javax.faces.application.FacesMessage;
  11. import javax.faces.component.UIComponent;
  12. import javax.faces.context.FacesContext;
  13. import javax.faces.model.SelectItem;
  14. import javax.faces.validator.ValidatorException;
  15. import javax.faces.view.ViewScoped;
  16. import javax.inject.Inject;
  17. import javax.inject.Named;
  18.  
  19. import org.omnifaces.util.Messages;
  20.  
  21. import br.com.rang.social.annotation.ExceptionHandler;
  22. import br.com.rang.social.persistence.dao.ConselhoDAO;
  23. import br.com.rang.social.persistence.dao.EmailDAO;
  24. import br.com.rang.social.persistence.dao.EstadoDAO;
  25. import br.com.rang.social.persistence.dao.MunicipioDAO;
  26. import br.com.rang.social.persistence.dao.TelefoneDAO;
  27. import br.com.rang.social.persistence.dao.EstabelecimentoDAO;
  28. import br.com.rang.social.persistence.dao.TipoLogradouroDAO;
  29. import br.com.rang.social.persistence.model.Conselho;
  30. import br.com.rang.social.persistence.model.Email;
  31. import br.com.rang.social.persistence.model.Endereco;
  32. import br.com.rang.social.persistence.model.Estado;
  33. import br.com.rang.social.persistence.model.Municipio;
  34. import br.com.rang.social.persistence.model.Telefone;
  35. import br.com.rang.social.persistence.model.Estabelecimento;
  36. import br.com.rang.social.persistence.model.TipoLogradouro;
  37. import br.com.rang.social.validators.ValidaCep;
  38.  
  39. @Named
  40. @ViewScoped
  41. public class CadastroEstabelecimentoBean implements Serializable {
  42.  
  43.     private final EstabelecimentoDAO estabelecimentoDAO;
  44.     private final MunicipioDAO municipioDAO;
  45.     private final TipoLogradouroDAO tipoLogradouroDAO;
  46.     private final EstadoDAO estadoDAO;
  47.     private final EmailDAO emailDAO;
  48.     private final TelefoneDAO telefoneDAO;
  49.  
  50.     private Estabelecimento estabelecimento;
  51.     private Telefone telefone;
  52.     private Email email;
  53.  
  54.     private List<Municipio> municipioList;
  55.     private List<Estado> estadoList;
  56.     private List<Telefone> telefoneExcluir;
  57.     private List<Email> emailExcluir;
  58.  
  59.     private Date currentDate;
  60.  
  61.     @Inject
  62.     public CadastroEstabelecimentoBean(EstabelecimentoDAO estabelecimentoDAO, MunicipioDAO municipioDAO,
  63.             TipoLogradouroDAO tipoLogradouroDAO, EstadoDAO estadoDAO, EmailDAO emailDAO, TelefoneDAO telefoneDAO) {
  64.         this.estabelecimentoDAO = estabelecimentoDAO;
  65.         this.municipioDAO = municipioDAO;
  66.         this.tipoLogradouroDAO = tipoLogradouroDAO;
  67.         this.estadoDAO = estadoDAO;
  68.         this.emailDAO = emailDAO;
  69.         this.telefoneDAO = telefoneDAO;
  70.     }
  71.  
  72.     @PostConstruct
  73.     public void init() {
  74.         inicializaEstabelecimento();
  75.         inicializaLista();
  76.     }
  77.  
  78.     public void inicializaEstabelecimento() {
  79.         estabelecimento = new Estabelecimento();
  80.         email = new Email();
  81.         telefone = new Telefone();
  82.         estabelecimento.setEndereco(new Endereco());
  83.         currentDate = new Date();
  84.     }
  85.  
  86.     public void inicializaLista() {
  87.         estadoList = estadoDAO.carregaEstadosAtivos();
  88.     }
  89.  
  90.     @ExceptionHandler
  91.     public void save() {
  92.         System.out.println("entrou no save");
  93.         System.out.println(estabelecimento.toString());
  94.         estabelecimentoDAO.create(estabelecimento);
  95.         Messages.create("Estabelecimento {0} adicionado com sucesso.", estabelecimento.getNome()).flash().add();
  96.         inicializaEstabelecimento();
  97.         inicializaLista();
  98.     }
  99.  
  100.     public List<Municipio> acMunicipio(String query) {
  101.         return municipioDAO.carregaMunicipioAtivo(query);
  102.     }
  103.  
  104.     public List<TipoLogradouro> acTipoLogradouro(String query) {
  105.         return tipoLogradouroDAO.carregaTipoLogradouroAtivo(query);
  106.     }
  107.  
  108.     public void validaCEP(FacesContext context, UIComponent component, Object value) throws ValidatorException {
  109.         if (estabelecimento.getEndereco() != null) {
  110.             if (estabelecimento.getEndereco().getMunicipio() != null) {
  111.                 if (estabelecimento.getEndereco().getMunicipio().getEstado() != null) {
  112.                     if (estabelecimento.getEndereco().getMunicipio().getEstado().getSigla() != null) {
  113.                         String uf = estabelecimento.getEndereco().getMunicipio().getEstado().getSigla();
  114.                         String s = value.toString();
  115.                         String mask = s.substring(0, 5);
  116.                         Integer cep = new Integer(mask);
  117.                         ValidaCep validaCep = new ValidaCep();
  118.                         Boolean valido = validaCep.validator(cep, uf);
  119.                         if (!valido) {
  120.                             FacesMessage msg = new FacesMessage("CEP Inválido.");
  121.                             msg.setSeverity(FacesMessage.SEVERITY_ERROR);
  122.                             throw new ValidatorException(msg);
  123.                         }
  124.                     }
  125.                 }
  126.             }
  127.         }
  128.     }
  129.  
  130.     public final void addEmail() {
  131.         if (estabelecimento.getEmailList() == null) {
  132.             estabelecimento.setEmailList(new ArrayList<Email>());
  133.         }
  134.         boolean jaTem = false;
  135.         for (int x = 0; x < estabelecimento.getEmailList().size(); x++) {
  136.             if (email.getDescricao().equals(estabelecimento.getEmailList().get(x).getDescricao())) {
  137.                 jaTem = true;
  138.                 break;
  139.             }
  140.  
  141.         }
  142.         if (jaTem) {
  143.             Messages.create("Email já informado").warn().add();
  144.         } else {
  145.             email.setDataCadastro(new Date());
  146.             email.setEstabelecimento(estabelecimento);
  147.             estabelecimento.getEmailList().add(email);
  148.             email = new Email();
  149.         }
  150.     }
  151.  
  152.     public void deletaEmail(Email email) {
  153.         if (emailExcluir == null) {
  154.             emailExcluir = new ArrayList<>();
  155.         }
  156.         for (int x = 0; x < estabelecimento.getEmailList().size(); x++) {
  157.             if (estabelecimento.getEmailList().get(x) == email) {
  158.                 if (email.getId() != null) {
  159.                     emailExcluir.add(email);
  160.                 }
  161.                 estabelecimento.getEmailList().remove(x);
  162.                 break;
  163.             }
  164.         }
  165.     }
  166.  
  167.     public final void addTelefone() {
  168.         try {
  169.             if (estabelecimento.getTelefoneList() == null) {
  170.                 estabelecimento.setTelefoneList(new ArrayList<Telefone>());
  171.             }
  172.             boolean jaTem = false;
  173.             for (int x = 0; x < estabelecimento.getTelefoneList().size(); x++) {
  174.                 if (telefone.getTelefone().equals(estabelecimento.getTelefoneList().get(x).getTelefone())) {
  175.                     jaTem = true;
  176.                     break;
  177.                 }
  178.             }
  179.             if (jaTem) {
  180.                 throw new Exception("Telefone já informado!");
  181.             }
  182.  
  183.             if (telefone.isPrincipal()) {
  184.                 for (int x = 0; x < estabelecimento.getTelefoneList().size(); x++) {
  185.                     estabelecimento.getTelefoneList().get(x).setPrincipal(false);
  186.                 }
  187.             }
  188.             telefone.setDataCadastro(new Date());
  189.             telefone.setEstabelecimento(estabelecimento);
  190.             estabelecimento.getTelefoneList().add(telefone);
  191.             telefone = new Telefone();
  192.  
  193.         } catch (Exception e) {
  194.             Messages.create(e.getMessage()).warn().add();
  195.         }
  196.     }
  197.  
  198.     public void deletaTelefone(Telefone telefone) {
  199.         if (telefoneExcluir == null) {
  200.             telefoneExcluir = new ArrayList<>();
  201.         }
  202.         for (int x = 0; x < estabelecimento.getTelefoneList().size(); x++) {
  203.             if (estabelecimento.getTelefoneList().get(x) == telefone) {
  204.                 if (telefone.getId() != null) {
  205.                     telefoneExcluir.add(telefone);
  206.                 }
  207.                 estabelecimento.getTelefoneList().remove(x);
  208.                 break;
  209.             }
  210.         }
  211.     }
  212.  
  213.     public Estabelecimento getEstabelecimento() {
  214.         return estabelecimento;
  215.     }
  216.  
  217.     public void setEstabelecimento(Estabelecimento estabelecimento) {
  218.         this.estabelecimento = estabelecimento;
  219.     }
  220.  
  221.     public List<Municipio> getMunicipioList() {
  222.         return municipioList;
  223.     }
  224.  
  225.     public void setMunicipioList(List<Municipio> municipioList) {
  226.         this.municipioList = municipioList;
  227.     }
  228.  
  229.     public Date getCurrentDate() {
  230.         return currentDate;
  231.     }
  232.  
  233.     public void setCurrentDate(Date currentDate) {
  234.         this.currentDate = currentDate;
  235.     }
  236.  
  237.     public Telefone getTelefone() {
  238.         return telefone;
  239.     }
  240.  
  241.     public void setTelefone(Telefone telefone) {
  242.         this.telefone = telefone;
  243.     }
  244.  
  245.     public Email getEmail() {
  246.         return email;
  247.     }
  248.  
  249.     public void setEmail(Email email) {
  250.         this.email = email;
  251.     }
  252.  
  253.     public List<Estado> getEstadoList() {
  254.         return estadoList;
  255.     }
  256.  
  257.     public void setEstadoList(List<Estado> estadoList) {
  258.         this.estadoList = estadoList;
  259.     }
  260.  
  261.     public List<Telefone> getTelefoneExcluir() {
  262.         return telefoneExcluir;
  263.     }
  264.  
  265.     public void setTelefoneExcluir(List<Telefone> telefoneExcluir) {
  266.         this.telefoneExcluir = telefoneExcluir;
  267.     }
  268.  
  269.     public List<Email> getEmailExcluir() {
  270.         return emailExcluir;
  271.     }
  272.  
  273.     public void setEmailExcluir(List<Email> emailExcluir) {
  274.         this.emailExcluir = emailExcluir;
  275.     }
  276.  
  277.    
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement