2017-06-21 23:36:39 +00:00
|
|
|
package com.iluwatar.cqrs.commandes;
|
|
|
|
|
|
|
|
import org.hibernate.Query;
|
|
|
|
import org.hibernate.Session;
|
|
|
|
import org.hibernate.SessionFactory;
|
|
|
|
|
|
|
|
import com.iluwatar.cqrs.domain.model.Author;
|
|
|
|
import com.iluwatar.cqrs.domain.model.Book;
|
|
|
|
import com.iluwatar.cqrs.util.HibernateUtil;
|
|
|
|
|
2017-06-22 23:56:57 +00:00
|
|
|
/**
|
2017-07-29 12:13:34 +01:00
|
|
|
* This class is an implementation of {@link ICommandService} interface. It uses Hibernate as an api for persistence.
|
2017-06-22 23:56:57 +00:00
|
|
|
*
|
|
|
|
*/
|
2017-06-21 23:36:39 +00:00
|
|
|
public class CommandServiceImpl implements ICommandService {
|
|
|
|
|
|
|
|
private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
|
|
|
|
|
|
|
|
private Author getAuthorByUsername(String username) {
|
2017-07-29 12:13:34 +01:00
|
|
|
Author author = null;
|
|
|
|
try (Session session = sessionFactory.openSession()) {
|
|
|
|
Query query = session.createQuery("from Author where username=:username");
|
|
|
|
query.setParameter("username", username);
|
|
|
|
author = (Author) query.uniqueResult();
|
|
|
|
}
|
2017-07-29 12:16:19 +01:00
|
|
|
if (author == null) {
|
|
|
|
throw new NullPointerException("Author " + username + " doesn't exist!");
|
|
|
|
}
|
2017-06-21 23:36:39 +00:00
|
|
|
return author;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Book getBookByTitle(String title) {
|
2017-07-29 12:13:34 +01:00
|
|
|
Book book = null;
|
|
|
|
try (Session session = sessionFactory.openSession()) {
|
|
|
|
Query query = session.createQuery("from Book where title=:title");
|
|
|
|
query.setParameter("title", title);
|
|
|
|
book = (Book) query.uniqueResult();
|
|
|
|
}
|
2017-07-29 12:16:19 +01:00
|
|
|
if (book == null) {
|
|
|
|
throw new NullPointerException("Book " + title + " doesn't exist!");
|
|
|
|
}
|
2017-06-21 23:36:39 +00:00
|
|
|
return book;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void authorCreated(String username, String name, String email) {
|
|
|
|
Author author = new Author(username, name, email);
|
2017-07-29 12:13:34 +01:00
|
|
|
try (Session session = sessionFactory.openSession()) {
|
|
|
|
session.beginTransaction();
|
|
|
|
session.save(author);
|
|
|
|
session.getTransaction().commit();
|
|
|
|
}
|
2017-06-21 23:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void bookAddedToAuthor(String title, double price, String username) {
|
|
|
|
Author author = getAuthorByUsername(username);
|
|
|
|
Book book = new Book(title, price, author);
|
2017-07-29 12:13:34 +01:00
|
|
|
try (Session session = sessionFactory.openSession()) {
|
|
|
|
session.beginTransaction();
|
|
|
|
session.save(book);
|
|
|
|
session.getTransaction().commit();
|
|
|
|
}
|
2017-06-21 23:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void authorNameUpdated(String username, String name) {
|
|
|
|
Author author = getAuthorByUsername(username);
|
|
|
|
author.setName(name);
|
2017-07-29 12:13:34 +01:00
|
|
|
try (Session session = sessionFactory.openSession()) {
|
|
|
|
session.beginTransaction();
|
|
|
|
session.update(author);
|
|
|
|
session.getTransaction().commit();
|
|
|
|
}
|
2017-06-21 23:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void authorUsernameUpdated(String oldUsername, String newUsername) {
|
|
|
|
Author author = getAuthorByUsername(oldUsername);
|
|
|
|
author.setUsername(newUsername);
|
2017-07-29 12:13:34 +01:00
|
|
|
try (Session session = sessionFactory.openSession()) {
|
|
|
|
session.beginTransaction();
|
|
|
|
session.update(author);
|
|
|
|
session.getTransaction().commit();
|
|
|
|
}
|
2017-06-21 23:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void authorEmailUpdated(String username, String email) {
|
|
|
|
Author author = getAuthorByUsername(username);
|
|
|
|
author.setEmail(email);
|
2017-07-29 12:13:34 +01:00
|
|
|
try (Session session = sessionFactory.openSession()) {
|
|
|
|
session.beginTransaction();
|
|
|
|
session.update(author);
|
|
|
|
session.getTransaction().commit();
|
|
|
|
}
|
2017-06-21 23:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void bookTitleUpdated(String oldTitle, String newTitle) {
|
|
|
|
Book book = getBookByTitle(oldTitle);
|
|
|
|
book.setTitle(newTitle);
|
2017-07-29 12:13:34 +01:00
|
|
|
try (Session session = sessionFactory.openSession()) {
|
|
|
|
session.beginTransaction();
|
|
|
|
session.update(book);
|
|
|
|
session.getTransaction().commit();
|
|
|
|
}
|
2017-06-21 23:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void bookPriceUpdated(String title, double price) {
|
|
|
|
Book book = getBookByTitle(title);
|
|
|
|
book.setPrice(price);
|
2017-07-29 12:13:34 +01:00
|
|
|
try (Session session = sessionFactory.openSession()) {
|
|
|
|
session.beginTransaction();
|
|
|
|
session.update(book);
|
|
|
|
session.getTransaction().commit();
|
|
|
|
}
|
2017-06-21 23:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|