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