Skip to content

Commit 01aae14

Browse files
authored
Updating code for chapter11
1 parent 51d52b5 commit 01aae14

139 files changed

Lines changed: 15052 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

code/chapter11/README.adoc

Lines changed: 1111 additions & 0 deletions
Large diffs are not rendered by default.

code/chapter11/catalog/README.adoc

Lines changed: 622 additions & 0 deletions
Large diffs are not rendered by default.

code/chapter11/catalog/pom.xml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://www.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>io.microprofile.tutorial</groupId>
7+
<artifactId>catalog</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<packaging>war</packaging>
10+
11+
<properties>
12+
13+
<!-- Setting the source and target of the Java Compiler -->
14+
<maven.compiler.source>21</maven.compiler.source>
15+
<maven.compiler.target>21</maven.compiler.target>
16+
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
19+
20+
<!-- Liberty configuration -->
21+
<liberty.var.default.http.port>5050</liberty.var.default.http.port>
22+
<liberty.var.default.https.port>5051</liberty.var.default.https.port>
23+
24+
<liberty.var.app.context.root>catalog</liberty.var.app.context.root>
25+
</properties>
26+
27+
<dependencies>
28+
<!-- Provided dependencies -->
29+
<!-- Add Lombok dependency -->
30+
<dependency>
31+
<groupId>org.projectlombok</groupId>
32+
<artifactId>lombok</artifactId>
33+
<version>1.18.36</version>
34+
<scope>provided</scope>
35+
</dependency>
36+
37+
<!-- Adding Jakarta EE dependencies -->
38+
<dependency>
39+
<groupId>jakarta.platform</groupId>
40+
<artifactId>jakarta.jakartaee-api</artifactId>
41+
<version>10.0.0</version>
42+
<scope>provided</scope>
43+
</dependency>
44+
45+
<!-- Adding MicroProfile dependency -->
46+
<dependency>
47+
<groupId>org.eclipse.microprofile</groupId>
48+
<artifactId>microprofile</artifactId>
49+
<version>7.1</version>
50+
<type>pom</type>
51+
<scope>provided</scope>
52+
</dependency>
53+
</dependencies>
54+
55+
<build>
56+
<finalName>${project.artifactId}</finalName>
57+
<plugins>
58+
<!-- Maven Compiler Plugin for Java 21 -->
59+
<plugin>
60+
<groupId>org.apache.maven.plugins</groupId>
61+
<artifactId>maven-compiler-plugin</artifactId>
62+
<version>3.13.0</version>
63+
<configuration>
64+
<release>21</release>
65+
</configuration>
66+
</plugin>
67+
68+
<!-- Enable liberty-maven plugin -->
69+
<plugin>
70+
<groupId>io.openliberty.tools</groupId>
71+
<artifactId>liberty-maven-plugin</artifactId>
72+
<version>3.11.2</version>
73+
<configuration>
74+
<serverName>mpServer</serverName>
75+
</configuration>
76+
</plugin>
77+
78+
<plugin>
79+
<groupId>org.apache.maven.plugins</groupId>
80+
<artifactId>maven-war-plugin</artifactId>
81+
<version>3.4.0</version>
82+
</plugin>
83+
</plugins>
84+
</build>
85+
</project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.microprofile.tutorial.store.product;
2+
3+
import jakarta.ws.rs.ApplicationPath;
4+
import jakarta.ws.rs.core.Application;
5+
6+
@ApplicationPath("/api")
7+
public class ProductRestApplication extends Application {
8+
// No additional configuration is needed here
9+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.microprofile.tutorial.store.product.entity;
2+
3+
import lombok.Data;
4+
import lombok.NoArgsConstructor;
5+
import lombok.AllArgsConstructor;
6+
7+
@Data
8+
@NoArgsConstructor
9+
@AllArgsConstructor
10+
public class Product {
11+
12+
private Long id;
13+
private String name;
14+
private String description;
15+
private Double price;
16+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package io.microprofile.tutorial.store.product.repository;
2+
3+
import io.microprofile.tutorial.store.product.entity.Product;
4+
import jakarta.enterprise.context.ApplicationScoped;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.concurrent.ConcurrentHashMap;
9+
import java.util.concurrent.atomic.AtomicLong;
10+
import java.util.logging.Logger;
11+
import java.util.stream.Collectors;
12+
13+
/**
14+
* Repository class for Product entity.
15+
* Provides in-memory persistence operations using ConcurrentHashMap.
16+
*/
17+
@ApplicationScoped
18+
public class ProductRepository {
19+
20+
private static final Logger LOGGER = Logger.getLogger(ProductRepository.class.getName());
21+
22+
// In-memory storage using ConcurrentHashMap for thread safety
23+
private final Map<Long, Product> productsMap = new ConcurrentHashMap<>();
24+
25+
// ID generator
26+
private final AtomicLong idGenerator = new AtomicLong(1);
27+
28+
/**
29+
* Constructor with sample data initialization.
30+
*/
31+
public ProductRepository() {
32+
// Initialize with sample products
33+
createProduct(new Product(null, "iPhone", "Apple iPhone 15", 999.99));
34+
createProduct(new Product(null, "MacBook", "Apple MacBook Air", 1299.0));
35+
createProduct(new Product(null, "iPad", "Apple iPad Pro", 799.0));
36+
LOGGER.info("ProductRepository initialized with sample products");
37+
}
38+
39+
/**
40+
* Retrieves all products.
41+
*
42+
* @return List of all products
43+
*/
44+
public List<Product> findAllProducts() {
45+
LOGGER.fine("Repository: Finding all products");
46+
return new ArrayList<>(productsMap.values());
47+
}
48+
49+
/**
50+
* Retrieves a product by ID.
51+
*
52+
* @param id Product ID
53+
* @return The product or null if not found
54+
*/
55+
public Product findProductById(Long id) {
56+
LOGGER.fine("Repository: Finding product with ID: " + id);
57+
return productsMap.get(id);
58+
}
59+
60+
/**
61+
* Creates a new product.
62+
*
63+
* @param product Product data to create
64+
* @return The created product with ID
65+
*/
66+
public Product createProduct(Product product) {
67+
// Generate ID if not provided
68+
if (product.getId() == null) {
69+
product.setId(idGenerator.getAndIncrement());
70+
} else {
71+
// Update idGenerator if the provided ID is greater than current
72+
long nextId = product.getId() + 1;
73+
while (true) {
74+
long currentId = idGenerator.get();
75+
if (nextId <= currentId || idGenerator.compareAndSet(currentId, nextId)) {
76+
break;
77+
}
78+
}
79+
}
80+
81+
LOGGER.fine("Repository: Creating product with ID: " + product.getId());
82+
productsMap.put(product.getId(), product);
83+
return product;
84+
}
85+
86+
/**
87+
* Updates an existing product.
88+
*
89+
* @param product Updated product data
90+
* @return The updated product or null if not found
91+
*/
92+
public Product updateProduct(Product product) {
93+
Long id = product.getId();
94+
if (id != null && productsMap.containsKey(id)) {
95+
LOGGER.fine("Repository: Updating product with ID: " + id);
96+
productsMap.put(id, product);
97+
return product;
98+
}
99+
LOGGER.warning("Repository: Product not found for update, ID: " + id);
100+
return null;
101+
}
102+
103+
/**
104+
* Deletes a product by ID.
105+
*
106+
* @param id ID of the product to delete
107+
* @return true if deleted, false if not found
108+
*/
109+
public boolean deleteProduct(Long id) {
110+
if (productsMap.containsKey(id)) {
111+
LOGGER.fine("Repository: Deleting product with ID: " + id);
112+
productsMap.remove(id);
113+
return true;
114+
}
115+
LOGGER.warning("Repository: Product not found for deletion, ID: " + id);
116+
return false;
117+
}
118+
119+
/**
120+
* Searches for products by criteria.
121+
*
122+
* @param name Product name (optional)
123+
* @param description Product description (optional)
124+
* @param minPrice Minimum price (optional)
125+
* @param maxPrice Maximum price (optional)
126+
* @return List of matching products
127+
*/
128+
public List<Product> searchProducts(String name, String description, Double minPrice, Double maxPrice) {
129+
LOGGER.fine("Repository: Searching for products with criteria");
130+
131+
return productsMap.values().stream()
132+
.filter(p -> name == null || p.getName().toLowerCase().contains(name.toLowerCase()))
133+
.filter(p -> description == null || p.getDescription().toLowerCase().contains(description.toLowerCase()))
134+
.filter(p -> minPrice == null || p.getPrice() >= minPrice)
135+
.filter(p -> maxPrice == null || p.getPrice() <= maxPrice)
136+
.collect(Collectors.toList());
137+
}
138+
}

0 commit comments

Comments
 (0)