Resolves checkstyle errors for converter, cqrs (#1063)
* Reduces checkstyle errors in converter * Reduces checkstyle errors in cqrs
This commit is contained in:
		
				
					committed by
					
						
						Ilkka Seppälä
					
				
			
			
				
	
			
			
			
						parent
						
							2f49648047
						
					
				
				
					commit
					4f9ee0189c
				
			@@ -23,12 +23,10 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.converter;
 | 
					package com.iluwatar.converter;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
import org.slf4j.Logger;
 | 
					import org.slf4j.Logger;
 | 
				
			||||||
import org.slf4j.LoggerFactory;
 | 
					import org.slf4j.LoggerFactory;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.List;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * The Converter pattern is a behavioral design pattern which allows a common way of bidirectional
 | 
					 * The Converter pattern is a behavioral design pattern which allows a common way of bidirectional
 | 
				
			||||||
 * conversion between corresponding types (e.g. DTO and domain representations of the logically
 | 
					 * conversion between corresponding types (e.g. DTO and domain representations of the logically
 | 
				
			||||||
@@ -38,8 +36,9 @@ import java.util.List;
 | 
				
			|||||||
public class App {
 | 
					public class App {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
 | 
					  private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * Program entry point
 | 
					   * Program entry point.
 | 
				
			||||||
   *
 | 
					   *
 | 
				
			||||||
   * @param args command line args
 | 
					   * @param args command line args
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -30,8 +30,9 @@ import java.util.stream.Collectors;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Generic converter, thanks to Java8 features not only provides a way of generic bidirectional
 | 
					 * Generic converter, thanks to Java8 features not only provides a way of generic bidirectional
 | 
				
			||||||
 * conversion between corresponding types, but also a common way of converting a collection of objects
 | 
					 * conversion between corresponding types, but also a common way of converting a collection of
 | 
				
			||||||
 * of the same type, reducing boilerplate code to the absolute minimum.
 | 
					 * objects of the same type, reducing boilerplate code to the absolute minimum.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 * @param <T> DTO representation's type
 | 
					 * @param <T> DTO representation's type
 | 
				
			||||||
 * @param <U> Domain representation's type
 | 
					 * @param <U> Domain representation's type
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
@@ -41,6 +42,8 @@ public class Converter<T, U> {
 | 
				
			|||||||
  private final Function<U, T> fromEntity;
 | 
					  private final Function<U, T> fromEntity;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
 | 
					   * Constructor.
 | 
				
			||||||
 | 
					   *
 | 
				
			||||||
   * @param fromDto    Function that converts given dto entity into the domain entity.
 | 
					   * @param fromDto    Function that converts given dto entity into the domain entity.
 | 
				
			||||||
   * @param fromEntity Function that converts given domain entity into the dto entity.
 | 
					   * @param fromEntity Function that converts given domain entity into the dto entity.
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
@@ -50,34 +53,44 @@ public class Converter<T, U> {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
 | 
					   * Converts DTO to Entity.
 | 
				
			||||||
 | 
					   *
 | 
				
			||||||
   * @param dto DTO entity
 | 
					   * @param dto DTO entity
 | 
				
			||||||
   * @return The domain representation - the result of the converting function application on dto entity.
 | 
					   * @return The domain representation - the result of the converting function application on dto
 | 
				
			||||||
 | 
					   *     entity.
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  public final U convertFromDto(final T dto) {
 | 
					  public final U convertFromDto(final T dto) {
 | 
				
			||||||
    return fromDto.apply(dto);
 | 
					    return fromDto.apply(dto);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
 | 
					   * Converts Entity to DTO.
 | 
				
			||||||
 | 
					   *
 | 
				
			||||||
   * @param entity domain entity
 | 
					   * @param entity domain entity
 | 
				
			||||||
   * @return The DTO representation - the result of the converting function application on domain entity.
 | 
					   * @return The DTO representation - the result of the converting function application on domain
 | 
				
			||||||
 | 
					   *     entity.
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  public final T convertFromEntity(final U entity) {
 | 
					  public final T convertFromEntity(final U entity) {
 | 
				
			||||||
    return fromEntity.apply(entity);
 | 
					    return fromEntity.apply(entity);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
 | 
					   * Converts list of DTOs to list of Entities.
 | 
				
			||||||
 | 
					   *
 | 
				
			||||||
   * @param dtos collection of DTO entities
 | 
					   * @param dtos collection of DTO entities
 | 
				
			||||||
   * @return List of domain representation of provided entities retrieved by
 | 
					   * @return List of domain representation of provided entities retrieved by mapping each of them
 | 
				
			||||||
   *        mapping each of them with the conversion function
 | 
					   *     with the conversion function
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  public final List<U> createFromDtos(final Collection<T> dtos) {
 | 
					  public final List<U> createFromDtos(final Collection<T> dtos) {
 | 
				
			||||||
    return dtos.stream().map(this::convertFromDto).collect(Collectors.toList());
 | 
					    return dtos.stream().map(this::convertFromDto).collect(Collectors.toList());
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
 | 
					   * Converts list of Entities to list of DTOs.
 | 
				
			||||||
 | 
					   *
 | 
				
			||||||
   * @param entities collection of domain entities
 | 
					   * @param entities collection of domain entities
 | 
				
			||||||
   * @return List of domain representation of provided entities retrieved by
 | 
					   * @return List of domain representation of provided entities retrieved by mapping each of them
 | 
				
			||||||
   *        mapping each of them with the conversion function
 | 
					   *     with the conversion function
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  public final List<T> createFromEntities(final Collection<U> entities) {
 | 
					  public final List<T> createFromEntities(final Collection<U> entities) {
 | 
				
			||||||
    return entities.stream().map(this::convertFromEntity).collect(Collectors.toList());
 | 
					    return entities.stream().map(this::convertFromEntity).collect(Collectors.toList());
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,7 +26,7 @@ package com.iluwatar.converter;
 | 
				
			|||||||
import java.util.Objects;
 | 
					import java.util.Objects;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * User class
 | 
					 * User class.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public class User {
 | 
					public class User {
 | 
				
			||||||
  private String firstName;
 | 
					  private String firstName;
 | 
				
			||||||
@@ -35,6 +35,8 @@ public class User {
 | 
				
			|||||||
  private String userId;
 | 
					  private String userId;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
 | 
					   * Constructor.
 | 
				
			||||||
 | 
					   *
 | 
				
			||||||
   * @param firstName user's first name
 | 
					   * @param firstName user's first name
 | 
				
			||||||
   * @param lastName  user's last name
 | 
					   * @param lastName  user's last name
 | 
				
			||||||
   * @param isActive  flag indicating whether the user is active
 | 
					   * @param isActive  flag indicating whether the user is active
 | 
				
			||||||
@@ -63,7 +65,8 @@ public class User {
 | 
				
			|||||||
    return userId;
 | 
					    return userId;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @Override public boolean equals(Object o) {
 | 
					  @Override
 | 
				
			||||||
 | 
					  public boolean equals(Object o) {
 | 
				
			||||||
    if (this == o) {
 | 
					    if (this == o) {
 | 
				
			||||||
      return true;
 | 
					      return true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -75,11 +78,13 @@ public class User {
 | 
				
			|||||||
        .equals(lastName, user.lastName) && Objects.equals(userId, user.userId);
 | 
					        .equals(lastName, user.lastName) && Objects.equals(userId, user.userId);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @Override public int hashCode() {
 | 
					  @Override
 | 
				
			||||||
 | 
					  public int hashCode() {
 | 
				
			||||||
    return Objects.hash(firstName, lastName, isActive, userId);
 | 
					    return Objects.hash(firstName, lastName, isActive, userId);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @Override public String toString() {
 | 
					  @Override
 | 
				
			||||||
 | 
					  public String toString() {
 | 
				
			||||||
    return "User{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\''
 | 
					    return "User{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\''
 | 
				
			||||||
        + ", isActive=" + isActive + ", userId='" + userId + '\'' + '}';
 | 
					        + ", isActive=" + isActive + ", userId='" + userId + '\'' + '}';
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -23,11 +23,10 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.converter;
 | 
					package com.iluwatar.converter;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
import java.util.Objects;
 | 
					import java.util.Objects;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * User DTO class
 | 
					 * User DTO class.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public class UserDto {
 | 
					public class UserDto {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -37,6 +36,8 @@ public class UserDto {
 | 
				
			|||||||
  private String email;
 | 
					  private String email;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
 | 
					   * Constructor.
 | 
				
			||||||
 | 
					   *
 | 
				
			||||||
   * @param firstName user's first name
 | 
					   * @param firstName user's first name
 | 
				
			||||||
   * @param lastName  user's last name
 | 
					   * @param lastName  user's last name
 | 
				
			||||||
   * @param isActive  flag indicating whether the user is active
 | 
					   * @param isActive  flag indicating whether the user is active
 | 
				
			||||||
@@ -65,7 +66,8 @@ public class UserDto {
 | 
				
			|||||||
    return email;
 | 
					    return email;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @Override public boolean equals(Object o) {
 | 
					  @Override
 | 
				
			||||||
 | 
					  public boolean equals(Object o) {
 | 
				
			||||||
    if (this == o) {
 | 
					    if (this == o) {
 | 
				
			||||||
      return true;
 | 
					      return true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -77,11 +79,13 @@ public class UserDto {
 | 
				
			|||||||
        .equals(lastName, userDto.lastName) && Objects.equals(email, userDto.email);
 | 
					        .equals(lastName, userDto.lastName) && Objects.equals(email, userDto.email);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @Override public int hashCode() {
 | 
					  @Override
 | 
				
			||||||
 | 
					  public int hashCode() {
 | 
				
			||||||
    return Objects.hash(firstName, lastName, isActive, email);
 | 
					    return Objects.hash(firstName, lastName, isActive, email);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @Override public String toString() {
 | 
					  @Override
 | 
				
			||||||
 | 
					  public String toString() {
 | 
				
			||||||
    return "UserDto{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\''
 | 
					    return "UserDto{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\''
 | 
				
			||||||
        + ", isActive=" + isActive + ", email='" + email + '\'' + '}';
 | 
					        + ", isActive=" + isActive + ", email='" + email + '\'' + '}';
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -23,12 +23,6 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.cqrs.app;
 | 
					package com.iluwatar.cqrs.app;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.math.BigInteger;
 | 
					 | 
				
			||||||
import java.util.List;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import org.slf4j.Logger;
 | 
					 | 
				
			||||||
import org.slf4j.LoggerFactory;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import com.iluwatar.cqrs.commandes.CommandServiceImpl;
 | 
					import com.iluwatar.cqrs.commandes.CommandServiceImpl;
 | 
				
			||||||
import com.iluwatar.cqrs.commandes.ICommandService;
 | 
					import com.iluwatar.cqrs.commandes.ICommandService;
 | 
				
			||||||
import com.iluwatar.cqrs.constants.AppConstants;
 | 
					import com.iluwatar.cqrs.constants.AppConstants;
 | 
				
			||||||
@@ -37,32 +31,35 @@ import com.iluwatar.cqrs.dto.Book;
 | 
				
			|||||||
import com.iluwatar.cqrs.queries.IQueryService;
 | 
					import com.iluwatar.cqrs.queries.IQueryService;
 | 
				
			||||||
import com.iluwatar.cqrs.queries.QueryServiceImpl;
 | 
					import com.iluwatar.cqrs.queries.QueryServiceImpl;
 | 
				
			||||||
import com.iluwatar.cqrs.util.HibernateUtil;
 | 
					import com.iluwatar.cqrs.util.HibernateUtil;
 | 
				
			||||||
 | 
					import java.math.BigInteger;
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					import org.slf4j.Logger;
 | 
				
			||||||
 | 
					import org.slf4j.LoggerFactory;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * CQRS : Command Query Responsibility Segregation. A pattern used to separate query services from commands or writes
 | 
					 * CQRS : Command Query Responsibility Segregation. A pattern used to separate query services from
 | 
				
			||||||
 * services. The pattern is very simple but it has many consequences. For example, it can be used to tackle down a
 | 
					 * commands or writes services. The pattern is very simple but it has many consequences. For
 | 
				
			||||||
 * complex domain, or to use other architectures that were hard to implement with the classical way.
 | 
					 * example, it can be used to tackle down a complex domain, or to use other architectures that were
 | 
				
			||||||
 * 
 | 
					 * hard to implement with the classical way.
 | 
				
			||||||
 * This implementation is an example of managing books and authors in a library. The persistence of books and authors is
 | 
					 | 
				
			||||||
 * done according to the CQRS architecture. A command side that deals with a data model to persist(insert,update,delete)
 | 
					 | 
				
			||||||
 * objects to a database. And a query side that uses native queries to get data from the database and return objects as
 | 
					 | 
				
			||||||
 * DTOs (Data transfer Objects).
 | 
					 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 | 
					 * <p>This implementation is an example of managing books and authors in a library. The persistence
 | 
				
			||||||
 | 
					 * of books and authors is done according to the CQRS architecture. A command side that deals with a
 | 
				
			||||||
 | 
					 * data model to persist(insert,update,delete) objects to a database. And a query side that uses
 | 
				
			||||||
 | 
					 * native queries to get data from the database and return objects as DTOs (Data transfer Objects).
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public class App {
 | 
					public class App {
 | 
				
			||||||
  private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
 | 
					  private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * Program entry point
 | 
					   * Program entry point.
 | 
				
			||||||
   *
 | 
					   *
 | 
				
			||||||
   * @param args
 | 
					   * @param args command line args
 | 
				
			||||||
   *          command line args
 | 
					 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  public static void main(String[] args) {
 | 
					  public static void main(String[] args) {
 | 
				
			||||||
    ICommandService commands = new CommandServiceImpl();
 | 
					    ICommandService commands = new CommandServiceImpl();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Create Authors and Books using CommandService
 | 
					    // Create Authors and Books using CommandService
 | 
				
			||||||
    commands.authorCreated(AppConstants.E_EVANS, "Eric Evans", "eEvans@email.com");
 | 
					    commands.authorCreated(AppConstants.E_EVANS, "Eric Evans", "evans@email.com");
 | 
				
			||||||
    commands.authorCreated(AppConstants.J_BLOCH, "Joshua Bloch", "jBloch@email.com");
 | 
					    commands.authorCreated(AppConstants.J_BLOCH, "Joshua Bloch", "jBloch@email.com");
 | 
				
			||||||
    commands.authorCreated(AppConstants.M_FOWLER, "Martin Fowler", "mFowler@email.com");
 | 
					    commands.authorCreated(AppConstants.M_FOWLER, "Martin Fowler", "mFowler@email.com");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -70,7 +67,8 @@ public class App {
 | 
				
			|||||||
    commands.bookAddedToAuthor("Effective Java", 40.54, AppConstants.J_BLOCH);
 | 
					    commands.bookAddedToAuthor("Effective Java", 40.54, AppConstants.J_BLOCH);
 | 
				
			||||||
    commands.bookAddedToAuthor("Java Puzzlers", 39.99, AppConstants.J_BLOCH);
 | 
					    commands.bookAddedToAuthor("Java Puzzlers", 39.99, AppConstants.J_BLOCH);
 | 
				
			||||||
    commands.bookAddedToAuthor("Java Concurrency in Practice", 29.40, AppConstants.J_BLOCH);
 | 
					    commands.bookAddedToAuthor("Java Concurrency in Practice", 29.40, AppConstants.J_BLOCH);
 | 
				
			||||||
    commands.bookAddedToAuthor("Patterns of Enterprise Application Architecture", 54.01, AppConstants.M_FOWLER);
 | 
					    commands.bookAddedToAuthor("Patterns of Enterprise"
 | 
				
			||||||
 | 
					        + " Application Architecture", 54.01, AppConstants.M_FOWLER);
 | 
				
			||||||
    commands.bookAddedToAuthor("Domain Specific Languages", 48.89, AppConstants.M_FOWLER);
 | 
					    commands.bookAddedToAuthor("Domain Specific Languages", 48.89, AppConstants.M_FOWLER);
 | 
				
			||||||
    commands.authorNameUpdated(AppConstants.E_EVANS, "Eric J. Evans");
 | 
					    commands.authorNameUpdated(AppConstants.E_EVANS, "Eric J. Evans");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -78,18 +76,18 @@ public class App {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    // Query the database using QueryService
 | 
					    // Query the database using QueryService
 | 
				
			||||||
    Author nullAuthor = queries.getAuthorByUsername("username");
 | 
					    Author nullAuthor = queries.getAuthorByUsername("username");
 | 
				
			||||||
    Author eEvans = queries.getAuthorByUsername(AppConstants.E_EVANS);
 | 
					    Author evans = queries.getAuthorByUsername(AppConstants.E_EVANS);
 | 
				
			||||||
    BigInteger jBlochBooksCount = queries.getAuthorBooksCount(AppConstants.J_BLOCH);
 | 
					    BigInteger blochBooksCount = queries.getAuthorBooksCount(AppConstants.J_BLOCH);
 | 
				
			||||||
    BigInteger authorsCount = queries.getAuthorsCount();
 | 
					    BigInteger authorsCount = queries.getAuthorsCount();
 | 
				
			||||||
    Book dddBook = queries.getBook("Domain-Driven Design");
 | 
					    Book dddBook = queries.getBook("Domain-Driven Design");
 | 
				
			||||||
    List<Book> jBlochBooks = queries.getAuthorBooks(AppConstants.J_BLOCH);
 | 
					    List<Book> blochBooks = queries.getAuthorBooks(AppConstants.J_BLOCH);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    LOGGER.info("Author username : {}", nullAuthor);
 | 
					    LOGGER.info("Author username : {}", nullAuthor);
 | 
				
			||||||
    LOGGER.info("Author eEvans : {}", eEvans);
 | 
					    LOGGER.info("Author evans : {}", evans);
 | 
				
			||||||
    LOGGER.info("jBloch number of books : {}", jBlochBooksCount);
 | 
					    LOGGER.info("jBloch number of books : {}", blochBooksCount);
 | 
				
			||||||
    LOGGER.info("Number of authors : {}", authorsCount);
 | 
					    LOGGER.info("Number of authors : {}", authorsCount);
 | 
				
			||||||
    LOGGER.info("DDD book : {}", dddBook);
 | 
					    LOGGER.info("DDD book : {}", dddBook);
 | 
				
			||||||
    LOGGER.info("jBloch books : {}", jBlochBooks);
 | 
					    LOGGER.info("jBloch books : {}", blochBooks);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    HibernateUtil.getSessionFactory().close();
 | 
					    HibernateUtil.getSessionFactory().close();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -23,17 +23,16 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.cqrs.commandes;
 | 
					package com.iluwatar.cqrs.commandes;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.iluwatar.cqrs.domain.model.Author;
 | 
				
			||||||
 | 
					import com.iluwatar.cqrs.domain.model.Book;
 | 
				
			||||||
 | 
					import com.iluwatar.cqrs.util.HibernateUtil;
 | 
				
			||||||
import org.hibernate.Query;
 | 
					import org.hibernate.Query;
 | 
				
			||||||
import org.hibernate.Session;
 | 
					import org.hibernate.Session;
 | 
				
			||||||
import org.hibernate.SessionFactory;
 | 
					import org.hibernate.SessionFactory;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.iluwatar.cqrs.domain.model.Author;
 | 
					 | 
				
			||||||
import com.iluwatar.cqrs.domain.model.Book;
 | 
					 | 
				
			||||||
import com.iluwatar.cqrs.util.HibernateUtil;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * This class is an implementation of {@link ICommandService} interface. It uses Hibernate as an api for persistence.
 | 
					 * This class is an implementation of {@link ICommandService} interface. It uses Hibernate as an api
 | 
				
			||||||
 *
 | 
					 * for persistence.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public class CommandServiceImpl implements ICommandService {
 | 
					public class CommandServiceImpl implements ICommandService {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -24,8 +24,7 @@
 | 
				
			|||||||
package com.iluwatar.cqrs.commandes;
 | 
					package com.iluwatar.cqrs.commandes;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * This interface represents the commands of the CQRS pattern
 | 
					 * This interface represents the commands of the CQRS pattern.
 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public interface ICommandService {
 | 
					public interface ICommandService {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -24,9 +24,7 @@
 | 
				
			|||||||
package com.iluwatar.cqrs.constants;
 | 
					package com.iluwatar.cqrs.constants;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * 
 | 
					 * Class to define the constants.
 | 
				
			||||||
 * Class to define the constants
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public class AppConstants {
 | 
					public class AppConstants {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -30,7 +30,6 @@ import javax.persistence.Id;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * This is an Author entity. It is used by Hibernate for persistence.
 | 
					 * This is an Author entity. It is used by Hibernate for persistence.
 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
@Entity
 | 
					@Entity
 | 
				
			||||||
public class Author {
 | 
					public class Author {
 | 
				
			||||||
@@ -42,13 +41,11 @@ public class Author {
 | 
				
			|||||||
  private String email;
 | 
					  private String email;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
 | 
					   * Constructor.
 | 
				
			||||||
   *
 | 
					   *
 | 
				
			||||||
   * @param username
 | 
					   * @param username username of the author
 | 
				
			||||||
   *          username of the author
 | 
					   * @param name     name of the author
 | 
				
			||||||
   * @param name
 | 
					   * @param email    email of the author
 | 
				
			||||||
   *          name of the author
 | 
					 | 
				
			||||||
   * @param email
 | 
					 | 
				
			||||||
   *          email of the author
 | 
					 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  public Author(String username, String name, String email) {
 | 
					  public Author(String username, String name, String email) {
 | 
				
			||||||
    this.username = username;
 | 
					    this.username = username;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -30,8 +30,8 @@ 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}
 | 
					 * This is a Book entity. It is used by Hibernate for persistence. Many books can be written by one
 | 
				
			||||||
 *
 | 
					 * {@link Author}
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
@Entity
 | 
					@Entity
 | 
				
			||||||
public class Book {
 | 
					public class Book {
 | 
				
			||||||
@@ -44,13 +44,11 @@ public class Book {
 | 
				
			|||||||
  private Author author;
 | 
					  private Author author;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
 | 
					   * Constructor.
 | 
				
			||||||
   *
 | 
					   *
 | 
				
			||||||
   * @param title
 | 
					   * @param title  title of the book
 | 
				
			||||||
   *          title of the book
 | 
					   * @param price  price of the book
 | 
				
			||||||
   * @param price
 | 
					   * @param author author of the book
 | 
				
			||||||
   *          price of the book
 | 
					 | 
				
			||||||
   * @param author
 | 
					 | 
				
			||||||
   *          author of the book
 | 
					 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  public Book(String title, double price, Author author) {
 | 
					  public Book(String title, double price, Author author) {
 | 
				
			||||||
    this.title = title;
 | 
					    this.title = title;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,9 +26,7 @@ package com.iluwatar.cqrs.dto;
 | 
				
			|||||||
import java.util.Objects;
 | 
					import java.util.Objects;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * 
 | 
					 * This is a DTO (Data Transfer Object) author, contains only useful information to be returned.
 | 
				
			||||||
 * This is a DTO (Data Transfer Object) author, contains only useful information to be returned
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public class Author {
 | 
					public class Author {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -37,13 +35,11 @@ public class Author {
 | 
				
			|||||||
  private String username;
 | 
					  private String username;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
 | 
					   * Constructor.
 | 
				
			||||||
   *
 | 
					   *
 | 
				
			||||||
   * @param name
 | 
					   * @param name     name of the author
 | 
				
			||||||
   *          name of the author
 | 
					   * @param email    email of the author
 | 
				
			||||||
   * @param email
 | 
					   * @param username username of the author
 | 
				
			||||||
   *          email of the author
 | 
					 | 
				
			||||||
   * @param username
 | 
					 | 
				
			||||||
   *          username of the author
 | 
					 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  public Author(String name, String email, String username) {
 | 
					  public Author(String name, String email, String username) {
 | 
				
			||||||
    this.name = name;
 | 
					    this.name = name;
 | 
				
			||||||
@@ -85,7 +81,8 @@ public class Author {
 | 
				
			|||||||
      return false;
 | 
					      return false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    Author other = (Author) obj;
 | 
					    Author other = (Author) obj;
 | 
				
			||||||
    return username.equals(other.getUsername()) && email.equals(other.getEmail()) && name.equals(other.getName());
 | 
					    return username.equals(other.getUsername()) && email.equals(other.getEmail()) && name
 | 
				
			||||||
 | 
					        .equals(other.getName());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,9 +26,7 @@ package com.iluwatar.cqrs.dto;
 | 
				
			|||||||
import java.util.Objects;
 | 
					import java.util.Objects;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * 
 | 
					 * This is a DTO (Data Transfer Object) book, contains only useful information to be returned.
 | 
				
			||||||
 * This is a DTO (Data Transfer Object) book, contains only useful information to be returned
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public class Book {
 | 
					public class Book {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -36,11 +34,10 @@ public class Book {
 | 
				
			|||||||
  private double price;
 | 
					  private double price;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
 | 
					   * Constructor.
 | 
				
			||||||
   *
 | 
					   *
 | 
				
			||||||
   * @param title
 | 
					   * @param title title of the book
 | 
				
			||||||
   *          title of the book
 | 
					   * @param price price of the book
 | 
				
			||||||
   * @param price
 | 
					 | 
				
			||||||
   *          price of the book
 | 
					 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  public Book(String title, double price) {
 | 
					  public Book(String title, double price) {
 | 
				
			||||||
    this.title = title;
 | 
					    this.title = title;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -23,16 +23,13 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.cqrs.queries;
 | 
					package com.iluwatar.cqrs.queries;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.iluwatar.cqrs.dto.Author;
 | 
				
			||||||
 | 
					import com.iluwatar.cqrs.dto.Book;
 | 
				
			||||||
import java.math.BigInteger;
 | 
					import java.math.BigInteger;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.iluwatar.cqrs.dto.Author;
 | 
					 | 
				
			||||||
import com.iluwatar.cqrs.dto.Book;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * 
 | 
					 * This interface represents the query methods of the CQRS pattern.
 | 
				
			||||||
 * This interface represents the query methods of the CQRS pattern
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public interface IQueryService {
 | 
					public interface IQueryService {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -23,23 +23,20 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
package com.iluwatar.cqrs.queries;
 | 
					package com.iluwatar.cqrs.queries;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.iluwatar.cqrs.constants.AppConstants;
 | 
				
			||||||
 | 
					import com.iluwatar.cqrs.dto.Author;
 | 
				
			||||||
 | 
					import com.iluwatar.cqrs.dto.Book;
 | 
				
			||||||
 | 
					import com.iluwatar.cqrs.util.HibernateUtil;
 | 
				
			||||||
import java.math.BigInteger;
 | 
					import java.math.BigInteger;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					 | 
				
			||||||
import org.hibernate.SQLQuery;
 | 
					import org.hibernate.SQLQuery;
 | 
				
			||||||
import org.hibernate.Session;
 | 
					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.constants.AppConstants;
 | 
					 | 
				
			||||||
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
 | 
					 * This class is an implementation of {@link IQueryService}. It uses Hibernate native queries to
 | 
				
			||||||
 * database.
 | 
					 * return DTOs from the database.
 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public class QueryServiceImpl implements IQueryService {
 | 
					public class QueryServiceImpl implements IQueryService {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -49,11 +46,12 @@ public class QueryServiceImpl implements IQueryService {
 | 
				
			|||||||
  public Author getAuthorByUsername(String username) {
 | 
					  public Author getAuthorByUsername(String username) {
 | 
				
			||||||
    Author authorDTo = null;
 | 
					    Author authorDTo = null;
 | 
				
			||||||
    try (Session session = sessionFactory.openSession()) {
 | 
					    try (Session session = sessionFactory.openSession()) {
 | 
				
			||||||
      SQLQuery sqlQuery = session
 | 
					      SQLQuery sqlQuery = session.createSQLQuery("SELECT a.username as \"username\","
 | 
				
			||||||
          .createSQLQuery("SELECT a.username as \"username\", a.name as \"name\", a.email as \"email\""
 | 
					          + " a.name as \"name\", a.email as \"email\""
 | 
				
			||||||
          + "FROM Author a where a.username=:username");
 | 
					          + "FROM Author a where a.username=:username");
 | 
				
			||||||
      sqlQuery.setParameter(AppConstants.USER_NAME, username);
 | 
					      sqlQuery.setParameter(AppConstants.USER_NAME, username);
 | 
				
			||||||
      authorDTo = (Author) sqlQuery.setResultTransformer(Transformers.aliasToBean(Author.class)).uniqueResult();
 | 
					      authorDTo = (Author) sqlQuery.setResultTransformer(Transformers.aliasToBean(Author.class))
 | 
				
			||||||
 | 
					          .uniqueResult();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return authorDTo;
 | 
					    return authorDTo;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@@ -62,10 +60,11 @@ public class QueryServiceImpl implements IQueryService {
 | 
				
			|||||||
  public Book getBook(String title) {
 | 
					  public Book getBook(String title) {
 | 
				
			||||||
    Book bookDTo = null;
 | 
					    Book bookDTo = null;
 | 
				
			||||||
    try (Session session = sessionFactory.openSession()) {
 | 
					    try (Session session = sessionFactory.openSession()) {
 | 
				
			||||||
      SQLQuery sqlQuery = session
 | 
					      SQLQuery sqlQuery = session.createSQLQuery("SELECT b.title as \"title\","
 | 
				
			||||||
          .createSQLQuery("SELECT b.title as \"title\", b.price as \"price\"" + " FROM Book b where b.title=:title");
 | 
					          + " b.price as \"price\"" + " FROM Book b where b.title=:title");
 | 
				
			||||||
      sqlQuery.setParameter("title", title);
 | 
					      sqlQuery.setParameter("title", title);
 | 
				
			||||||
      bookDTo = (Book) sqlQuery.setResultTransformer(Transformers.aliasToBean(Book.class)).uniqueResult();
 | 
					      bookDTo =
 | 
				
			||||||
 | 
					          (Book) sqlQuery.setResultTransformer(Transformers.aliasToBean(Book.class)).uniqueResult();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return bookDTo;
 | 
					    return bookDTo;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@@ -87,7 +86,8 @@ public class QueryServiceImpl implements IQueryService {
 | 
				
			|||||||
    BigInteger bookcount = null;
 | 
					    BigInteger bookcount = null;
 | 
				
			||||||
    try (Session session = sessionFactory.openSession()) {
 | 
					    try (Session session = sessionFactory.openSession()) {
 | 
				
			||||||
      SQLQuery sqlQuery = session.createSQLQuery(
 | 
					      SQLQuery sqlQuery = session.createSQLQuery(
 | 
				
			||||||
          "SELECT count(b.title)" + " FROM  Book b, Author a where b.author_id = a.id and a.username=:username");
 | 
					          "SELECT count(b.title)" + " FROM  Book b, Author a"
 | 
				
			||||||
 | 
					              + " where b.author_id = a.id and a.username=:username");
 | 
				
			||||||
      sqlQuery.setParameter(AppConstants.USER_NAME, username);
 | 
					      sqlQuery.setParameter(AppConstants.USER_NAME, username);
 | 
				
			||||||
      bookcount = (BigInteger) sqlQuery.uniqueResult();
 | 
					      bookcount = (BigInteger) sqlQuery.uniqueResult();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -31,8 +31,8 @@ import org.slf4j.Logger;
 | 
				
			|||||||
import org.slf4j.LoggerFactory;
 | 
					import org.slf4j.LoggerFactory;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * This class simply returns one instance of {@link SessionFactory} initialized when the application is started
 | 
					 * This class simply returns one instance of {@link SessionFactory} initialized when the application
 | 
				
			||||||
 *
 | 
					 * is started.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public class HibernateUtil {
 | 
					public class HibernateUtil {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -42,7 +42,8 @@ public class HibernateUtil {
 | 
				
			|||||||
  private static SessionFactory buildSessionFactory() {
 | 
					  private static SessionFactory buildSessionFactory() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // configures settings from hibernate.cfg.xml
 | 
					    // configures settings from hibernate.cfg.xml
 | 
				
			||||||
    final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
 | 
					    final StandardServiceRegistry registry =
 | 
				
			||||||
 | 
					        new StandardServiceRegistryBuilder().configure().build();
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
      return new MetadataSources(registry).buildMetadata().buildSessionFactory();
 | 
					      return new MetadataSources(registry).buildMetadata().buildSessionFactory();
 | 
				
			||||||
    } catch (Exception ex) {
 | 
					    } catch (Exception ex) {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user