Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,16 @@
*/
package com.iluwatar.abstractdocument;

import com.iluwatar.abstractdocument.domain.Car;
import com.iluwatar.abstractdocument.domain.HasModel;
import com.iluwatar.abstractdocument.domain.HasParts;
import com.iluwatar.abstractdocument.domain.HasPrice;
import com.iluwatar.abstractdocument.domain.HasType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.iluwatar.abstractdocument.domain.Car;
import com.iluwatar.abstractdocument.domain.enums.Property;

/**
* The Abstract Document pattern enables handling additional, non-static
* properties. This pattern uses concept of traits to enable type safety and
Expand All @@ -55,20 +53,20 @@ public App() {
LOGGER.info("Constructing parts and car");

Map<String, Object> carProperties = new HashMap<>();
carProperties.put(HasModel.PROPERTY, "300SL");
carProperties.put(HasPrice.PROPERTY, 10000L);
carProperties.put(Property.MODEL.toString(), "300SL");
carProperties.put(Property.PRICE.toString(), 10000L);

Map<String, Object> wheelProperties = new HashMap<>();
wheelProperties.put(HasType.PROPERTY, "wheel");
wheelProperties.put(HasModel.PROPERTY, "15C");
wheelProperties.put(HasPrice.PROPERTY, 100L);
wheelProperties.put(Property.TYPE.toString(), "wheel");
wheelProperties.put(Property.MODEL.toString(), "15C");
wheelProperties.put(Property.PRICE.toString(), 100L);

Map<String, Object> doorProperties = new HashMap<>();
doorProperties.put(HasType.PROPERTY, "door");
doorProperties.put(HasModel.PROPERTY, "Lambo");
doorProperties.put(HasPrice.PROPERTY, 300L);
doorProperties.put(Property.TYPE.toString(), "door");
doorProperties.put(Property.MODEL.toString(), "Lambo");
doorProperties.put(Property.PRICE.toString(), 300L);

carProperties.put(HasParts.PROPERTY, Arrays.asList(wheelProperties, doorProperties));
carProperties.put(Property.PARTS.toString(), Arrays.asList(wheelProperties, doorProperties));

Car car = new Car(carProperties);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@
import java.util.Optional;

import com.iluwatar.abstractdocument.Document;
import com.iluwatar.abstractdocument.domain.enums.Property;

/**
* HasModel trait for static access to 'model' property
*/
public interface HasModel extends Document {

String PROPERTY = "model";

default Optional<String> getModel() {
return Optional.ofNullable((String) get(PROPERTY));
return Optional.ofNullable((String) get(Property.MODEL.toString()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@
import java.util.stream.Stream;

import com.iluwatar.abstractdocument.Document;
import com.iluwatar.abstractdocument.domain.enums.Property;

/**
* HasParts trait for static access to 'parts' property
*/
public interface HasParts extends Document {

String PROPERTY = "parts";

default Stream<Part> getParts() {
return children(PROPERTY, Part::new);
return children(Property.PARTS.toString(), Part::new);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@
import java.util.Optional;

import com.iluwatar.abstractdocument.Document;
import com.iluwatar.abstractdocument.domain.enums.Property;

/**
* HasPrice trait for static access to 'price' property
*/
public interface HasPrice extends Document {

String PROPERTY = "price";

default Optional<Number> getPrice() {
return Optional.ofNullable((Number) get(PROPERTY));
return Optional.ofNullable((Number) get(Property.PRICE.toString()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@
*/
package com.iluwatar.abstractdocument.domain;

import com.iluwatar.abstractdocument.Document;

import java.util.Optional;

import com.iluwatar.abstractdocument.Document;
import com.iluwatar.abstractdocument.domain.enums.Property;

/**
* HasType trait for static access to 'type' property
*/
public interface HasType extends Document {

String PROPERTY = "type";

default Optional<String> getType() {
return Optional.ofNullable((String) get(PROPERTY));
return Optional.ofNullable((String) get(Property.TYPE.toString()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.iluwatar.abstractdocument.domain.enums;

/**
*
* Enum To Describe Property type
*
*/
public enum Property {

PARTS, TYPE, PRICE, MODEL
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,17 @@
*/
package com.iluwatar.abstractdocument;

import com.iluwatar.abstractdocument.domain.Car;
import com.iluwatar.abstractdocument.domain.HasModel;
import com.iluwatar.abstractdocument.domain.HasParts;
import com.iluwatar.abstractdocument.domain.HasPrice;
import com.iluwatar.abstractdocument.domain.HasType;
import com.iluwatar.abstractdocument.domain.Part;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

import com.iluwatar.abstractdocument.domain.Car;
import com.iluwatar.abstractdocument.domain.Part;
import com.iluwatar.abstractdocument.domain.enums.Property;

/**
* Test for Part and Car
Expand All @@ -51,9 +49,9 @@ public class DomainTest {
@Test
public void shouldConstructPart() {
Map<String, Object> partProperties = new HashMap<>();
partProperties.put(HasType.PROPERTY, TEST_PART_TYPE);
partProperties.put(HasModel.PROPERTY, TEST_PART_MODEL);
partProperties.put(HasPrice.PROPERTY, TEST_PART_PRICE);
partProperties.put(Property.TYPE.toString(), TEST_PART_TYPE);
partProperties.put(Property.MODEL.toString(), TEST_PART_MODEL);
partProperties.put(Property.PRICE.toString(), TEST_PART_PRICE);
Part part = new Part(partProperties);

assertEquals(TEST_PART_TYPE, part.getType().get());
Expand All @@ -64,9 +62,9 @@ public void shouldConstructPart() {
@Test
public void shouldConstructCar() {
Map<String, Object> carProperties = new HashMap<>();
carProperties.put(HasModel.PROPERTY, TEST_CAR_MODEL);
carProperties.put(HasPrice.PROPERTY, TEST_CAR_PRICE);
carProperties.put(HasParts.PROPERTY, Arrays.asList(new HashMap<>(), new HashMap<>()));
carProperties.put(Property.MODEL.toString(), TEST_CAR_MODEL);
carProperties.put(Property.PRICE.toString(), TEST_CAR_PRICE);
carProperties.put(Property.PARTS.toString(), Arrays.asList(new HashMap<>(), new HashMap<>()));
Car car = new Car(carProperties);

assertEquals(TEST_CAR_MODEL, car.getModel().get());
Expand Down
29 changes: 16 additions & 13 deletions caching/src/main/java/com/iluwatar/caching/DbManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import org.bson.Document;

import com.iluwatar.caching.constants.CachingConstants;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoDatabase;
Expand Down Expand Up @@ -90,12 +91,12 @@ public static UserAccount readFromDb(String userId) {
}
}
FindIterable<Document> iterable =
db.getCollection("user_accounts").find(new Document("userID", userId));
db.getCollection(CachingConstants.USER_ACCOUNT).find(new Document(CachingConstants.USER_ID, userId));
if (iterable == null) {
return null;
}
Document doc = iterable.first();
return new UserAccount(userId, doc.getString("userName"), doc.getString("additionalInfo"));
return new UserAccount(userId, doc.getString(CachingConstants.USER_NAME), doc.getString(CachingConstants.ADD_INFO));
}

/**
Expand All @@ -113,9 +114,9 @@ public static void writeToDb(UserAccount userAccount) {
e.printStackTrace();
}
}
db.getCollection("user_accounts").insertOne(
new Document("userID", userAccount.getUserId()).append("userName",
userAccount.getUserName()).append("additionalInfo", userAccount.getAdditionalInfo()));
db.getCollection(CachingConstants.USER_ACCOUNT).insertOne(
new Document(CachingConstants.USER_ID ,userAccount.getUserId()).append(CachingConstants.USER_NAME,
userAccount.getUserName()).append(CachingConstants.ADD_INFO, userAccount.getAdditionalInfo()));
}

/**
Expand All @@ -133,10 +134,10 @@ public static void updateDb(UserAccount userAccount) {
e.printStackTrace();
}
}
db.getCollection("user_accounts").updateOne(
new Document("userID", userAccount.getUserId()),
new Document("$set", new Document("userName", userAccount.getUserName()).append(
"additionalInfo", userAccount.getAdditionalInfo())));
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
new Document(CachingConstants.USER_ID, userAccount.getUserId()),
new Document("$set", new Document(CachingConstants.USER_NAME, userAccount.getUserName())
.append(CachingConstants.ADD_INFO, userAccount.getAdditionalInfo())));
}

/**
Expand All @@ -155,10 +156,12 @@ public static void upsertDb(UserAccount userAccount) {
e.printStackTrace();
}
}
db.getCollection("user_accounts").updateOne(
new Document("userID", userAccount.getUserId()),
new Document("$set", new Document("userID", userAccount.getUserId()).append("userName",
userAccount.getUserName()).append("additionalInfo", userAccount.getAdditionalInfo())),
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
new Document(CachingConstants.USER_ID, userAccount.getUserId()),
new Document("$set",
new Document(CachingConstants.USER_ID, userAccount.getUserId())
.append(CachingConstants.USER_NAME, userAccount.getUserName()).append(CachingConstants.ADD_INFO,
userAccount.getAdditionalInfo())),
new UpdateOptions().upsert(true));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.iluwatar.caching.constants;

/**
*
* Constant class for defining constants
*
*/
public class CachingConstants {

public static final String USER_ACCOUNT = "user_accounts";
public static final String USER_ID = "userID";
public static final String USER_NAME = "userName";
public static final String ADD_INFO = "additionalInfo";

}
27 changes: 14 additions & 13 deletions cqrs/src/main/java/com/iluwatar/cqrs/app/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import com.iluwatar.cqrs.commandes.CommandServiceImpl;
import com.iluwatar.cqrs.commandes.ICommandService;
import com.iluwatar.cqrs.constants.AppConstants;
import com.iluwatar.cqrs.dto.Author;
import com.iluwatar.cqrs.dto.Book;
import com.iluwatar.cqrs.queries.IQueryService;
Expand Down Expand Up @@ -60,27 +61,27 @@ public static void main(String[] args) {
ICommandService commands = new CommandServiceImpl();

// Create Authors and Books using CommandService
commands.authorCreated("eEvans", "Eric Evans", "eEvans@email.com");
commands.authorCreated("jBloch", "Joshua Bloch", "jBloch@email.com");
commands.authorCreated("mFowler", "Martin Fowler", "mFowler@email.com");
commands.authorCreated(AppConstants.E_EVANS, "Eric Evans", "eEvans@email.com");
commands.authorCreated(AppConstants.J_BLOCH, "Joshua Bloch", "jBloch@email.com");
commands.authorCreated(AppConstants.M_FOWLER, "Martin Fowler", "mFowler@email.com");

commands.bookAddedToAuthor("Domain-Driven Design", 60.08, "eEvans");
commands.bookAddedToAuthor("Effective Java", 40.54, "jBloch");
commands.bookAddedToAuthor("Java Puzzlers", 39.99, "jBloch");
commands.bookAddedToAuthor("Java Concurrency in Practice", 29.40, "jBloch");
commands.bookAddedToAuthor("Patterns of Enterprise Application Architecture", 54.01, "mFowler");
commands.bookAddedToAuthor("Domain Specific Languages", 48.89, "mFowler");
commands.authorNameUpdated("eEvans", "Eric J. Evans");
commands.bookAddedToAuthor("Domain-Driven Design", 60.08, AppConstants.E_EVANS);
commands.bookAddedToAuthor("Effective Java", 40.54, 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("Patterns of Enterprise Application Architecture", 54.01, AppConstants.M_FOWLER);
commands.bookAddedToAuthor("Domain Specific Languages", 48.89, AppConstants.M_FOWLER);
commands.authorNameUpdated(AppConstants.E_EVANS, "Eric J. Evans");

IQueryService queries = new QueryServiceImpl();

// Query the database using QueryService
Author nullAuthor = queries.getAuthorByUsername("username");
Author eEvans = queries.getAuthorByUsername("eEvans");
BigInteger jBlochBooksCount = queries.getAuthorBooksCount("jBloch");
Author eEvans = queries.getAuthorByUsername(AppConstants.E_EVANS);
BigInteger jBlochBooksCount = queries.getAuthorBooksCount(AppConstants.J_BLOCH);
BigInteger authorsCount = queries.getAuthorsCount();
Book dddBook = queries.getBook("Domain-Driven Design");
List<Book> jBlochBooks = queries.getAuthorBooks("jBloch");
List<Book> jBlochBooks = queries.getAuthorBooks(AppConstants.J_BLOCH);

LOGGER.info("Author username : {}", nullAuthor);
LOGGER.info("Author eEvans : {}", eEvans);
Expand Down
15 changes: 15 additions & 0 deletions cqrs/src/main/java/com/iluwatar/cqrs/constants/AppConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.iluwatar.cqrs.constants;

/**
*
* Class to define the constants
*
*/
public class AppConstants {

public static final String E_EVANS = "eEvans";
public static final String J_BLOCH = "jBloch";
public static final String M_FOWLER = "mFowler";
public static final String USER_NAME = "username";

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.hibernate.SessionFactory;
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;
Expand All @@ -50,7 +51,7 @@ public Author getAuthorByUsername(String username) {
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);
sqlQuery.setParameter(AppConstants.USER_NAME, username);
authorDTo = (Author) sqlQuery.setResultTransformer(Transformers.aliasToBean(Author.class)).uniqueResult();
}
return authorDTo;
Expand All @@ -74,7 +75,7 @@ public List<Book> getAuthorBooks(String username) {
try (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);
sqlQuery.setParameter(AppConstants.USER_NAME, username);
bookDTos = sqlQuery.setResultTransformer(Transformers.aliasToBean(Book.class)).list();
}
return bookDTos;
Expand All @@ -86,7 +87,7 @@ public BigInteger getAuthorBooksCount(String username) {
try (Session session = sessionFactory.openSession()) {
SQLQuery sqlQuery = session.createSQLQuery(
"SELECT count(b.title)" + " FROM Book b, Author a where b.author_id = a.id and a.username=:username");
sqlQuery.setParameter("username", username);
sqlQuery.setParameter(AppConstants.USER_NAME, username);
bookcount = (BigInteger) sqlQuery.uniqueResult();
}
return bookcount;
Expand Down
Loading