Skip to content

Commit a29d709

Browse files
committed
Merge branch 'main' of github.com:JNOSQL/demos-se
2 parents 653bdfc + 0a65ab4 commit a29d709

File tree

14 files changed

+376
-0
lines changed

14 files changed

+376
-0
lines changed

mongodb-record/.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
target/
2+
pom.xml.tag
3+
pom.xml.releaseBackup
4+
pom.xml.versionsBackup
5+
pom.xml.next
6+
test-output/
7+
/doc
8+
*.iml
9+
*.log
10+
.classpath
11+
-project
12+
/.resourceCache
13+
/.project
14+
/.idea
15+
.settings/

mongodb-record/pom.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
13+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
14+
<modelVersion>4.0.0</modelVersion>
15+
<artifactId>mongodb-record</artifactId>
16+
<name>JNoSQL Demo using Java SE MongoDB using Records</name>
17+
18+
<parent>
19+
<groupId>org.eclipse.jnosql.mapping</groupId>
20+
<artifactId>mapping-demo-java-se</artifactId>
21+
<version>1.1.1</version>
22+
</parent>
23+
24+
<properties>
25+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.eclipse.jnosql.databases</groupId>
31+
<artifactId>jnosql-mongodb</artifactId>
32+
<version>1.1.2-SNAPSHOT</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>net.datafaker</groupId>
36+
<artifactId>datafaker</artifactId>
37+
<version>2.0.2</version>
38+
</dependency>
39+
</dependencies>
40+
41+
</project>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.jnosql.demo.se;
13+
14+
import jakarta.enterprise.inject.se.SeContainer;
15+
import jakarta.enterprise.inject.se.SeContainerInitializer;
16+
import net.datafaker.Faker;
17+
import org.eclipse.jnosql.mapping.DatabaseQualifier;
18+
19+
import java.util.Arrays;
20+
import java.util.List;
21+
import java.util.Objects;
22+
import java.util.Optional;
23+
24+
public class App {
25+
26+
27+
public static void main(String[] args) {
28+
29+
Faker faker = new Faker();
30+
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
31+
32+
var baggageRepository = container.select(BaggageRepository.class, DatabaseQualifier.ofDocument()).get();
33+
34+
35+
System.out.println("=".repeat(50));
36+
System.out.println("baggageRepository.register()");
37+
var baggage01 = baggageRepository.register(Baggage.create(faker));
38+
System.out.println("Baggage " + baggage01.ticket());
39+
System.out.println("Baggage items: ");
40+
Arrays.stream(baggage01.items()).forEach(System.out::println);
41+
42+
System.out.println("=".repeat(50));
43+
System.out.println("baggageRepository.findByTicket()");
44+
Baggage registeredBaggage = baggageRepository.findByTicket(baggage01.ticket());
45+
46+
System.out.println("Baggage " + registeredBaggage.ticket());
47+
System.out.println("Baggage items: ");
48+
Arrays.stream(registeredBaggage.items()).forEach(System.out::println);
49+
System.out.println("-".repeat(50));
50+
51+
baggageRepository.registerAll(Baggage.create(faker), Baggage.create(faker));
52+
53+
System.out.println("=".repeat(50));
54+
System.out.println("baggageRepository.findAll()");
55+
List<Baggage> baggageList = baggageRepository.findAll();
56+
System.out.println("Baggage List: " + baggageList.size());
57+
System.out.println("-".repeat(50));
58+
for (var baggage : baggageList) {
59+
System.out.println("Baggage " + baggage.ticket());
60+
System.out.println("Baggage items: ");
61+
Arrays.stream(registeredBaggage.items()).forEach(System.out::println);
62+
System.out.println("-".repeat(50));
63+
}
64+
System.out.println("=".repeat(50));
65+
System.out.println("baggageRepository.unregisterAll()");
66+
baggageRepository.unregisterAll(baggageList);
67+
68+
System.out.println("=".repeat(50));
69+
System.out.println("baggageRepository.findAll()");
70+
List<Baggage> actualBaggageList = baggageRepository.findAll();
71+
System.out.println("Baggage List: " + actualBaggageList.size());
72+
73+
}
74+
}
75+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.jnosql.demo.se;
2+
3+
import jakarta.nosql.Column;
4+
import jakarta.nosql.Entity;
5+
import jakarta.nosql.Id;
6+
import net.datafaker.Faker;
7+
8+
import java.util.stream.IntStream;
9+
10+
@Entity
11+
public record Baggage(
12+
@Id String ticket,
13+
@Column BaggageItem[] items
14+
) {
15+
public static Baggage create(Faker faker) {
16+
return new Baggage(
17+
faker.code().isbn10(),
18+
IntStream.rangeClosed(0, faker.random().nextInt(1, 5))
19+
.mapToObj(i -> BaggageItem.of(faker))
20+
.toArray(BaggageItem[]::new)
21+
);
22+
}
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.jnosql.demo.se;
2+
3+
import jakarta.nosql.Column;
4+
import jakarta.nosql.Entity;
5+
import net.datafaker.Faker;
6+
7+
@Entity
8+
public record BaggageItem(
9+
@Column String description,
10+
@Column Integer weight,
11+
@Column BaggageItemSize size
12+
) {
13+
public static BaggageItem of(Faker faker) {
14+
return new BaggageItem(
15+
faker.lorem().sentence(faker.random().nextInt(1, 2)),
16+
faker.random().nextInt(1, 50),
17+
BaggageItemSize.values()[faker.random().nextInt(0, 2)]);
18+
}
19+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.jnosql.demo.se;
2+
3+
public enum BaggageItemSize {
4+
SMALL, MEDIUM, LARGE;
5+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.jnosql.demo.se;
2+
3+
import jakarta.data.repository.By;
4+
import jakarta.data.repository.Delete;
5+
import jakarta.data.repository.Find;
6+
import jakarta.data.repository.Repository;
7+
import jakarta.data.repository.Save;
8+
9+
import java.util.Arrays;
10+
import java.util.List;
11+
12+
@Repository
13+
public interface BaggageRepository {
14+
15+
@Save
16+
Baggage register(Baggage baggage);
17+
18+
default void registerAll(Baggage... baggages) {
19+
Arrays.stream(baggages).forEach(this::register);
20+
}
21+
22+
@Find
23+
Baggage findByTicket(@By("ticket") String ticket);
24+
25+
@Find
26+
List<Baggage> findAll();
27+
28+
@Delete
29+
void unregister(Baggage baggage);
30+
31+
default void unregisterAll(List<Baggage> baggageList){
32+
baggageList.forEach(this::unregister);
33+
}
34+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.jnosql.demo.se;
2+
3+
import jakarta.enterprise.context.ApplicationScoped;
4+
import jakarta.inject.Inject;
5+
import org.eclipse.jnosql.mapping.Database;
6+
import static org.eclipse.jnosql.mapping.DatabaseType.DOCUMENT;
7+
8+
@ApplicationScoped
9+
public class BaggageSystem {
10+
11+
@Inject
12+
@Database(DOCUMENT)
13+
BaggageRepository baggageRepository;
14+
15+
public BaggageRepository getBaggageRepository() {
16+
return baggageRepository;
17+
}
18+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
package org.jnosql.demo.se;
12+
13+
import jakarta.nosql.Column;
14+
import jakarta.nosql.Entity;
15+
import jakarta.nosql.Id;
16+
17+
import java.util.UUID;
18+
19+
@Entity
20+
public record Task(
21+
@Id String id,
22+
@Column String description,
23+
@Column boolean enabled) {
24+
25+
public static Task newEnabledTask(String description){
26+
return new Task(UUID.randomUUID().toString(),description,true);
27+
}
28+
29+
public static Task newDisabledTask(String description){
30+
return new Task(UUID.randomUUID().toString(),description,false);
31+
}
32+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
package org.jnosql.demo.se;
12+
13+
import jakarta.enterprise.inject.se.SeContainer;
14+
import jakarta.enterprise.inject.se.SeContainerInitializer;
15+
import net.datafaker.Faker;
16+
import org.eclipse.jnosql.mapping.DatabaseQualifier;
17+
18+
import java.util.List;
19+
20+
public class TaskApp {
21+
22+
public static void main(String[] args) {
23+
24+
Faker faker = new Faker();
25+
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
26+
27+
var tasks = container.select(Tasks.class, DatabaseQualifier.ofDocument()).get();
28+
29+
tasks.save(Task.newEnabledTask("task 1"));
30+
tasks.save(Task.newEnabledTask("task 2"));
31+
tasks.save(Task.newEnabledTask("task 3"));
32+
tasks.save(Task.newDisabledTask("task 4"));
33+
34+
List<Task> enabledTasks = tasks.listAllEnabled();
35+
System.out.println("Enabled tasks:");
36+
enabledTasks.forEach(System.out::println);
37+
38+
System.out.println();
39+
40+
List<Task> disabledTasks = tasks.listAllDisabled();
41+
System.out.println("Disabled tasks:");
42+
disabledTasks.forEach(System.out::println);
43+
44+
tasks.findAll().forEach(tasks::remove);
45+
}
46+
}
47+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
package org.jnosql.demo.se;
12+
13+
import jakarta.data.repository.Delete;
14+
import jakarta.data.repository.Find;
15+
import jakarta.data.repository.Query;
16+
import jakarta.data.repository.Repository;
17+
import jakarta.data.repository.Save;
18+
19+
import java.util.List;
20+
21+
@Repository
22+
public interface Tasks {
23+
24+
@Save
25+
Task save(Task task);
26+
27+
@Query("from Task where enabled = true")
28+
List<Task> listAllEnabled();
29+
30+
@Query("from Task where enabled = false")
31+
List<Task> listAllDisabled();
32+
33+
@Find
34+
List<Task> findAll();
35+
36+
@Delete
37+
void remove(Task task);
38+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
13+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
14+
bean-discovery-mode="annotated" version="2.0">
15+
</beans>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
jnosql.document.database=olympus
13+
jnosql.mongodb.url=mongodb://localhost:27017

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<module>mongodb</module>
4949
<module>mongodb-lite</module>
5050
<module>mongodb-double</module>
51+
<module>mongodb-record</module>
5152
<module>oracle-nosql</module>
5253
<module>redis</module>
5354
<module>couchdb</module>

0 commit comments

Comments
 (0)