Blame |
Last modification |
View Log
| Download
| RSS feed
package br.com.ec.domain.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Size;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.validator.constraints.NotEmpty;
import br.com.ec.core.interfaces.Alterar;
import br.com.ec.core.interfaces.Cadastrar;
import br.com.ec.core.util.StringUtil;
import br.com.ec.core.util.VerificadorUtil;
import br.com.ec.domain.model.tipos.TipoSexo;
import br.com.ec.domain.util.ValidadorCpfCnpjUtil;
import br.com.ec.web.converters.FormatarUtil;
@
Entity
@Table
(name=
"sec_cliente", schema=
"sc_sec")
public class Cliente
implements Serializable {
private static final long serialVersionUID = 1L
;
private String nome
;
private Long sequencial
;
private String cpfCnpj
;
private String sexo
;
private String email
;
private String contato
;
private String telefone
;
private String endereco
;
private String observacao
;
private Date dataNascimento
;
private Date dataCadastro
;
private Boolean indicadorNovidades
;
private Boolean ativo
;
private Modelo modelo
;
private Set<Venda
> listaVendas
;
private Set<EstoqueAuditoria
> listaTrocas
;
private Set<Cupom
> listaCupons
;
private List<Pedido
> pedidos =
new ArrayList<Pedido
>();
public Cliente
() {
listaVendas =
new HashSet<Venda
>();
listaTrocas =
new HashSet<EstoqueAuditoria
>();
listaCupons=
new HashSet<Cupom
>();
}
@Id
@SequenceGenerator
(name =
"sq_cliente")
@GeneratedValue
(strategy = GenerationType.
IDENTITY)
@Column
(name=
"seq_cliente", nullable=
false)
public Long getSequencial
() {
return sequencial
;
}
public void setSequencial
(Long sequencial
) {
this.
sequencial = sequencial
;
}
@Column
(name=
"dsc_nome")
@
NotEmpty(message =
"Parâmetro obrigatório não preenchido: Nome", groups =
{Cadastrar.
class, Alterar.
class})
@Size
(max =
120, message =
"Limite de caracteres ultrapassado: Nome")
public String getNome
() {
return nome
;
}
public void setNome
(String nome
) {
this.
nome = StringUtil.
setarUpperCaseComTrim(nome.
trim());
}
@Column
(name=
"dsc_cpf_cnpj")
@Size
(max =
14, message =
"Limite de caracteres ultrapassado: CPF/CNPJ")
@
NotEmpty(message =
"Parâmetro obrigatório não preenchido: CPF/CNPJ", groups =
{Cadastrar.
class, Alterar.
class})
public String getCpfCnpj
() {
return cpfCnpj
;
}
public void setCpfCnpj
(String cpfCnpj
) {
this.
cpfCnpj = cpfCnpj
;
}
@Column
(name=
"tip_sexo")
public String getSexo
() {
return sexo
;
}
public void setSexo
(String sexo
) {
this.
sexo = sexo
;
}
@Column
(name=
"dsc_email")
@Size
(max =
120, message =
"Limite de caracteres ultrapassado: Email")
public String getEmail
() {
return email
;
}
public void setEmail
(String email
) {
this.
email = email
;
}
@Column
(name=
"dsc_contato")
@Size
(max =
120, message =
"Limite de caracteres ultrapassado: Contato")
public String getContato
() {
return contato
;
}
public void setContato
(String contato
) {
this.
contato = contato
;
}
@Column
(name=
"dsc_telefone")
@Size
(max =
120, message =
"Limite de caracteres ultrapassado: Telefone")
public String getTelefone
() {
return telefone
;
}
public void setTelefone
(String telefone
) {
this.
telefone = telefone
;
}
@Column
(name=
"dsc_endereco")
@Size
(max =
250, message =
"Limite de caracteres ultrapassado: Endereço")
public String getEndereco
() {
return endereco
;
}
public void setEndereco
(String endereco
) {
this.
endereco = StringUtil.
setarUpperCaseComTrim(endereco
);
}
@Column
(name=
"dsc_observacao")
@Size
(max =
240, message =
"Limite de caracteres ultrapassado: Observação")
public String getObservacao
() {
return observacao
;
}
public void setObservacao
(String observacao
) {
this.
observacao = StringUtil.
setarUpperCaseComTrim(observacao
);
}
@Column
(name=
"dat_nascimento")
public Date getDataNascimento
() {
return dataNascimento
;
}
public void setDataNascimento
(Date dataNascimento
) {
this.
dataNascimento = dataNascimento
;
}
@Column
(name=
"dat_cadastro")
public Date getDataCadastro
() {
return dataCadastro
;
}
public void setDataCadastro
(Date dataCadastro
) {
this.
dataCadastro = dataCadastro
;
}
@Column
(name=
"ind_novidades")
public Boolean getIndicadorNovidades
() {
return indicadorNovidades
;
}
public void setIndicadorNovidades
(Boolean indicadorNovidades
) {
this.
indicadorNovidades = indicadorNovidades
;
}
@Column
(name=
"ind_ativo", nullable=
false)
public Boolean getAtivo
() {
return ativo
;
}
public void setAtivo
(Boolean ativo
) {
this.
ativo = ativo
;
}
@ManyToOne
@ForeignKey
(name=
"fk_cliente_modelo")
@JoinColumn
(name =
"seq_modelo", nullable=
true)
public Modelo getModelo
() {
return modelo
;
}
public void setModelo
(Modelo modelo
) {
this.
modelo = modelo
;
}
@OneToMany
(mappedBy=
"cliente", fetch=FetchType.
LAZY, orphanRemoval=
false)
public Set<Venda
> getListaVendas
() {
return listaVendas
;
}
public void setListaVendas
(Set<Venda
> listaVendas
) {
this.
listaVendas = listaVendas
;
}
@OneToMany
(mappedBy=
"cliente", fetch=FetchType.
LAZY, orphanRemoval=
false)
public Set<EstoqueAuditoria
> getListaTrocas
() {
return listaTrocas
;
}
public void setListaTrocas
(Set<EstoqueAuditoria
> listaTrocas
) {
this.
listaTrocas = listaTrocas
;
}
@OneToMany
(mappedBy=
"cliente", fetch=FetchType.
LAZY, orphanRemoval=
false)
public Set<Cupom
> getListaCupons
() {
return listaCupons
;
}
public void setListaCupons
(Set<Cupom
> listaCupons
) {
this.
listaCupons = listaCupons
;
}
@Transient
public List<Pedido
> getPedidos
() {
return pedidos
;
}
public void setPedidos
(List<Pedido
> pedidos
) {
this.
pedidos = pedidos
;
}
@
Override
public int hashCode
() {
final int prime =
31;
int result =
1;
result = prime
* result
+
((sequencial ==
null) ? 0 : sequencial.
hashCode());
return result
;
}
@
Override
public boolean equals
(Object obj
) {
if (this == obj
)
return true;
if (obj ==
null)
return false;
if (getClass
() != obj.
getClass())
return false;
Cliente other =
(Cliente
) obj
;
if (sequencial ==
null) {
if (other.
sequencial !=
null)
return false;
} else if (!sequencial.
equals(other.
sequencial))
return false;
return true;
}
@Transient
public String getDescricaoDoModelo
() {
return VerificadorUtil.
naoEstaNulo(getModelo
())? getModelo
().
getDescricao() :
"";
}
@Transient
public String getDescricaoDoSexo
() {
return VerificadorUtil.
naoEstaNulo(getSexo
())? TipoSexo.
parse(getSexo
()).
getDescricao():
"";
}
@Transient
public String getCpfCnpjFormatado
() {
return VerificadorUtil.
naoEstaNulo(getCpfCnpj
())? FormatarUtil.
retornarCpfCnpjFormatado(getCpfCnpj
()) :
"";
}
@Transient
public Boolean ehCpf
() {
if (VerificadorUtil.
naoEstaNulo(getCpfCnpj
()) && getCpfCnpj
().
length() ==
11) {
return ValidadorCpfCnpjUtil.
isCPF(getCpfCnpj
());
}
return false;
}
@Transient
public Boolean ehCnpj
() {
if (VerificadorUtil.
naoEstaNulo(getCpfCnpj
()) && getCpfCnpj
().
length() ==
14) {
return ValidadorCpfCnpjUtil.
isCNPJ(getCpfCnpj
());
}
return false;
}
@Transient
public Boolean cpfCnpjEhValido
() {
return ehCpf
() || ehCnpj
()? true :
false;
}
@Transient
public List<Venda
> getVendasOrdenadas
() {
List<Venda
> vendasOrdenadas =
null;
if (VerificadorUtil.
naoEstaNulo(getListaVendas
())) {
vendasOrdenadas =
new ArrayList<Venda
>(getListaVendas
());
Collections.
sort(vendasOrdenadas,
new Comparator<Venda
>() {
public int compare
(Venda p1, Venda p2
) {
return p2.
getDataVenda().
compareTo(p1.
getDataVenda());
};
});
}
return vendasOrdenadas
;
}
@Transient
public List<EstoqueAuditoria
> getTrocasOrdenadas
() {
List<EstoqueAuditoria
> trocasOrdenadas =
null;
if (VerificadorUtil.
naoEstaNulo(getListaTrocas
())) {
trocasOrdenadas =
new ArrayList<EstoqueAuditoria
>(getListaTrocas
());
Collections.
sort(trocasOrdenadas,
new Comparator<EstoqueAuditoria
>() {
public int compare
(EstoqueAuditoria p1, EstoqueAuditoria p2
) {
return p2.
getDataAtualizacao().
compareTo(p1.
getDataAtualizacao());
};
});
}
return trocasOrdenadas
;
}
@Transient
public List<Cupom
> getCuponsOrdenados
() {
List<Cupom
> cuponsOrdenados =
null;
if (VerificadorUtil.
naoEstaNulo(getListaCupons
())) {
cuponsOrdenados =
new ArrayList<Cupom
>(getListaCupons
());
Collections.
sort(cuponsOrdenados,
new Comparator<Cupom
>() {
public int compare
(Cupom p1, Cupom p2
) {
return p2.
getDataHoraEmissao().
compareTo(p1.
getDataHoraEmissao());
};
});
}
return cuponsOrdenados
;
}
}