Skip to content

Commit 686b162

Browse files
committed
feat: update structure code demo
Signed-off-by: Otavio Santana <[email protected]>
1 parent a6a8aa0 commit 686b162

File tree

5 files changed

+201
-0
lines changed

5 files changed

+201
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2022 Contributors to the Eclipse Foundation
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*/
11+
12+
package org.soujava.demos.mongodb.document;
13+
14+
15+
import jakarta.enterprise.inject.se.SeContainer;
16+
import jakarta.enterprise.inject.se.SeContainerInitializer;
17+
import net.datafaker.Faker;
18+
import org.eclipse.jnosql.mapping.document.DocumentTemplate;
19+
20+
import java.util.List;
21+
import java.util.Set;
22+
import java.util.logging.Logger;
23+
24+
25+
public class App {
26+
27+
private static final Logger LOGGER = Logger.getLogger(App.class.getName());
28+
29+
public static void main(String[] args) {
30+
var faker = new Faker();
31+
LOGGER.info("Starting the application");
32+
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
33+
var template = container.select(DocumentTemplate.class).get();
34+
var electronicsCategory = new Category("Electronics", "All electronics");
35+
var computerCategory = new Category("Computers", "All computers");
36+
var tags = List.of("smartphone", "tablet", "laptop");
37+
var manufacturer = new Manufacturer("Apple", "One Infinite Loop Cupertino, CA 95014", "+1-408-996-1010");
38+
Product macBookPro = Product.builder().categories(Set.of(electronicsCategory, computerCategory))
39+
.manufacturer(manufacturer)
40+
.name("MacBook Pro")
41+
.tags(tags)
42+
.build();
43+
44+
Product product = template.insert(macBookPro);
45+
46+
LOGGER.info("Product saved: " + product);
47+
template.select(Product.class).where("id")
48+
.eq(product.getId())
49+
.result().forEach(p -> LOGGER.info("Product found: " + p));
50+
51+
52+
}
53+
}
54+
55+
private App() {
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.soujava.demos.mongodb.document;
2+
3+
import jakarta.nosql.Column;
4+
import jakarta.nosql.Embeddable;
5+
6+
@Embeddable(Embeddable.EmbeddableType.GROUPING)
7+
public record Category(@Column String name, @Column String description) {
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.soujava.demos.mongodb.document;
2+
3+
import jakarta.nosql.Column;
4+
import jakarta.nosql.Embeddable;
5+
6+
7+
@Embeddable(Embeddable.EmbeddableType.GROUPING)
8+
//@Embeddable(Embeddable.EmbeddableType.FLAT)
9+
public record Manufacturer(@Column String name, @Column String address, @Column String contactNumber) {
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package org.soujava.demos.mongodb.document;
2+
3+
import jakarta.nosql.Column;
4+
import jakarta.nosql.Convert;
5+
import jakarta.nosql.Entity;
6+
import jakarta.nosql.Id;
7+
import org.eclipse.jnosql.databases.mongodb.mapping.ObjectIdConverter;
8+
9+
import java.util.List;
10+
import java.util.Objects;
11+
import java.util.Set;
12+
13+
@Entity
14+
public class Product {
15+
16+
@Id
17+
@Convert(ObjectIdConverter.class)
18+
private String id;
19+
20+
@Column
21+
private String name;
22+
23+
@Column
24+
private Manufacturer manufacturer;
25+
26+
@Column
27+
private List<String> tags;
28+
29+
@Column
30+
private Set<Category> categories;
31+
32+
Product(String name, Manufacturer manufacturer, List<String> tags, Set<Category> categories) {
33+
this.name = name;
34+
this.manufacturer = manufacturer;
35+
this.tags = tags;
36+
this.categories = categories;
37+
}
38+
39+
Product() {
40+
}
41+
42+
public String getId() {
43+
return id;
44+
}
45+
46+
public String getName() {
47+
return name;
48+
}
49+
50+
public Manufacturer getManufacturer() {
51+
return manufacturer;
52+
}
53+
54+
public List<String> getTags() {
55+
return tags;
56+
}
57+
58+
public Set<Category> getCategories() {
59+
return categories;
60+
}
61+
62+
@Override
63+
public boolean equals(Object o) {
64+
if (o == null || getClass() != o.getClass()) {
65+
return false;
66+
}
67+
Product product = (Product) o;
68+
return Objects.equals(id, product.id);
69+
}
70+
71+
@Override
72+
public int hashCode() {
73+
return Objects.hashCode(id);
74+
}
75+
76+
@Override
77+
public String toString() {
78+
return "Product{" +
79+
"id='" + id + '\'' +
80+
", name='" + name + '\'' +
81+
", manufacturer=" + manufacturer +
82+
", tags=" + tags +
83+
", categories=" + categories +
84+
'}';
85+
}
86+
87+
public static ProductBuilder builder() {
88+
return new ProductBuilder();
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.soujava.demos.mongodb.document;
2+
3+
import java.util.List;
4+
import java.util.Set;
5+
6+
public class ProductBuilder {
7+
private String name;
8+
private Manufacturer manufacturer;
9+
private List<String> tags;
10+
private Set<Category> categories;
11+
12+
public ProductBuilder name(String name) {
13+
this.name = name;
14+
return this;
15+
}
16+
17+
public ProductBuilder manufacturer(Manufacturer manufacturer) {
18+
this.manufacturer = manufacturer;
19+
return this;
20+
}
21+
22+
public ProductBuilder tags(List<String> tags) {
23+
this.tags = tags;
24+
return this;
25+
}
26+
27+
public ProductBuilder categories(Set<Category> categories) {
28+
this.categories = categories;
29+
return this;
30+
}
31+
32+
public Product build() {
33+
return new Product(name, manufacturer, tags, categories);
34+
}
35+
}

0 commit comments

Comments
 (0)