Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.com.rang.social.bean.estabelecimento;
- import java.io.Serializable;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.Iterator;
- import java.util.List;
- import javax.annotation.PostConstruct;
- import javax.faces.application.FacesMessage;
- import javax.faces.component.UIComponent;
- import javax.faces.context.FacesContext;
- import javax.faces.model.SelectItem;
- import javax.faces.validator.ValidatorException;
- import javax.faces.view.ViewScoped;
- import javax.inject.Inject;
- import javax.inject.Named;
- import org.omnifaces.util.Messages;
- import br.com.rang.social.annotation.ExceptionHandler;
- import br.com.rang.social.persistence.dao.ConselhoDAO;
- import br.com.rang.social.persistence.dao.EmailDAO;
- import br.com.rang.social.persistence.dao.EstadoDAO;
- import br.com.rang.social.persistence.dao.MunicipioDAO;
- import br.com.rang.social.persistence.dao.TelefoneDAO;
- import br.com.rang.social.persistence.dao.EstabelecimentoDAO;
- import br.com.rang.social.persistence.dao.TipoLogradouroDAO;
- import br.com.rang.social.persistence.model.Conselho;
- import br.com.rang.social.persistence.model.Email;
- import br.com.rang.social.persistence.model.Endereco;
- import br.com.rang.social.persistence.model.Estado;
- import br.com.rang.social.persistence.model.Municipio;
- import br.com.rang.social.persistence.model.Telefone;
- import br.com.rang.social.persistence.model.Estabelecimento;
- import br.com.rang.social.persistence.model.TipoLogradouro;
- import br.com.rang.social.validators.ValidaCep;
- @Named
- @ViewScoped
- public class CadastroEstabelecimentoBean implements Serializable {
- private final EstabelecimentoDAO estabelecimentoDAO;
- private final MunicipioDAO municipioDAO;
- private final TipoLogradouroDAO tipoLogradouroDAO;
- private final EstadoDAO estadoDAO;
- private final EmailDAO emailDAO;
- private final TelefoneDAO telefoneDAO;
- private Estabelecimento estabelecimento;
- private Telefone telefone;
- private Email email;
- private List<Municipio> municipioList;
- private List<Estado> estadoList;
- private List<Telefone> telefoneExcluir;
- private List<Email> emailExcluir;
- private Date currentDate;
- @Inject
- public CadastroEstabelecimentoBean(EstabelecimentoDAO estabelecimentoDAO, MunicipioDAO municipioDAO,
- TipoLogradouroDAO tipoLogradouroDAO, EstadoDAO estadoDAO, EmailDAO emailDAO, TelefoneDAO telefoneDAO) {
- this.estabelecimentoDAO = estabelecimentoDAO;
- this.municipioDAO = municipioDAO;
- this.tipoLogradouroDAO = tipoLogradouroDAO;
- this.estadoDAO = estadoDAO;
- this.emailDAO = emailDAO;
- this.telefoneDAO = telefoneDAO;
- }
- @PostConstruct
- public void init() {
- inicializaEstabelecimento();
- inicializaLista();
- }
- public void inicializaEstabelecimento() {
- estabelecimento = new Estabelecimento();
- email = new Email();
- telefone = new Telefone();
- estabelecimento.setEndereco(new Endereco());
- currentDate = new Date();
- }
- public void inicializaLista() {
- estadoList = estadoDAO.carregaEstadosAtivos();
- }
- @ExceptionHandler
- public void save() {
- System.out.println("entrou no save");
- System.out.println(estabelecimento.toString());
- estabelecimentoDAO.create(estabelecimento);
- Messages.create("Estabelecimento {0} adicionado com sucesso.", estabelecimento.getNome()).flash().add();
- inicializaEstabelecimento();
- inicializaLista();
- }
- public List<Municipio> acMunicipio(String query) {
- return municipioDAO.carregaMunicipioAtivo(query);
- }
- public List<TipoLogradouro> acTipoLogradouro(String query) {
- return tipoLogradouroDAO.carregaTipoLogradouroAtivo(query);
- }
- public void validaCEP(FacesContext context, UIComponent component, Object value) throws ValidatorException {
- if (estabelecimento.getEndereco() != null) {
- if (estabelecimento.getEndereco().getMunicipio() != null) {
- if (estabelecimento.getEndereco().getMunicipio().getEstado() != null) {
- if (estabelecimento.getEndereco().getMunicipio().getEstado().getSigla() != null) {
- String uf = estabelecimento.getEndereco().getMunicipio().getEstado().getSigla();
- String s = value.toString();
- String mask = s.substring(0, 5);
- Integer cep = new Integer(mask);
- ValidaCep validaCep = new ValidaCep();
- Boolean valido = validaCep.validator(cep, uf);
- if (!valido) {
- FacesMessage msg = new FacesMessage("CEP Inválido.");
- msg.setSeverity(FacesMessage.SEVERITY_ERROR);
- throw new ValidatorException(msg);
- }
- }
- }
- }
- }
- }
- public final void addEmail() {
- if (estabelecimento.getEmailList() == null) {
- estabelecimento.setEmailList(new ArrayList<Email>());
- }
- boolean jaTem = false;
- for (int x = 0; x < estabelecimento.getEmailList().size(); x++) {
- if (email.getDescricao().equals(estabelecimento.getEmailList().get(x).getDescricao())) {
- jaTem = true;
- break;
- }
- }
- if (jaTem) {
- Messages.create("Email já informado").warn().add();
- } else {
- email.setDataCadastro(new Date());
- email.setEstabelecimento(estabelecimento);
- estabelecimento.getEmailList().add(email);
- email = new Email();
- }
- }
- public void deletaEmail(Email email) {
- if (emailExcluir == null) {
- emailExcluir = new ArrayList<>();
- }
- for (int x = 0; x < estabelecimento.getEmailList().size(); x++) {
- if (estabelecimento.getEmailList().get(x) == email) {
- if (email.getId() != null) {
- emailExcluir.add(email);
- }
- estabelecimento.getEmailList().remove(x);
- break;
- }
- }
- }
- public final void addTelefone() {
- try {
- if (estabelecimento.getTelefoneList() == null) {
- estabelecimento.setTelefoneList(new ArrayList<Telefone>());
- }
- boolean jaTem = false;
- for (int x = 0; x < estabelecimento.getTelefoneList().size(); x++) {
- if (telefone.getTelefone().equals(estabelecimento.getTelefoneList().get(x).getTelefone())) {
- jaTem = true;
- break;
- }
- }
- if (jaTem) {
- throw new Exception("Telefone já informado!");
- }
- if (telefone.isPrincipal()) {
- for (int x = 0; x < estabelecimento.getTelefoneList().size(); x++) {
- estabelecimento.getTelefoneList().get(x).setPrincipal(false);
- }
- }
- telefone.setDataCadastro(new Date());
- telefone.setEstabelecimento(estabelecimento);
- estabelecimento.getTelefoneList().add(telefone);
- telefone = new Telefone();
- } catch (Exception e) {
- Messages.create(e.getMessage()).warn().add();
- }
- }
- public void deletaTelefone(Telefone telefone) {
- if (telefoneExcluir == null) {
- telefoneExcluir = new ArrayList<>();
- }
- for (int x = 0; x < estabelecimento.getTelefoneList().size(); x++) {
- if (estabelecimento.getTelefoneList().get(x) == telefone) {
- if (telefone.getId() != null) {
- telefoneExcluir.add(telefone);
- }
- estabelecimento.getTelefoneList().remove(x);
- break;
- }
- }
- }
- public Estabelecimento getEstabelecimento() {
- return estabelecimento;
- }
- public void setEstabelecimento(Estabelecimento estabelecimento) {
- this.estabelecimento = estabelecimento;
- }
- public List<Municipio> getMunicipioList() {
- return municipioList;
- }
- public void setMunicipioList(List<Municipio> municipioList) {
- this.municipioList = municipioList;
- }
- public Date getCurrentDate() {
- return currentDate;
- }
- public void setCurrentDate(Date currentDate) {
- this.currentDate = currentDate;
- }
- public Telefone getTelefone() {
- return telefone;
- }
- public void setTelefone(Telefone telefone) {
- this.telefone = telefone;
- }
- public Email getEmail() {
- return email;
- }
- public void setEmail(Email email) {
- this.email = email;
- }
- public List<Estado> getEstadoList() {
- return estadoList;
- }
- public void setEstadoList(List<Estado> estadoList) {
- this.estadoList = estadoList;
- }
- public List<Telefone> getTelefoneExcluir() {
- return telefoneExcluir;
- }
- public void setTelefoneExcluir(List<Telefone> telefoneExcluir) {
- this.telefoneExcluir = telefoneExcluir;
- }
- public List<Email> getEmailExcluir() {
- return emailExcluir;
- }
- public void setEmailExcluir(List<Email> emailExcluir) {
- this.emailExcluir = emailExcluir;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement