add logs, javadoc and format to checkstyle conventions
This commit is contained in:
parent
40c00ca2af
commit
a2dba5bf6d
@ -8,6 +8,10 @@ import com.iluwatar.cqrs.domain.model.Author;
|
|||||||
import com.iluwatar.cqrs.domain.model.Book;
|
import com.iluwatar.cqrs.domain.model.Book;
|
||||||
import com.iluwatar.cqrs.util.HibernateUtil;
|
import com.iluwatar.cqrs.util.HibernateUtil;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is implementation of {@link ICommandService} interface. It uses Hibernate as an api for persistence.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class CommandServiceImpl implements ICommandService {
|
public class CommandServiceImpl implements ICommandService {
|
||||||
|
|
||||||
private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
|
private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
package com.iluwatar.cqrs.commandes;
|
package com.iluwatar.cqrs.commandes;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This interface represents the commands of the CQRS pattern
|
||||||
|
*
|
||||||
|
*/
|
||||||
public interface ICommandService {
|
public interface ICommandService {
|
||||||
|
|
||||||
public abstract void authorCreated(String username, String name, String email);
|
public abstract void authorCreated(String username, String name, String email);
|
||||||
|
@ -6,8 +6,7 @@ import javax.persistence.GenerationType;
|
|||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* This is an Author entity. It is used by Hibernate for persistence.
|
||||||
* @author Sabiq Ihab
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@Entity
|
@Entity
|
||||||
@ -22,8 +21,11 @@ public class Author {
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param username
|
* @param username
|
||||||
|
* username of the author
|
||||||
* @param name
|
* @param name
|
||||||
|
* name of the author
|
||||||
* @param email
|
* @param email
|
||||||
|
* email of the author
|
||||||
*/
|
*/
|
||||||
public Author(String username, String name, String email) {
|
public Author(String username, String name, String email) {
|
||||||
super();
|
super();
|
||||||
|
@ -7,8 +7,7 @@ import javax.persistence.Id;
|
|||||||
import javax.persistence.ManyToOne;
|
import javax.persistence.ManyToOne;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* This is a Book entity. It is used by Hibernate for persistence. Many books can be written by one {@link Author}
|
||||||
* @author Sabiq Ihab
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@Entity
|
@Entity
|
||||||
@ -24,8 +23,11 @@ public class Book {
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param title
|
* @param title
|
||||||
|
* title of the book
|
||||||
* @param price
|
* @param price
|
||||||
|
* price of the book
|
||||||
* @param author
|
* @param author
|
||||||
|
* author of the book
|
||||||
*/
|
*/
|
||||||
public Book(String title, double price, Author author) {
|
public Book(String title, double price, Author author) {
|
||||||
super();
|
super();
|
||||||
|
@ -1,19 +1,33 @@
|
|||||||
package com.iluwatar.cqrs.dto;
|
package com.iluwatar.cqrs.dto;
|
||||||
|
|
||||||
public class AuthorDTO {
|
/**
|
||||||
|
*
|
||||||
|
* This is a DTO (Data Transfer Object) author, contains only useful information to be returned
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class Author {
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
private String email;
|
private String email;
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
public AuthorDTO(String name, String email, String username) {
|
/**
|
||||||
|
*
|
||||||
|
* @param name
|
||||||
|
* name of the author
|
||||||
|
* @param email
|
||||||
|
* email of the author
|
||||||
|
* @param username
|
||||||
|
* username of the author
|
||||||
|
*/
|
||||||
|
public Author(String name, String email, String username) {
|
||||||
super();
|
super();
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.email = email;
|
this.email = email;
|
||||||
this.username = username;
|
this.username = username;
|
||||||
}
|
}
|
||||||
|
|
||||||
public AuthorDTO() {
|
public Author() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
@ -1,17 +1,29 @@
|
|||||||
package com.iluwatar.cqrs.dto;
|
package com.iluwatar.cqrs.dto;
|
||||||
|
|
||||||
public class BookDTO {
|
/**
|
||||||
|
*
|
||||||
|
* This is a DTO (Data Transfer Object) book, contains only useful information to be returned
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class Book {
|
||||||
|
|
||||||
private String title;
|
private String title;
|
||||||
private double price;
|
private double price;
|
||||||
|
|
||||||
public BookDTO(String title, double price) {
|
/**
|
||||||
|
*
|
||||||
|
* @param title
|
||||||
|
* title of the book
|
||||||
|
* @param price
|
||||||
|
* price of the book
|
||||||
|
*/
|
||||||
|
public Book(String title, double price) {
|
||||||
super();
|
super();
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.price = price;
|
this.price = price;
|
||||||
}
|
}
|
||||||
|
|
||||||
public BookDTO() {
|
public Book() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
@ -3,16 +3,21 @@ package com.iluwatar.cqrs.queries;
|
|||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.iluwatar.cqrs.dto.AuthorDTO;
|
import com.iluwatar.cqrs.dto.Author;
|
||||||
import com.iluwatar.cqrs.dto.BookDTO;
|
import com.iluwatar.cqrs.dto.Book;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* This interface represents the query methods of the CQRS pattern
|
||||||
|
*
|
||||||
|
*/
|
||||||
public interface IQueryService {
|
public interface IQueryService {
|
||||||
|
|
||||||
public abstract AuthorDTO getAuthorByUsername(String username);
|
public abstract Author getAuthorByUsername(String username);
|
||||||
|
|
||||||
public abstract BookDTO getBook(String title);
|
public abstract Book getBook(String title);
|
||||||
|
|
||||||
public abstract List<BookDTO> getAuthorBooks(String username);
|
public abstract List<Book> getAuthorBooks(String username);
|
||||||
|
|
||||||
public abstract BigInteger getAuthorBooksCount(String username);
|
public abstract BigInteger getAuthorBooksCount(String username);
|
||||||
|
|
||||||
|
@ -8,47 +8,51 @@ import org.hibernate.Session;
|
|||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.transform.Transformers;
|
import org.hibernate.transform.Transformers;
|
||||||
|
|
||||||
import com.iluwatar.cqrs.dto.AuthorDTO;
|
import com.iluwatar.cqrs.dto.Author;
|
||||||
import com.iluwatar.cqrs.dto.BookDTO;
|
import com.iluwatar.cqrs.dto.Book;
|
||||||
import com.iluwatar.cqrs.util.HibernateUtil;
|
import com.iluwatar.cqrs.util.HibernateUtil;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is an implementation of {@link IQueryService}. It uses Hibernate native queries to return DTOs from the
|
||||||
|
* database.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class QueryServiceImpl implements IQueryService {
|
public class QueryServiceImpl implements IQueryService {
|
||||||
|
|
||||||
private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
|
private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AuthorDTO getAuthorByUsername(String username) {
|
public Author getAuthorByUsername(String username) {
|
||||||
Session session = sessionFactory.openSession();
|
Session session = sessionFactory.openSession();
|
||||||
SQLQuery sqlQuery = session
|
SQLQuery sqlQuery = session
|
||||||
.createSQLQuery("SELECT a.username as \"username\", a.name as \"name\", a.email as \"email\""
|
.createSQLQuery("SELECT a.username as \"username\", a.name as \"name\", a.email as \"email\""
|
||||||
+ "FROM Author a where a.username=:username");
|
+ "FROM Author a where a.username=:username");
|
||||||
sqlQuery.setParameter("username", username);
|
sqlQuery.setParameter("username", username);
|
||||||
AuthorDTO authorDTO = (AuthorDTO) sqlQuery.setResultTransformer(Transformers.aliasToBean(AuthorDTO.class))
|
Author authorDTo = (Author) sqlQuery.setResultTransformer(Transformers.aliasToBean(Author.class)).uniqueResult();
|
||||||
.uniqueResult();
|
|
||||||
session.close();
|
session.close();
|
||||||
return authorDTO;
|
return authorDTo;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BookDTO getBook(String title) {
|
public Book getBook(String title) {
|
||||||
Session session = sessionFactory.openSession();
|
Session session = sessionFactory.openSession();
|
||||||
SQLQuery sqlQuery = session
|
SQLQuery sqlQuery = session
|
||||||
.createSQLQuery("SELECT b.title as \"title\", b.price as \"price\"" + " FROM Book b where b.title=:title");
|
.createSQLQuery("SELECT b.title as \"title\", b.price as \"price\"" + " FROM Book b where b.title=:title");
|
||||||
sqlQuery.setParameter("title", title);
|
sqlQuery.setParameter("title", title);
|
||||||
BookDTO bookDTO = (BookDTO) sqlQuery.setResultTransformer(Transformers.aliasToBean(BookDTO.class)).uniqueResult();
|
Book bookDTo = (Book) sqlQuery.setResultTransformer(Transformers.aliasToBean(Book.class)).uniqueResult();
|
||||||
session.close();
|
session.close();
|
||||||
return bookDTO;
|
return bookDTo;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<BookDTO> getAuthorBooks(String username) {
|
public List<Book> getAuthorBooks(String username) {
|
||||||
Session session = sessionFactory.openSession();
|
Session session = sessionFactory.openSession();
|
||||||
SQLQuery sqlQuery = session.createSQLQuery("SELECT b.title as \"title\", b.price as \"price\""
|
SQLQuery sqlQuery = session.createSQLQuery("SELECT b.title as \"title\", b.price as \"price\""
|
||||||
+ " FROM Author a , Book b where b.author_id = a.id and a.username=:username");
|
+ " FROM Author a , Book b where b.author_id = a.id and a.username=:username");
|
||||||
sqlQuery.setParameter("username", username);
|
sqlQuery.setParameter("username", username);
|
||||||
List<BookDTO> bookDTOs = sqlQuery.setResultTransformer(Transformers.aliasToBean(BookDTO.class)).list();
|
List<Book> bookDTos = sqlQuery.setResultTransformer(Transformers.aliasToBean(Book.class)).list();
|
||||||
session.close();
|
session.close();
|
||||||
return bookDTOs;
|
return bookDTos;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -4,27 +4,27 @@ import org.hibernate.SessionFactory;
|
|||||||
import org.hibernate.boot.MetadataSources;
|
import org.hibernate.boot.MetadataSources;
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* This class simply returns one instance of {@link SessionFactory} initialized when the application is started
|
||||||
* @author Sabiq Ihab
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class HibernateUtil {
|
public class HibernateUtil {
|
||||||
|
|
||||||
private static final SessionFactory SESSIONFACTORY = buildSessionFactory();
|
private static final SessionFactory SESSIONFACTORY = buildSessionFactory();
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(HibernateUtil.class);
|
||||||
|
|
||||||
private static SessionFactory buildSessionFactory() {
|
private static SessionFactory buildSessionFactory() {
|
||||||
// A SessionFactory is set up once for an application!
|
|
||||||
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure() // configures settings //
|
// configures settings from hibernate.cfg.xml
|
||||||
// from hibernate.cfg.xml
|
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
|
||||||
.build();
|
|
||||||
try {
|
try {
|
||||||
return new MetadataSources(registry).buildMetadata().buildSessionFactory();
|
return new MetadataSources(registry).buildMetadata().buildSessionFactory();
|
||||||
} catch (Throwable ex) {
|
} catch (Exception ex) {
|
||||||
StandardServiceRegistryBuilder.destroy(registry);
|
StandardServiceRegistryBuilder.destroy(registry);
|
||||||
// TODO HibernateUtil : change print with logger
|
LOGGER.error("Initial SessionFactory creation failed." + ex);
|
||||||
System.err.println("Initial SessionFactory creation failed." + ex);
|
|
||||||
throw new ExceptionInInitializerError(ex);
|
throw new ExceptionInInitializerError(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
13
cqrs/src/main/resources/logback.xml
Normal file
13
cqrs/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<root level="info">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
Loading…
x
Reference in New Issue
Block a user