Advertisement
Zwierzak24

Untitled

Jul 31st, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.86 KB | None | 0 0
  1.  
  2.     @Repository
  3.     @Transactional
  4.     public class CustomerDaoImpl implements CustomerDao {
  5.  
  6.         @Autowired
  7.         private SessionFactory sessionFactory;
  8.  
  9.         public void addCustomer(Customer customer) {
  10.             Session session = sessionFactory.getCurrentSession();
  11.  
  12.             customer.getBillingAddress().setCustomer(customer);
  13.             customer.getShippingAddress().setCustomer(customer);
  14.  
  15.             session.saveOrUpdate(customer);
  16.             session.saveOrUpdate(customer.getBillingAddress());
  17.             session.saveOrUpdate(customer.getShippingAddress());
  18.  
  19.             Users newUser = new Users();
  20.             newUser.setUsername(customer.getUsername());
  21.             newUser.setPassword(customer.getPassword());
  22.             newUser.setEnabled(true);
  23.             newUser.setCustomerId(customer.getCustomerId());
  24.  
  25.             Authorities newAuthority = new Authorities();
  26.             newAuthority.setAuthority("ROLE_USER");
  27.             session.saveOrUpdate(newUser);
  28.             session.saveOrUpdate(newAuthority);
  29.             newAuthority.setUsername(customer.getUsername());
  30.  
  31.             Cart newCart = new Cart();
  32.             newCart.setCustomer(customer);
  33.             customer.setCart(newCart);
  34.  
  35.             session.saveOrUpdate(customer);
  36.             session.saveOrUpdate(newCart);
  37.  
  38.             session.flush();
  39.  
  40.         }
  41.  
  42.         public Customer getCustomerById(int id) {
  43.             Session session = sessionFactory.getCurrentSession();
  44.             Customer customer = (Customer) session.get(Customer.class, id);
  45.             session.flush();
  46.        
  47.             return customer;
  48.         }
  49.  
  50.         public List<Customer> getAllCustomers() {
  51.             Session session = sessionFactory.getCurrentSession();
  52.             Query query = session.createQuery("from Customer ");
  53.             List<Customer> customerList = query.list();
  54.  
  55.             return customerList;
  56.  
  57.         }
  58.  
  59.         public Customer getCustomerByUsername(String username) {
  60.             Session session = sessionFactory.getCurrentSession();
  61.             Query query = session.createQuery("from Customer where username =          ?");
  62.                 query.setString(0, username);
  63.  
  64.             return (Customer) query.uniqueResult();
  65.         }
  66.  
  67.         public void deleteCustomer(Customer customer) {
  68.             Session session = sessionFactory.getCurrentSession();
  69.             session.delete(customer);
  70.             session.flush();
  71.  
  72.         }
  73.  
  74.     }  
  75.    
  76.     @Entity
  77.     public class Users {
  78.  
  79.         @Id
  80.         @GeneratedValue(strategy = GenerationType.IDENTITY)
  81.         private int usersId;
  82.         private String username;
  83.         private String password;
  84.         private Boolean enabled;
  85.    
  86.  
  87.         private int customerId;
  88.    
  89.    
  90.         public int getUsersId() {
  91.             return usersId;
  92.         }
  93.  
  94.         public void setUsersId(int usersId) {
  95.             this.usersId = usersId;
  96.         }
  97.  
  98.     public String getUsername() {
  99.         return username;
  100.     }
  101.  
  102.     public void setUsername(String username) {
  103.         this.username = username;
  104.     }
  105.  
  106.     public String getPassword() {
  107.         return password;
  108.     }
  109.  
  110.     public void setPassword(String password) {
  111.         this.password = password;
  112.     }
  113.  
  114.     public Boolean getEnabled() {
  115.         return enabled;
  116.     }
  117.  
  118.     public void setEnabled(Boolean enabled) {
  119.         this.enabled = enabled;
  120.     }
  121.  
  122.     public int getCustomerId() {
  123.         return customerId;
  124.     }
  125.  
  126.     public void setCustomerId(int customerId) {
  127.         this.customerId = customerId;
  128.     }
  129. }
  130.  
  131. @Entity
  132. public class Authorities {
  133.  
  134.     @Id
  135.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  136.     private int authoritiesId;
  137.     private String username;
  138.     private String authority;
  139.  
  140.     public int getAuthoritiesId() {
  141.         return authoritiesId;
  142.     }
  143.  
  144.     public void setAuthoritiesId(int authoritiesId) {
  145.         this.authoritiesId = authoritiesId;
  146.     }
  147.  
  148.     public String getUsername() {
  149.         return username;
  150.     }
  151.  
  152.     public void setUsername(String username) {
  153.         this.username = username;
  154.     }
  155.  
  156.     public String getAuthority() {
  157.         return authority;
  158.     }
  159.  
  160.     public void setAuthority(String authority) {
  161.         this.authority = authority;
  162.     }
  163. }
  164.  
  165. @Entity
  166. public class Customer implements Serializable {
  167.  
  168.     private static final long serialVersionUID = 5140900014886997914L;
  169.  
  170.     @Id
  171.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  172.     private int customerId;
  173.  
  174.     @NotEmpty(message = "Nazwa użytkownika nie może pozostać pusta!")
  175.  
  176.     private String customerName;
  177.  
  178.     @NotEmpty(message = "Uzupełnij adres email!")
  179.     private String customerEmail;
  180.     private String customerPhone;
  181.  
  182.     @NotEmpty(message = "Nazwa użytkownika nie może pozostać pusta!")
  183.     private String username;
  184.  
  185.     @NotEmpty(message = "Uzupełnij hasło!")
  186.     @Size(min = 6, max = 16, message = "Hasło musi zawierać od 6 do 16 znaków!")
  187.     private String password;
  188.  
  189.     private boolean enabled;
  190.  
  191.     @OneToOne(mappedBy = "customer", cascade = CascadeType.REMOVE)
  192.     @JoinColumn(name = "billingAddressId")
  193.     private BillingAddress billingAddress;
  194.  
  195.     @OneToOne(mappedBy = "customer", cascade = CascadeType.REMOVE)
  196.     @JoinColumn(name = "shippingAddressId")
  197.     private ShippingAddress shippingAddress;
  198.  
  199.     @OneToOne( cascade = CascadeType.REMOVE, mappedBy = "customer")
  200.     @JoinColumn(name = "cartId")
  201.     @JsonIgnore
  202.     private Cart cart;
  203.    
  204.    
  205.  
  206.     public int getCustomerId() {
  207.         return customerId;
  208.     }
  209.  
  210.     public void setCustomerId(int customerId) {
  211.         this.customerId = customerId;
  212.     }
  213.  
  214.     public String getCustomerName() {
  215.         return customerName;
  216.     }
  217.  
  218.     public void setCustomerName(String customerName) {
  219.         this.customerName = customerName;
  220.     }
  221.  
  222.     public String getCustomerEmail() {
  223.         return customerEmail;
  224.     }
  225.  
  226.     public void setCustomerEmail(String customerEmail) {
  227.         this.customerEmail = customerEmail;
  228.     }
  229.  
  230.     public String getCustomerPhone() {
  231.         return customerPhone;
  232.     }
  233.  
  234.     public void setCustomerPhone(String customerPhone) {
  235.         this.customerPhone = customerPhone;
  236.     }
  237.  
  238.     public String getUsername() {
  239.         return username;
  240.     }
  241.  
  242.     public void setUsername(String username) {
  243.         this.username = username;
  244.     }
  245.  
  246.     public String getPassword() {
  247.         return password;
  248.     }
  249.  
  250.     public void setPassword(String password) {
  251.         this.password = password;
  252.     }
  253.  
  254.     public boolean isEnabled() {
  255.         return enabled;
  256.     }
  257.  
  258.     public void setEnabled(boolean enabled) {
  259.         this.enabled = enabled;
  260.     }
  261.  
  262.     public BillingAddress getBillingAddress() {
  263.         return billingAddress;
  264.     }
  265.  
  266.     public void setBillingAddress(BillingAddress billingAddress) {
  267.         this.billingAddress = billingAddress;
  268.     }
  269.  
  270.     public ShippingAddress getShippingAddress() {
  271.         return shippingAddress;
  272.     }
  273.  
  274.     public void setShippingAddress(ShippingAddress shippingAddress) {
  275.         this.shippingAddress = shippingAddress;
  276.     }
  277.  
  278.     public Cart getCart() {
  279.         return cart;
  280.     }
  281.  
  282.     public void setCart(Cart cart) {
  283.         this.cart = cart;
  284.     }
  285.  
  286. }
  287. @Controller
  288. public class RegisterController {
  289.  
  290.     @RequestMapping("/register")
  291.     public String registerCustomer(Model model) {
  292.         Customer customer = new Customer();
  293.         BillingAddress billingAddress = new BillingAddress();
  294.         ShippingAddress shippingAddress = new ShippingAddress();
  295.         customer.setBillingAddress(billingAddress);
  296.         customer.setShippingAddress(shippingAddress);
  297.  
  298.         model.addAttribute("customer", customer);
  299.         return "registerCustomer";
  300.     }
  301.    
  302.     @Autowired
  303.     private CustomerService customerService;
  304.  
  305.  
  306.     @RequestMapping(value = "/register", method = RequestMethod.POST)
  307.     public String registerCustomerPost(@Valid @ModelAttribute("customer") Customer customer, BindingResult result,
  308.             Model model) {
  309.  
  310.         if (result.hasErrors()) {
  311.             return "registerCustomer";
  312.         }
  313.  
  314.         List<Customer> customerList = customerService.getAllCustomers();
  315.  
  316.         for (int i = 0; i < customerList.size(); i++) {
  317.             if (customer.getCustomerEmail().equals(customerList.get(i).getCustomerEmail())) {
  318.                 model.addAttribute("emailMsg", "Email już istnieje w bazie danych!");
  319.  
  320.                 return "registerCustomer";
  321.             }
  322.  
  323.             if (customer.getUsername().equals(customerList.get(i).getUsername())) {
  324.                 model.addAttribute("usernameMsg", "Użytkownik o dane nazwie już istnieje w bazie!");
  325.  
  326.                 return "registerCustomer";
  327.             }
  328.         }
  329.  
  330.        
  331.         customer.setEnabled(true);
  332.         customerService.addCustomer(customer);
  333.         return "registerCustomerSuccess";
  334.     }
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement