add logs, javadoc and format to checkstyle conventions

This commit is contained in:
Sabiq Ihab 2017-06-22 23:56:57 +00:00
parent 40c00ca2af
commit a2dba5bf6d
11 changed files with 105 additions and 45 deletions

View File

@ -8,6 +8,10 @@ import com.iluwatar.cqrs.domain.model.Author;
import com.iluwatar.cqrs.domain.model.Book;
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 {
private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

View File

@ -1,5 +1,9 @@
package com.iluwatar.cqrs.commandes;
/**
* This interface represents the commands of the CQRS pattern
*
*/
public interface ICommandService {
public abstract void authorCreated(String username, String name, String email);

View File

@ -6,8 +6,7 @@ import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
*
* @author Sabiq Ihab
* This is an Author entity. It is used by Hibernate for persistence.
*
*/
@Entity
@ -22,8 +21,11 @@ public class Author {
/**
*
* @param username
* username of the author
* @param name
* name of the author
* @param email
* email of the author
*/
public Author(String username, String name, String email) {
super();

View File

@ -7,8 +7,7 @@ import javax.persistence.Id;
import javax.persistence.ManyToOne;
/**
*
* @author Sabiq Ihab
* This is a Book entity. It is used by Hibernate for persistence. Many books can be written by one {@link Author}
*
*/
@Entity
@ -24,8 +23,11 @@ public class Book {
/**
*
* @param title
* title of the book
* @param price
* price of the book
* @param author
* author of the book
*/
public Book(String title, double price, Author author) {
super();

View File

@ -1,19 +1,33 @@
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 email;
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();
this.name = name;
this.email = email;
this.username = username;
}
public AuthorDTO() {
public Author() {
super();
}

View File

@ -1,17 +1,29 @@
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 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();
this.title = title;
this.price = price;
}
public BookDTO() {
public Book() {
super();
}

View File

@ -3,16 +3,21 @@ package com.iluwatar.cqrs.queries;
import java.math.BigInteger;
import java.util.List;
import com.iluwatar.cqrs.dto.AuthorDTO;
import com.iluwatar.cqrs.dto.BookDTO;
import com.iluwatar.cqrs.dto.Author;
import com.iluwatar.cqrs.dto.Book;
/**
*
* This interface represents the query methods of the CQRS pattern
*
*/
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);

View File

@ -8,47 +8,51 @@ import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import com.iluwatar.cqrs.dto.AuthorDTO;
import com.iluwatar.cqrs.dto.BookDTO;
import com.iluwatar.cqrs.dto.Author;
import com.iluwatar.cqrs.dto.Book;
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 {
private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
@Override
public AuthorDTO getAuthorByUsername(String username) {
public Author getAuthorByUsername(String username) {
Session session = sessionFactory.openSession();
SQLQuery sqlQuery = session
.createSQLQuery("SELECT a.username as \"username\", a.name as \"name\", a.email as \"email\""
+ "FROM Author a where a.username=:username");
sqlQuery.setParameter("username", username);
AuthorDTO authorDTO = (AuthorDTO) sqlQuery.setResultTransformer(Transformers.aliasToBean(AuthorDTO.class))
.uniqueResult();
Author authorDTo = (Author) sqlQuery.setResultTransformer(Transformers.aliasToBean(Author.class)).uniqueResult();
session.close();
return authorDTO;
return authorDTo;
}
@Override
public BookDTO getBook(String title) {
public Book getBook(String title) {
Session session = sessionFactory.openSession();
SQLQuery sqlQuery = session
.createSQLQuery("SELECT b.title as \"title\", b.price as \"price\"" + " FROM Book b where b.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();
return bookDTO;
return bookDTo;
}
@Override
public List<BookDTO> getAuthorBooks(String username) {
public List<Book> getAuthorBooks(String username) {
Session session = sessionFactory.openSession();
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");
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();
return bookDTOs;
return bookDTos;
}
@Override

View File

@ -4,27 +4,27 @@ import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Sabiq Ihab
* This class simply returns one instance of {@link SessionFactory} initialized when the application is started
*
*/
public class HibernateUtil {
private static final SessionFactory SESSIONFACTORY = buildSessionFactory();
private static final Logger LOGGER = LoggerFactory.getLogger(HibernateUtil.class);
private static SessionFactory buildSessionFactory() {
// A SessionFactory is set up once for an application!
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure() // configures settings //
// from hibernate.cfg.xml
.build();
// configures settings from hibernate.cfg.xml
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
try {
return new MetadataSources(registry).buildMetadata().buildSessionFactory();
} catch (Throwable ex) {
} catch (Exception ex) {
StandardServiceRegistryBuilder.destroy(registry);
// TODO HibernateUtil : change print with logger
System.err.println("Initial SessionFactory creation failed." + ex);
LOGGER.error("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

View File

@ -3,13 +3,13 @@
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:mem:test</property>
<property name="connection.username">sa</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.iluwatar.cqrs.domain.model.Author" />
<mapping class="com.iluwatar.cqrs.domain.model.Book" />
</session-factory>
<session-factory>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:mem:test</property>
<property name="connection.username">sa</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.iluwatar.cqrs.domain.model.Author" />
<mapping class="com.iluwatar.cqrs.domain.model.Book" />
</session-factory>
</hibernate-configuration>

View 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>