Skip to content

Commit e4c2ac5

Browse files
committed
feat: Adds EntityListener support
chore: Adds License headers
1 parent 937f324 commit e4c2ac5

14 files changed

+278
-114
lines changed

core/pom.xml

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,45 @@
77
file, You can obtain one at http://mozilla.org/MPL/2.0/.
88
99
-->
10-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
11-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
12-
<modelVersion>4.0.0</modelVersion>
13-
14-
<parent>
15-
<groupId>org.seedstack.addons.mongodb</groupId>
16-
<artifactId>mongodb</artifactId>
17-
<version>4.0.0-SNAPSHOT</version>
18-
</parent>
19-
20-
<artifactId>mongodb-core</artifactId>
21-
22-
<dependencies>
23-
<dependency>
24-
<groupId>org.mongodb</groupId>
25-
<artifactId>mongodb-driver-sync</artifactId>
26-
<version>${mongodb.version}</version>
27-
<scope>provided</scope>
28-
</dependency>
29-
30-
<dependency>
31-
<groupId>org.seedstack.seed</groupId>
32-
<artifactId>seed-testing-junit4</artifactId>
33-
<version>${seed.version}</version>
34-
<scope>test</scope>
35-
</dependency>
36-
<dependency>
37-
<groupId>org.assertj</groupId>
38-
<artifactId>assertj-core</artifactId>
39-
<version>${assertj.version}</version>
40-
<scope>test</scope>
41-
</dependency>
42-
<dependency>
43-
<groupId>ch.qos.logback</groupId>
44-
<artifactId>logback-classic</artifactId>
45-
<version>${logback.version}</version>
46-
<scope>test</scope>
47-
</dependency>
48-
</dependencies>
10+
<project xmlns="http://maven.apache.org/POM/4.0.0"
11+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
12+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
13+
<modelVersion>4.0.0</modelVersion>
14+
15+
<parent>
16+
<groupId>org.seedstack.addons.mongodb</groupId>
17+
<artifactId>mongodb</artifactId>
18+
<version>4.0.0-SNAPSHOT</version>
19+
</parent>
20+
21+
<artifactId>mongodb-core</artifactId>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.mongodb</groupId>
26+
<artifactId>mongodb-driver-sync</artifactId>
27+
<version>${mongodb.version}</version>
28+
<scope>provided</scope>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.seedstack.seed</groupId>
33+
<artifactId>seed-testing-junit4</artifactId>
34+
<version>${seed.version}</version>
35+
<scope>test</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.assertj</groupId>
39+
<artifactId>assertj-core</artifactId>
40+
<version>${assertj.version}</version>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>ch.qos.logback</groupId>
45+
<artifactId>logback-classic</artifactId>
46+
<version>${logback.version}</version>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
4950

5051
</project>

morphia/src/license/THIRD-PARTY.properties

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright © 2013-2024, The SeedStack authors <http://seedstack.org>
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
package org.seedstack.mongodb.morphia;
9+
10+
import java.lang.reflect.Type;
11+
12+
import org.seedstack.business.internal.utils.BusinessUtils;
13+
14+
public abstract class BaseEntityListener<T> implements EntityListener<T> {
15+
16+
private final Class<T> listenerClass;
17+
18+
@SuppressWarnings({ "unchecked", "rawtypes" })
19+
public BaseEntityListener() {
20+
Type[] generics = BusinessUtils.resolveGenerics(EntityListener.class, this.getClass());
21+
this.listenerClass = (Class) generics[0];
22+
}
23+
24+
@Override
25+
public Class<T> getListenerClass() {
26+
return listenerClass;
27+
}
28+
29+
}

morphia/src/main/java/org/seedstack/mongodb/morphia/BaseMorphiaRepository.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import org.seedstack.business.spi.SpecificationTranslator;
3131
import org.seedstack.mongodb.morphia.internal.DatastoreFactory;
3232
import org.seedstack.mongodb.morphia.internal.specification.MorphiaTranslationContext;
33-
import org.seedstack.seed.Logging;
34-
import org.slf4j.Logger;
3533

3634
import com.mongodb.client.MongoCollection;
3735

@@ -46,6 +44,7 @@
4644
import dev.morphia.query.filters.Filters;
4745
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
4846

