Add @CustomDocument annotation
This commit is contained in:
parent
bd8e3b284b
commit
f44e19f6d5
@ -12,16 +12,16 @@ import com.mongodb.client.model.Indexes;
|
|||||||
import com.mongodb.client.model.UpdateOneModel;
|
import com.mongodb.client.model.UpdateOneModel;
|
||||||
import com.mongodb.client.model.UpdateOptions;
|
import com.mongodb.client.model.UpdateOptions;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
import me.braydon.feather.common.Tuple;
|
import me.braydon.feather.common.Tuple;
|
||||||
import me.braydon.feather.database.Repository;
|
import me.braydon.feather.database.Repository;
|
||||||
|
import me.braydon.feather.database.impl.mongodb.annotation.CustomDocument;
|
||||||
import me.braydon.feather.database.impl.mongodb.annotation.Index;
|
import me.braydon.feather.database.impl.mongodb.annotation.Index;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Collections;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link MongoDB} {@link Repository} implementation.
|
* The {@link MongoDB} {@link Repository} implementation.
|
||||||
@ -90,17 +90,29 @@ public class MongoRepository<ID, E> extends Repository<MongoDB, ID, E> {
|
|||||||
* @param entities the entities to save
|
* @param entities the entities to save
|
||||||
* @see E for entity
|
* @see E for entity
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override @SneakyThrows
|
||||||
public void saveAll(@NonNull E... entities) {
|
public void saveAll(@NonNull E... entities) {
|
||||||
List<UpdateOneModel<Document>> updateModels = new ArrayList<>(); // The update models to bulk write
|
List<UpdateOneModel<Document>> updateModels = new ArrayList<>(); // The update models to bulk write
|
||||||
|
|
||||||
for (E entity : entities) {
|
for (E entity : entities) {
|
||||||
me.braydon.feather.data.Document<Object> document = new me.braydon.feather.data.Document<>(entity); // Create a document from the entity
|
me.braydon.feather.data.Document<Object> document = new me.braydon.feather.data.Document<>(entity); // Create a document from the entity
|
||||||
|
Document bsonDocument; // The Bson document to save
|
||||||
|
|
||||||
|
Method customDocumentMethod = Arrays.stream(entity.getClass().getDeclaredMethods())
|
||||||
|
.filter(method -> method.isAnnotationPresent(CustomDocument.class))
|
||||||
|
.findFirst().orElse(null); // Get the @CustomDocument method
|
||||||
|
|
||||||
|
// We have a custom document method
|
||||||
|
if (customDocumentMethod != null && (customDocumentMethod.getReturnType() == Document.class)) {
|
||||||
|
bsonDocument = (Document) customDocumentMethod.invoke(entity); // Get our custom document
|
||||||
|
} else { // Otherwise, use our mapped data
|
||||||
|
bsonDocument = new Document(document.toMappedData());
|
||||||
|
}
|
||||||
|
|
||||||
// Add our update model to the list
|
// Add our update model to the list
|
||||||
updateModels.add(new UpdateOneModel<>(
|
updateModels.add(new UpdateOneModel<>(
|
||||||
Filters.eq(document.getIdKey(), document.getKey()),
|
Filters.eq(document.getIdKey(), document.getKey()),
|
||||||
new Document("$set", new Document(document.toMappedData())),
|
new Document("$set", bsonDocument),
|
||||||
new UpdateOptions().upsert(true)
|
new UpdateOptions().upsert(true)
|
||||||
));
|
));
|
||||||
|
|
||||||
|
@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2023 Braydon (Rainnny). All rights reserved.
|
||||||
|
*
|
||||||
|
* For inquiries, please contact braydonrainnny@gmail.com
|
||||||
|
*/
|
||||||
|
package me.braydon.feather.database.impl.mongodb.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Methods tagged with this annotation will be invoked
|
||||||
|
* when a document is being saved to the database, this
|
||||||
|
* allows is to save a custom document, rather than
|
||||||
|
* parsing the entity and creating one that way.
|
||||||
|
*
|
||||||
|
* @author Braydon
|
||||||
|
*/
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
@Documented @Inherited
|
||||||
|
public @interface CustomDocument { }
|
Loading…
x
Reference in New Issue
Block a user