From 6ad3c55e6bc15ba5238174cf0a594ca74c0b0bba Mon Sep 17 00:00:00 2001 From: Tanya Date: Wed, 26 Aug 2015 13:48:23 +0300 Subject: [PATCH] add Customer: - entity - dao - service --- .../deliveryservice/mvc/dao/CustomerDAO.java | 26 +++++++ .../mvc/dao/CustomerDAOImpl.java | 55 +++++++++++++++ .../deliveryservice/mvc/entity/Customer.java | 67 +++++++++++++++++++ .../mvc/service/CustomerService.java | 26 +++++++ .../mvc/service/CustomerServiceImpl.java | 44 ++++++++++++ 5 files changed, 218 insertions(+) create mode 100644 src/main/java/com/javastudy/deliveryservice/mvc/dao/CustomerDAO.java create mode 100644 src/main/java/com/javastudy/deliveryservice/mvc/dao/CustomerDAOImpl.java create mode 100644 src/main/java/com/javastudy/deliveryservice/mvc/entity/Customer.java create mode 100644 src/main/java/com/javastudy/deliveryservice/mvc/service/CustomerService.java create mode 100644 src/main/java/com/javastudy/deliveryservice/mvc/service/CustomerServiceImpl.java diff --git a/src/main/java/com/javastudy/deliveryservice/mvc/dao/CustomerDAO.java b/src/main/java/com/javastudy/deliveryservice/mvc/dao/CustomerDAO.java new file mode 100644 index 0000000..3ea1426 --- /dev/null +++ b/src/main/java/com/javastudy/deliveryservice/mvc/dao/CustomerDAO.java @@ -0,0 +1,26 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.javastudy.deliveryservice.mvc.dao; + +import com.javastudy.deliveryservice.mvc.entity.Customer; +import java.util.List; + +/** + * + * @author Tanya + */ +public interface CustomerDAO { + public void persist(Customer customer); + + public List findAll(); + + public void remove(Customer customer); + + public Customer findByID(Long id); + + public Customer merge(Customer customer); +} + diff --git a/src/main/java/com/javastudy/deliveryservice/mvc/dao/CustomerDAOImpl.java b/src/main/java/com/javastudy/deliveryservice/mvc/dao/CustomerDAOImpl.java new file mode 100644 index 0000000..1fda2a5 --- /dev/null +++ b/src/main/java/com/javastudy/deliveryservice/mvc/dao/CustomerDAOImpl.java @@ -0,0 +1,55 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.javastudy.deliveryservice.mvc.dao; + +import com.javastudy.deliveryservice.mvc.entity.Customer; +import java.util.List; +import javax.persistence.EntityManager; +import javax.persistence.Query; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; + +/** + * + * @author Tanya + */ +@Repository +public class CustomerDAOImpl implements CustomerDAO { + + @Autowired + EntityManager manager; + + public void persist(Customer customer) { + manager.getTransaction().begin(); + manager.persist(customer); + manager.getTransaction().commit(); + } + + public List findAll() { + Query query = manager.createQuery("SELECT c FROM Customer c"); + List list = query.getResultList(); + return list; + } + + public void remove(Customer customer) { + manager.getTransaction().begin(); + manager.remove(manager.merge(customer)); + manager.getTransaction().commit(); + } + + public Customer findByID(Long id) { + Customer customer = manager.find(Customer.class, id); + return customer; + } + + public Customer merge(Customer customer) { + manager.getTransaction().begin(); + customer = manager.merge(customer); + manager.getTransaction().commit(); + return customer; + } + +} diff --git a/src/main/java/com/javastudy/deliveryservice/mvc/entity/Customer.java b/src/main/java/com/javastudy/deliveryservice/mvc/entity/Customer.java new file mode 100644 index 0000000..79b8014 --- /dev/null +++ b/src/main/java/com/javastudy/deliveryservice/mvc/entity/Customer.java @@ -0,0 +1,67 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.javastudy.deliveryservice.mvc.entity; + +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.UniqueConstraint; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; +import org.hibernate.validator.constraints.Email; + +/** + * + * @author Tanya + */ +@Entity +@Table(name = "customers", + uniqueConstraints = @UniqueConstraint(columnNames = {"login", "phone", "email"}) +) +public class Customer implements Serializable { + + @Id + @Column(name = "id") + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + @Column(name = "login") + @Size(min = 6, max = 16) + @NotNull + private String login; + + @Column(name = "password") + @Size(min = 6, max = 16) + @NotNull + private String password; + + @Column(name = "name") + @Size(min = 3, max = 16) + @NotNull + private String name; + + @Column(name = "phone") + @Size(min = 4, max = 20) + @NotNull + private String phone; + + @Column(name = "email") + @Email + @Size(min = 5, max = 64) + @NotNull + private String email; + + /* + @OneToOne + @JoinColumn(name = "id_adress", referencedColumnName = "id") + private Adress adress; + + */ +} diff --git a/src/main/java/com/javastudy/deliveryservice/mvc/service/CustomerService.java b/src/main/java/com/javastudy/deliveryservice/mvc/service/CustomerService.java new file mode 100644 index 0000000..c401131 --- /dev/null +++ b/src/main/java/com/javastudy/deliveryservice/mvc/service/CustomerService.java @@ -0,0 +1,26 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.javastudy.deliveryservice.mvc.service; + +import com.javastudy.deliveryservice.mvc.entity.Customer; +import java.util.List; + +/** + * + * @author Tanya + */ +public interface CustomerService { + + public void persist(Customer customer); + + public List findAll(); + + public void remove(Customer customer); + + public Customer findByID(Long id); + + public Customer merge(Customer customer); +} diff --git a/src/main/java/com/javastudy/deliveryservice/mvc/service/CustomerServiceImpl.java b/src/main/java/com/javastudy/deliveryservice/mvc/service/CustomerServiceImpl.java new file mode 100644 index 0000000..9b23eec --- /dev/null +++ b/src/main/java/com/javastudy/deliveryservice/mvc/service/CustomerServiceImpl.java @@ -0,0 +1,44 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.javastudy.deliveryservice.mvc.service; + +import com.javastudy.deliveryservice.mvc.dao.CustomerDAO; +import com.javastudy.deliveryservice.mvc.entity.Customer; +import java.util.List; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * + * @author Tanya + */ +@Service +public class CustomerServiceImpl implements CustomerService { + + @Autowired + CustomerDAO customerDAO; + + public void persist(Customer customer) { + customerDAO.persist(customer); + } + + public List findAll() { + return customerDAO.findAll(); + } + + public void remove(Customer customer) { + customerDAO.remove(customer); + } + + public Customer findByID(Long id) { + return customerDAO.findByID(id); + } + + public Customer merge(Customer customer) { + return customerDAO.merge(customer); + } + +}