47+
@SuppressWarnings("rawtypes")
4948
/**
5049
* This class can serve as a base class for Morphia repositories. It provides methods for common
5150
* CRUD operations as well as access to the data store through the {@link #getDatastore()} ()}
@@ -59,10 +58,8 @@
5958
public abstract class BaseMorphiaRepository<A extends AggregateRoot<ID>, ID> extends BaseRepository<A, ID> {
6059
public static final String ID_KEY = "_id";
6160
private Datastore datastore;
62-
private SpecificationTranslator<MorphiaTranslationContext, Filter> specificationTranslator;
6361

64-
@Logging
65-
private Logger logger;
62+
private SpecificationTranslator<MorphiaTranslationContext, Filter> specificationTranslator;
6663

6764
public BaseMorphiaRepository() {
6865

@@ -176,7 +173,6 @@ private Query<A> buildQuery(Specification<A> specification) {
176173
Filter filter = specificationTranslator.translate(
177174
specification,
178175
new MorphiaTranslationContext<>(query));
179-
logger.info("Querying {} with filter {}",getAggregateRootClass(),filter);
180176
return query.filter(filter);
181177
}
182178

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright © 2013-2024, The SeedStack authors <http://seedstack.org>
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
package org.seedstack.mongodb.morphia;
9+
10+
import org.bson.Document;
11+
12+
import dev.morphia.Datastore;
13+
14+
public interface EntityListener<T> {
15+
16+
Class<T> getListenerClass();
17+
18+
default void postLoad(T entity, Document document, Datastore datastore) {
19+
// NO-OP
20+
};
21+
22+
default void preLoad(T entity, Document document, Datastore datastore) {
23+
// NO-OP
24+
};
25+
26+
default void postPersist(T entity, Document document, Datastore datastore) {
27+
// NO-OP
28+
};
29+
30+
default void prePersist(T entity, Document document, Datastore datastore) {
31+
// NO-OP
32+
};
33+
34+
}

morphia/src/main/java/org/seedstack/mongodb/morphia/internal/DatastoreFactory.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import static org.seedstack.mongodb.morphia.internal.MorphiaUtils.createDatastoreAnnotation;
1111
import static org.seedstack.mongodb.morphia.internal.MorphiaUtils.getMongoClientConfig;
1212

13-
import java.util.Arrays;
14-
1513
import javax.inject.Inject;
1614

1715
import org.seedstack.mongodb.morphia.MorphiaDatastore;
@@ -28,13 +26,13 @@
2826
public class DatastoreFactory {
2927
private final Application application;
3028
private final Injector injector;
31-
private final ValidatingEntityInterceptor validationInterceptor;
29+
private final SeedEntityListener seedEntityListener;
3230

3331
@Inject
34-
DatastoreFactory(Application application, Injector injector, ValidatingEntityInterceptor validationInterceptor) {
32+
DatastoreFactory(Application application, Injector injector, SeedEntityListener seedEntityListener) {
3533
this.application = application;
3634
this.injector = injector;
37-
this.validationInterceptor = validationInterceptor;
35+
this.seedEntityListener = seedEntityListener;
3836
}
3937

4038
public Datastore createDatastore(Class<?> morphiaClass) {
@@ -49,6 +47,6 @@ public Datastore createDatastore(String clientName, String dbName) {
4947
MongoClient client = injector.getInstance(Key.get(MongoClient.class, Names.named(clientName)));
5048
String dbAlias = MorphiaUtils.resolveDatabaseAlias(getMongoClientConfig(application, clientName), dbName);
5149

52-
return new DatastoreWrapper(Morphia.createDatastore(client, dbAlias), Arrays.asList(validationInterceptor));
50+
return new DatastoreWrapper(Morphia.createDatastore(client, dbAlias), seedEntityListener);
5351
}
5452
}

morphia/src/main/java/org/seedstack/mongodb/morphia/internal/DatastoreWrapper.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
/*
2+
* Copyright © 2013-2024, The SeedStack authors <http://seedstack.org>
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
18
package org.seedstack.mongodb.morphia.internal;
29

3-
import java.util.Collection;
4-
510
import dev.morphia.Datastore;
611
import dev.morphia.DatastoreImpl;
7-
import dev.morphia.EntityListener;
812

913
class DatastoreWrapper extends DatastoreImpl {
1014

11-
public DatastoreWrapper(Datastore datastore, Collection<EntityListener<?>> extraListeners) {
15+
public DatastoreWrapper(Datastore datastore, SeedEntityListener seedListener) {
1216
super((DatastoreImpl) datastore);
1317
// TODO: Find a way to wrap mapper instead of adding an interceptor
14-
extraListeners.forEach(listener -> getMapper().addInterceptor(listener));
18+
getMapper().addInterceptor(seedListener);
1519

1620
}
1721

morphia/src/main/java/org/seedstack/mongodb/morphia/internal/MorphiaModule.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,35 @@
1313

1414
import java.util.Collection;
1515

16+
import org.seedstack.mongodb.morphia.EntityListener;
1617
import org.seedstack.mongodb.morphia.MorphiaDatastore;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
1720

1821
import com.google.inject.AbstractModule;
1922
import com.google.inject.Key;
2023
import com.google.inject.Scopes;
24+
import com.google.inject.multibindings.Multibinder;
2125

2226
import dev.morphia.Datastore;
2327

2428
class MorphiaModule extends AbstractModule {
29+
private static final Logger LOGGER = LoggerFactory.getLogger(MorphiaModule.class);
2530
private final Collection<MorphiaDatastore> morphiaDatastoresAnnotation;
26-
private final Collection<Class<?>> morphiaEntityListeners;
31+
private final Collection<Class<? extends EntityListener<?>>> seedEntityListeners;
2732

2833
MorphiaModule(Collection<MorphiaDatastore> morphiaDatastoresAnnotation,
29-
Collection<Class<?>> morphiaEntityListeners) {
34+
Collection<Class<? extends EntityListener<?>>> seedEntityListeners) {
3035
super();
3136
this.morphiaDatastoresAnnotation = morphiaDatastoresAnnotation;
32-
this.morphiaEntityListeners = morphiaEntityListeners;
37+
this.seedEntityListeners = seedEntityListeners;
3338

3439
}
3540

41+
@SuppressWarnings("rawtypes")
3642
@Override
3743
protected void configure() {
3844
bind(DatastoreFactory.class);
39-
bind(ValidatingEntityInterceptor.class);
4045

4146
if (morphiaDatastoresAnnotation != null && !morphiaDatastoresAnnotation.isEmpty()) {
4247
for (MorphiaDatastore morphiaDatastore : morphiaDatastoresAnnotation) {
@@ -46,5 +51,15 @@ protected void configure() {
4651
}
4752
}
4853

54+
Multibinder<EntityListener> entityListenerMultibinder = Multibinder.newSetBinder(binder(),
55+
EntityListener.class);
56+
57+
for (Class<? extends EntityListener> listener : seedEntityListeners) {
58+
LOGGER.debug("Binding {}", listener.toGenericString());
59+
entityListenerMultibinder.addBinding().to(listener);
60+
}
61+
62+
bind(SeedEntityListener.class).asEagerSingleton();
63+
4964
}
5065
}

0 commit comments

Comments
 (0)