Rev 182 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | espaco | 1 | package br.com.ec.domain.model; |
| 2 | |||
| 3 | import java.io.Serializable; |
||
| 4 | import java.util.ArrayList; |
||
| 5 | import java.util.Collections; |
||
| 6 | import java.util.Comparator; |
||
| 7 | import java.util.Date; |
||
| 8 | import java.util.HashSet; |
||
| 9 | import java.util.List; |
||
| 10 | import java.util.Set; |
||
| 11 | |||
| 12 | import javax.persistence.Column; |
||
| 13 | import javax.persistence.Entity; |
||
| 14 | import javax.persistence.FetchType; |
||
| 15 | import javax.persistence.GeneratedValue; |
||
| 16 | import javax.persistence.GenerationType; |
||
| 17 | import javax.persistence.Id; |
||
| 18 | import javax.persistence.JoinColumn; |
||
| 19 | import javax.persistence.ManyToOne; |
||
| 20 | import javax.persistence.OneToMany; |
||
| 21 | import javax.persistence.Table; |
||
| 22 | import javax.persistence.Transient; |
||
| 23 | import javax.validation.constraints.Size; |
||
| 24 | |||
| 25 | import org.hibernate.annotations.ForeignKey; |
||
| 26 | import org.hibernate.validator.constraints.NotEmpty; |
||
| 27 | |||
| 28 | import br.com.ec.domain.model.tipos.TipoSexo; |
||
| 29 | import br.com.ec.domain.util.ValidadorCpfCnpjUtil; |
||
| 30 | import br.edu.cesmac.core.interfaces.Alterar; |
||
| 31 | import br.edu.cesmac.core.interfaces.Cadastrar; |
||
| 32 | import br.edu.cesmac.core.util.StringUtil; |
||
| 33 | import br.edu.cesmac.core.util.VerificadorUtil; |
||
| 34 | import br.edu.cesmac.web.converters.FormatarUtil; |
||
| 35 | |||
| 36 | @Entity |
||
| 37 | @Table(name="sec_cliente", schema="sc_sec") |
||
| 38 | public class Cliente implements Serializable { |
||
| 39 | |||
| 40 | private static final long serialVersionUID = 1L; |
||
| 41 | |||
| 42 | private String nome; |
||
| 43 | private Long sequencial; |
||
| 44 | private String cpfCnpj; |
||
| 45 | private String sexo; |
||
| 46 | private String email; |
||
| 47 | private String contato; |
||
| 48 | private String telefone; |
||
| 49 | private String endereco; |
||
| 50 | private String observacao; |
||
| 51 | private Date dataNascimento; |
||
| 52 | private Date dataCadastro; |
||
| 53 | private Boolean indicadorNovidades; |
||
| 54 | private Boolean ativo; |
||
| 55 | |||
| 56 | private Modelo modelo; |
||
| 57 | private Set<Venda> listaVendas; |
||
| 58 | private Set<EstoqueAuditoria> listaTrocas; |
||
| 59 | private Set<Cupom> listaCupons; |
||
| 60 | private List<Pedido> pedidos = new ArrayList<Pedido>(); |
||
| 61 | |||
| 62 | public Cliente() { |
||
| 63 | listaVendas = new HashSet<Venda>(); |
||
| 64 | listaTrocas = new HashSet<EstoqueAuditoria>(); |
||
| 65 | listaCupons= new HashSet<Cupom>(); |
||
| 66 | } |
||
| 67 | |||
| 68 | @Id |
||
| 69 | @GeneratedValue(strategy = GenerationType.IDENTITY) |
||
| 70 | @Column(name="seq_cliente", nullable=false) |
||
| 71 | public Long getSequencial() { |
||
| 72 | return sequencial; |
||
| 73 | } |
||
| 74 | public void setSequencial(Long sequencial) { |
||
| 75 | this.sequencial = sequencial; |
||
| 76 | } |
||
| 77 | |||
| 78 | @Column(name="dsc_nome") |
||
| 79 | @NotEmpty(message = "Parâmetro obrigatório não preenchido: Nome", groups = {Cadastrar.class, Alterar.class}) |
||
| 80 | @Size(max = 120, message = "Limite de caracteres ultrapassado: Nome") |
||
| 81 | public String getNome() { |
||
| 82 | return nome; |
||
| 83 | } |
||
| 84 | public void setNome(String nome) { |
||
| 85 | this.nome = StringUtil.setarUpperCaseComTrim(nome.trim()); |
||
| 86 | } |
||
| 87 | |||
| 88 | @Column(name="dsc_cpf_cnpj") |
||
| 89 | @Size(max = 14, message = "Limite de caracteres ultrapassado: CPF/CNPJ") |
||
| 90 | @NotEmpty(message = "Parâmetro obrigatório não preenchido: CPF/CNPJ", groups = {Cadastrar.class, Alterar.class}) |
||
| 91 | public String getCpfCnpj() { |
||
| 92 | return cpfCnpj; |
||
| 93 | } |
||
| 94 | public void setCpfCnpj(String cpfCnpj) { |
||
| 95 | this.cpfCnpj = cpfCnpj; |
||
| 96 | } |
||
| 97 | |||
| 98 | @Column(name="tip_sexo") |
||
| 99 | public String getSexo() { |
||
| 100 | return sexo; |
||
| 101 | } |
||
| 102 | public void setSexo(String sexo) { |
||
| 103 | this.sexo = sexo; |
||
| 104 | } |
||
| 105 | |||
| 106 | @Column(name="dsc_email") |
||
| 107 | @Size(max = 120, message = "Limite de caracteres ultrapassado: Email") |
||
| 108 | public String getEmail() { |
||
| 109 | return email; |
||
| 110 | } |
||
| 111 | public void setEmail(String email) { |
||
| 112 | this.email = email; |
||
| 113 | } |
||
| 114 | |||
| 115 | @Column(name="dsc_contato") |
||
| 116 | @Size(max = 120, message = "Limite de caracteres ultrapassado: Contato") |
||
| 117 | public String getContato() { |
||
| 118 | return contato; |
||
| 119 | } |
||
| 120 | public void setContato(String contato) { |
||
| 121 | this.contato = contato; |
||
| 122 | } |
||
| 123 | |||
| 124 | @Column(name="dsc_telefone") |
||
| 125 | @Size(max = 120, message = "Limite de caracteres ultrapassado: Telefone") |
||
| 126 | public String getTelefone() { |
||
| 127 | return telefone; |
||
| 128 | } |
||
| 129 | public void setTelefone(String telefone) { |
||
| 130 | this.telefone = telefone; |
||
| 131 | } |
||
| 132 | |||
| 133 | @Column(name="dsc_endereco") |
||
| 134 | @Size(max = 250, message = "Limite de caracteres ultrapassado: Endereço") |
||
| 135 | public String getEndereco() { |
||
| 136 | return endereco; |
||
| 137 | } |
||
| 138 | public void setEndereco(String endereco) { |
||
| 139 | this.endereco = StringUtil.setarUpperCaseComTrim(endereco); |
||
| 140 | } |
||
| 141 | |||
| 142 | @Column(name="dsc_observacao") |
||
| 143 | @Size(max = 240, message = "Limite de caracteres ultrapassado: Observação") |
||
| 144 | public String getObservacao() { |
||
| 145 | return observacao; |
||
| 146 | } |
||
| 147 | public void setObservacao(String observacao) { |
||
| 148 | this.observacao = StringUtil.setarUpperCaseComTrim(observacao); |
||
| 149 | } |
||
| 150 | |||
| 151 | @Column(name="dat_nascimento") |
||
| 152 | public Date getDataNascimento() { |
||
| 153 | return dataNascimento; |
||
| 154 | } |
||
| 155 | public void setDataNascimento(Date dataNascimento) { |
||
| 156 | this.dataNascimento = dataNascimento; |
||
| 157 | } |
||
| 158 | |||
| 159 | @Column(name="dat_cadastro") |
||
| 160 | public Date getDataCadastro() { |
||
| 161 | return dataCadastro; |
||
| 162 | } |
||
| 163 | public void setDataCadastro(Date dataCadastro) { |
||
| 164 | this.dataCadastro = dataCadastro; |
||
| 165 | } |
||
| 166 | |||
| 167 | @Column(name="ind_novidades") |
||
| 168 | public Boolean getIndicadorNovidades() { |
||
| 169 | return indicadorNovidades; |
||
| 170 | } |
||
| 171 | public void setIndicadorNovidades(Boolean indicadorNovidades) { |
||
| 172 | this.indicadorNovidades = indicadorNovidades; |
||
| 173 | } |
||
| 174 | |||
| 175 | @Column(name="ind_ativo", nullable=false) |
||
| 176 | public Boolean getAtivo() { |
||
| 177 | return ativo; |
||
| 178 | } |
||
| 179 | public void setAtivo(Boolean ativo) { |
||
| 180 | this.ativo = ativo; |
||
| 181 | } |
||
| 182 | |||
| 183 | @ManyToOne |
||
| 184 | @ForeignKey(name="fk_cliente_modelo") |
||
| 185 | @JoinColumn(name = "seq_modelo", nullable=true) |
||
| 186 | public Modelo getModelo() { |
||
| 187 | return modelo; |
||
| 188 | } |
||
| 189 | public void setModelo(Modelo modelo) { |
||
| 190 | this.modelo = modelo; |
||
| 191 | } |
||
| 192 | |||
| 193 | @OneToMany(mappedBy="cliente", fetch=FetchType.LAZY, orphanRemoval=false) |
||
| 194 | public Set<Venda> getListaVendas() { |
||
| 195 | return listaVendas; |
||
| 196 | } |
||
| 197 | public void setListaVendas(Set<Venda> listaVendas) { |
||
| 198 | this.listaVendas = listaVendas; |
||
| 199 | } |
||
| 200 | |||
| 201 | @OneToMany(mappedBy="cliente", fetch=FetchType.LAZY, orphanRemoval=false) |
||
| 202 | public Set<EstoqueAuditoria> getListaTrocas() { |
||
| 203 | return listaTrocas; |
||
| 204 | } |
||
| 205 | public void setListaTrocas(Set<EstoqueAuditoria> listaTrocas) { |
||
| 206 | this.listaTrocas = listaTrocas; |
||
| 207 | } |
||
| 208 | |||
| 209 | @OneToMany(mappedBy="cliente", fetch=FetchType.LAZY, orphanRemoval=false) |
||
| 210 | public Set<Cupom> getListaCupons() { |
||
| 211 | return listaCupons; |
||
| 212 | } |
||
| 213 | public void setListaCupons(Set<Cupom> listaCupons) { |
||
| 214 | this.listaCupons = listaCupons; |
||
| 215 | } |
||
| 216 | |||
| 217 | @Transient |
||
| 218 | public List<Pedido> getPedidos() { |
||
| 219 | return pedidos; |
||
| 220 | } |
||
| 221 | public void setPedidos(List<Pedido> pedidos) { |
||
| 222 | this.pedidos = pedidos; |
||
| 223 | } |
||
| 224 | |||
| 225 | @Override |
||
| 226 | public int hashCode() { |
||
| 227 | final int prime = 31; |
||
| 228 | int result = 1; |
||
| 229 | result = prime * result |
||
| 230 | + ((sequencial == null) ? 0 : sequencial.hashCode()); |
||
| 231 | return result; |
||
| 232 | } |
||
| 233 | |||
| 234 | @Override |
||
| 235 | public boolean equals(Object obj) { |
||
| 236 | if (this == obj) |
||
| 237 | return true; |
||
| 238 | if (obj == null) |
||
| 239 | return false; |
||
| 240 | if (getClass() != obj.getClass()) |
||
| 241 | return false; |
||
| 242 | Cliente other = (Cliente) obj; |
||
| 243 | if (sequencial == null) { |
||
| 244 | if (other.sequencial != null) |
||
| 245 | return false; |
||
| 246 | } else if (!sequencial.equals(other.sequencial)) |
||
| 247 | return false; |
||
| 248 | return true; |
||
| 249 | } |
||
| 250 | |||
| 251 | @Transient |
||
| 252 | public String getDescricaoDoModelo() { |
||
| 253 | return VerificadorUtil.naoEstaNulo(getModelo())? getModelo().getDescricao() : ""; |
||
| 254 | } |
||
| 255 | |||
| 256 | @Transient |
||
| 257 | public String getDescricaoDoSexo() { |
||
| 258 | return VerificadorUtil.naoEstaNulo(getSexo())? TipoSexo.parse(getSexo()).getDescricao(): ""; |
||
| 259 | } |
||
| 260 | |||
| 261 | @Transient |
||
| 262 | public String getCpfCnpjFormatado() { |
||
| 263 | return VerificadorUtil.naoEstaNulo(getCpfCnpj())? FormatarUtil.retornarCpfCnpjFormatado(getCpfCnpj()) : ""; |
||
| 264 | } |
||
| 265 | |||
| 266 | @Transient |
||
| 267 | public Boolean ehCpf() { |
||
| 268 | if (VerificadorUtil.naoEstaNulo(getCpfCnpj()) && getCpfCnpj().length() == 11) { |
||
| 269 | return ValidadorCpfCnpjUtil.isCPF(getCpfCnpj()); |
||
| 270 | } |
||
| 271 | return false; |
||
| 272 | } |
||
| 273 | |||
| 274 | @Transient |
||
| 275 | public Boolean ehCnpj() { |
||
| 276 | if (VerificadorUtil.naoEstaNulo(getCpfCnpj()) && getCpfCnpj().length() == 14) { |
||
| 277 | return ValidadorCpfCnpjUtil.isCNPJ(getCpfCnpj()); |
||
| 278 | } |
||
| 279 | return false; |
||
| 280 | } |
||
| 281 | |||
| 282 | @Transient |
||
| 283 | public Boolean cpfCnpjEhValido() { |
||
| 284 | return ehCpf() || ehCnpj()? true : false; |
||
| 285 | } |
||
| 286 | |||
| 287 | @Transient |
||
| 288 | public List<Venda> getVendasOrdenadas() { |
||
| 289 | List<Venda> vendasOrdenadas = null; |
||
| 290 | if (VerificadorUtil.naoEstaNulo(getListaVendas())) { |
||
| 291 | vendasOrdenadas = new ArrayList<Venda>(getListaVendas()); |
||
| 292 | Collections.sort(vendasOrdenadas, new Comparator<Venda>() { |
||
| 293 | public int compare(Venda p1, Venda p2) { |
||
| 294 | return p2.getDataVenda().compareTo(p1.getDataVenda()); |
||
| 295 | }; |
||
| 296 | }); |
||
| 297 | } |
||
| 298 | return vendasOrdenadas; |
||
| 299 | } |
||
| 300 | |||
| 301 | @Transient |
||
| 302 | public List<EstoqueAuditoria> getTrocasOrdenadas() { |
||
| 303 | List<EstoqueAuditoria> trocasOrdenadas = null; |
||
| 304 | if (VerificadorUtil.naoEstaNulo(getListaTrocas())) { |
||
| 305 | trocasOrdenadas = new ArrayList<EstoqueAuditoria>(getListaTrocas()); |
||
| 306 | Collections.sort(trocasOrdenadas, new Comparator<EstoqueAuditoria>() { |
||
| 307 | public int compare(EstoqueAuditoria p1, EstoqueAuditoria p2) { |
||
| 308 | return p2.getDataAtualizacao().compareTo(p1.getDataAtualizacao()); |
||
| 309 | }; |
||
| 310 | }); |
||
| 311 | } |
||
| 312 | return trocasOrdenadas; |
||
| 313 | } |
||
| 314 | |||
| 315 | @Transient |
||
| 316 | public List<Cupom> getCuponsOrdenados() { |
||
| 317 | List<Cupom> cuponsOrdenados = null; |
||
| 318 | if (VerificadorUtil.naoEstaNulo(getListaCupons())) { |
||
| 319 | cuponsOrdenados = new ArrayList<Cupom>(getListaCupons()); |
||
| 320 | Collections.sort(cuponsOrdenados, new Comparator<Cupom>() { |
||
| 321 | public int compare(Cupom p1, Cupom p2) { |
||
| 322 | return p2.getDataHoraEmissao().compareTo(p1.getDataHoraEmissao()); |
||
| 323 | }; |
||
| 324 | }); |
||
| 325 | } |
||
| 326 | return cuponsOrdenados; |
||
| 327 | } |
||
| 328 | |||
| 329 | } |